-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ass_1.py
417 lines (362 loc) · 11.6 KB
/
Ass_1.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import numpy as np
import nltk
from collections import OrderedDict
from nltk.corpus import gutenberg
from nltk.corpus import brown
import operator
import math
import string
#files = gutenberg.fileids()
vocab = {}
train = []
test =[]
unknown = {}
pred_count_dict = {}
succ_count_dict = {}
def fetch_train_test(corpora, test_corpus):
train = []
test = []
unknown.clear()
vocab.clear()
pred_count_dict.clear()
succ_count_dict.clear()
for corpus in corpora:
if corpus == 'brown':
files = brown.fileids()
elif corpus == 'gutenberg':
files = gutenberg.fileids()
else:
print("config Error")
for file in files:
if corpus == 'brown':
sentences = brown.sents(file)
elif corpus == 'gutenberg':
sentences = gutenberg.sents(file)
else:
print("config Error")
permute = np.ones(len(sentences))
if corpus == test_corpus:
permute[:int(len(sentences) * 0.2)] = 0
np.random.shuffle(permute)
for index in range(len(sentences)):
if permute[index] == 0:
test.append(sentences[index])
else:
train.append(sentences[index])
return [train, test]
def remove_less_freq(dictionary):
dictionary = {k: v for k,v in dictionary.items() if (v >= 2)}
return dictionary;
def less_freq(dictionary):
dictionary = {k: v for k,v in dictionary.items() if (v <= 1)}
return dictionary;
def replace_unk():
for sentence in train:
for idx, item in enumerate(sentence):
if item in unknown:
sentence[idx] = 'unk'
for sentence in test:
for idx, item in enumerate(sentence):
if item not in vocab:
sentence[idx] = 'unk'
def buildNGram(sentences, N = 2):
vocabulary = {}
for sentence in sentences:
tokens = nltk.word_tokenize("starttoken " +" ".join(sentence) + " stoptoken")
for i in range(N, len(tokens)):
token = ""
for j in range(N, 0, -1):
if j != N:
token = token + " " +tokens[i-j].lower()
else:
token = token + tokens[i-j].lower()
if token in vocabulary:
vocabulary[token] += 1
else:
vocabulary[token] = 1
return vocabulary;
def buildVocabulary(sentences):
vocabulary = {}
for sentence in sentences:
tokens = nltk.word_tokenize("starttoken " +" ".join(sentence) + " stoptoken")
for token in tokens:
token = token.lower()
if token in vocabulary:
vocabulary[token] += 1
else:
vocabulary[token] = 1
#For Sorting the Dictionary
sorted_vocabulary = OrderedDict(sorted(vocabulary.items(), key=operator.itemgetter(1)))
#print(type(sorted_vocabulary))
return sorted_vocabulary;
def whichGram(num):
if num == 1:
return vocab
elif num == 2:
return biGram
elif num == 3:
return triGram
#elif num == 4:
# return fourGram
else:
print("First build the model", num, " and approach me")
def count_succ(sentence, N):
"""
return uniq count and also count
"""
if sentence in succ_count_dict:
return succ_count_dict[sentence]
count = 0
uniqcount = 0
gram = whichGram(N + 1)
for word in vocab:
search = word + ' ' + sentence
if search in gram:
count += gram[search]
uniqcount += 1
succ_count_dict[sentence] = [uniqcount, count]
return [uniqcount, count]
def count_pred(sentence, N):
"""
return uniq count and also count
"""
if sentence in pred_count_dict:
return pred_count_dict[sentence]
count = 0
uniqcount = 0
gram = whichGram(N + 1)
for word in vocab:
search = sentence + ' ' + word
if search in gram:
count += gram[search]
uniqcount += 1
pred_count_dict[sentence] = [uniqcount, count]
return [uniqcount, count]
def kneser_nay_prob(sentence, N):
if N <= 2:
delta = 1
else:
delta = 0.75
gram = whichGram(N)
if sentence in gram:
count = gram[sentence]
else:
count = 0
tokens = nltk.word_tokenize(sentence)
premise = ""
for i in range(len(tokens) - 1):
if i == 0:
premise = premise + tokens[i].lower()
else:
premise = premise + " " + tokens[i].lower()
hypothesis = tokens[len(tokens) - 1]
premise_pred_count = count_pred(premise, N-1)
hypo_succ_count = count_succ(hypothesis, 1)
"""
if count == 0:
return hypo_succ_count[0] / len(biGram)
"""
term1 = max(count - delta, 0) / vocab[premise]
lambda_term = delta * premise_pred_count[0] / premise_pred_count[1]
return (term1 + (lambda_term * (hypo_succ_count[0] / len(biGram))))
def calculate_perplexity(N):
perplexity = 0
count = 0
for sentence in test:
count += 1
prob = 0
tokens = nltk.word_tokenize("starttoken " +" ".join(sentence) + " stoptoken")
for i in range(N, len(tokens)):
token = ""
for j in range(N, 0, -1):
if j != N:
token = token + " " + (tokens[i-j]).lower()
else:
token = token + tokens[i-j].lower()
try:
prob = prob + math.log(kneser_nay_prob(token, N))
except:
prob = prob + math.log(1/len(vocab))
#print("problem : ", token)
if count%2000 == 0:
print("Perplexity for ", count, " : ", perplexity/len(test))
prob = -(prob)/len(tokens)
perplexity += math.exp(prob)
return perplexity/len(test)
def generate_sentence():
sentence = ""
count = 0
candidate = ""
used = {}
for k,v in triGram.items():
if v > count:
ignore = False
tokens = nltk.word_tokenize(k)
for token in tokens:
if not token.isalpha():
ignore = True
if ignore == True:
continue
candidate = k
#print(k, " : ", v)
count = v
used[candidate] = 1
sentence = sentence + candidate
#print("sentence : " , sentence)
"""
begining three words are fixed
"""
changed = True
while True:
if changed == False:
break
changed = False
candidate = ""
count = 0
tokens = nltk.word_tokenize(sentence)
pred = tokens[len(tokens) - 2] + " " +tokens[len(tokens) - 1]
for word in vocab:
check = pred + " " + word
tokens = nltk.word_tokenize(check)
"""
for token in tokens:
if not token.isalpha():
ignore = True
if ignore == True:
continue
"""
if check in used:
continue
if check in triGram:
changed = True
if count < triGram[check]:
count = triGram[check]
candidate = check
if changed == True:
tokens = nltk.word_tokenize(candidate)
sentence = sentence + " " + tokens[len(tokens) - 1]
used[candidate] = 1
#print("sentence : " , sentence)
final_sent = ""
tokens = nltk.word_tokenize(sentence)
for token in tokens:
if token == 'starttoken' or token == 'brownstoptoken':
continue
elif token == 'unk':
final_sent = final_sent + " " + vocab[np.random.randint(0, len(vocab))]
else:
final_sent = final_sent + " " + token
print("sentence is : ", final_sent)
##############################################
print('Configuration : train = Brown, Test = Brown')
train, test = fetch_train_test(['brown'], 'brown')
print("train : ", len(train), "test : ", len(test))
vocab = buildVocabulary(train)
infrequent_words = less_freq(vocab);
permute = np.ones(len(infrequent_words))
permute[:int(len(infrequent_words) * 0.03)] = 0
for i in range(int(len(infrequent_words) * 0.9)):
infrequent_words.popitem()
"""
Just some emperical handling
"""
if 'a.m.' in vocab:
infrequent_words['a.m.'] = 1
if 'p.m.' in vocab:
infrequent_words['p.m.'] = 1
for k,v in infrequent_words.items():
if k in infrequent_words:
vocab.pop(k)
unknown[k] = 1
vocab['unk'] = len(unknown)
replace_unk();
biGram = buildNGram(train, 2)
triGram = buildNGram(train, 3)
#perp = calculate_perplexity(2)
#print("perplexity is : ", perp)
generate_sentence()
print("***********************************************************");
print('Configuration : train = Gutenberg, Test = Gutenberg')
train, test = fetch_train_test(['gutenberg'], 'gutenberg')
print("train : ", len(train), "test : ", len(test))
vocab = buildVocabulary(train)
infrequent_words = less_freq(vocab);
permute = np.ones(len(infrequent_words))
permute[:int(len(infrequent_words) * 0.03)] = 0
for i in range(int(len(infrequent_words) * 0.9)):
infrequent_words.popitem()
"""
#Just some emperical handling
"""
if 'a.m.' in vocab:
infrequent_words['a.m.'] = 1
if 'p.m.' in vocab:
infrequent_words['p.m.'] = 1
for k,v in infrequent_words.items():
if k in infrequent_words:
vocab.pop(k)
unknown[k] = 1
vocab['unk'] = len(unknown)
replace_unk();
biGram = buildNGram(train, 2)
triGram = buildNGram(train, 3)
#perp = calculate_perplexity(2)
#print("perplexity is : ", perp)
generate_sentence()
print("***********************************************************");
print('Configuration : train = Brown + Gutenberg, Test = Brown')
train, test = fetch_train_test(['brown', 'gutenberg'], 'brown')
print("train : ", len(train), "test : ", len(test))
vocab = buildVocabulary(train)
infrequent_words = less_freq(vocab);
permute = np.ones(len(infrequent_words))
permute[:int(len(infrequent_words) * 0.03)] = 0
for i in range(int(len(infrequent_words) * 0.9)):
infrequent_words.popitem()
"""
#Just some emperical handling
"""
if 'a.m.' in vocab:
infrequent_words['a.m.'] = 1
if 'p.m.' in vocab:
infrequent_words['p.m.'] = 1
for k,v in infrequent_words.items():
if k in infrequent_words:
vocab.pop(k)
unknown[k] = 1
vocab['unk'] = len(unknown)
replace_unk();
biGram = buildNGram(train, 2)
triGram = buildNGram(train, 3)
#perp = calculate_perplexity(2)
#print("perplexity is : ", perp)
generate_sentence()
print("***********************************************************");
print('Configuration : train = Brown + Gutenberg, Test = Gutenberg')
train, test = fetch_train_test(['brown', 'gutenberg'], 'gutenberg')
print("train : ", len(train), "test : ", len(test))
vocab = buildVocabulary(train)
infrequent_words = less_freq(vocab);
permute = np.ones(len(infrequent_words))
permute[:int(len(infrequent_words) * 0.03)] = 0
for i in range(int(len(infrequent_words) * 0.9)):
infrequent_words.popitem()
"""
#Just some emperical handling
"""
if 'a.m.' in vocab:
infrequent_words['a.m.'] = 1
if 'p.m.' in vocab:
infrequent_words['p.m.'] = 1
for k,v in infrequent_words.items():
if k in infrequent_words:
vocab.pop(k)
unknown[k] = 1
vocab['unk'] = len(unknown)
replace_unk();
biGram = buildNGram(train, 2)
triGram = buildNGram(train, 3)
#perp = calculate_perplexity(2)
#print("perplexity is : ", perp)
generate_sentence()
print("***********************************************************");