-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopicline.py
279 lines (241 loc) · 8.67 KB
/
topicline.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# coding: utf-8
import numpy as np
import random, os
import math
from scipy.special import gamma,gammaln
import sys
import collections
import numpy as np, math, random
from scipy.special import gamma, gammaln
def discrete(probs):
return np.argmax(np.random.multinomial(1,probs))
def gamma_pdf(x,k,theta):
x,k,theta = map(float,(x,k,theta))
return (x**(k-1))*(math.exp(-x/theta))/((theta**k)*gamma(k))
def exp_pdf(x,k):
return k*math.exp(-k*x)
def slice_sample(likelihood, x0):
old_lik = likelihood(x0)
old_alpha = x0
lnt = old_lik - np.random.exponential(1)
w = old_alpha/32.
L = max(0, old_alpha - w*random.random())
R = L + w
K = 4
while K > 0 and (lnt < likelihood(L) or lnt < likelihood(R)):
V = random.random()
if V < 0.5:
if L-(R-L) < 0:
print "L would be", L-(R-L), "R is", R
L = max(0, L-(R-L))
else:
R = R+(R-L)
K = K-1
rej = True
while rej:
U = random.random()
x1 = L+U*(R-L)
rr = likelihood(x1)
if lnt < rr:
break
else:
if x1 < old_alpha:
L = x1
else:
R = x1
return x1
def cl(tprior, gmean, gvar, tcount):
"Can deal with a vector of alphas"
p0 = sum(np.log(gamma_pdf(tp, gmean, gvar)) for tp in tprior)
p0 += tcount.shape[0]*(gammaln(sum(tprior))-sum(gammaln(tp) for tp in tprior))
for d in xrange(tcount.shape[0]):
for i,t in enumerate(tcount[d]):
p0 += gammaln(t + tprior[i])
p0 -= gammaln(sum(tcount[d]) + sum(tprior))
return p0
def hcl(tprior, gmean, gvar, tcount, s1):
"Assumes a single hyperparameter"
p0 = np.log(gamma_pdf(tprior, gmean, gvar))
s0 = len(tcount)
p0 += s0*(gammaln(tprior*s1)-sum(gammaln(tprior) for i in xrange(s1)))
for d in xrange(s0):
for i,t in tcount[d].items():
p0 += gammaln(t + tprior)
p0 += (s1-len(tcount[d]))*gammaln(tprior)
p0 -= gammaln(sum(tcount[d]) + s1*tprior)
return p0
import re
wre = re.compile(r"(\w)+")
def get_words(text, stop=True):
"A simple tokenizer"
l = 0
while l < len(text):
s = wre.search(text,l)
try:
w = text[s.start():s.end()].lower()
if stop:
yield w
elif w not in stoplist:
yield w
l = s.end()
except:
break
class LDASampler(object):
def __init__(self, dirs, npt):
self.all_words = []
self.reverse_map = {}
self.doc_map = {}
self.documents = []
self.Ndocuments = 0
self.Nwords = 0
self.npt = npt
self.load_docs(dirs)
self.Nwords = len(self.all_words)
self.Ndocuments = len(self.documents)
self.assignments = [[0 for w in d] for d in self.documents]
self.initialize()
def load_doc(self,doc):
"Creates a bag of words for a single document"
v = []
document = doc
for w in get_words(document):
w = w.lower()
if not w in self.reverse_map:
self.reverse_map[w] = self.Nwords
self.all_words.append(w)
self.Nwords += 1
v.append(self.reverse_map[w])
if not v: return
self.doc_map[document] = len(self.documents)
self.docs.append(doc)
self.documents.append(v)
def load_docs(self, dirs):
self.Nproducts = 0
self.prod = []
self.docs = []
for id,d in enumerate(dirs):
for fname in os.listdir(d):
doc = file(os.path.join(d,fname)).read()
self.prod.append(id)
self.load_doc(doc)
self.Nproducts += 1
def initialize(self):
self.generic = collections.defaultdict(lambda:0)
self.ngeneric = [0]
self.products = [[collections.defaultdict(lambda:0)
for p in xrange(self.Nproducts)]
for i in xrange(self.npt)]
self.pnames = [["Categoria %s, topico %s"%(p,i)
for p in xrange(self.Nproducts)]
for i in xrange(self.npt)]
self.nproducts = [[[0] for p in xrange(self.Nproducts)]
for i in xrange(self.npt)]
self.reviews = [collections.defaultdict(lambda: 0) for d in self.documents]
self.rnames = ["Documento %s"%d for d in xrange(len(self.documents))]
self.nreviews = [[0] for d in self.documents]
self.tcount = np.zeros((self.Ndocuments, self.npt+2))
self.tsamp = np.zeros((self.Ndocuments, self.npt+2))
self.tprior = np.array([2. for i in xrange(self.npt+2)])
self.beta = [0.1] + [0.1 for i in xrange(self.npt)] + [.01]
self.all_topics = [[self.generic]] + self.products+[self.reviews]
self.all_tnames = [["generic"]] + self.pnames + [self.rnames]
self.dtopics = []
self.dsums = []
tc = self.tprior/sum(self.tprior)
for d in xrange(self.Ndocuments):
self.dtopics.append([self.generic]+
[self.products[i][self.prod[d]]
for i in xrange(self.npt)] +
[self.reviews[d]] )
self.dsums.append([self.ngeneric] +
[self.nproducts[i][self.prod[d]]
for i in xrange(self.npt)] +
[self.nreviews[d]])
for i,w in enumerate(self.documents[d]):
t = discrete(tc)
self.assignments[d][i] = t
self.tcount[d,t] += 1
self.dtopics[d][t][w] += 1
self.dsums[d][t][0] += 1
def resample_sentence(self, d,i,w,pt):
"Resamples the topic assignments of a sentence"
to = self.assignments[d][i]
assert self.documents[d][i] == w, "%s %s %s"%(d,i,w)
assert self.dtopics[d][to][w] > 0, "%d %d %d %s"%(d, to, w, i)
assert self.dsums[d][to][0] > 0
self.dtopics[d][to][w] -= 1
self.dsums[d][to][0] -= 1
self.tcount[d,to] -= 1
pt *= 0
tc = self.tcount[d] + self.tprior
for j in xrange(len(pt)):
pt[j] = (tc[j]*(self.dtopics[d][j][w]+self.beta[j])/
(self.dsums[d][j][0]+self.beta[j]*self.Nwords))
pt /= np.sum(pt)
nt = discrete(pt)
self.assignments[d][i] = nt
self.dtopics[d][nt][w] += 1
self.dsums[d][nt][0] += 1
self.tcount[d,nt] += 1
def resample_tprior(self):
for i, t in enumerate(self.tprior):
def partial_lik(t0):
self.tprior[i] = t0
return cl(self.tprior, 0.1, 10, self.tcount)
self.tprior[i] = slice_sample(partial_lik, t)
def resample_beta(self):
for i,t in enumerate(self.beta):
def beta_lik(beta):
self.beta[i] = beta
return hcl(self.beta[i],0.1,10,self.all_topics[i],self.Nwords)
self.beta[i] = slice_sample(beta_lik, self.beta[i])
def likelihood(self):
l0 = cl(self.tprior, 0.1, 10, self.tcount)
for b,t in zip(self.beta,self.all_topics):
l0 += hcl(b,0.1,10,t,self.Nwords)
return l0
def iterate(self):
pt = np.zeros(len(self.tprior))
self.resample_tprior()
self.resample_beta()
print self.tprior, self.beta, self.likelihood()
for document in xrange(self.Ndocuments):
for i,word in enumerate(self.documents[document]):
self.resample_sentence(document,i,word,pt)
self.tsamp += self.tcount
def run(self,its):
"The sampler itself."
iteration = 0
print "iterating.."
for i in xrange(its):
iteration += 1
self.iterate()
for n,i in zip(self.all_tnames, self.all_topics):
for nn,t in zip(n,i):
print nn
print_keyw_topic(self, t, 411)
def print_topic(model, t, n):
s = np.argsort(-t)
for w in s[:n]:
print " ",model.all_words[w], t[w]
#def print_topic(model, t, n):
# s = np.argsort(-t)
# for w in s[:n]:
# print " ",model.all_words[w]
def print_keyw_topic(model, t, n):
tt = np.zeros(len(model.all_words))
for k,v in t.items():
tt[k] = v
print_topic(model, tt, n)
def top_keyw_topic(model, t, n):
tt = np.zeros(len(model.all_words))
for k,v in t.items():
tt[k] = v
s = np.argsort(-tt)
return [model.all_words[i] for i in s[:n]]
if __name__ == '__main__':
NTOPICS = 2
import sys
print sys.argv[1:]
s = LDASampler(sys.argv[1:], NTOPICS)
s.run(100)