-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlangmodels.py
244 lines (200 loc) · 8.33 KB
/
langmodels.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
import sys
import math
import copy
# TODO: refactor / clean up
PHI = '/phi/'
def flatten_list_of_list(lol):
return [item for sublist in lol for item in sublist]
def prob_unigram(normalized_tok, freq_of_unigram, total_unigrams):
return freq_of_unigram[normalized_tok] / total_unigrams
def prob_bigram(normalized_tok_1, normalized_tok_2, freq_of_bigram, freq_of_unigram, phi_count, do_smooth):
#Pr(w{k}|w{k−1}) = Freq(w{k−1} w{k})/Freq(w{k−1}).
#Pr(tok2|tok1) = Freq(tok1 tok2) / Freq(tok1)
unigram_freq = None
if normalized_tok_1 == PHI:
unigram_1_freq = phi_count
else:
unigram_1_freq = freq_of_unigram[normalized_tok_1]
if do_smooth:
vocab_size = len(freq_of_unigram.keys())
if freq_of_bigram.get((normalized_tok_1, normalized_tok_2)) is None: # Happens for never before seen bigram
return 1 / (unigram_1_freq + vocab_size)
else:
return (freq_of_bigram[(normalized_tok_1,
normalized_tok_2)] + 1) / (unigram_1_freq + vocab_size)
else:
if freq_of_bigram.get((normalized_tok_1, normalized_tok_2)) is None: # Happens for never before seen bigram
return 0.0 # Probability of 0
else:
return freq_of_bigram[(normalized_tok_1,
normalized_tok_2)] / unigram_1_freq
def prob_of_sentence_unigram(sentence, freq_of_unigram, total_unigrams):
log_prob_sum = 0.0
for tok in sentence:
normalized_tok = tok.lower()
prob_tok = prob_unigram(normalized_tok, freq_of_unigram, total_unigrams);
# don't plug in 0 to log
if prob_tok != 0:
log_prob_sum += math.log2(prob_tok)
return log_prob_sum
def prob_of_sentence_bigram(sentence, freq_of_bigram, freq_of_unigram, phi_count, do_smooth):
log_prob_sum = 0.0
for i in range(len(sentence)-1):
j = i+1
normalized_tok_1 = sentence[i].lower()
normalized_tok_2 = sentence[j].lower()
pr_wk_given_wk_minus_1 = prob_bigram(normalized_tok_1,
normalized_tok_2,
freq_of_bigram,
freq_of_unigram,
phi_count,
do_smooth)
if pr_wk_given_wk_minus_1 != 0:
log_prob_sum += math.log2(pr_wk_given_wk_minus_1)
else:
return None # To mark as undefined
return log_prob_sum
"""
The main entry point of this program.
"""
def main():
'''
Parse CLI args
'''
training_file_path = sys.argv[1]
cli_flag = sys.argv[2] # Expects "-test"
test_file_path = sys.argv[3]
'''
Parse training file into tokens
'''
training_file_tokens = []
with open(training_file_path) as training_file:
for line in training_file:
training_file_tokens.append(line.split())
'''
Parse test file into tokens
'''
test_file_tokens = []
with open(test_file_path) as test_file:
for line in test_file:
test_file_tokens.append(line.split())
'''
Generate unigram frequency table
'''
unsmoothed_freq_of_unigram = {}
for sentence in training_file_tokens:
for tok in sentence:
normalized_tok = tok.lower()
if unsmoothed_freq_of_unigram.get(normalized_tok) is None:
# Haven't seen key yet => init count to 1
unsmoothed_freq_of_unigram[normalized_tok] = 1
else:
# Increment count
unsmoothed_freq_of_unigram[normalized_tok] += 1
#print('freq_table: %s' % unsmoothed_freq_of_unigram)
"""
Calculate unigram probability for each sentence
"""
'''
[['this', 'is', 'the', 'structure', '.']
['it', 'looks', 'like', 'this', '.']]
'''
unigram_sentence_to_prob = {}
for sentence in test_file_tokens:
unigram_sentence_to_prob[' '.join(sentence)] = prob_of_sentence_unigram(sentence, unsmoothed_freq_of_unigram, len(flatten_list_of_list(training_file_tokens)))
# print(unigram_sentence_to_prob)
############## BIGRAM (UNSMOOTHED) =-------------------------------------------
'''
Generate unsmoothed bigram frequency table
'''
training_file_tokens_w_phi = copy.deepcopy(training_file_tokens)
# Generate bigram token list with _PHI_ (sentence starter)
for sentence in training_file_tokens_w_phi:
sentence.insert(0, PHI) # Prepend PHI to every sentence
unsmoothed_freq_of_bigram = {}
for sentence in training_file_tokens_w_phi:
for i in range(len(sentence)-1):
j = i+1
normalized_tok_1 = sentence[i].lower()
normalized_tok_2 = sentence[j].lower()
key = (normalized_tok_1, normalized_tok_2)
if unsmoothed_freq_of_bigram.get(key) is None:
# Haven't seen key yet => init count to 1
unsmoothed_freq_of_bigram[key] = 1
else:
# Increment count
unsmoothed_freq_of_bigram[key] += 1
#print('unsmoothed_freq_of_bigram: %s' % unsmoothed_freq_of_bigram)
"""
Calculate unsmoothed bigram probability for each sentence
"""
test_file_tokens_w_phi = copy.deepcopy(test_file_tokens)
for sentence in test_file_tokens_w_phi:
sentence.insert(0, PHI) # Prepend PHI to every sentence
'''
[['/phi/', 'this', 'is', 'the', 'structure', '.']
['/phi/', it', 'looks', 'like', 'this', '.']]
'''
unsmoothed_bigram_sentence_to_prob = {}
for sentence in test_file_tokens_w_phi:
prob_s = prob_of_sentence_bigram(sentence,
unsmoothed_freq_of_bigram,
unsmoothed_freq_of_unigram,
len(training_file_tokens),
False)
if prob_s is None:
unsmoothed_bigram_sentence_to_prob[' '.join(sentence)] = 'undefined'
else:
unsmoothed_bigram_sentence_to_prob[' '.join(sentence)] = prob_s
# == phi_count
'''
Smoothen bigram frequency table
'''
# TODO: add one to each bigram (but not unigram) freqency. Then recalculate probabilities
"""
Calculate smoothed bigram probability for each sentence
"""
'''
[['/phi/', 'this', 'is', 'the', 'structure', '.']
['/phi/', it', 'looks', 'like', 'this', '.']]
'''
smoothed_bigram_sentence_to_prob = {}
for sentence in test_file_tokens_w_phi:
prob_s = prob_of_sentence_bigram(sentence,
unsmoothed_freq_of_bigram,
unsmoothed_freq_of_unigram,
len(training_file_tokens_w_phi),
True)
if prob_s is None:
smoothed_bigram_sentence_to_prob[' '.join(sentence)] = 'undefined'
else:
smoothed_bigram_sentence_to_prob[' '.join(sentence)] = prob_s
# == phi_count
'''
Print output
'''
for sentence in test_file_tokens:
sentence_w_phi = copy.deepcopy(sentence)
sentence_w_phi.insert(0, PHI)
key_no_phi = ' '.join(sentence)
key_w_phi = ' '.join(sentence_w_phi)
unigram_val = None; unsmoothed_bigram_val = None; smoothed_bigram_val = None
if unigram_sentence_to_prob[key_no_phi] == 'undefined':
unigram_val = 'undefined'
else:
unigram_val = round(float(unigram_sentence_to_prob[key_no_phi]), 4)
if unsmoothed_bigram_sentence_to_prob[key_w_phi] == 'undefined':
unsmoothed_bigram_val = 'undefined'
else:
unsmoothed_bigram_val = round(float(unsmoothed_bigram_sentence_to_prob[key_w_phi]), 4)
if smoothed_bigram_sentence_to_prob[key_w_phi] == 'undefined':
smoothed_bigram_val = 'undefined'
else:
smoothed_bigram_val = round(float(smoothed_bigram_sentence_to_prob[key_w_phi]), 4)
print('S = %s\n' % key_no_phi)
print('Unsmoothed Unigrams, logprob(S) = %s' % unigram_val)
print('Unsmoothed Bigrams, logprob(S) = %s' % unsmoothed_bigram_val)
print('Smoothed Bigrams, logprob(S) = %s' % smoothed_bigram_val)
print('')
if __name__ == "__main__":
main()