-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathinfer_topics.py
More file actions
167 lines (133 loc) · 4.86 KB
/
infer_topics.py
File metadata and controls
167 lines (133 loc) · 4.86 KB
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
import sys
import os
import nltk
import spacy
import gensim
import sklearn
import keras
import pandas as pd
import numpy as np
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):
print("(1) Performing preprocessing...")
pre_new = preprocess(new_doc) # remove stop-words, lemmatize, and stem
print(f'{pre_new[0:5]} ...')
print("(2) Building term-frequency dictionary...")
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]))]
print(f'{[x for x in first_five]}')
print("(3) Inferring topic distribution...")
vector = model[dict_new] # get topic probability distribution for new_doc
print("\nTopic Probability Distribution:")
print(vector)
if __name__ == '__main__':
filepath = str(sys.argv[1]) # path to saved tf-lda* files, stop.txt, unstop.txt
new_doc = str(sys.argv[2])
filename_model = filepath + '/' + 'tf-lda.model'
filename_dict = filepath + '/' + 'tf-lda.dict'
filename_stop = filepath + '/' + 'stop.txt'
filename_unstop = filepath + '/' + 'unstop.txt'
print(f'\nLoading model files and stopwords...')
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))
print(f'Loaded {len(my_stopwords)} stopwords.\n')
model = gensim.models.LdaModel.load(filename_model)
dictionary = corpora.Dictionary.load(filename_dict)
# print all topics
for i in range(0, model.num_topics):
print(f'Topic #{i}: {model.print_topic(i)}')
print(f'\nPerforming inference on new document...')
infer_topic(new_doc)