-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnlp_hw1 (1).py
336 lines (286 loc) · 10.2 KB
/
nlp_hw1 (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
# -*- coding: utf-8 -*-
"""NLP_HW1.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1Haz5duS_tl0DsdNcNiElTjqOGHRaDXWn
"""
from types import resolve_bases
import sys
from collections import defaultdict
import math
import random
import os
import os.path
"""
COMS W4705 - Natural Language Processing - Fall 2022
Programming Homework 1 - Trigram Language Models
Daniel Bauer
Alice Diakova
"""
def corpus_reader(corpusfile, lexicon=None):
with open(corpusfile,'r') as corpus:
for line in corpus:
if line.strip():
sequence = line.lower().strip().split()
if lexicon:
yield [word if word in lexicon else "UNK" for word in sequence]
else:
yield sequence
def get_lexicon(corpus):
word_counts = defaultdict(int)
for sentence in corpus:
for word in sentence:
word_counts[word] += 1
return set(word for word in word_counts if word_counts[word] > 1)
def get_ngrams_helper(sequence, n, res):
temp_tup = list()
temp_tup.append('START')
x = 1
for i in range(0, len(sequence)):
if x == n:
break
if i == len(sequence) - 1:
temp_tup.append('STOP')
res.append(tuple(temp_tup))
return res
temp_tup.append(sequence[i])
x+=1
res.append(tuple(temp_tup))
for i in range(0, len(sequence)):
temp_tup = list()
x = 0
for j in range(i, len(sequence)+1):
if x == n:
break
if j == len(sequence):
temp_tup.append('STOP')
res.append(tuple(temp_tup))
return res
temp_tup.append(sequence[j])
x+=1
res.append(tuple(temp_tup))
if n == 1:
temp_tup = list()
temp_tup.append('STOP')
res.append(tuple(temp_tup))
return res
def get_ngrams(sequence, n):
"""
COMPLETE THIS FUNCTION (PART 1)
Given a sequence, this function should return a list of n-grams, where each n-
gram is a Python tuple.
This should work for arbitrary values of 1 <= n < len(sequence).
"""
if n == 0 or len(sequence) == 0:
return []
res = list()
if n < len(sequence) or (n == 1 and len(sequence) == 1):
res = get_ngrams_helper(sequence, n, res)
else:
sub = n - len(sequence)
if sub >= 2:
#only need a single n-gram
temp_tup = list()
for i in range(0, sub-1):
temp_tup.append('START')
for w in sequence:
temp_tup.append(w)
temp_tup.append('STOP')
res.append(tuple(temp_tup))
else:
start_count = n-1
while start_count > 0: #iterates through each tuple, stops when we have seen the last word in a tuple but the tuple is not yet of length n
t = start_count
temp_tup = list()
while t > 0:
temp_tup.append('START')
t -= 1
i = 0
while i < n-(start_count) and i < len(sequence):
temp_tup.append(sequence[i])
i+=1
res.append(tuple(temp_tup))
start_count -= 1
#not worried about STARTs in n-grams anymore:
for i in range(0, len(sequence)):
temp_tup = list()
x = 0
for j in range(i, len(sequence)+1):
if x == n:
break
if j == len(sequence):
temp_tup.append('STOP')
res.append(tuple(temp_tup))
return res
temp_tup.append(sequence[j])
x+=1
res.append(tuple(temp_tup))
return res
class TrigramModel(object):
def __init__(self, corpusfile):
# Iterate through the corpus once to build a lexicon
generator = corpus_reader(corpusfile)
self.lexicon = get_lexicon(generator)
self.lexicon.add("UNK")
self.lexicon.add("START")
self.lexicon.add("STOP")
# Now iterate through the corpus again and count ngrams
generator = corpus_reader(corpusfile, self.lexicon)
self.word_count = 0
self.count_ngrams(generator)
def count_ngrams(self, corpus):
"""
COMPLETE THIS METHOD (PART 2)
Given a corpus iterator, populate dictionaries of unigram, bigram,
and trigram counts.
"""
self.unigramcounts = {} # might want to use defaultdict or Counter instead
self.bigramcounts = {}
self.trigramcounts = {}
for sentence in corpus:
sentence_unigrams = get_ngrams(sentence, 1)
for unigram in sentence_unigrams:
if unigram not in self.unigramcounts:
self.unigramcounts[unigram] = 1
else:
self.unigramcounts[unigram] += 1
if unigram[0] != 'START':
# according to edstem: "The word count includes the count of stop tokens, but it excludes the count of start tokens"
self.word_count += 1
sentence_bigrams = get_ngrams(sentence, 2)
for bigram in sentence_bigrams:
if bigram not in self.bigramcounts:
self.bigramcounts[bigram] = 1
else:
self.bigramcounts[bigram] += 1
sentence_trigrams = get_ngrams(sentence, 3)
for trigram in sentence_trigrams:
if trigram not in self.trigramcounts:
self.trigramcounts[trigram] = 1
else:
self.trigramcounts[trigram] += 1
#print("word count: "+ str(self.word_count))
return
def raw_trigram_probability(self,trigram):
"""
COMPLETE THIS METHOD (PART 3)
Returns the raw (unsmoothed) trigram probability
"""
if trigram not in self.trigramcounts.keys():
return 0
temp = list()
temp.append(trigram[0])
temp.append(trigram[1])
bigram = tuple(temp)
if bigram not in self.bigramcounts.keys():
return 0
return float(self.trigramcounts[trigram])/ self.bigramcounts[bigram]
def raw_bigram_probability(self, bigram):
"""
COMPLETE THIS METHOD (PART 3)
Returns the raw (unsmoothed) bigram probability
"""
if bigram not in self.bigramcounts.keys():
return 0
temp = list()
temp.append(bigram[0])
unigram = tuple(temp)
return float(self.bigramcounts[bigram]) / self.unigramcounts[unigram]
def raw_unigram_probability(self, unigram):
"""
COMPLETE THIS METHOD (PART 3)
Returns the raw (unsmoothed) unigram probability.
"""
#hint: recomputing the denominator every time the method is called
# can be slow! You might want to compute the total number of words once,
# store in the TrigramModel instance, and then re-use it.
if unigram not in self.unigramcounts.keys():
return 0
return float(self.unigramcounts[unigram]) / self.word_count
def generate_sentence(self,t=20):
"""
COMPLETE THIS METHOD (OPTIONAL)
Generate a random sentence from the trigram model. t specifies the
max length, but the sentence may be shorter if STOP is reached.
"""
return result
def smoothed_trigram_probability(self, trigram):
"""
COMPLETE THIS METHOD (PART 4)
Returns the smoothed trigram probability (using linear interpolation).
"""
lambda1 = 1/3.0
lambda2 = 1/3.0
lambda3 = 1/3.0
temp = list()
temp.append(trigram[0])
temp.append(trigram[1])
bigram = tuple(temp)
temp = list()
temp.append(trigram[0])
unigram = tuple(temp)
return lambda1 * self.raw_trigram_probability(trigram) + lambda2 * self.raw_bigram_probability(bigram) + lambda3 * self.raw_unigram_probability(unigram)
def sentence_logprob(self, sentence):
"""
COMPLETE THIS METHOD (PART 5)
Returns the log probability of an entire sequence.
"""
logprob = 0
tri = get_ngrams(sentence, 3)
for trigram in tri:
logprob += math.log2(self.smoothed_trigram_probability(trigram))
return logprob
def perplexity(self, corpus):
"""
COMPLETE THIS METHOD (PART 6)
Returns the log probability of an entire sequence.
"""
res = 0
for sentence in corpus:
res += self.sentence_logprob(sentence)
#res is now the sum of the log probs of each sentence
res /= float(self.word_count)
return 2**(-res)
def essay_scoring_experiment(training_file1, training_file2, testdir1, testdir2):
model1 = TrigramModel(training_file1)
model2 = TrigramModel(training_file2)
total = 0
correct = 0
for f in os.listdir(testdir1):
pp = model1.perplexity(corpus_reader(os.path.join(testdir1, f),
model1.lexicon))
# ..
#NOTE: MY OTHER FUNCTIONS WERE LEADING TO THE PERPLEXITY ALL
#BEING AROUND 1. I KNOW THE FOLLOWING THRESHOLD IS NOT IDEAL
#FOR DETERMINING WHICH FILES WERE CORRECT BUT IT IS MY WAY OF
#REPRESENTING HOW TO DETERMINE CORRECTNESS USING PERPLEXITY
if pp < 1.001:
correct += 1
total += 1
for f in os.listdir(testdir2):
pp = model2.perplexity(corpus_reader(os.path.join(testdir2, f),
model2.lexicon))
# ..
if pp < 1.001:
correct += 1
total += 1
return correct / float(total)
if __name__ == "__main__":
#model = TrigramModel('hw1_data/brown_train.txt')
# put test code here...
# or run the script from the command line with
# $ python -i trigram_model.py [corpus_file]
# >>>
#
# you can then call methods on the model instance in the interactive
# Python prompt.
# Testing perplexity:
#dev_corpus = corpus_reader('hw1_data/brown_train.txt', model.lexicon)
#pp = model.perplexity(dev_corpus)
#print(pp)
#Essay scoring experiment:
acc = essay_scoring_experiment('hw1_data/ets_toefl_data/train_high.txt',
'hw1_data/ets_toefl_data/train_low.txt',
"hw1_data/ets_toefl_data/test_high",
"hw1_data/ets_toefl_data/test_low")
print(acc)