This repository has been archived by the owner on Jan 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostags.py
executable file
·396 lines (330 loc) · 11.3 KB
/
postags.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
#!/usr/bin/env python
#%%
import math
import string
import numpy as np
import pandas as pd
from collections import Counter
from collections import defaultdict
#%%
def assign_unk(word):
"""Assign tokens to unknown words."""
punct = set(string.punctuation)
noun_suffix = ["action", "age", "ance", "cy", "dom", "ee",
"ence", "er", "hood", "ion", "ism", "ist", "ity",
"ling", "ment", "ness", "or", "ry", "scape",
"ship", "ty"]
verb_suffix = ["ate", "ify", "ise", "ize"]
adj_suffix = ["able", "ese", "ful", "i", "ian", "ible", "ic",
"ish", "ive", "less", "ly", "ous"]
adv_suffix = ["ward", "wards", "wise"]
if (any(char.isdigit() for char in word)):
return "--unk_digit--"
elif any(char in punct for char in word):
return "--unk_punct--"
elif any(char.isupper() for char in word):
return "--unk_upper--"
elif any(word.endswith(suffix) for suffix in noun_suffix):
return "--unk_suffix--"
elif any(word.endswith(suffix) for suffix in verb_suffix):
return "--unk_verb--"
elif any(word.endswith(suffix) for suffix in adj_suffix):
return "--unk_adj--"
elif any(word.endswith(suffix) for suffix in adv_suffix):
return "--unk_adv--"
else:
return "--unk--"
#%%
def get_word_tag (line, vocab):
if not line.split():
word = "--n--"
tag = "--s--"
else:
word, tag = line.split()
if word not in vocab:
word = assign_unk(word)
return word, tag
#%%
def preprocess(vocab, data):
"""Preprocess the data"""
orig = []
prep = []
with open(data, 'r') as df:
for cnt, word in enumerate(df):
if not word.split():
orig.append(word.strip())
word = "--n--"
prep.append(word)
continue
elif word.strip() not in vocab:
orig.append(word.strip())
word = assign_unk(word)
prep.append(word)
continue
else:
orig.append(word.strip())
prep.append(word.strip())
assert(len(orig) == len(open(data, 'r').readlines()))
assert(len(prep) == len(open(data, 'r').readlines()))
return orig, prep
#%%
def pmatrix(matrix, index, cols=None):
if cols is None: cols = index
print(pd.DataFrame(matrix, index=index, columns=cols))
#%%
def create_dictionaries(training_corpus, vocab):
""" Build the count dictionaries
Input:
training_corpus: A taggeed corpus "WORD\tTAG\n" per line
vocab: A dicionary where keys are words and value its index
Output:
emission_counts: Counts of (tag, word)
transition_counts: Counts of (tag, tag)
tag_counts: Counts of tag
"""
emission_counts = defaultdict(int)
transition_counts = defaultdict(int)
tag_counts = defaultdict(int)
# Start of line
prev_tag = "--s--"
# Line count
for i, word_tag in enumerate(training_corpus):
if i % 50000 == 0:
print(f"Word count = {i}")
word, tag = get_word_tag(word_tag, vocab)
transition_counts[(prev_tag, tag)] += 1
emission_counts[(tag, word)] += 1
tag_counts[tag] += 1
prev_tag = tag
#print(f"{i:5d} -- ptag: {prev_tag} -- tag: {tag} -- word: {word}")
return emission_counts, transition_counts, tag_counts
#%%
def predict_pos_mm(prep, y, emission_counts, vocab, states):
num_correct = 0
# (tag,word)
all_words = set(emission_counts.keys())
total = len(y)
for word, y_tup in zip(prep, y):
y_tup_l = y_tup.split()
if len(y_tup_l) == 2:
true_label = y_tup_l[1]
else:
# y tuple do not countain (word, pos). Need to check why...
continue
count_final = 0
pos_final = ''
if word in vocab:
for pos in states:
key = (pos, word)
if key in emission_counts:
count = emission_counts[key]
if count > count_final:
count_final = count
pos_final = pos
if pos_final == true_label:
num_correct += 1
accuracy = num_correct / total
return accuracy
#%%
def create_transition_matrix(alpha, tag_counts, transition_counts):
all_tags = sorted(tag_counts.keys())
num_tags = len(all_tags)
A = np.zeros((num_tags, num_tags))
trans_keys = set(transition_counts.keys())
for i in range(num_tags):
for j in range(num_tags):
count = 0
key = (all_tags[i], all_tags[j])
if key in transition_counts:
count = transition_counts[key]
count_prev_tag = tag_counts[all_tags[i]]
A[i,j] = (count + alpha)/(count_prev_tag + (alpha * num_tags))
return A
#%%
def create_emission_matrix(alpha, tag_counts, emission_counts, vocab):
num_tags = len(tag_counts)
all_tags = sorted(tag_counts.keys())
num_words = len(vocab)
#all_words = list(vocab.keys())
all_words = vocab
B = np.zeros((num_tags, num_words))
emis_keys = set(list(emission_counts.keys()))
for i in range(num_tags):
for j in range(num_words):
count = 0
key = (all_tags[i], all_words[j])
if key in emission_counts:
count = emission_counts[(key)]
count_tag = tag_counts[all_tags[i]]
B[i,j] = (count + alpha)/(count_tag + alpha * num_words)
return B
#%% Viterbi algorithm
def viterbi_initialize(states, tag_counts, A, B, corpus, vocab):
"""Initialize the helpe matrices to viterbi algorithms.
Input:
states: a list of PoS
tag_counts: dict mapping PoS to count.
A: Transmission matrix dim: (num_tags, num_tags)
B: Emission matrix dim: (num_tags, len(vocab))
corpus: List of words: format: WORD\tPOS\n
vocab: a dictionary with keys as words and index as id.
Output:
best_probs: matrix (num_tags, len(corpus)) of floats
best_paths: matrix (num_tags, len(corpus)) of ints
"""
num_tags = len(tag_counts)
C = np.zeros((num_tags, len(corpus)), dtype=np.float)
D = np.zeros((num_tags, len(corpus)), dtype=np.int)
s_idx = states.index('--s--')
for i in range(len(states)):
if A[s_idx, i] == 0:
C[i, 0] = np.float('-inf')
else:
C[i, 0] = math.log(A[s_idx, i]) + math.log(B[i, vocab[corpus[0]]])
return C, D
#%%
def viterbi_forward(A, B, corpus, best_probs, best_paths, vocab):
"""A: Transition matrix np.array dim(TAGS, TAGS)
B: Emission Matrix np.array dim(TAGS, #VOCAB)
corpus: list of preprocessed word to fit
best_props: np.array (matrix C) dim(TAGS, #CORPUS)
best_paths: np.array (matrix D) dim(TAGS, #CORPUS)
vocab: list of valid words
"""
num_tags = best_probs.shape[0]
C = best_probs
D = best_paths
for i in range(1, len(corpus)):
if i % 5000 == 0:
print(f"words processed: {i:>8}")
# For each unique PoS tag that word can be
for j in range(A.shape[1]):
C_i = np.float('-inf')
D_i = None
# For each PoS tag that previous word can be
for k in range(A.shape[0]):
cindex = vocab[prep[i]]
b = math.log(B[j, cindex])
a = math.log(A[k, j])
c = C[k,i-1]
prob = c + a + b
if prob > C_i:
C_i = prob
D_i = k
C[j, i] = C_i
D[j, i] = D_i
return C, D
#%%
def viterbi_backward(best_probs, best_paths, corpus, states):
"""Return the best path"""
C = best_probs
D = best_paths
m = best_paths.shape[1]
z = [None] * m
num_tags = best_probs.shape[0]
best_prob_for_last_word = float('-inf')
pred = [None] * m
s = np.argmax(C[:, -1])
pred[m - 1] = states[s]
z[m - 1] = D[s, -1]
for i in range(m-1, 0, -1):
pred[i-1] = states[z[i]]
z[i-1] = D[z[i], i-1]
return pred
#%%
def compute_accuracy(y_predicted, y_labels):
num_correct = 0
total = 0
for p, y in zip(y_predicted, y_labels):
word_tag_tuple = y.split()
if len(word_tag_tuple) != 2:
continue
word, tag = word_tag_tuple
if p == tag:
num_correct += 1
total += 1
return num_correct/total
#%%
with open("data/WSJ_02-21.pos", "r") as f:
training_corpus = f.readlines()
#%%
with open("data/hmm_vocab.txt", "r") as f:
voc_l = f.read().split('\n')
#%%
vocab = {}
for i, word in enumerate(sorted(voc_l)):
vocab[word] = i
#%%
with open("data/WSJ_24.pos", "r") as f:
y = f.readlines()
#%%
_, prep = preprocess(vocab, "data/test.words")
#%%
emission_counts, transition_counts, tag_counts = create_dictionaries(training_corpus, vocab)
#%%
states = sorted(tag_counts.keys())
#%%
accuracy_predict_pos = predict_pos_mm(prep, y, emission_counts, vocab, states)
print(f"Accuracy of prediction using predict_pos_mm is {accuracy_predict_pos:.4f}")
#%%
alpha = 0.001
A = create_transition_matrix(alpha, tag_counts, transition_counts)
#%%
B = create_emission_matrix(alpha, tag_counts, emission_counts, list(vocab))
#%%
best_probs, best_paths = viterbi_initialize(states, tag_counts, A, B, prep, vocab)
#%%
best_probs, best_paths = viterbi_forward(A, B, prep, best_probs, best_paths, vocab)
#%%
pred = viterbi_backward(best_probs, best_paths, prep, states)
#%%
print(f"Accuracy of the Viterbi algorithm is {compute_accuracy(pred, y):.4f}")
### These are tests used to test the functions
### Need to rewrite them to be more helpful.
###
### #%% TESTES
### # Testing your function
### print("View a subset of transition matrix A")
### A_sub = pd.DataFrame(A[30:35,30:35], index=states[30:35], columns = states[30:35] )
### print(A_sub)
###
### #%%
### # creating your emission probability matrix. this takes a few minutes to run.
###
### print(f"View Matrix position at row 0, column 0: {B[0,0]:.9f}")
### print(f"View Matrix position at row 3, column 1: {B[3,1]:.9f}")
###
### # Try viewing emissions for a few words in a sample dataframe
### cidx = ['725','adroitly','engineers', 'promoted', 'synergy']
###
### # Get the integer ID for each word
### cols = [vocab[a] for a in cidx]
###
### # Choose POS tags to show in a sample dataframe
### rvals =['CD','NN','NNS', 'VB','RB','RP']
###
### # For each POS tag, get the row number from the 'states' list
### rows = [states.index(a) for a in rvals]
###
### # Get the emissions for the sample of words, and the sample of POS tags
### B_sub = pd.DataFrame(B[np.ix_(rows,cols)], index=rvals, columns = cidx )
### print(B_sub)
###
### #%% Test viterbi initialization
### print(f"best_probs[0,0]: {best_probs[0,0]:.4f}")
### print(f"best_paths[2,3]: {best_paths[2,3]:.4f}")
###
### #% Teest viterbi forward
### print(f"A at row 0, col 0: {A[0,0]:.9f}")
### print(f"A at row 3, col 1: {A[3,1]:.4f}")
###
### #%%
### m=len(pred)
### print('The prediction for pred[-7:m-1] is: \n', prep[-7:m-1], "\n", pred[-7:m-1], "\n")
### print('The prediction for pred[0:8] is: \n', pred[0:7], "\n", prep[0:7])
###
### #%%
### print('The third word is:', prep[3])
### print('Your prediction is:', pred[3])
### print('Your corresponding label y is: ', y[3])
###