-
Notifications
You must be signed in to change notification settings - Fork 293
/
demo_flashtext.py
66 lines (54 loc) · 2.11 KB
/
demo_flashtext.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from collections import Counter
from flashtext import KeywordProcessor
import scattertext as st
class FlashTextExtact(st.FeatsFromSpacyDoc):
'''
'''
def set_keyword_processor(self, keyword_processor):
'''
:param keyword_processor: set, phrases to look for
:return: self
'''
self.keyword_processor_ = keyword_processor
return self
def get_feats(self, doc):
'''
Parameters
----------
doc, Spacy Doc
Returns
-------
Counter noun chunk -> count
'''
return Counter(self.keyword_processor_.extract_keywords(str(doc)))
keyword_processor = KeywordProcessor(case_sensitive=False)
for phrase in ['the president', 'presidents', 'presidential', 'barack obama', 'mitt romney', 'george bush',
'george w. bush', 'bill clinton', 'ronald regan', 'obama', 'romney',
'barack', 'mitt', 'bush', 'clinton', 'reagan', 'mr. president', 'united states of america']:
keyword_processor.add_keyword(phrase)
feature_extractor = FlashTextExtact().set_keyword_processor(keyword_processor)
convention_df = st.SampleCorpora.ConventionData2012.get_data()
convention_df['parse'] = convention_df['text'].apply(st.whitespace_nlp_with_sentences)
corpus = (st.CorpusFromPandas(convention_df,
category_col='party',
text_col='text',
nlp=st.whitespace_nlp_with_sentences,
feats_from_spacy_doc=feature_extractor)
.build())
print(corpus.get_term_freq_df())
html = st.produce_scattertext_explorer(
corpus,
category='democrat',
category_name='Democratic',
not_category_name='Republican',
metadata=convention_df['speaker'],
term_scorer=st.RankDifference(),
transform=st.Scalers.dense_rank,
pmi_threshold_coefficient=0,
minimum_term_frequency=0,
minimum_not_category_term_frequency=0,
use_full_doc=True
)
file_name = 'demo_specific_phrases.html'
open(file_name, 'wb').write(html.encode('utf-8'))
print('Open %s in Chrome or Firefox.' % file_name)