-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixing CredTFIDF so that it you can use it to visualize metadata (i.e…
…., topics) in addition to terms. Added a flashtext-based functionality to let you visualize topics which have entries with spaces or otherwise contain multiple tokens. This is in PhraseFeatsFromTopicModel.
- Loading branch information
1 parent
8e68d81
commit c7b791b
Showing
7 changed files
with
126 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from collections import Counter | ||
from functools import reduce | ||
|
||
from scattertext import FeatsFromSpacyDoc | ||
from scattertext.features.FeatsFromTopicModel import FeatsFromTopicModelBase | ||
|
||
|
||
class PhraseFeatsFromTopicModel(FeatsFromTopicModelBase, FeatsFromSpacyDoc): | ||
''' | ||
This class allows you to make use of a topic model which has multi-token entries (i.e., terms in topics which | ||
have spaces in them.) | ||
It requires Flashtext to be installed. | ||
''' | ||
def __init__(self, | ||
topic_model, | ||
use_lemmas=False, | ||
entity_types_to_censor=set(), | ||
entity_types_to_use=None, | ||
tag_types_to_censor=set(), | ||
strip_final_period=False, | ||
keyword_processor_args = {'case_sensitive' :False}): | ||
from flashtext import KeywordProcessor | ||
self._keyword_processor = KeywordProcessor(**keyword_processor_args) | ||
self._topic_model = topic_model | ||
for keyphrase in reduce(lambda x, y: set(x) | set(y), topic_model.values()): | ||
self._keyword_processor.add_keyword(keyphrase) | ||
FeatsFromSpacyDoc.__init__(self, use_lemmas, entity_types_to_censor, | ||
tag_types_to_censor, strip_final_period) | ||
FeatsFromTopicModelBase.__init__(self, topic_model) | ||
|
||
|
||
def get_top_model_term_lists(self): | ||
return self._topic_model | ||
|
||
def _get_terms_from_doc(self, doc): | ||
return Counter(self._keyword_processor.extract_keywords(doc)) | ||
|
||
def get_feats(self, doc): | ||
return Counter(self._get_terms_from_doc(str(doc))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from collections import Counter | ||
from unittest import TestCase, mock | ||
from unittest.mock import patch, MagicMock | ||
import sys | ||
|
||
from scattertext.features.PhraseFeatsFromTopicModel import PhraseFeatsFromTopicModel | ||
|
||
|
||
class TestPhraseFeatsFromTopicModel(TestCase): | ||
def test_get_doc_get_feats(self): | ||
flashtext = MagicMock() | ||
flashtext.KeywordProcessor().extract_keywords.return_value = ['A b', 'A b', 'C e F', 'B'] | ||
sys.modules["flashtext"] = flashtext | ||
|
||
expected = Counter({'A b': 2, 'C e F': 1, 'B': 1}) | ||
|
||
actual = PhraseFeatsFromTopicModel( | ||
topic_model={'Topic A': ['A b', 'b', 'C e F'], | ||
'Topic B': ['B', 'C e F']} | ||
).get_feats('A b A b C e F B') | ||
|
||
self.assertEqual(expected, actual) | ||
|
||
|
||
def test_get_doc_metadata(self): | ||
flashtext = MagicMock() | ||
flashtext.KeywordProcessor().extract_keywords.return_value = ['A b', 'A b', 'C e F', 'B'] | ||
sys.modules["flashtext"] = flashtext | ||
|
||
expected = Counter({'Topic A': 3, 'Topic B': 2}) | ||
|
||
actual = PhraseFeatsFromTopicModel( | ||
topic_model={'Topic A': ['A b', 'b', 'C e F'], | ||
'Topic B': ['B', 'C e F']} | ||
).get_doc_metadata('A b A b C e F B') | ||
|
||
self.assertEqual(expected, actual) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters