-
Notifications
You must be signed in to change notification settings - Fork 4
/
jsd_topics.py
203 lines (157 loc) · 5.4 KB
/
jsd_topics.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import sys
import os
import nltk
import spacy
import gensim
import sklearn
import keras
import pandas as pd
import numpy as np
from scipy.spatial import distance
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer, SnowballStemmer
from nltk.stem.porter import *
nltk.download('wordnet')
nltk.download('stopwords')
from gensim.parsing.preprocessing import STOPWORDS
from gensim.utils import simple_preprocess
from gensim import corpora, models
from keras.preprocessing.text import text_to_word_sequence
from sklearn.feature_extraction import stop_words
# define stopwords
def add_words(filename):
with open(filename) as f:
additional_words = f.readlines()
additional_words = [x.strip() for x in additional_words]
return additional_words
def remove_words(filename):
with open(filename) as f:
unstop = f.readlines()
unstop = [x.strip() for x in unstop]
return unstop
def define_stopwords():
"""
default: combine SKLEARN, NLTK, and SPACY stopwords -> 'sns_set'
alternative: set custom 'additional_words' and 'unstop' words (to ignore)
function returns a list: 'stopwords'
"""
# corpus-specific stop words [OPTIONAL]
# add 'stop.txt' to local directory, pass as argument 2
additional_words = ['nan']
# don't remove these words which may be important in our context [OPTIONAL]
# add 'unstop.txt' to local directory, pass as argument 3
unstop = []
gen_stop = gensim.parsing.preprocessing.STOPWORDS
nlp = spacy.load('en')
spacy_stop = nlp.Defaults.stop_words # .add("my_new_stopword")
sk_stop = stop_words.ENGLISH_STOP_WORDS
nltk_stop = stopwords.words('english')
custom_stop = additional_words
sns_stop = []
all_stop = []
# combine sklearn, nltk, and spacy stop word lists: sns_stop
# also add these to all_stop
for i in gen_stop:
if i not in unstop:
sns_stop.append(i)
all_stop.append(i)
for i in spacy_stop:
if i not in unstop:
sns_stop.append(i)
all_stop.append(i)
for i in sk_stop:
if i not in unstop:
sns_stop.append(i)
all_stop.append(i)
for i in nltk_stop:
if i not in unstop:
sns_stop.append(i)
all_stop.append(i)
# add corpus specific stop words to all_stop
for i in custom_stop:
if i not in unstop:
if i not in all_stop:
all_stop.append(i)
sns_set = list(set(sns_stop))
all_set = list(set(all_stop))
if len(custom_stop) == 0 and len(unstop) == 0:
# print(f'sns_set stopwords = {len(sns_set)} words: \nExamples: \n{[x for x in sns_set[0:10]]}\n{[x for x in sns_set[10:20]]}')
my_stopwords = sns_set
else:
# print(f'all_set (custom) stopwords = {len(all_set)} words: \nExamples: \n{[x for x in all_set[0:10]]}\n{[x for x in all_set[10:20]]}')
my_stopwords = all_set
return my_stopwords
# preprocessing functions
stemmer = PorterStemmer()
def lemmatize_stemming(text):
return stemmer.stem(WordNetLemmatizer().lemmatize(text, pos='v'))
def preprocess(text):
result = []
for token in gensim.utils.simple_preprocess(text):
if token not in my_stopwords and len(token) > 2:
result.append(lemmatize_stemming(token))
return result
def word_split(doc):
words = []
for word in doc.split(' '):
words.append(word)
return words
# Infer Topic Probability Distribution of New Document
def infer_topic(new_doc):
pre_new = preprocess(new_doc) # remove stop-words, lemmatize, and stem
dict_new = dictionary.doc2bow(pre_new) # build term-frequency dictionary
first_five = [f'{dict_new[i][0]}: \"{dictionary[dict_new[i][0]]}\"*{dict_new[i][1]}' for i in range(len(dict_new[0:5]))]
vector = model[dict_new] # get topic probability distribution for new_doc
return vector
# Compare two probability distributions using Jensen-Shannon Distance
# distance = sqrt(divergence)
def jsdist(p, q):
return distance.jensenshannon(p, q, base=None)
# Interpolate missing topics and probability values (missing -> 0%)
def interp_jsdist(p, q):
p_topics = [x[0] for x in p]
q_topics = [x[0] for x in q]
p_missing = list(set(q_topics).difference(p_topics))
q_missing = list(set(p_topics).difference(q_topics))
if len(p_missing) > 0:
for i in p_missing:
missing = (i, 0)
p.append(missing)
if len(q_missing) > 0:
for i in q_missing:
missing = (i, 0)
q.append(missing)
p = sorted(vec_A)
q = sorted(vec_B)
print("\nDistributions: ")
print(p)
print(q)
print("\nDistance: ")
pq_dist = jsdist(p, q)[1]
return pq_dist
# run program
if __name__ == '__main__':
filepath = str(sys.argv[1]) # path to saved tf-lda* files, stop.txt, unstop.txt
doc_A = str(sys.argv[2])
doc_B = str(sys.argv[3])
filename_model = filepath + '/' + 'tf-lda.model'
filename_dict = filepath + '/' + 'tf-lda.dict'
filename_stop = filepath + '/' + 'stop.txt'
filename_unstop = filepath + '/' + 'unstop.txt'
my_stopwords = define_stopwords()
new_words = add_words(filename_stop)
new_unstop = remove_words(filename_unstop)
for i in new_words:
my_stopwords.append(i)
my_stopwords = [w for w in my_stopwords if w not in new_unstop]
my_stopwords = list(set(my_stopwords))
model = gensim.models.LdaModel.load(filename_model)
dictionary = corpora.Dictionary.load(filename_dict)
# print all topics
print()
for i in range(0, model.num_topics):
print(f'Topic #{i}: {model.print_topic(i)}')
print(f'\nPerforming inference on documents...')
vec_A = infer_topic(doc_A)
vec_B = infer_topic(doc_B)
print(interp_jsdist(vec_A, vec_B))