-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlsa.py
98 lines (84 loc) · 3.8 KB
/
lsa.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
import os.path
from gensim import corpora
from gensim.models import LsiModel
from gensim.models.coherencemodel import CoherenceModel
import matplotlib.pyplot as plt
import os
def prepare_corpus(doc_clean):
"""
Input : clean document
Purpose: create term dictionary of our courpus and Converting list of documents (corpus) into Document Term Matrix
Output : term dictionary and Document Term Matrix
"""
# Creating the term dictionary of our courpus, where every unique term is assigned an index. dictionary = corpora.Dictionary(doc_clean)
dictionary = corpora.Dictionary(doc_clean)
# Converting list of documents (corpus) into Document Term Matrix using dictionary prepared above.
doc_term_matrix = [dictionary.doc2bow(doc) for doc in doc_clean]
# generate LDA model
return dictionary, doc_term_matrix
def create_gensim_lsa_model(doc_clean, number_of_topics, words, year):
"""
Input : clean document, number of topics and number of words associated with each topic
Purpose: create LSA model using gensim
Output : return LSA model
"""
dictionary, doc_term_matrix = prepare_corpus(doc_clean)
# generate LSA model
lsamodel = LsiModel(doc_term_matrix, num_topics=number_of_topics, id2word=dictionary) # train model
res = lsamodel.print_topics(num_topics=number_of_topics, num_words=words)
#print coherence score
cohere_score = compute_coherence_score(lsamodel, dictionary, doc_clean)
# print("Topics in LSA model:")
# for i in res:
# print(i)
print_topics(lsamodel, year, cohere_score)
return lsamodel
# print lsa results into a file.txt
def print_topics(lsamodel, year, coherence):
path = f'output/{year}/LSA'
if not os.path.exists(path):
os.makedirs(path)
file = open(f'{path}/{year}lsa.txt', 'w')
res = lsamodel.print_topics(num_topics=10, num_words=10)
res = str(res).split(')')
file.write("Coherence : " + str(coherence) + '\n')
for i in res:
file.write(i + '\n')
file.close()
def compute_coherence_values(dictionary, doc_term_matrix, doc_clean, stop, start=2, step=3):
"""
Input : dictionary : Gensim dictionary
corpus : Gensim corpus
texts : List of input texts
stop : Max num of topics
purpose : Compute c_v coherence for various number of topics
Output : model_list : List of LSA 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, stop, step):
# generate LSA model
model = LsiModel(doc_term_matrix, num_topics=num_topics, id2word=dictionary) # train model
model_list.append(model)
coherencemodel = CoherenceModel(model=model, texts=doc_clean, dictionary=dictionary, coherence='c_v')
coherence_values.append(coherencemodel.get_coherence())
return model_list, coherence_values
def plot_graph(doc_clean, start, stop, step, year):
dictionary, doc_term_matrix = prepare_corpus(doc_clean)
model_list, coherence_values = compute_coherence_values(dictionary, doc_term_matrix, doc_clean,
stop, start, step)
# Show graph
x = range(start, stop, step)
plt.plot(x, coherence_values)
plt.xlabel("Number of Topics")
plt.ylabel("Coherence score")
plt.legend(("coherence_values"), loc='best')
path = f'coherence_score_lsa'
if not os.path.exists(path):
os.makedirs(path)
plt.savefig(f'{path}/{year}.png')
#compute cohernce score for LSA model
def compute_coherence_score(model, dictionary, doc_clean):
coherencemodel = CoherenceModel(model=model, texts=doc_clean, dictionary=dictionary, coherence='c_v')
return coherencemodel.get_coherence()