Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Topic Modeling: fails on POS tags #1091

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions orangecontrib/text/tests/test_topic_modeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from orangecontrib.text.topics import LdaWrapper, HdpWrapper, LsiWrapper, NmfWrapper
from orangecontrib.text.corpus import Corpus
from orangecontrib.text import preprocess
from orangecontrib.text.tag import AveragedPerceptronTagger


class BaseTests:
Expand Down Expand Up @@ -82,6 +83,17 @@ def test_existing_attributes(self):
self.assertEqual(self.model.doc_topic.shape[1],
self.model.actual_topics)

def test_pos_tags(self):
corpus = Corpus.from_file('deerwester')
pp_list = [preprocess.WordPunctTokenizer(),
AveragedPerceptronTagger(),
preprocess.PosTagFilter("NN")]
for pp in pp_list:
corpus = pp(corpus)
self.model.fit_transform(corpus)
self.assertTrue(all("_NN" in word for word in
self.model.get_top_words_by_id(0, 10)[0]))


class LDATests(unittest.TestCase, BaseTests):
def setUp(self):
Expand Down
4 changes: 3 additions & 1 deletion orangecontrib/text/topics/topics.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def infer_ngrams_corpus(corpus, return_dict=False):
(i, attribute.name) for i, attribute in enumerate(corpus.domain.attributes)
if 'bow-feature' in attribute.attributes
]

if len(bow_features) == 0:
corpus = BowVectorizer().transform(corpus)
bow_features = [
Expand All @@ -74,7 +75,8 @@ def infer_ngrams_corpus(corpus, return_dict=False):
feature_presence = corpus.X.sum(axis=0)
keep = [(i, a) for i, a in bow_features if feature_presence[0, i] > 0]
# sort features by the order in the dictionary
dictionary = Dictionary(corpus.ngrams_iterator(include_postags=True), prune_at=None)
dictionary = Dictionary(corpus.ngrams_iterator(include_postags=False),
prune_at=None)
idx_of_keep = np.argsort([dictionary.token2id[a] for _, a in keep])
keep = [keep[i][0] for i in idx_of_keep]
result = []
Expand Down
Loading