-
Notifications
You must be signed in to change notification settings - Fork 293
/
demo_g2.py
72 lines (58 loc) · 1.88 KB
/
demo_g2.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
67
68
69
70
71
72
import spacy
from scattertext.Scalers import dense_rank, scale
from scattertext.termscoring.g2 import G2
from scattertext.CorpusFromParsedDocuments import CorpusFromParsedDocuments
from scattertext.SampleCorpora import ConventionData2012
from scattertext import produce_frequency_explorer
nlp = spacy.blank('en')
nlp.add_pipe('sentencizer')
corpus = CorpusFromParsedDocuments(
ConventionData2012.get_data().assign(
Parse=lambda df: df.text.apply(nlp)
),
category_col='party',
parsed_col='Parse',
).build().get_unigram_corpus().remove_infrequent_words(5)
term_scorer = G2(
corpus=corpus,
).set_categories('democrat', ['republican']).use_token_counts_as_doc_sizes()
html = produce_frequency_explorer(
corpus,
category='democrat',
category_name='Democratic',
not_category_name='Republican',
minimum_term_frequency=0,
pmi_threshold_coefficient=0,
width_in_pixels=1000,
metadata=lambda c: c.get_df()['speaker'],
term_scorer=term_scorer,
frequency_transform=dense_rank,
transform=dense_rank,
score_scaler=dense_rank
)
open('./demo_g2.html', 'wb').write(html.encode('utf-8'))
print('Open ./demo_g2.html in Chrome or Firefox.')
alpha = 0.05
term_scorer = G2(
corpus=corpus,
alpha=alpha,
correct=True
).set_categories('democrat', ['republican']).use_token_counts_as_doc_sizes()
html = produce_frequency_explorer(
corpus,
category='democrat',
category_name='Democratic',
not_category_name='Republican',
minimum_term_frequency=0,
pmi_threshold_coefficient=0,
width_in_pixels=1000,
metadata=lambda c: c.get_df()['speaker'],
term_scorer=term_scorer,
frequency_transform=dense_rank,
transform=dense_rank,
score_scaler=dense_rank
)
print(term_scorer.get_score_df())
fn = f'demo_g2_alpha_{alpha}.html'
open(f'./{fn}', 'wb').write(html.encode('utf-8'))
print(f'Open ./{fn} in Chrome or Firefox.')