-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathlda.py
142 lines (111 loc) · 4.14 KB
/
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
133
134
135
136
137
138
139
140
141
142
# -*- coding:utf-8 -*-
# to be included from LSTM.pyx
import gensim
import codecs
import logging
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
class LDA():
def __init__(self, stopwords):
"""Init with text stopwords file"""
#self.STOPWORDS = set(open(stopwords_file, "r").read().split())
self.STOPWORDS = set(stopwords)
def train_BH(self, file_corpus, file_dictionary, TOPICS):
"""
Train LDA model and save it
"""
#prepare corpus
text = []
for line in open(file_corpus):
text.append(line.lower().strip().split())
#remove stopwords
text = [[word for word in line if word not in self.STOPWORDS] for line in text]
splitted_coprus = []
foo = []
for i in range(len(text)):
#if (i % self.SENTENCES) == (self.SENTENCES - 1):
try:
text[i][0]
except:
continue
if text[i][0] == "_____":
splitted_coprus.append(foo)
foo = []
foo += text[i]
#print splitted_corpus
self.dictionary = gensim.corpora.Dictionary(splitted_coprus)
self.dictionary.save(file_dictionary) # store the dictionary, for future reference
corpus = [self.dictionary.doc2bow(text) for text in splitted_coprus]
gensim.corpora.MmCorpus.serialize('.mm', corpus) # store to disk, for later use
# go LDA
self.lda = gensim.models.LdaModel(corpus, id2word=self.dictionary, num_topics=TOPICS, passes=10, chunksize=100) # initialize an LSI transformation
def train(self, file_corpus, file_dictionary, TOPICS, SENTENCES=10):
"""
Train LDA model and save it
"""
#prepare corpus
text = []
for line in open(file_corpus):
text.append(line.lower().strip().split())
#remove stopwords
text = [[word for word in line if word not in self.STOPWORDS] for line in text]
splitted_corpus = []
foo = []
for i in range(len(text)):
if (i % SENTENCES) == (SENTENCES - 1):
splitted_corpus.append(foo)
foo = []
foo += text[i]
self.dictionary = gensim.corpora.Dictionary(splitted_corpus)
self.dictionary.save(file_dictionary) # store the dictionary, for future reference
corpus = [self.dictionary.doc2bow(text) for text in splitted_corpus]
gensim.corpora.MmCorpus.serialize('.mm', corpus) # store to disk, for later use
# go LDA
self.lda = gensim.models.LdaModel(corpus, id2word=self.dictionary, num_topics=TOPICS, passes=10, chunksize=100) # initialize an LSI transformation
def save(self, file_model, file_dictionary):
"""Saves the LDA model"""
self.lda.save(file_model)
self.dictionary.save(file_dictionary)
print self.dictionary
def load(self, file_model, file_dictionary):
"""Loads the LDA model"""
self.lda = gensim.models.LdaModel.load(file_model)
self.dictionary = gensim.corpora.Dictionary.load(file_dictionary)
self.len = len(self.lda.__getitem__(self.dictionary.doc2bow(["."]), eps=0))
print self.len
print self.dictionary
def cache_to_fv(self, cache):
"""Converts cache (list of lowercased words) to FV"""
cache = [x.encode('utf-8') for x in cache] # from unicode to utf-8
vec_bow = self.dictionary.doc2bow(cache)
vec_lda = self.lda.__getitem__(vec_bow, eps=0) # convert the query to LDA space
out = [0.0] * self.len
for i in range(self.len):
out[i] = vec_lda[i][1]
return out
if __name__ == "__main__":
#l = LDA("cz_stopwords.txt")
stopwords_file = "cz_stopwords.txt"
STOPWORDS = set(open(stopwords_file, "r").read().split())
l = LDA(STOPWORDS)
import sys
train_file = sys.argv[1]
test_file = sys.argv[2]
#TOPICS = 50
#LAST_N = 50 # how many words count as a history?
for TOPICS in [20, 30, 40, 50, 70, 100]:
l.train_BH(train_file, train_file+".dict", TOPICS)
l.save(train_file + str(TOPICS) + ".lda", train_file + str(TOPICS) + ".dict")
l.load(train_file + str(TOPICS) +".lda", train_file + str(TOPICS) + ".dict")
"""
stopwords_file = "cz_stopwords.txt"
STOPWORDS = set(open(stopwords_file, "r").read().split())
#print l.cache_to_fv("this is a very big test of japan")
#t = open(test_file).read().lower().split()
for i in range(len(t)):
# last N words
cache = t[i - LAST_N : i]
# remove stopwords
cache = [word for word in cache if word not in STOPWORDS]
print cache
print l.cache_to_fv(cache)
"""