-
Notifications
You must be signed in to change notification settings - Fork 0
/
hmm.py
336 lines (268 loc) · 7.87 KB
/
hmm.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#!/usr/bin/python
# -*- coding: utf-8 -*-
from random import *
import os
import sys
reload(sys)
sys.setdefaultencoding('utf8')
from nltk.corpus import brown
from collections import defaultdict
import math
# A = {'cp' :{'cp' : 0.7, 'ip' : 0.3},'ip' :{'cp' : 0.5, 'ip' : 0.5}}
# B = {'cp' :{'Cola' : 0.6, 'ice_tea' : 0.1, 'lemonade' : 0.3},'ip' :{'Cola' : 0.1, 'ice_tea' : 0.7, 'lemonade' : 0.2}}
# pi={'cp':1,'ip': 0}
# alphas=defaultdict(list)
# betas=defaultdict(list)
# observation=["woods","clotted",]
# for state in pi:
# alphas[state].append(pi[state])
def forward_procedure(observation,A,B,pi,alphas):
index=0
for sequence in observation:
for state in A:
probability_sum=0
for transistion in A:
probability_sum+= A[transistion][state]* B[transistion][sequence]*alphas[transistion][index]
alphas[state].append((probability_sum))
index+=1
return alphas
# for x in xrange(len(observation)+2):
# for state in pi:
# betas[state].append(1)
# for state in pi:
# betas[state][0]=pi[state]
def backward_procedure(observation,A,B,pi,betas):
index=len(observation)+1
for sequence in observation:
for state in A:
probability_sum=0
for transistion in A:
# print state ,sequence,transistion,A[state][transistion], B[state][sequence],betas[transistion][index]
probability_sum+= A[state][transistion]* B[state][sequence]*betas[transistion][index]
betas[state][index-1]=probability_sum
index-=1
return betas
# alphas=forward_procedure(observation,A,B,pi,alphas)
# Alpha_prob=0
# for state in pi:
# Alpha_prob+=alphas[state][len(observation)]
# observation.reverse()
# betas=backward_procedure(observation,A,B,pi,betas)
# for state in pi:
# betas[state]=betas[state][1:]
# Beta_prob=0
# for state in pi:
# Beta_prob+=betas[state][0]*pi[state]
# observation.reverse()
def baum_welch(observation,A,B,Allalphas,Allbetas,Alpha_prob):
prob_state_list=[]
gamma_state_list=[]
P_i_j=defaultdict(list)
gamma=defaultdict(int)
for timesequence in xrange(len(observation)):
temp={}
temp2={}
for state in A:
for transistion in A:
#print timesequence, state,transistion,observation[timesequence], Allalphas[state][timesequence] , A[state][transistion] , B[state][observation[timesequence]] , Allbetas[transistion][timesequence + 1],
prob_state=(Allalphas[state][timesequence] * A[state][transistion] * B[state][observation[timesequence]] * Allbetas[transistion][timesequence + 1])/Alpha_prob
#print prob_state
temp[transistion]=prob_state
#print temp
temp2[state]=temp
temp={}
#print temp2
for k,v in temp2.items():
res=0
for k2,v2 in v.items():
# print k,k2,v2
res+=v2
#print res
gamma[k]=res
#print gamma
gamma_state_list.append(gamma)
gamma={}
P_i_j=temp2.copy()
prob_state_list.append(P_i_j)
temp2={}
P_i_j=defaultdict(list)
return gamma_state_list,prob_state_list
def normalize(A):
for k,v in A.items():
res=0;
for k2,v2 in v.items():
res+=v2
for k3,v3 in v.items():
A[k][k3] =A[k][k3]/res
return A
# gamma_state_prob_list,zeta_prob_state_list=baum_welch(observation,A,B,alphas,betas,Alpha_prob)
def A_New(A,observation,zeta_prob_state_list,gamma_state_prob_list):
for state in A:
for transistion in A:
gammasum=0
res=0
for sequence in xrange(len(observation)):
res+=zeta_prob_state_list[sequence][state][transistion]
gammasum+=gamma_state_prob_list[sequence][state]
A[state][transistion]=res/gammasum
return A
def pi_new(A,pi,gamma_state_prob_list):
res=0
for state in A:
pi[state]=gamma_state_prob_list[0][state]
res+=gamma_state_prob_list[0][state]
for state in A:
pi[state]=pi[state]/res
return pi
def getB_keys(B):
emissionkeys=[]
for k,v in B.items():
for k2,v2 in v.items():
emissionkeys.append(k2)
return emissionkeys
def B_new(A,B,observation,gamma_state_prob_list):
emissionkeys=getB_keys(B)
for transistion in A:
for emission in emissionkeys:
gammasum=0
res=0
for sequence in xrange(len(observation)):
if observation[sequence]==emission:
res+=gamma_state_prob_list[sequence][transistion]
gammasum+=gamma_state_prob_list[sequence][transistion]
B[transistion][emission]=res/gammasum
return B
# A=A_New(A,observation,zeta_prob_state_list,gamma_state_prob_list)
# pi=pi_new(A,pi,gamma_state_prob_list)
# B=B_new(A,B,observation,gamma_state_prob_list)
# A=normalize(A)
# B=normalize(B)
# print pi
# print A
# print B
def generatePI(states):
pi=defaultdict(int)
for sequence in xrange(states):
pi["tag"+str(sequence)]=random()
res=0
for k,v in pi.items():
res+=v;
for k,v in pi.items():
pi[k]=pi[k]/res
return pi
def generate_A(words,states):
A=defaultdict(dict)
for sequence in xrange(states):
temp={}
for sequence2 in xrange(states):
temp["tag"+str(sequence2)]=random()
A["tag"+str(sequence)]=temp
normalize(A)
return A
def generate_B(words,states):
B=defaultdict(dict)
for sequence in xrange(states):
temp={}
for sequence2 in words:
temp[sequence2]=random()
B["tag"+str(sequence)]=temp
normalize(B)
return B
def manageData(words,states):
for i in xrange(1,len(brown.sents())):
observation=brown.sents()
observation=[key.strip().lower().encode('utf-8') for key in observation]
alphas=defaultdict(list)
betas=defaultdict(list)
#print words
print len(words)
tokens=[key.strip().lower().encode('utf-8') for key in words]
#print len(tokens),type(tokens)
vocabulary=list(set(tokens))
print len(vocabulary)
pi=generatePI(states)
#print pi
A=generate_A(vocabulary,states)
#print A
B=generate_B(vocabulary,states)
#print len(B['tag0']) , "------------"
# print "before calculation A & B"
A=normalize(A)
B=normalize(B)
#print A
# print B
for state in pi:
alphas[state].append(pi[state])
alphas=forward_procedure(observation,A,B,pi,alphas)
Alpha_prob=0
for state in pi:
Alpha_prob+=alphas[state][len(observation)]
#print alphas[state][len(observation)]
print Alpha_prob, " initial prob"
# print alphas
observation.reverse()
for x in xrange(len(observation)+2):
for state in pi:
betas[state].append(1)
for state in pi:
betas[state][0]=pi[state]
betas=backward_procedure(observation,A,B,pi,betas)
for state in pi:
betas[state]=betas[state][1:]
Beta_prob=0
for state in pi:
Beta_prob+=betas[state][0]*pi[state]
observation.reverse()
#print alphas
for j in xrange(20):
#print len(A),len(B),len(alphas),len(betas)
gamma_state_prob_list,zeta_prob_state_list=baum_welch(observation,A,B,alphas,betas,Alpha_prob)
A=A_New(A,observation,zeta_prob_state_list,gamma_state_prob_list)
pi=pi_new(A,pi,gamma_state_prob_list)
B=B_new(A,B,observation,gamma_state_prob_list)
A=normalize(A)
B=normalize(B)
# print "pi----------------------------"
# print pi
# print
# print "A----------------------------"
# print A
# print
# print "B----------------------------"
# print B
alphas=defaultdict(list)
betas=defaultdict(list)
for state in pi:
alphas[state].append(pi[state])
alphas=forward_procedure(observation,A,B,pi,alphas)
#print "--------------------------------"
#print alphas
Alpha_prob=0
for state in pi:
Alpha_prob+=alphas[state][len(observation)]
#print alphas[state][len(observation)]
print Alpha_prob, " final prob"
observation.reverse()
for x in xrange(len(observation)+2):
for state in pi:
betas[state].append(1)
for state in pi:
betas[state][0]=pi[state]
betas=backward_procedure(observation,A,B,pi,betas)
for state in pi:
betas[state]=betas[state][1:]
Beta_prob=0
for state in pi:
Beta_prob+=betas[state][0]*pi[state]
observation.reverse()
test=[]
#manageData(brown.words(),10)
# with open("dataset") as f:
# line= f.read().split()
# for x in line:
# test.append(x)
manageData(brown.words(),len(brown.words))
# print Alpha_prob , Beta_prob
# print alphas
# print betas
#print betas