-
Notifications
You must be signed in to change notification settings - Fork 0
/
Topic Modelling.py
50 lines (40 loc) · 1.65 KB
/
Topic Modelling.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
# CREATING DICTIONARY AS WORD AND VECTOR
# CORPUS TEXT WITHOUT PUNCTUATIONS,ALPHABETS AND WORD ROOT FORM
# LSI ,HDP AND LDA MODELS
def dict_corp(file) :
file1=file+'_Token.txt'
corpus=file+'_Corpus.pkl'
lsid=file+'_Lsi.gensim'
HDP=file+'_HDP.gensim
LDA=file+'_LDA.gensim'
Dict=file+'_dictionary.gensim'
text_data=''
abs1=[]
with open(file1) as f3:
text_data = simplejson.load(f3)
# DICTIONARY SAVE
Dictionary = corpora.Dictionary(text_data)
Dictionary.save(Dict)
# CORPUS SAVE
Corpus = [Dictionary.doc2bow(text) for text in text_data]
pickle.dump(Corpus, open(corpus, 'wb'))
# LSI MODEL CODE
Lsi_Model=LsiModel(corpus=Corpus,num_topics=10,id2word=Dictionary)
Lsi_Model.save(lsid)
# HDP MODEL
HDP_Model=HdpModel(corpus=Corpus,id2word=Dictionary)
HDP_Model.save(HDP)
Ldamodel = gensim.models.ldamodel.LdaModel(corpus, num_topics = 10, id2word=dictionary, passes=10)
Ldamodel.save(LDA)
lda_display = pyLDAvis.gensim.prepare(Ldamodel, corpus, dictionary, sort_topics=False)
pyLDAvis.display(lda_display)
#FILE NAMES
EFiles=['3_1_WEEK','3_2_WEEK','3_3_WEEK','3_4_WEEK','3_5_WEEK',
'4_1_WEEK','4_2_WEEK','4_3_WEEK','4_4_WEEK','4_5_WEEK',
'5_1_WEEK','5_2_WEEK','5_3_WEEK','5_4_WEEK','5_5_WEEK',
'6_1_WEEK','6_2_WEEK','6_3_WEEK','6_4_WEEK','6_5_WEEK',
'7_1_WEEK','7_2_WEEK','7_3_WEEK','7_4_WEEK','7_5_WEEK']
# PASSING FILE NAME TO DICTIONARY AND CORPUS AND LSI MODEL
for E in EFiles:
print(E)
dict_corp(E)