Repository navigation
Expand file tree
/
Copy pathVisualizeNLPModelTopics.py
More file actions
76 lines (65 loc) · 2.89 KB
/
Copy pathVisualizeNLPModelTopics.py
File metadata and controls
76 lines (65 loc) · 2.89 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
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 22 15:21:22 2019
@author: sabbatim
"""
#import modules
import gensim.corpora as corpora
import gensim
import pyLDAvis
import pyLDAvis.gensim
from PDFtoDatabase import cleanupdocuments #custom cleanup module
import SQLite #custom sql query module
import sys
#user variables
#location of sqlite database
database_location = r"E:\NATCARB\NATCARBCleanuptool\Test_Training_Data\WorkingPapersNATCARB.sqlite"
# =============================================================================
# Using sys.argv requires the user to make a command line argument so if this
# is being run from an IDE then you'll want to use the above line to get to
# your database. If used from the command line you need to add your path to the
# database. To run from a UNIX command line us the line below, make sure to
# change your path.
# python VisualizeNLPModelTopcs.py '/home/greenbur/NLP/Python Code/WorkingPapersCS.sqlite'
# Same thing goes for the savemodelpath below. This will add another argument
# to your command line. So you will run the below command, keep in mind the path
# needs to be changed:
# python VisualizeNLPModelTopcs.py '/home/greenbur/NLP/Python Code/WorkingPapersCS.sqlite' '/home/greenbur/NLP/Results/GOMXlgvec.txt'
# The above command is all one line.
# =============================================================================
#database_location = sys.argv[1]
#path where model will be saved
savemodelpath = r"E:\NATCARB\NATCARBCleanuptool\Test_Training_Data\NATCARBvec2.txt"
#savemodelpath = sys.argv[2]
#load document data from database
#connect to sqlite database and load data
cursor, conn = SQLite.connect_to_databse(database_location)
datatable = SQLite.list_all_rows(cursor, 'papers')
#collect paper text and load to python list
paperdata = []
for row in datatable:
paperdata.append(row[6])
#clean text for word to vec processing
cleandoc = cleanupdocuments(paperdata)
print("Documents loaded and ready to process")
#Generate dictionary
id2word = corpora.Dictionary(cleandoc)
#generate corpus with doc2bow
corpus = [id2word.doc2bow(text) for text in cleandoc]
#generate LDA model for 10 topics
print("generating LDA model")
lda_model = gensim.models.ldamodel.LdaModel(corpus=corpus,
id2word=id2word,
num_topics=10,
random_state=100,
update_every=1,
chunksize=100,
passes=10,
alpha='auto',
per_word_topics=True)
print("print topics")
print(lda_model.show_topics())
#generate Visualiziation
vis = pyLDAvis.gensim.prepare(lda_model, corpus, id2word)
print("Open interactive visualization in web browser")
pyLDAvis.show(vis)