-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinal_model.py
495 lines (417 loc) · 18.5 KB
/
final_model.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
# from google.colab import files
# uploaded = files.upload()
#These above two lines are only for Vivian running the code through google collab
import numpy as np
import torch
import re
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from collections import Counter
import math
##############################################
## N-gram LM SECTION
##############################################
# bigram counts used in trigram model
bigram_counts = dict()
# This function will compute bigram conditional probabilities
# as in Eq 3.11 of Jurafsky & Martin Ch 3.
def bigram_training(training_set, smoothing):
global bigram_counts
bigram_count = Counter()
unigram_count = Counter()
if smoothing:
unique_bigrams = set()
# count bigram and unigram occurences in corpus
for line in training_set:
word_list = line
last_word = len(word_list) - 1
for i,w in enumerate(word_list):
# special begin-symbol
if i == 0:
bg = '<s> ' + w
# every other case
else:
bg = word_list[i-1] + ' ' + w
# increment counts
bigram_count[bg] += 1
unigram_count[w] += 1
# special end-symbol
bg = w + ' </s>'
bigram_count[bg] += 1
# every line has a special begin and end symbol
unigram_count['<s>'] += 1
unigram_count['</s>'] += 1
if smoothing:
# unique vocabulary
unique_bigrams.update(set(word_list))
# compute probabilities and add to probs dictionary
probs = {}
for bg in bigram_count:
w1,w2 = bg.split()
num = bigram_count[bg]
denom = unigram_count[w1]
bigram_counts['%s %s'%(w1,w2)] = bigram_count[bg]
if smoothing:
V = len(unique_bigrams) + 1 # adds in UNK
num += 1
denom += V
if (num == 0 or denom == 0):
probs['%s | %s'%(w2,w1)] = 0.0
else:
p = float(num)/float(denom)
probs['%s | %s'%(w2,w1)] = p
return probs
def bigram_testing(test_set, model):
sentence_probs = dict()
# calculate bigram sentence probabilities
for line in test_set:
bigram_prob_log_sum = 0
word_list = line
last_word = len(word_list) - 1
num_bigrams = 0
for i,w in enumerate(word_list):
if i == 0:
w1 = '<s>'
w2 = w
else:
w1 = word_list[i-1]
w2 = w
try:
bigram_prob_log_sum += math.log(model['%s | %s'%(w2,w1)], 2)
except:
bigram_prob_log_sum += 0
num_bigrams += 1
# omit beginning of sentence marker from total word tokens N
w1 = w
w2 = '</s>'
try:
bigram_prob_log_sum += math.log(model['%s | %s'%(w2,w1)], 2)
except:
bigram_prob_log_sum += 0
# add to bigram sentence probabilities dictionary
sentence = " ".join(line)
# sentence cross-entropy (used in perplexity calculation below)
sentence_probs['%s|%s' %(sentence, num_bigrams)] = bigram_prob_log_sum
return sentence_probs
def bigram_perplexity(sentence_probs):
num_bigrams = 0
bigram_prob_log_sum = 0
for sentence, prob in sentence_probs.items():
sentence,bigram_count = sentence.split("|")
num_bigrams += int(bigram_count)
try:
bigram_prob_log_sum += prob
except:
bigram_prob_log_sum += 0
# entropy definition of perplexity
return math.pow(2, -(bigram_prob_log_sum / num_bigrams))
# This function will compute trigram conditional probabilities
# as in Eq 3.11 of Jurafsky & Martin Ch 3.
def trigram_training(training_set, smoothing):
global bigram_counts
trigram_count = Counter()
if smoothing:
unique_trigrams = set()
# count trigram occurences in corpus
for line in training_set:
word_list = line
last_word = len(word_list) - 1
for i in range(last_word):
# first word
if i == 0:
tg = '<s> ' + word_list[i] + ' ' + word_list[i+1]
# last word
elif i == (last_word - 1):
tg = word_list[i] + ' ' + word_list[i+1] + ' </s>'
# every other case
else:
tg = word_list[i-1] + ' ' + word_list[i] + ' ' + word_list[i+1]
# increment counts
trigram_count[tg] += 1
if smoothing:
# unique vocabulary
unique_trigrams.update(set(word_list))
# compute probabilities and add to probs dictionary
probs = {}
for tg in trigram_count:
w1,w2,w3 = tg.split()
num = trigram_count[tg]
denom = bigram_counts['%s %s'%(w1,w2)]
if smoothing:
V = len(unique_trigrams) + 1 # adds in UNK
num += 1
denom += V
if (num == 0 or denom == 0):
probs['%s | %s %s'%(w3,w1,w2)] = 0.0
else:
p = float(num)/float(denom)
probs['%s | %s %s'%(w3,w1,w2)] = p
return probs
def trigram_testing(test_set, model):
sentence_probs = dict()
# calculate trigram sentence probabilities
for line in test_set:
trigram_prob_log_sum = 0
word_list = line
last_word = len(word_list) - 1
# adjust for not adding the beginning of sentence marker
num_trigrams = -1
for i in range(last_word):
if i == 0:
w1 = '<s>'
w2 = word_list[i]
w3 = word_list[i+1]
elif i == (last_word - 1):
w1 = word_list[i]
w2 = word_list[i+1]
w3 = '</s>'
else:
w1 = word_list[i-1]
w2 = word_list[i]
w3 = word_list[i+1]
try:
trigram_prob_log_sum += math.log(model['%s | %s %s'%(w3,w1,w2)], 2)
except:
pass
num_trigrams += 1
# add to trigram sentence probabilities dictionary
sentence = " ".join(line)
# sentence cross-entropy (used in perplexity calculation below)
sentence_probs['%s|%s' %(sentence, num_trigrams)] = trigram_prob_log_sum
return sentence_probs
def trigram_perplexity(sentence_probs):
num_trigrams = 0
trigram_prob_log_sum = 0
for sentence, prob in sentence_probs.items():
sentence,trigram_count = sentence.split("|")
num_trigrams += int(trigram_count)
try:
trigram_prob_log_sum += prob
except:
trigram_prob_log_sum += 0
return math.pow(2, -(trigram_prob_log_sum / num_trigrams))
# preprocesses corpus
def corp_lines(file_name, novel):
corpus_lines = []
# read in and preprocess the corpus
raw_corpus = open(file_name, "r")
corpus_file = raw_corpus.readlines()
raw_corpus.close()
if (novel):
raw_corpus = open(file_name, "r")
corpus_file = raw_corpus.read()
# avoid extraneous newlines
corpus_file = corpus_file.replace("Mr.", "Mr")
corpus_file = corpus_file.replace("Mrs.", "Mrs")
# remove page endings
for i in range(2, 349):
corpus_file = corpus_file.replace("Page | " + str(i) + " Harry Potter and the Philosophers Stone - J.K. Rowling ", "")
# remove chapters
for i in range(1, 62):
corpus_file = corpus_file.replace("Chapter " + str(i), "")
# make lines given to model full sentences
corpus_file = corpus_file.replace("\n", " ")
# split novels by periods
corpus_file = corpus_file.rsplit(".")
check = open("check"+file_name, "w")
for line in corpus_file:
# remove ham and spam tags
if (file_name == "SMSSpamCollection.txt"):
line = line[3:]
# -> all lower case
line = line.lower()
# remove tweet start and ends
line = line.replace("<p>", " ")
line = line.replace("</p>", " ")
# remove punctuation
line = re.sub("[^a-zA-Z]", " ", line)
# remove extra spaces
words = line.split()
line = " ".join(words)
if (line != ""):
check.write(line + "\n")
words = line.split()
corpus_lines.append(words)
raw_corpus.close()
check.close()
return corpus_lines
# find the minimum lines of all the files
def minimum_lines(genre, novel, poem):
if (genre):
news = corp_lines("eng_news_2016_10K-sentences.txt", 0)
wiki = corp_lines("eng_wikipedia_2016_10K-sentences.txt", 0)
texts = corp_lines("SMSSpamCollection.txt", 0)
tweets = corp_lines("tweets3.txt", 0)
novel = corp_lines("philosophers_stone.txt", 1)
return min(len(news), len(wiki), len(texts), len(tweets), len(novel))
if (novel):
ps = corp_lines("philosophers_stone.txt", 1)
pnp = corp_lines("pride_and_prejudice.txt", 1)
a = corp_lines("arthur.txt", 1)
return min(len(ps), len(pnp), len(a))
if (poem):
s = corp_lines("sonnets.txt", 0)
w = corp_lines("leaves_of_grass.txt", 0)
f = corp_lines("frost.txt", 0)
return min(len(s),len(w),len(f))
# Takes in file name string of the corpus for training (baseline). Splits into 2:1.
# Returns the training array, testing array of baseline corpus, and size of testing array
def process_baseline(file_name, novel, poem):
corpus_lines = corp_lines(file_name, novel)
# get total number of corpus sentences
num_corpus = len(corpus_lines)
# split into training and test sets (2:1 split)
corpus_training = []
corpus_test = []
corpus_first_test = round(num_corpus*2/3)
# add in training lines to training array
for training in range(corpus_first_test):
corpus_training.append(corpus_lines[training])
# take the minimum of testing lines in this corpus and lines in all corpuses
end_test = num_corpus - corpus_first_test
if (poem):
min_lines = minimum_lines(0,0,1)
elif (novel):
min_lines = minimum_lines(0,1,0)
else:
min_lines = minimum_lines(1,0,0)
num_test = min(min_lines, end_test)
print(num_test)
end_test = corpus_first_test+num_test
# add in test lines to test array
for test in range(corpus_first_test, end_test):
corpus_test.append(corpus_lines[test])
test_length = len(corpus_test)
return corpus_training, corpus_test, test_length
# Takes in the file name of the other testing corpora, and size of test size
# Returns testing array of other corpus, matching size of baseline test array
def process_test_corpora(file_name, test_size, novel):
corpus_lines = corp_lines(file_name, novel)
# add appropriate number of elements to corpus_test
corpus_test = []
for test in range(test_size):
corpus_test.append(corpus_lines[test])
return corpus_test
def perplexity_generator(a, b, c, novel, poem):
##strings for names of txt files
##################################### General N-Gram Perplexity Generator ######################################
a_training, a_test_abaseline, a_test_size = process_baseline(a, novel, poem)
# all are with smoothing
# train bigram and trigram model on novel
bigram_model_a = bigram_training(a_training, 1)
trigram_model_a = trigram_training(a_training, 1)
# process testing corpora with novel baseline testing size
b_test_abaseline = process_test_corpora(b, a_test_size, novel)
c_test_abaseline = process_test_corpora(c, a_test_size, novel)
# clean up names
a = a[:-4]
b = b[:-4]
c = c[:-4]
a = a.replace("_", " ")
b = b.replace("_", " ")
c = c.replace("_", " ")
a = a.upper()
b = b.upper()
c = c.upper()
print("\n===================== " + a + " CORPUS-TRAINED MODEL =====================\n")
# bigrams
# A perplexities trained on A
bigram_test_probs_a = bigram_testing(a_test_abaseline, bigram_model_a)
print(a + " bigram perplexity: " + str(bigram_perplexity(bigram_test_probs_a)))
# B perplexities trained on A
bigram_test_probs_b = bigram_testing(b_test_abaseline, bigram_model_a)
print(b + " bigram perplexity: " + str(bigram_perplexity(bigram_test_probs_b)))
# C perplexities trained on A
bigram_test_probs_c = bigram_testing(c_test_abaseline, bigram_model_a)
print(c + " bigram perplexity: " + str(bigram_perplexity(bigram_test_probs_c)) + "\n\n")
# trigrams
trigram_test_probs_a = trigram_testing(a_test_abaseline, trigram_model_a)
print(a + " trigram perplexity: " + str(trigram_perplexity(trigram_test_probs_a)))
trigram_test_probs_b = trigram_testing(b_test_abaseline, trigram_model_a)
print(b + " trigram perplexity: " + str(trigram_perplexity(trigram_test_probs_b)))
trigram_test_probs_c = trigram_testing(c_test_abaseline, trigram_model_a)
print(c + " trigram perplexity: " + str(trigram_perplexity(trigram_test_probs_c)) + "\n")
def perplexity_generator_big(a, b, c, d, e, novel, poem):
##strings for names of txt files
##################################### General N-Gram Perplexity Generator ######################################
a_training, a_test_abaseline, a_test_size = process_baseline(a, novel, poem)
# all are with smoothing
# train bigram and trigram model on novel
bigram_model_a = bigram_training(a_training, 1)
trigram_model_a = trigram_training(a_training, 1)
b_novel = b == "philosophers_stone.txt"
c_novel = c == "philosophers_stone.txt"
d_novel = c == "philosophers_stone.txt"
e_novel = e == "philosophers_stone.txt"
# process testing corpora with novel baseline testing size
b_test_abaseline = process_test_corpora(b, a_test_size, b_novel)
c_test_abaseline = process_test_corpora(c, a_test_size, c_novel)
d_test_abaseline = process_test_corpora(d, a_test_size, d_novel)
e_test_abaseline = process_test_corpora(e, a_test_size, e_novel)
# clean up names
a = a[:-4]
b = b[:-4]
c = c[:-4]
d = d[:-4]
e = e[:-4]
a = a.replace("_", " ")
b = b.replace("_", " ")
c = c.replace("_", " ")
d = d.replace("_", " ")
e = e.replace("_", " ")
a = a.upper()
b = b.upper()
c = c.upper()
d = d.upper()
e = e.upper()
print("\n===================== " + a + " CORPUS-TRAINED MODEL =====================\n")
# bigrams
# A perplexities trained on A
bigram_test_probs_a = bigram_testing(a_test_abaseline, bigram_model_a)
print(a + " bigram perplexity: " + str(bigram_perplexity(bigram_test_probs_a)))
# B perplexities trained on A
bigram_test_probs_b = bigram_testing(b_test_abaseline, bigram_model_a)
print(b + " bigram perplexity: " + str(bigram_perplexity(bigram_test_probs_b)))
# C perplexities trained on A
bigram_test_probs_c = bigram_testing(c_test_abaseline, bigram_model_a)
print(c + " bigram perplexity: " + str(bigram_perplexity(bigram_test_probs_c)))
# D perplexities trained on A
bigram_test_probs_d = bigram_testing(d_test_abaseline, bigram_model_a)
print(d + " bigram perplexity: " + str(bigram_perplexity(bigram_test_probs_d)))
# E perplexities trained on A
bigram_test_probs_e = bigram_testing(e_test_abaseline, bigram_model_a)
print(e + " bigram perplexity: " + str(bigram_perplexity(bigram_test_probs_e)) + "\n\n")
# trigrams
trigram_test_probs_a = trigram_testing(a_test_abaseline, trigram_model_a)
print(a + " trigram perplexity: " + str(trigram_perplexity(trigram_test_probs_a)))
trigram_test_probs_b = trigram_testing(b_test_abaseline, trigram_model_a)
print(b + " trigram perplexity: " + str(trigram_perplexity(trigram_test_probs_b)))
trigram_test_probs_c = trigram_testing(c_test_abaseline, trigram_model_a)
print(c + " trigram perplexity: " + str(trigram_perplexity(trigram_test_probs_c)))
trigram_test_probs_d = trigram_testing(d_test_abaseline, trigram_model_a)
print(d + " trigram perplexity: " + str(trigram_perplexity(trigram_test_probs_d)))
trigram_test_probs_e = trigram_testing(e_test_abaseline, trigram_model_a)
print(e + " trigram perplexity: " + str(trigram_perplexity(trigram_test_probs_e)) + "\n")
if __name__ == '__main__':
##################################### News Baseline ####################################
perplexity_generator_big("eng_news_2016_10K-sentences.txt", "eng_wikipedia_2016_10K-sentences.txt", "tweets3.txt", "SMSSpamCollection.txt", "philosophers_stone.txt", 0, 0)
##################################### Wiki Baseline ####################################
perplexity_generator_big("eng_wikipedia_2016_10K-sentences.txt", "tweets3.txt", "SMSSpamCollection.txt", "philosophers_stone.txt", "eng_news_2016_10K-sentences.txt", 0, 0)
#################################### Tweet Baseline ####################################
perplexity_generator_big("tweets3.txt", "SMSSpamCollection.txt", "philosophers_stone.txt", "eng_news_2016_10K-sentences.txt", "eng_wikipedia_2016_10K-sentences.txt", 0, 0)
#################################### Texts Baseline ####################################
perplexity_generator_big("SMSSpamCollection.txt", "philosophers_stone.txt", "eng_news_2016_10K-sentences.txt", "eng_wikipedia_2016_10K-sentences.txt", "tweets3.txt", 0, 0)
##################################### Novels Baseline ####################################
perplexity_generator_big("philosophers_stone.txt", "eng_news_2016_10K-sentences.txt", "eng_wikipedia_2016_10K-sentences.txt", "tweets3.txt", "SMSSpamCollection.txt", 1, 0)
##################################### Novels Over Time ####################################
##################################### Old Baseline ######################################
perplexity_generator("arthur.txt", "pride_and_prejudice.txt", "philosophers_stone.txt", 1, 0)
##################################### Middle Baseline ###################################
perplexity_generator("pride_and_prejudice.txt", "philosophers_stone.txt", "arthur.txt", 1, 0)
##################################### Young Baseline #####################################
perplexity_generator("philosophers_stone.txt", "arthur.txt", "pride_and_prejudice.txt", 1, 0)
##################################### Poems Over Time ####################################
perplexity_generator("sonnets.txt", "leaves_of_grass.txt", "frost.txt", 0, 1)
perplexity_generator("leaves_of_grass.txt", "frost.txt", "sonnets.txt", 0, 1)
perplexity_generator("frost.txt", "sonnets.txt", "leaves_of_grass.txt", 0, 1)