-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlda_p.py
236 lines (190 loc) · 5.13 KB
/
lda_p.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
#!/usr/bin/python
# use folding-in tech, more details please see:
# <http://stats.stackexchange.com/questions/9315/topic-prediction-using-latent-dirichlet-allocation>
import copy
import random
voc_items = []
voc_map = {}
n_kt = []
n_k = []
K = V = 0
iter_num = 20
alpha = 1
beta = 0.1
ac_nkt = {}
ac_nk = {}
def load_dic(fn):
global voc_items, voc_map
fi = open(fn, "r")
for line in fi:
line = line.strip()
items = line.split("\t")
if len(items) != 2:
print "[ERR Format in Dic]: %s" % line
continue
wid = int(items[0].strip())
w = items[1].strip()
voc_items.append(w)
voc_map[w] = wid
if wid != len(voc_items)-1:
print "[ERR: loose some word] (%d,%d) %s" % (wid, len(voc_items)-1, w)
fi.close()
def load_stat(fn):
global K, V, n_k, n_kt
fi = open(fn, "r")
i = 0
for line in fi:
tmpli = line.strip().split(" ")
tmp_n_li = map(lambda x:int(x.strip()), tmpli)
if i == 0:
if len(tmp_n_li) != 2:
print "[ERR: bad KV line] %s" % line
K = tmp_n_li[0]
V = tmp_n_li[1]
elif i == 1:
if len(tmp_n_li) != K:
print "[ERR: bad n_k line] %d/%d" % (len(tmp_n_li), K)
n_k = tmp_n_li
else: # n_kt
if len(tmp_n_li) != V:
print "[ERR: n_kt line not match voc] topic %d: %d/%d" % (len(n_kt), len(tmp_n_li), V)
n_kt.append(tmp_n_li)
i += 1
if len(n_kt) != K:
print "[ERR: n_kt not enough topic lines] %d/%d" % (len(n_kt), K)
fi.close()
def wl2idl(wl):
global voc_map
ret = []
for w in wl:
w = w.strip()
if not voc_map.has_key(w):
continue
ret.append(voc_map[w])
return ret
def inverse_df(pm):
tmp = pm[:]
i = 1
while i < len(pm):
tmp[i] += tmp[i-1]
i += 1
u = random.uniform(0,1) * tmp[len(tmp)-1]
for index in range(len(tmp)):
if tmp[index] > u:
break
return index
def compute_p(n_mk, t):
global K, V, n_kt, n_k, alpha, beta
ret = []
for k in range(K):
n1 = float(beta) + n_kt[k][t]
d1 = float(beta) + n_k[k]
n2 = float(alpha) + n_mk[k]
ret.append(n1/d1 * n2)
return ret
def reg_action(k, t, delta):
global ac_nkt, ac_nk
if not ac_nkt.has_key(k):
ac_nkt[k] = {}
if not ac_nkt[k].has_key(t):
ac_nkt[k][t] = 0
ac_nkt[k][t] += delta
if not ac_nk.has_key(k):
ac_nk[k] = 0
ac_nk[k] += delta
def reset_stat():
global n_k, n_kt, ac_nkt, ac_nk
for k in ac_nk:
n_k[k] -= ac_nk[k]
for k in ac_nkt:
for t in ac_nkt[k]:
n_kt[k][t] -= ac_nkt[k][t]
ac_nkt.clear()
ac_nk.clear()
def predict(widl):
global K, V, n_k, n_kt, iter_num
# init
z = []
n_mk = []
n_m = 0
for i in range(K):
n_mk.append(0)
l = len(widl)
for n in range(l):
t = widl[n]
k = random.randint(0, K-1)
z.append(k)
n_mk[k] += 1; n_m += 1
n_kt[k][t] += 1; n_k[k] += 1
reg_action(k, t, 1)
# samp
for i in range(iter_num):
for n in range(l):
t = widl[n]
k = z[n]
n_mk[k] -= 1; n_m -= 1;
n_kt[k][t] -= 1; n_k[k] -= 1
reg_action(k, t, -1)
p = compute_p(n_mk, t)
k_tilde = inverse_df(p)
z[n] = k_tilde
n_mk[k_tilde] += 1; n_m += 1;
n_kt[k_tilde][t] += 1; n_k[k_tilde] += 1
reg_action(k_tilde, t, 1)
reset_stat()
return z
def tidl2str(tidl):
ret = ""
for tid in tidl:
ret += "%d " % tid
return ret
def tidl2dist(tidl):
ret = {}
cnt = len(tidl)
if cnt == 0:
return ret
for tid in tidl:
if not ret.has_key(tid):
ret[tid] = 0.0
ret[tid] += 1.0
for tid in ret:
ret[tid] /= float(cnt)
return ret
def tid_dis2str(tid_dis):
ret = ""
for k,v in sorted(tid_dis.items(), key=lambda x:x[1], reverse=True):
ret += "%d:%f " % (k, v)
return ret
def process(fnd, fna, fnt):
fi = open(fnd, "r")
fo1 = open(fna, "w")
fo2 = open(fnt, "w")
for line in fi:
li = line.split("\t")
if len(li) != 2:
print "[ERR: illegal doc] %s" % line
continue
doc_no = int(li[0].strip())
word_list = li[1].strip().split(" ")
wid_list = wl2idl(word_list)
tidl = predict(wid_list)
tidls = tidl2str(tidl)
fo1.write("%d\t%s\n" % (doc_no, tidls))
tid_dis = tidl2dist(tidl)
tid_dis_str = tid_dis2str(tid_dis)
fo2.write("%d\t%s\n" % (doc_no, tid_dis_str))
fi.close()
fo1.close()
fo2.close()
def main():
path = "./data/"
fn_dic = path + "dic.dat"
fn_stat = path + "stat.dat"
fn_data = path + "test_data" #
fn_assign = path + "test_p_assign.dat" #
fn_top_dis = path + "test_p_td.dat" #
load_dic(fn_dic)
load_stat(fn_stat)
process(fn_data, fn_assign, fn_top_dis)
if __name__ == "__main__":
main()