-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlda.py
More file actions
398 lines (331 loc) · 18.2 KB
/
Copy pathlda.py
File metadata and controls
398 lines (331 loc) · 18.2 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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import re
import numpy as np
import pandas as pd
from pprint import pprint
# Gensim
import gensim
import gensim.corpora as corpora
from gensim.utils import simple_preprocess
from gensim.models import CoherenceModel
# spacy for lemmatization
import spacy
# Plotting tools
import pyLDAvis
import pyLDAvis.gensim # don't skip this
import matplotlib.pyplot as plt
# Enable logging for gensim - optional
import logging
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.ERROR)
# NLTK Stop words
from nltk.corpus import stopwords
stop_words = stopwords.words('english')
stop_words.extend(['say', 'says', 'said', 'year', 'time', 'way',
'week', 'make', 'thursday', 'monday', 'tuesday', 'wednesday', 'friday' ])
#show all padas columns for readability
pd.set_option('display.max_columns', None)
#import data
df = pd.read_csv('data/articles_2014-2018_clean.csv')
df = df[['text', 'article_id', 'author_id']]
df.head()
#turn text column back in to list (pandas load is making it a string)
df['text'] = df['text'].str.replace('[','').str.replace(']', '').str.replace("'", "")
#df['text'] = df['text'].map(lambda x: [x])
# Convert to list
data = df['text'].values.tolist()
#tokenize
def sent_to_words(sentences):
for sentence in sentences:
yield(gensim.utils.simple_preprocess(str(sentence), deacc=True)) # deacc=True removes punctuations
data_words = list(sent_to_words(data))
# Build the bigram and trigram models
bigram = gensim.models.Phrases(data_words, min_count=5, threshold=100) # higher threshold fewer phrases.
trigram = gensim.models.Phrases(bigram[data_words], threshold=100)
# Faster way to get a sentence clubbed as a trigram/bigram
bigram_mod = gensim.models.phrases.Phraser(bigram)
trigram_mod = gensim.models.phrases.Phraser(trigram)
# See trigram example
print(trigram_mod[bigram_mod[data_words[200]]])
# Define functions for stopwords, bigrams, trigrams and lemmatization
def remove_stopwords(texts):
return [[word for word in simple_preprocess(str(doc)) if word not in stop_words] for doc in texts]
def make_bigrams(texts):
return [bigram_mod[doc] for doc in texts]
def make_trigrams(texts):
return [trigram_mod[bigram_mod[doc]] for doc in texts]
def lemmatization(texts, allowed_postags=['NOUN', 'VERB']): #, 'ADJ', 'VERB', 'ADV'
"""https://spacy.io/api/annotation"""
texts_out = []
for sent in texts:
doc = nlp(" ".join(sent))
texts_out.append([token.lemma_ for token in doc if token.pos_ in allowed_postags])
return texts_out
# Remove Stop Words
data_words_nostops = remove_stopwords(data_words)
# Form Bigrams
data_words_bigrams = make_bigrams(data_words_nostops)
# Initialize spacy 'en' model, keeping only tagger component (for efficiency)
# python3 -m spacy download en
nlp = spacy.load('en', disable=['parser', 'ner'])
# Do lemmatization keeping only noun, vb
data_lemmatized = lemmatization(data_words_bigrams, allowed_postags=['NOUN', 'VERB' ]) #'ADV', 'ADJ'
# Create Dictionary and remove extremely common and rare words
id2word = corpora.Dictionary(data_lemmatized)
id2word.filter_extremes(no_below=15, no_above=0.5, keep_n= 100000)
# Create Corpus
texts = data_lemmatized
# Term Document Frequency
corpus = [id2word.doc2bow(text) for text in texts]
# Human readable format of corpus (term-frequency)
[[(id2word[id], freq) for id, freq in cp] for cp in corpus[:1]]
# Build LDA model #1
lda_model = gensim.models.ldamodel.LdaModel(corpus=corpus,
id2word=id2word,
num_topics=34,
random_state=1,
update_every=1,
chunksize=500,
passes=10,
alpha='auto')
'''
# Build LDA model #2
lda_model2 = gensim.models.ldamodel.LdaModel(corpus=corpus,
id2word=id2word,
num_topics=16,
random_state=1,
update_every=1,
chunksize=500,
passes=10,
alpha='auto')
'''
# Print the Keyword in the 10 topics
pprint(lda_model.print_topics(num_topics=34))
doc_lda = lda_model[corpus]
# Compute Perplexity
print('\nPerplexity: ', lda_model.log_perplexity(corpus)) # a measure of how good the model is. lower the better.
# Compute Coherence Score
coherence_model_lda = CoherenceModel(model=lda_model, texts=data_lemmatized, dictionary=id2word, coherence='c_v')
coherence_lda = coherence_model_lda.get_coherence()
print('\nCoherence Score: ', coherence_lda)
# Visualize the topics
pyLDAvis.disable_notebook()
vis = pyLDAvis.gensim.prepare(lda_model, corpus, id2word)
#save as HTML
pyLDAvis.save_html(vis, 'lda.html')
# try using mallets LDA - gensim has a wrapper to allow it to be buuilt on top of the gensim lda
mallet_path = '/home/jake/data_science/mallet/mallet-2.0.8/bin/mallet'
ldamallet = gensim.models.wrappers.LdaMallet(mallet_path, corpus=corpus, num_topics=34, id2word=id2word)
# Show Topics from mallet
pprint(ldamallet.show_topics(formatted=False))
# Compute Coherence Score from mallet
coherence_model_ldamallet = CoherenceModel(model=ldamallet, texts=data_lemmatized, dictionary=id2word, coherence='c_v')
coherence_ldamallet = coherence_model_ldamallet.get_coherence()
print('\nCoherence Score: ', coherence_ldamallet)
#use this to determine how many topics to use. it loops through and tries the model many different times
def compute_coherence_values(dictionary, corpus, texts, limit, start=2, step=3):
"""
Compute c_v coherence for various number of topics
Parameters:
----------
dictionary : Gensim dictionary
corpus : Gensim corpus
texts : List of input texts
limit : Max num of topics
Returns:
-------
model_list : List of LDA topic models
coherence_values : Coherence values corresponding to the LDA model with respective number of topics
"""
coherence_values = []
model_list = []
for num_topics in range(start, limit, step):
model = gensim.models.wrappers.LdaMallet(mallet_path, corpus=corpus, num_topics=num_topics, id2word=id2word)
model_list.append(model)
coherencemodel = CoherenceModel(model=model, texts=texts, dictionary=dictionary, coherence='c_v')
coherence_values.append(coherencemodel.get_coherence())
return model_list, coherence_values
#run the above compute coherence values to find the best
# Can take a long time to run.
model_list, coherence_values = compute_coherence_values(dictionary=id2word,
corpus=corpus, texts=data_lemmatized,
start=10, limit=45, step=2)
#plot results of the multiple mallet topic tries
# Show graph
limit=45; start=10; step=2;
x = range(start, limit, step)
plt.plot(x, coherence_values)
plt.title('Coherence Score per Number of Topics')
plt.xlabel("Num Topics")
plt.ylabel("Coherence score")
plt.legend(("coherence_values"), loc='best')
plt.show()
# Print the coherence scores
for m, cv in zip(x, coherence_values):
print("Num Topics =", m, " has Coherence Value of", round(cv, 4))
# Select the model and print the topics
optimal_model = model_list[12] #model 12 is the best (13th listed in the 34 topics)
model_topics = optimal_model.show_topics(formatted=False)
pprint(optimal_model.print_topics(num_topics=34, num_words=20))
#create a table to show dominant topic for each document
def format_topics_sentences(ldamodel=optimal_model, corpus=corpus, texts=data):
# Init output
sent_topics_df = pd.DataFrame()
# Get main topic in each document
for i, row in enumerate(ldamodel[corpus]):
row = sorted(row, key=lambda x: (x[1]), reverse=True)
# Get the Dominant topic, Perc Contribution and Keywords for each document
for j, (topic_num, prop_topic) in enumerate(row):
if j == 0: # => dominant topic
wp = ldamodel.show_topic(topic_num)
topic_keywords = ", ".join([word for word, prop in wp])
sent_topics_df = sent_topics_df.append(pd.Series([int(topic_num), round(prop_topic,4), topic_keywords]), ignore_index=True)
else:
break
sent_topics_df.columns = ['Dominant_Topic', 'Perc_Contribution', 'Topic_Keywords']
# Add original text to the end of the output
contents = pd.Series(texts)
sent_topics_df = pd.concat([sent_topics_df, contents], axis=1)
return(sent_topics_df)
df_topic_sents_keywords = format_topics_sentences(ldamodel=optimal_model, corpus=corpus, texts=data)
# create df of each article with associated dominant topic
df_dominant_topic = df_topic_sents_keywords.reset_index()
df_dominant_topic.columns = ['Document_No', 'Dominant_Topic', 'Topic_Perc_Contrib', 'Keywords', 'Text']
#add topics to articles df and clean up unneded columns.
df_articles = pd.read_csv('data/articles_2014-2018_clean.csv')
df_articles = pd.concat([df_articles, df_dominant_topic], axis=1, sort=False)
df_articles = df_articles.drop(['Text', 'Unnamed: 0'], axis=1)
#create topics df
# Number of Documents for Each Topic
topic_counts = df_topic_sents_keywords['Dominant_Topic'].value_counts().sort_index()
# Percentage of Documents for Each Topic
topic_contribution = round(topic_counts/topic_counts.sum(), 4).sort_index()
# Topic Number and Keywords
topic_num_keywords = df_topic_sents_keywords[['Dominant_Topic', 'Topic_Keywords']]
topic_num_keywords = topic_num_keywords.drop_duplicates(subset ="Dominant_Topic").set_index('Dominant_Topic')
topic_num_keywords = topic_num_keywords.sort_index()
# Concatenate
df_topics = pd.concat([topic_num_keywords, topic_counts, topic_contribution], axis=1, sort=False)
# Change Column names and re-index to get topic num as a column
df_topics.columns = ['topic_keywords', 'num_docs', 'percent_docs']
df_topics.reset_index(level=0, inplace=True)
df_topics.columns = ['topic', 'topic_keywords', 'num_docs', 'percent_docs']
#create topic names (manually inferred)
topic_names = ['Presidential Politics', 'Society', 'Hope and Resilience',
'Performing Arts', 'Economy', 'Technology', 'World News',
'National News', 'Court and Law', 'Disaster', 'Elections',
'Conflict', 'Business', 'Education', 'Fashion',
'Food', 'Family', 'Art', 'Transportation',
'Announcements', 'Housing', 'Crime', 'Movements',
'Government and Regulation', 'Music', 'Innovation', 'Film and TV',
'Science and Research', 'Foreign Affairs', 'Local Sports', 'Travel',
'Football (and Futbol)', 'Health', 'Books']
#add topic names to articles df and save
mydict = {v: k for v, k in enumerate(topic_names)}
df_articles['topic_name'] = df_articles['Dominant_Topic'].map(mydict)
#insert topic names in to topics df
topic_names = pd.Series(topic_names)
df_topics = pd.concat([df_topics, topic_names], axis=1, sort=False)
df_topics.columns = ['topic', 'topic_keywords', 'num_docs', 'percent_docs', 'topic_name']
#save topics df
df_topics.to_csv('data/topics.csv')
#OHE the dominant topic column
df_articles['Dominant_Topic'] = df_articles['Dominant_Topic'].astype('int16')
df_articles['topic_num'] = df_articles['Dominant_Topic']
df_articles = pd.get_dummies(df_articles, prefix=['topic_num'], columns=['Dominant_Topic'])
#save df_articles
df_articles.to_csv('data/articles.csv')
#convert mallet model to gensim in order to display using pyLDAvis
lda_model_mallet = gensim.models.wrappers.ldamallet.malletmodel2ldamodel(ldamallet)
# Visualize the mallet LDA topics
pyLDAvis.disable_notebook()
mallet_vis = pyLDAvis.gensim.prepare(lda_model_mallet, corpus, id2word)
#save as HTML
pyLDAvis.save_html(mallet_vis, 'lda_mallet.html')
#create authors df with counts of articles in each topic
df_authors = df_articles[['author_id', 'byline_person_0_firstname', 'byline_person_0_middlename', 'byline_person_0_lastname', 'author']]
df_authors = df_authors.drop_duplicates().set_index('author_id')
df_authors.reset_index(level=0, inplace=True)
df_authors_sums = df_articles.groupby(['author_id']).sum()
df_authors_sums = df_authors_sums.drop(['Document_No', 'Topic_Perc_Contrib', 'topic_num'], axis=1)
df_authors_sums.reset_index(level=0, inplace=True)
df_authors = df_authors.set_index('author_id').join(df_authors_sums.set_index('author_id'))
df_authors = df_authors.dropna(axis='rows')
#add percentage columns to authors df #find a more pythonic way to do this!!!!!
df_authors['total_articles'] = df_authors.sum(axis=1) #total articles for each author
df_authors['topic_num_0_perc'] = df_authors['topic_num_0'] / df_authors['total_articles']
df_authors['topic_num_1_perc'] = df_authors['topic_num_1'] / df_authors['total_articles']
df_authors['topic_num_2_perc'] = df_authors['topic_num_2'] / df_authors['total_articles']
df_authors['topic_num_3_perc'] = df_authors['topic_num_3'] / df_authors['total_articles']
df_authors['topic_num_4_perc'] = df_authors['topic_num_4'] / df_authors['total_articles']
df_authors['topic_num_5_perc'] = df_authors['topic_num_5'] / df_authors['total_articles']
df_authors['topic_num_6_perc'] = df_authors['topic_num_6'] / df_authors['total_articles']
df_authors['topic_num_7_perc'] = df_authors['topic_num_7'] / df_authors['total_articles']
df_authors['topic_num_8_perc'] = df_authors['topic_num_8'] / df_authors['total_articles']
df_authors['topic_num_9_perc'] = df_authors['topic_num_9'] / df_authors['total_articles']
df_authors['topic_num_10_perc'] = df_authors['topic_num_10'] / df_authors['total_articles']
df_authors['topic_num_11_perc'] = df_authors['topic_num_11'] / df_authors['total_articles']
df_authors['topic_num_12_perc'] = df_authors['topic_num_12'] / df_authors['total_articles']
df_authors['topic_num_13_perc'] = df_authors['topic_num_13'] / df_authors['total_articles']
df_authors['topic_num_14_perc'] = df_authors['topic_num_14'] / df_authors['total_articles']
df_authors['topic_num_15_perc'] = df_authors['topic_num_15'] / df_authors['total_articles']
df_authors['topic_num_16_perc'] = df_authors['topic_num_16'] / df_authors['total_articles']
df_authors['topic_num_17_perc'] = df_authors['topic_num_17'] / df_authors['total_articles']
df_authors['topic_num_18_perc'] = df_authors['topic_num_18'] / df_authors['total_articles']
df_authors['topic_num_19_perc'] = df_authors['topic_num_19'] / df_authors['total_articles']
df_authors['topic_num_20_perc'] = df_authors['topic_num_20'] / df_authors['total_articles']
df_authors['topic_num_21_perc'] = df_authors['topic_num_21'] / df_authors['total_articles']
df_authors['topic_num_22_perc'] = df_authors['topic_num_22'] / df_authors['total_articles']
df_authors['topic_num_23_perc'] = df_authors['topic_num_23'] / df_authors['total_articles']
df_authors['topic_num_24_perc'] = df_authors['topic_num_24'] / df_authors['total_articles']
df_authors['topic_num_25_perc'] = df_authors['topic_num_25'] / df_authors['total_articles']
df_authors['topic_num_26_perc'] = df_authors['topic_num_26'] / df_authors['total_articles']
df_authors['topic_num_27_perc'] = df_authors['topic_num_27'] / df_authors['total_articles']
df_authors['topic_num_28_perc'] = df_authors['topic_num_28'] / df_authors['total_articles']
df_authors['topic_num_29_perc'] = df_authors['topic_num_29'] / df_authors['total_articles']
df_authors['topic_num_30_perc'] = df_authors['topic_num_30'] / df_authors['total_articles']
df_authors['topic_num_31_perc'] = df_authors['topic_num_31'] / df_authors['total_articles']
df_authors['topic_num_32_perc'] = df_authors['topic_num_32'] / df_authors['total_articles']
df_authors['topic_num_33_perc'] = df_authors['topic_num_33'] / df_authors['total_articles']
df_authors = df_authors.round(2) #round off for easy percentage reading
#add dominant topic for each author
df_authors['dominant_topic'] = df_authors[['topic_num_0',
'topic_num_1',
'topic_num_2',
'topic_num_3',
'topic_num_4',
'topic_num_5',
'topic_num_6',
'topic_num_7',
'topic_num_8',
'topic_num_9',
'topic_num_10',
'topic_num_11',
'topic_num_12',
'topic_num_13',
'topic_num_14',
'topic_num_15',
'topic_num_16',
'topic_num_17',
'topic_num_18',
'topic_num_19',
'topic_num_20',
'topic_num_21',
'topic_num_22',
'topic_num_23',
'topic_num_24',
'topic_num_25',
'topic_num_26',
'topic_num_27',
'topic_num_28',
'topic_num_29',
'topic_num_30',
'topic_num_31',
'topic_num_32',
'topic_num_33'
]].idxmax(axis=1)
df_authors['dominant_topic'] = df_authors['dominant_topic'].str.replace('topic_num_', '')
df_authors['dominant_topic'] = df_authors['dominant_topic'].astype('int16')
#add topic names to authors df and save
mydict = {v: k for v, k in enumerate(topic_names)}
df_authors['dominant_topic_name'] = df_authors['dominant_topic'].map(mydict)
df_authors = df_authors.reset_index()
df_authors.to_csv('data/authors.csv')