-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhmmdecode.py
More file actions
205 lines (159 loc) · 6.11 KB
/
hmmdecode.py
File metadata and controls
205 lines (159 loc) · 6.11 KB
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
from __future__ import division
from collections import defaultdict
import sys
import time
def viterbi(observation):
# print(observation)
len_observtion = len(observation)
V = [defaultdict(int)]
backpointer = [defaultdict(int)]
# print(states)
for i in states:
key_for_emission = i+' '+observation[0]
if observation[0] in possible_tags_of_words:
V[0][i] = ((transition['<s> '+i]+1)/(context['<s>']+len(states)))*(emission[key_for_emission]/context[i])
else:
V[0][i] = ((transition['<s> '+i]+1)/(context['<s>']+len(states)))
backpointer[0][i] = '<s>'
# print V[0]
for t in range(1, len(observation)):
V.append(defaultdict(int))
backpointer.append(defaultdict(int))
if observation[t] in possible_tags_of_words:
# if the word is seen in training data
# print (observation[t]+" is seen")
all_tags_for_this_word = possible_tags_of_words[observation[t]]
for y in all_tags_for_this_word:
key_for_emission = y+' '+observation[t]
max_value = -1
max_previous_state = ''
for previous_state in states:
transition_pro = (transition[previous_state+' '+y]+1)/(context[previous_state]+len(states))
emission_pro = (emission[key_for_emission])/(context[y])
# prob = max(V[t - 1][y0]*(transition[y0+' '+y]/context[y0])*(emission[key_for_emission]/context[y]) for y0 in states)
if V[t-1][previous_state] == 0:
continue
else:
temp_value = V[t-1][previous_state]*transition_pro*emission_pro
if max_value <= temp_value:
max_value = temp_value
max_previous_state = previous_state
backpointer[t][y] = max_previous_state
# if max_value==(-sys.maxint-1):
# V[t][y] = 0
# else:
V[t][y] = max_value
else:
# The word is not found in the training corpus
# print (observation[t]+" is not seen")
for y in states:
max_value = - 1
max_previous_state = ''
for previous_state in states:
transition_pro = (transition[previous_state+' '+y]+1)/(context[previous_state]+len(states))
if V[t-1][previous_state] == 0:
continue
else:
temp_value = V[t-1][previous_state]*transition_pro
if max_value <= temp_value:
max_value = temp_value
max_previous_state = previous_state
backpointer[t][y] = max_previous_state
V[t][y] = max_value
# print(V[-1])
# find max in last stage
max_value = -1
max_state_last = ''
for key, value in V[-1].iteritems():
if value>=max_value:
max_value = value
max_state_last = key
# print("bc")
my_tags = []
current_state = max_state_last
back_counter = len_observtion-1
while back_counter!=-1 or current_state!='<s>':
my_tags.insert(0, current_state)
# print my_tags
# print ("Rascal"+str(current_state))
current_state = backpointer[back_counter][current_state]
if current_state=='':
for key, val in backpointer[back_counter-1].iteritems():
current_state = (backpointer[back_counter-1][key])
my_tags.insert(0,key)
back_counter-=1
break
back_counter -= 1
# print(current_state),
# print(back_counter)
return my_tags
import sys
file_path = sys.argv[1]
lines = [line.rstrip('\n') for line in open('hmmmodel.txt')]
# print(len(lines))
len_transition, len_emission, len_context, len_words = lines[0].split(" ")
len_transition = int(len_transition)
len_emission = int(len_emission)
len_context = int(len_context)
len_words = int(len_words)
transition = defaultdict(int)
emission = defaultdict(int)
context = defaultdict(int)
states = set()
possible_tags_of_words = dict()
lines.pop(0)
for i in range(0,len_transition):
splitted_line = lines[0].split(" ")
transition[splitted_line[0]+' '+splitted_line[1]] = int(splitted_line[2])
lines.pop(0)
for i in range(0,len_emission):
splitted_line = lines[0].split(" ")
emission[splitted_line[0]+' '+splitted_line[1]] = int(splitted_line[2])
lines.pop(0)
for i in range(0,len_context):
splitted_line = lines[0].split(" ")
context[splitted_line[0]] = int(splitted_line[1])
lines.pop(0)
for i in range(0,len_words):
splitted_line = lines[0].strip().split(" ")
word = splitted_line[0]
possible_tags_of_words[word] = set()
for j in range(1,len(splitted_line)):
possible_tags_of_words[word].add(splitted_line[j])
lines.pop(0)
for key, value in context.iteritems():
states.add(key)
# print possible_tags_of_words[0]
# print(len(lines))
# Starting Viterbi
input_lines = []
for line in open(file_path):
input_lines.append(line.rstrip('\n'))
file_output = open('hmmoutput.txt','wb')
start = time.time()
for line in input_lines:
#split every line with a " "
#this will be our observation
observation = [word for word in line.split(' ')]
try:
tags_returned = viterbi(observation)
# print tags_returned
output_line = ""
for i in range(0,len(observation)):
output_line+=observation[i]+'/'+tags_returned[i]+" "
file_output.write(output_line+'\n')
except:
tags_returned = []
tag = ""
for key, value in possible_tags_of_words.iteritems():
tag = key
break
for i in range(0, len(observation)):
tags_returned.append(tag)
output_line = ""
for i in range(0,len(observation)):
output_line+=observation[i]+'/'+tags_returned[i]+" "
file_output.write(output_line+'\n')
pass
end = time.time()
print (end-start)