-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodelTopics.py
137 lines (123 loc) · 4.34 KB
/
modelTopics.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
133
134
135
136
137
import sqlite3 as sql
import os, sys, codecs, logging, itertools, pickle
from sys import stdout
from collections import defaultdict
from operator import itemgetter
from gensim import corpora, models
from gensim.utils import smart_open, simple_preprocess
from gensim.parsing.preprocessing import STOPWORDS
sys.stdout = codecs.getwriter('utf8')(sys.stdout)
logging.basicConfig(format='%(levelname)s : %(message)s', level=logging.INFO)
logging.root.level = logging.INFO
STOPWORDS2 = set(['january','february','march','april','may','june','july','august','september','october','november','december'])
STOPWORDS2.update(list(STOPWORDS))
def tokenize(text):
return [token for token in simple_preprocess(text) \
if token not in STOPWORDS2]
#iterates over all threads, returning a (tid, tokens) tuple
def iter_tokdocs(path):
con = sql.connect(path)
cur = con.cursor()
command = '''SELECT thread_id FROM Threads WHERE parent_bid=1'''
cur.execute(command)
threads = cur.fetchall()
for tid in threads:
command = '''SELECT posts FROM Threads t
WHERE t.thread_id = ?'''
cur.execute(command, (tid[0],))
text = cur.fetchone()
tokens = tokenize(text[0])
if len(tokens) > 200:
yield tid[0], tokens
def len_tokdocs(path):
con = sql.connect(path)
cur = con.cursor()
command = '''SELECT COUNT(*) FROM Threads WHERE parent_bid=1'''
cur.execute(command)
return cur.fetchone()[0]
class ThreadsCorpus(object):
def __init__(self, db_path, dictionary, clip_docs=None):
self.db_path = db_path
self.dictionary = dictionary
self.clip_docs = clip_docs
def __iter__(self):
self.titles = []
for title, tokens in itertools.islice(iter_tokdocs(self.db_path), self.clip_docs):
self.titles.append(title)
yield self.dictionary.doc2bow(tokens)
def __len__(self):
return len_tokdocs(db_path)
def exportThread(con, tid, path):
command = '''
SELECT html_content FROM Threads WHERE thread_id=?
'''
with con:
cur = con.cursor()
cur.execute(command, (tid,))
ablob = cur.fetchone()
with open(path, 'wb') as f:
f.write(ablob[0])
def exportCorpus(dbpath, cpath, dpath):
print("Creating corpus...")
id2word = corpora.Dictionary(tokens for _, tokens in iter_tokdocs(dbpath))
id2word.filter_extremes(no_below=20, no_above=0.2)
id2word.save(dpath)
threads_corpus = ThreadsCorpus(dbpath, id2word)
corpora.MmCorpus.serialize(cpath, threads_corpus)
def modelTopics(cpath, dpath, mpath, numTopics):
print("Modeling topics...")
mm_corpus = corpora.MmCorpus(cpath)
id2word_docs = corpora.Dictionary.load(dpath)
lda_model = models.LdaModel(mm_corpus, num_topics=numTopics, id2word=id2word_docs, passes=100, iterations=50)
lda_model.save(mpath)
print(lda_model.print_topics(-1))
def returnlist():
return []
def exportTopics(dbpath, dpath, mpath):
th2to = {}
to2th = {}
lda_model = models.LdaModel.load(mpath)
dictionary = corpora.Dictionary.load(dpath)
topics = ['_'.join([w[1] for w in lda_model.show_topic(i)]) for i in range(lda_model.num_topics)]
l = len_tokdocs(dbpath)
for i, thread in enumerate(iter_tokdocs(dbpath)):
th2to[thread[0]] = lda_model[dictionary.doc2bow(thread[1])]
stdout.write('\rtransforming topics: {}%'.format(int(100*i/l)))
stdout.flush()
#"""
to2th = defaultdict(returnlist)
for thread in th2to.keys():
for topic in th2to[thread]:
to2th[topic[0]].append((thread, topic[1]))
#"""
#to2th[topic[0]] = [[(h, o[1]) for o in th2to[h]] for h in th2to.keys()]
con = sql.connect(dbpath)
if not os.path.exists("topics"):
os.mkdir("topics")
for topid in to2th.keys():
topicpath = os.path.join("topics", topics[topid])
if not os.path.exists(topicpath):
os.mkdir(topicpath)
m = sorted(to2th[topid], key=itemgetter(1), reverse=True)
for i in range(5):
tid = m[i][0]
filename = str(m[i][0]) + '.html'
exportThread(con, tid, os.path.join(topicpath, filename))
def main():
if len(sys.argv) != 3:
print("Usage: python modelTopics.py <db_name> <num_topics>")
return
dbpath = sys.argv[1]
numTopics = int(sys.argv[2])
if not os.path.exists(dbpath):
print("Error: database does not exist")
if not os.path.exists("data"):
os.mkdir("data")
cpath = os.path.join("data", "threads_bow.mm")
dpath = os.path.join("data", "threads_dict.dict")
mpath = os.path.join("data", "threads_model.lda")
#exportCorpus(dbpath, cpath, dpath)
#modelTopics(cpath, dpath, mpath, numTopics)
exportTopics(dbpath, dpath, mpath)
if __name__=='__main__':
main()