-
Notifications
You must be signed in to change notification settings - Fork 0
/
task3_submission.py
367 lines (289 loc) · 10.8 KB
/
task3_submission.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
# !/usr/bin/env python
# -*- coding: utf-8 -*-
######################################################################
#
# (c) Copyright University of Southampton, 2021
#
# Copyright in this software belongs to University of Southampton,
# Highfield, University Road, Southampton SO17 1BJ
#
# Created By : Stuart E. Middleton
# Created Date : 2021/01/29
# Project : Teaching
#
######################################################################
from __future__ import absolute_import, division, print_function, unicode_literals
import sys, codecs, json, math, time, warnings, re, logging
warnings.simplefilter( action='ignore', category=FutureWarning )
import nltk, numpy, scipy, sklearn, sklearn_crfsuite, sklearn_crfsuite.metrics
from sklearn.metrics import make_scorer
from sklearn.metrics import classification_report
LOG_FORMAT = ('%(levelname) -s %(asctime)s %(message)s')
logger = logging.getLogger( __name__ )
logging.basicConfig( level=logging.INFO, format=LOG_FORMAT )
logger.info('logging started')
def get_dict(result_pred, text):
result = []
for i in range(len(result_pred)):
conlltags = []
for tg, word in zip(result_pred[i], text[i]):
conlltags.append(((word['word'], word['postag'], tg)))
for tree in nltk.chunk.conlltags2tree(conlltags):
if type(tree) == nltk.tree.Tree:
l = tree.label()
s = " ".join([token for token, pos in tree.leaves()]).lower().strip()
result.append((s, l))
dic = {}
for value, key in result:
if key not in dic:
dic.setdefault(key, []).append(value)
elif value not in dic[key]:
dic.setdefault(key, []).append(value)
return dic
def parseFile(f, dict_ontonotes):
list = []
for str_file in f :
for str_sent_index in dict_ontonotes[str_file] :
# ignore sents with non-PENN POS tags
if 'XX' in dict_ontonotes[str_file][str_sent_index]['pos'] :
continue
if 'VERB' in dict_ontonotes[str_file][str_sent_index]['pos'] :
continue
list_entry = []
# compute IOB tags for named entities (if any)
ne_type_last = None
for nTokenIndex in range(len(dict_ontonotes[str_file][str_sent_index]['tokens'])) :
strToken = dict_ontonotes[str_file][str_sent_index]['tokens'][nTokenIndex]
strPOS = dict_ontonotes[str_file][str_sent_index]['pos'][nTokenIndex]
ne_type = None
if 'ne' in dict_ontonotes[str_file][str_sent_index] :
dict_ne = dict_ontonotes[str_file][str_sent_index]['ne']
if not 'parse_error' in dict_ne :
for str_NEIndex in dict_ne :
if nTokenIndex in dict_ne[str_NEIndex]['tokens'] :
ne_type = dict_ne[str_NEIndex]['type']
break
if ne_type != None :
if ne_type == ne_type_last :
strIOB = 'I-' + ne_type
else :
strIOB = 'B-' + ne_type
else :
strIOB = 'O'
ne_type_last = ne_type
list_entry.append( ( strToken, strPOS, strIOB ) )
list.append( list_entry )
return list
def create_dataset(filePath = None, max_files = None) :
dataset_file = filePath
# load parsed ontonotes dataset
readHandle = codecs.open( dataset_file, 'r', 'utf-8', errors = 'replace' )
str_json = readHandle.read()
readHandle.close()
dict_ontonotes = json.loads( str_json )
# make a training and test split
list_files = list( dict_ontonotes.keys() )
# if len(list_files) > max_files :
# list_files = list_files[ :max_files ]
nSplit = math.floor( len(list_files))
list_train_files = list_files[:nSplit]
# list_test_files = list_files[ nSplit : ]
# sent = (tokens, pos, IOB_label)
list_train = parseFile(list_train_files, dict_ontonotes)
# list_test = []
return list_train
def word2features(sent, i):
# print(sent)
word = sent[i][0]
postag = sent[i][1]
features = {
'word' : word,
'postag': postag,
# token shape
'word.lower()': word.lower(),
'word.isupper()': word.isupper(),
'word.istitle()': word.istitle(),
'word.isdigit()': word.isdigit(),
# token suffix
# 'word.suffix': word.lower()[-3:],
# POS prefix
# 'postag[:2]': postag[:2],
}
if i > 0:
word_prev = sent[i-1][0]
postag_prev = sent[i-1][1]
features.update({
'-1:word.lower()': word_prev.lower(),
'-1:postag': postag_prev,
'-1:word.lower()': word_prev.lower(),
'-1:word.isupper()': word_prev.isupper(),
'-1:word.istitle()': word_prev.istitle(),
'-1:word.isdigit()': word_prev.isdigit(),
# '-1:word.suffix': word_prev.lower()[-3:],
# '-1:postag[:2]': postag_prev[:2],
})
else:
features['BOS'] = True
if i < len(sent)-1:
word_next = sent[i+1][0]
postag_next = sent[i+1][1]
features.update({
'+1:word.lower()': word_next.lower(),
'+1:postag': postag_next,
'+1:word.lower()': word_next.lower(),
'+1:word.isupper()': word_next.isupper(),
'+1:word.istitle()': word_next.istitle(),
'+1:word.isdigit()': word_next.isdigit(),
# '+1:word.suffix': word_next.lower()[-3:],
# '+1:postag[:2]': postag_next[:2],
})
else:
features['EOS'] = True
return features
def extract_features(sentence):
return [word2features(sentence, i) for i in range(len(sentence))]
def extract_labels(sentence):
return [label for token, postag, label in sentence]
def crf_model(X_train, y_train):
crf = sklearn_crfsuite.CRF(
algorithm='lbfgs',
c1=0.1,
c2=0.1,
max_iterations=20,
all_possible_transitions=True
)
crf.fit(X_train, y_train)
return crf
def preprocess_textfile(filename):
with open(filename, 'r', encoding='utf-8', errors='replace') as file:
text = file.read()
# Replace newline characters and tab characters with a single space
# text = re.sub(r'[\r\n\t]+', ' ', text)
# Tokenize the text into sentences
sentences = nltk.sent_tokenize(text)
tagged_words = []
# Process each sentence
for sentence in sentences:
# Tokenize the sentence into words and tag each word
words = nltk.word_tokenize(sentence)
# print(words)
tagged = nltk.pos_tag(words)
# print(tagged)
tagged_words.append(tagged)
# print(tagged_words)
featured = [extract_features(sent) for sent in tagged_words]
# featured = []
# for word in tagged_words:
# print(word)
# featured.append(extract_features(word))
return featured
# def task5_train_crf_model( X_train, Y_train, max_iter, labels) :
# # randomized search to discover best parameters for CRF model
# crf = sklearn_crfsuite.CRF(
# algorithm='lbfgs',
# max_iterations=max_iter,
# all_possible_transitions=True
# )
# params_space = {
# 'c1': scipy.stats.expon(scale=0.5),
# 'c2': scipy.stats.expon(scale=0.05),
# }
# # optimize for micro F1 score
# f1_scorer = make_scorer( sklearn_crfsuite.metrics.flat_f1_score, average='weighted', labels=labels )
# logger.info( 'starting randomized search for hyperparameters' )
# n_folds = 2
# n_candidates = 10
# rs = sklearn.model_selection.RandomizedSearchCV(crf, params_space, cv=n_folds, verbose=1, n_jobs=-1, n_iter=n_candidates, scoring=f1_scorer)
# rs.fit(X_train, Y_train)
# # output the results
# logger.info( 'best params: {}'.format( rs.best_params_ ) )
# logger.info( 'best micro F1 score: {}'.format( rs.best_score_ ) )
# logger.info( 'model size: {:0.2f}M'.format( rs.best_estimator_.size_ / 1000000 ) )
# logger.info( 'cv_results_ = ' + repr(rs.cv_results_) )
# # visualize the results in hyperparameter space
# _x = [s['c1'] for s in rs.cv_results_['params']]
# _y = [s['c2'] for s in rs.cv_results_['params']]
# _c = [s for s in rs.cv_results_['mean_test_score']]
# # return the best model
# crf = rs.best_estimator_
# return crf
def exec_ner( file1_chapter = None, file2_chapter = None, file3_chapter = None, ontonotes_file = None ) :
# CHANGE CODE BELOW TO TRAIN A CRF NER MODEL TO TAG THE CHAPTER OF TEXT (task 3)
# Input >> www.gutenberg.org sourced plain text file for a chapter of a book
# Output >> ne.json = { <ne_type> : [ <phrase>, <phrase>, ... ] }
train_sents = create_dataset( filePath = ontonotes_file)
X_train = [extract_features(s) for s in train_sents]
y_train = [extract_labels(s) for s in train_sents]
# X_test = [extract_features(s) for s in test_sents]
# Y_test = [extract_labels(s) for s in test_sents]
# set_labels = set([])
# for data in [y_train,Y_test] :
# for n_sent in range(len(data)) :
# for str_label in data[n_sent] :
# set_labels.add( str_label )
# labels = list( set_labels )
# labels.remove('O')
# model = task5_train_crf_model(X_train, y_train, 100, labels)
# print(model)
model = crf_model(X_train, y_train)
file1 = preprocess_textfile(file1_chapter)
file2 = preprocess_textfile(file2_chapter)
file3 = preprocess_textfile(file3_chapter)
dictNE1 = get_dict(model.predict(file1),file1)
dictNE2 = get_dict(model.predict(file2),file2)
dictNE3 = get_dict(model.predict(file3),file3)
# DO NOT CHANGE THE BELOW CODE WHICH WILL SERIALIZE THE ANSWERS FOR THE AUTOMATED TEST HARNESS TO LOAD AND MARK
# FILTER NE dict by types required for task 3
listAllowedTypes = [ 'DATE', 'CARDINAL', 'ORDINAL', 'NORP' ]
listKeys = list( dictNE1.keys() )
for strKey in listKeys :
for nIndex in range(len(dictNE1[strKey])) :
dictNE1[strKey][nIndex] = dictNE1[strKey][nIndex].strip().lower()
if not strKey in listAllowedTypes :
del dictNE1[strKey]
#123
listKeys = list( dictNE2.keys() )
for strKey in listKeys :
for nIndex in range(len(dictNE2[strKey])) :
dictNE2[strKey][nIndex] = dictNE2[strKey][nIndex].strip().lower()
if not strKey in listAllowedTypes :
del dictNE2[strKey]
listKeys = list( dictNE3.keys() )
for strKey in listKeys :
for nIndex in range(len(dictNE3[strKey])) :
dictNE3[strKey][nIndex] = dictNE3[strKey][nIndex].strip().lower()
if not strKey in listAllowedTypes :
del dictNE3[strKey]
# write filtered NE dict
writeHandle = codecs.open( 'ne1.json', 'w', 'utf-8', errors = 'replace' )
strJSON = json.dumps( dictNE1, indent=2 )
writeHandle.write( strJSON + '\n' )
writeHandle.close()
writeHandle = codecs.open( 'ne2.json', 'w', 'utf-8', errors = 'replace' )
strJSON = json.dumps( dictNE2, indent=2 )
writeHandle.write( strJSON + '\n' )
writeHandle.close()
writeHandle = codecs.open( 'ne3.json', 'w', 'utf-8', errors = 'replace' )
strJSON = json.dumps( dictNE3, indent=2 )
writeHandle.write( strJSON + '\n' )
writeHandle.close()
if __name__ == '__main__':
if len(sys.argv) < 8 :
raise Exception( 'missing command line args : ' + repr(sys.argv) )
ontonotes_file = sys.argv[1]
book1_file = sys.argv[2]
chapter1_file = sys.argv[3]
book2_file = sys.argv[4]
chapter2_file = sys.argv[5]
book3_file = sys.argv[6]
chapter3_file = sys.argv[7]
logger.info( 'ontonotes = ' + repr(ontonotes_file) )
logger.info( 'book1 = ' + repr(book1_file) )
logger.info( 'chapter1 = ' + repr(chapter1_file) )
logger.info( 'book2 = ' + repr(book2_file) )
logger.info( 'chapter2 = ' + repr(chapter2_file) )
logger.info( 'book3 = ' + repr(book3_file) )
logger.info( 'chapter3 = ' + repr(chapter3_file) )
# DO NOT CHANGE THE CODE IN THIS FUNCTION
exec_ner( chapter1_file, chapter2_file, chapter3_file, ontonotes_file )
logger.info( 'done')