-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIBMmodel1.py
More file actions
184 lines (112 loc) · 4.13 KB
/
IBMmodel1.py
File metadata and controls
184 lines (112 loc) · 4.13 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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import numpy as np
import math
from nltk.tokenize import word_tokenize
from collections import defaultdict
# In[2]:
#DATA
file = open('./train.en', 'r')
train_en = file.read()
sentences_train_en = train_en.split("\n")
file = open('./train.hi', 'r')
train_hi = file.read()
sentences_train_hi = train_hi.split("\n")
file = open('./test.en', 'r')
test_en = file.read()
sentences_test_en = test_en.split("\n")
file = open('./test.hi', 'r')
test_hi = file.read()
sentences_test_hi = test_hi.split("\n")
file = open('./dev.en.txt', 'r')
dev_en = file.read()
sentences_dev_en = dev_en.split("\n")
file = open('./dev.hi', 'r')
dev_hi = file.read()
sentences_dev_hi = dev_hi.split("\n")
# In[59]:
def is_converged(new, old, epoch):
epsilone = 0.0000001
new = list(new.values())
old = list(old.values())
print("here")
for i in range(len(old)):
print("s",epoch)
if math.fabs(new[i]-old[i]) > epsilone:
return False
return True
# In[60]:
def perform_EM(en_sentences, hi_sentences):
translation_prob = defaultdict(float)
translation_prob_prev = defaultdict(float)
uni_ini = 0.00001
epoch = 0
while not is_converged(translation_prob, translation_prob_prev, epoch):
# while(epoch<2):
translation_prob_prev = translation_prob
print(len(translation_prob_prev))
print(len(translation_prob))
epoch += 1
print("epoch num:", epoch,"\n")
count = defaultdict(float)
total = defaultdict(float)
for index_sen, hin_sen in enumerate(hi_sentences):
#compute normalization
hin_sen_words = hin_sen.split(" ")
s_total = defaultdict(float)
for hin_word in hin_sen_words:
s_total[hin_word] = 0
eng_sen_words = en_sentences[index_sen].split(" ")
for eng_word in eng_sen_words:
if epoch == 1:
s_total[hin_word] += uni_ini
translation_prob[(hin_word, eng_word)] = uni_ini
else:
s_total[hin_word] += translation_prob[(hin_word, eng_word)]
#collect counts
for hin_word in hin_sen_words:
eng_sen_words = en_sentences[index_sen].split(" ")
for eng_word in eng_sen_words:
if epoch == 1:
translation_prob[(hin_word, eng_word)] = uni_ini
count[(hin_word, eng_word)] += uni_ini/s_total[hin_word]
total[eng_word] += uni_ini/s_total[hin_word]
else:
count[(hin_word, eng_word)] += translation_prob[(hin_word, eng_word)]/s_total[hin_word]
total[eng_word] += translation_prob[(hin_word, eng_word)]/s_total[hin_word]
#estimate probabilities
for (hin_word, eng_word) in translation_prob.keys():
translation_prob[(hin_word, eng_word)] = count[(hin_word, eng_word)]/total[eng_word]
print(len(translation_prob_prev))
print(len(translation_prob))
return translation_prob
# In[7]:
def train_model(sentences_train_en, sentences_train_hi):
translation_prob = perform_EM(sentences_train_en, sentences_train_hi)
return translation_prob
# In[20]:
def test_model(dataset, tef):
# tef = np.load('./models/IBMmodel1tef.npy')
for sentence in dataset:
translate_sentence(sentence, tef)
# In[9]:
def translate_sentence(sentence, tef):
tokens = sentence.split(" ")
# for token in tokens:
# print(list(tef.items())[0])
# max_score = -1
# max_sentence = ""
# prob = get_translation_prob(end_sentence, tef)
# if prob > max_score:
# max_score = prob
# max_sentence = poss_sentence
# In[61]:
tef = train_model(sentences_train_en, sentences_train_hi)
# In[ ]:
# np.save("./models/IBMmodel1tef_3", tef)
# In[ ]:
# test_model(test_en_tokenised_sentence, tef)
# In[10]:
# In[11]:
# In[ ]: