-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
289 lines (230 loc) · 9 KB
/
models.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import os
import multiprocessing
import pickle
import wget
from gensim.corpora import Dictionary, HashDictionary
from gensim.models import LdaModel, LsiModel, KeyedVectors, RpModel, TfidfModel
from gensim.models.doc2vec import Doc2Vec, TaggedDocument
def text_corpus(dataframe):
"""Returns a list of unique documents stored in a DataFrame.
Precomputed corpuses are read from file if previously cached, or generated then cached otherwise.
Parameters
----------
dataframe : Pandas DataFrame
The DataFrame containing the documents to process.
Returns
-------
corpus : list(str)
List of unique documents stored in the DataFrame.
"""
filename = 'caches/models/corpus.model'
if not os.path.isfile(filename):
corpus = set(dataframe['Headline'].values).union(dataframe['articleBody'].values)
corpus = [doc.split() for doc in corpus]
pickle.dump(corpus, open(filename, 'wb'))
else:
corpus = pickle.load(open(filename, 'rb'))
return corpus
def dictionary_corpus(dataframe):
"""Returns a Dictionary mapping words to ids.
Precomputed Dictionaries are read from file if previously cached, or generated then cached otherwise.
Parameters
----------
dataframe : Pandas DataFrame
The DataFrame containing the documents to process.
Returns
-------
dictionary : Gensim Dictionary
Dictionary mapping words to ids.
"""
filename = 'caches/models/dictionary.model'
if not os.path.isfile(filename):
corpus = text_corpus(dataframe)
dictionary = Dictionary(corpus)
dictionary.save(filename)
else:
dictionary = Dictionary.load(filename)
return dictionary
def hashdictionary_corpus(dataframe, id_range=32000):
"""Returns a HashDictionary mapping words to ids.
Precomputed HashDictionaries are read from file if previously cached, or generated then cached otherwise.
Parameters
----------
dataframe : Pandas DataFrame
The DataFrame containing the documents to process.
id_range : int
The maximum number of ids available.
Returns
-------
dictionary : Gensim HashDictionary
HashDictionary mapping words to ids.
"""
filename = 'caches/models/dictionary_{}.model'.format(id_range)
if not os.path.isfile(filename):
corpus = text_corpus(dataframe)
dictionary = HashDictionary(corpus, id_range=id_range)
dictionary.save(filename)
else:
dictionary = HashDictionary.load(filename)
return dictionary
def bow_corpus(dataframe):
"""Returns a list of BoW vectors corresponding to documents stored in a DataFrame.
Precomputed BoW vectors are read from file if previously cached, or generated then cached otherwise.
Parameters
----------
dataframe : Pandas DataFrame
The DataFrame containing the documents to process.
Returns
-------
corpus : list(list((int,int)))
List of BoW vectors corresponding documents stored in the DataFrame.
"""
filename = 'caches/models/bow.model'
if not os.path.isfile(filename):
corpus = text_corpus(dataframe)
dictionary = dictionary_corpus(dataframe)
bow = [dictionary.doc2bow(doc) for doc in corpus]
pickle.dump(bow, open(filename, 'wb'))
else:
bow = pickle.load(open(filename, 'rb'))
return bow
def tfidf(dataframe, max_words=None):
"""Returns a tf-idf model for documents stored in a DataFrame.
Precomputed models are read from file if previously cached, or generated then cached otherwise.
Parameters
----------
dataframe : Pandas DataFrame
The DataFrame containing the documents to process.
max_words : int (default is 2000000)
The maximum number of words stored by the model.
Returns
-------
model : Gensim TfidfModel
tf-idf model for documents stored in the DataFrame.
"""
suffix = '_{}'.format(max_words) if max_words else ''
filename = 'caches/models/tfidf{}.model'.format(suffix)
if not os.path.isfile(filename):
if max_words:
dictionary = hashdictionary_corpus(dataframe, id_range=max_words)
else:
dictionary = dictionary_corpus(dataframe)
tfidf_model = TfidfModel(dictionary=dictionary)
tfidf_model.save(filename)
else:
tfidf_model = TfidfModel.load(filename)
return tfidf_model
def lsi(dataframe, num_topics=300):
"""Returns an LSI model for documents stored in a DataFrame.
Precomputed models are read from file if previously cached, or generated then cached otherwise.
Parameters
----------
dataframe : Pandas DataFrame
The DataFrame containing the documents to process.
num_topics : int (default is 300)
The number of topics to train the LSI model with.
Returns
-------
model : Gensim LsiModel
LSI model for documents stored in the DataFrame.
"""
filename = 'caches/models/lsi.model'
if not os.path.isfile(filename):
dictionary = dictionary_corpus(dataframe)
bow = bow_corpus(dataframe)
tfidf_model = tfidf(dataframe)
tfidf_corpus = tfidf_model[bow]
lsi_model = LsiModel(tfidf_corpus, id2word=dictionary, num_topics=num_topics)
lsi_model.save(filename)
else:
lsi_model = LsiModel.load(filename)
return lsi_model
def rp(dataframe, num_topics=300):
"""Returns an RP model for documents stored in a DataFrame.
Precomputed models are read from file if previously cached, or generated then cached otherwise.
Parameters
----------
dataframe : Pandas DataFrame
The DataFrame containing the documents to process.
num_topics : int (default is 300)
The number of topics to train the RP model with.
Returns
-------
model : Gensim RpModel
RP model for documents stored in the DataFrame.
"""
filename = 'caches/models/rp.model'
if not os.path.isfile(filename):
dictionary = dictionary_corpus(dataframe)
bow = bow_corpus(dataframe)
tfidf_model = tfidf(dataframe)
tfidf_corpus = tfidf_model[bow]
rp_model = RpModel(tfidf_corpus, id2word=dictionary, num_topics=num_topics)
rp_model.save(filename)
else:
rp_model = RpModel.load(filename)
return rp_model
def lda(dataframe, num_topics=100):
"""Returns an LDA model for documents stored in a DataFrame.
Precomputed models are read from file if previously cached, or generated then cached otherwise.
Parameters
----------
dataframe : Pandas DataFrame
The DataFrame containing the documents to process.
num_topics : int (default is 300)
The number of topics to train the LDA model with.
Returns
-------
model : Gensim LdaMulticore
LDA model for documents stored in the DataFrame.
"""
filename = 'caches/models/lda.model'
if not os.path.isfile(filename):
dictionary = dictionary_corpus(dataframe)
bow = bow_corpus(dataframe)
lda_model = LdaModel(bow, id2word=dictionary, num_topics=num_topics, passes=20)
lda_model.save(filename)
else:
lda_model = LdaModel.load(filename)
return lda_model
_word2vec_model = None
def word2vec():
"""Returns a word2vec model pretrained on the Google News dataset.
A single instance of the model is lazily loaded.
Returns
-------
model : Gensim Word2Vec
word2vec model pretrained on the Google News dataset.
"""
global _word2vec_model
if not _word2vec_model:
filename = 'pretrained_models/GoogleNews-vectors-negative300.bin.gz'
if not os.path.isfile(filename):
wget.download('https://s3.ca-central-1.amazonaws.com/fnc-1/GoogleNews-vectors-negative300.bin.gz', out='pretrained_models')
_word2vec_model = KeyedVectors.load_word2vec_format('pretrained_models/GoogleNews-vectors-negative300.bin.gz', binary=True)
_word2vec_model.init_sims(replace=True)
return _word2vec_model
def doc2vec(dataframe, size=100):
"""Returns a doc2vec model for documents stored in a DataFrame.
Precomputed models are read from file previously cached, or generated then cached otherwise.
Parameters
----------
dataframe : Pandas DataFrame
The DataFrame containing the documents to process.
size : int (default is 100)
Dimensionality of the generated document vectors.
Returns
-------
model : Gensim Doc2Vec
doc2vec model for documents stored in the DataFrame.
"""
filename = 'caches/models/doc2vec.model'
if not os.path.isfile(filename):
corpus = text_corpus(dataframe)
tagged_documents = [TaggedDocument(doc, [i]) for i, doc in enumerate(corpus)]
doc2vec_model = Doc2Vec(tagged_documents, size=size, window=10, min_count=2, iter=20, workers=multiprocessing.cpu_count())
doc2vec_model.delete_temporary_training_data(keep_doctags_vectors=False)
doc2vec_model.save(filename)
else:
doc2vec_model = Doc2Vec.load(filename)
return doc2vec_model