-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgensim_lda.py
132 lines (102 loc) · 4.05 KB
/
gensim_lda.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
# -*- coding: utf-8 -*-
"""
by mdja, itb, 2019
"""
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
import nltk
from nltk.corpus import stopwords
from sklearn.externals import joblib
# Enable logging for gensim - optional
import logging
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.ERROR)
import warnings
warnings.filterwarnings("ignore",category=DeprecationWarning)
def get_datawords(is_load =True, is_saved=False):
if (is_load):
datafile = 'processed_papers.csv'
raw_data = pd.read_csv(datafile)
reindexed_data = raw_data['clean_content']
reindexed_data.index = raw_data['id']
data_words = []
for i in range(reindexed_data.shape[0]):
#for i in range(2):
data_words.append(reindexed_data.iloc[i].split())
if (is_saved):
joblib.dump(data_words,'data_words.dat')
else:
data_words = joblib.load('data_words.dat')
return data_words
def make_bigrams(texts, bigram_mod):
return [bigram_mod[doc] for doc in texts]
def make_trigrams(texts, trigram_mod):
return [trigram_mod[bigram_mod[doc]] for doc in texts]
def compute_performace(lda_model, text_data, corpus, dictionary):
# 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=text_data, dictionary=dictionary, coherence='c_v')
coherence_lda = coherence_model_lda.get_coherence()
print('\nCoherence Score: ', coherence_lda)
nltk.download('punkt')
stop_words = stopwords.words('english')
data_words = get_datawords()
# Build the bigram and trigram models
bigram = gensim.models.phrases.Phrases(data_words, min_count=5, threshold=100) # higher threshold fewer phrases.
trigram = gensim.models.phrases.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
pprint(trigram_mod[bigram_mod[data_words[0]]])
# Form Bigrams
data_words_bigrams = make_bigrams(data_words, bigram_mod)
#nlp = spacy.load('en', disable=['parser', 'ner'])
#data_lemmatized = lemmatization(data_words_bigrams, allowed_postags=['NOUN', 'ADJ', 'VERB', 'ADV'])
#print(data_lemmatized[:1])
# Create Dictionary
dictionary = corpora.Dictionary(data_words_bigrams)
joblib.dump(dictionary,'dictionary.dat')
# Create Corpus
texts = data_words_bigrams
# Term Document Frequency
corpus = [dictionary.doc2bow(text) for text in texts]
joblib.dump(corpus,'corpus.dat')
# View
#print(corpus[:1])
# Human readable format of corpus (term-frequency)
[[(dictionary[id], freq) for id, freq in cp] for cp in corpus[:1]]
is_load_model = False
if (not is_load_model):
lda_model = gensim.models.ldamodel.LdaModel(corpus=corpus,
id2word=dictionary,
num_topics=20,
random_state=100,
update_every=1,
chunksize=100,
passes=10,
alpha='auto',
per_word_topics=True)
joblib.dump(lda_model, 'lda_model_gensim.dat')
else:
lda_model = joblib.load('lda_model_gensim.dat')
# Print the Keyword in the 10 topics
pprint(lda_model.print_topics())
doc_lda = lda_model[corpus]
#compute_performace(lda_model, data_words_bigrams, corpus, dictionary)
pyLDAvis.enable_notebook()
vis = pyLDAvis.gensim.prepare(lda_model, corpus, dictionary)
vis