-
Notifications
You must be signed in to change notification settings - Fork 0
/
NamedEntityRecognition.py
464 lines (366 loc) · 15.8 KB
/
NamedEntityRecognition.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
from os import listdir
from os.path import isfile, join
import nltk
from nltk.corpus import ieer
from nltk.corpus import names as nltknames
from SPARQLWrapper import SPARQLWrapper, JSON
import re
import sys
from collections import defaultdict
import time
import os
corpus_root = "/home/george/nltk_data/corpora/assignment/wsj_training/"
training_path = "/home/george/nltk_data/corpora/assignment/wsj_training/wsj_training/"
untagged_path = "/home/george/nltk_data/corpora/assignment/wsj_untagged/wsj_untagged/"
test_path_untagged = "/home/george/nltk_data/corpora/assignment/wsj_test_untagged/"
test_path_tagged = "/home/george/nltk_data/corpora/assignment/wsj_test_tagged/"
dbpedia_path_ttl = "/home/george/PycharmProjects/nlp_assignment/wrd_instances.ttl"
dbpedia_path_csv = "/home/george/PycharmProjects/nlp_assignment/wrd_instances.csv"
dbp_ent_path = "/home/george/PycharmProjects/nlp_assignment/entities.txt"
more_entities = "/home/george/PycharmProjects/nlp_assignment/more_entities.txt"
names = set().union(nltknames.words("male.txt"), nltknames.words("female.txt"))
titles = {"Mr.", "Mrs.", "Dr.", "Sir", "Prof.", "Professor", "Ms.", "Rev.", "President", "Pres.", "Judge", "Mayor",
"Sr", "Jr", "King", "Queen", "Prince", "Princess"}
business_words = {"Co.", "Company", "Assoc.", "Association", "Inc.", "Incorporated", "Inc", "Corp.", "Corporation",
"Ltd.", "Group", "PLC", "Club", "Court", "Commission", "Bureau", "Ministry", "Institute", "School"}
location_prev_words = {"in"}
person_prev_words = {}
organization_prev_words = {}
# Collect the entites from the ieer dataset
def get_ieer_entities():
entity_dict = {}
entity_names = []
for doc in ieer.parsed_docs():
for st in doc.text.subtrees():
if st.label() in ["PERSON", "ORGANIZATION", "LOCATION"]:
entity_names += [" ".join(st.leaves())]
entity_dict[" ".join(st.leaves())] = st.label()
return entity_dict, set(entity_names)
# Collect the entities, their POS tags and their given in the training files
def get_training_entities(file_count, test=False):
if test:
active_path = test_path_tagged
else:
active_path = training_path
onlyfiles = [f for f in listdir(active_path) if isfile(join(active_path, f))]
if ".DS_Store" in onlyfiles:
onlyfiles.remove(".DS_Store")
onlyfiles = sorted(onlyfiles)
file_count = min(file_count, len(onlyfiles))
text = ""
for f in onlyfiles[:file_count]:
with open(active_path + f, 'r') as mf:
text += mf.read()
print("Files Loaded")
docs_pattern = r'<ENAMEX TYPE=".*?">.*?</ENAMEX>'
doc_tuples = re.findall(docs_pattern, text, re.DOTALL)
entities = []
i = 0
for t in doc_tuples:
i += 1
s = re.compile(r'["<>]')
t2 = s.split(t)
tag = t2[2]
entity = t2[4]
tagged_entity = nltk.pos_tag(entity.split())
rtags = map(list, zip(*tagged_entity))
rtags = rtags[1]
sys.stdout.write("\r%d%%" % int(i * 100 / len(doc_tuples)))
sys.stdout.flush()
entities += [(entity, tagged_entity, rtags, tag)]
print("")
print("%d entities loaded" % len(entities))
return entities
# Create a dict of frequencies for the POS tags
def compile_pos_tags(ent_list):
tag_lists = [(" ".join(x[2]), x[3]) for x in ent_list]
fq = defaultdict(int)
for l in tag_lists:
fq[l] += 1
return fq
# Create the grammar for the specific type of entity
def create_grammar(ent_list, grammar_multiplier):
illegal_chars = set([":", ")", "("])
# Get a list of the POS tags for the entities and their type [(['NNP', 'NNP'], "PERSON")]
tag_list = [(a[0].split(), a[1]) for a in set([(" ".join(x[2]), x[3]) for x in ent_list])]
# Sort the list of POS tags so that it's ordered by length to promote greedy matching
pos_frequencies = compile_pos_tags(ent_list)
avg_freq = sum(pos_frequencies.values()) / len(pos_frequencies)
tag_list = [x for x in tag_list if pos_frequencies[(" ".join(x[0]), x[1])] > avg_freq * grammar_multiplier]
tag_list = sorted(tag_list, key=lambda l: pos_frequencies[(" ".join(l[0]), l[1])] - (len(l[0]) ** 100))
# Create the grammar
grammar_list = [t[1] + ": {<" + "><".join(t[0]) + ">}" for t in tag_list if
len(set(t[0]) & illegal_chars) == 0 and (
"NNP" in t[0] or "NNPS" in t[0] or ("NN" in t[0] and len(t[0]) == 1))]
grammar = "\n".join(grammar_list)
print("Grammar created")
return grammar
# Delete all files in the untagged directory with extension .result.txt
def delete_files(active_path):
for f in os.listdir(active_path):
if f.endswith(".result"):
os.remove(active_path + f)
# Is the given entity a name
def is_name(entity):
name = entity.split()
if len(titles & set(name)) > 0:
return True
contains_name = False
for n in name:
if n in names:
contains_name = True
if not n[0].isupper():
return False
if "." in n and not len(n) == 2:
return False
return contains_name
# Is the given entity a location
def is_location(entity):
if len(entity) <= 7 and entity[-1] == "." and entity[0].isupper():
return True
return False
# Is the given entity an organization
def is_organization(entity, past_entities):
org = entity.split()
if len(business_words & set(org)) > 0:
return True
if entity.isupper() and len(org) == 1 and len(entity) <= 7:
return True
past_orgs = [e for e in past_entities if e[1] == "ORGANIZATION"]
for o in past_orgs:
org_split = set(o[0].split())
if len(set(org) & org_split) > 0:
return True
return False
# Get the type f the given entity
def get_relation(entity, ieer_entity_dict, ieer_entity_names, dbp_ent_set, sample_entities, past_entities, prev_word):
es = entity.split()
# es = [w for w in es if not w in titles]
entity_name = " ".join(es)
entity = "_".join(es)
entity = re.sub(r'[^\P{P}\w\.\_]+', "", entity)
if (entity, "PERSON") in sample_entities:
return "PERSON"
if (entity, "LOCATION") in sample_entities:
return "LOCATION"
if (entity, "ORGANIZATION") in sample_entities:
return "ORGANIZATION"
if entity_name in ieer_entity_names:
return ieer_entity_dict[entity_name]
if prev_word is not None:
if prev_word in location_prev_words:
return "LOCATION"
if prev_word in person_prev_words:
return "PERSON"
if prev_word in organization_prev_words:
return "ORGANIZATION"
if len(es) == 1 and entity_name[0].islower():
return None
if is_organization(entity_name, past_entities):
return "ORGANIZATION"
if is_name(entity_name):
return "PERSON"
if is_location(entity_name):
return "LOCATION"
if entity not in dbp_ent_set:
return None
if True:
sp = SPARQLWrapper("http://dbpedia.org/sparql")
sp.setQuery(""" select ?t
where {
OPTIONAL { <http://dbpedia.org/resource/%s> a ?t } .
}""" % entity)
sp.setReturnFormat(JSON)
try:
results = sp.query().convert()
if results["results"]["bindings"] == [{}]: return None
for r in results["results"]["bindings"]:
v = r["t"]["value"]
if v == "http://dbpedia.org/ontology/Person":
return "PERSON"
elif v == "http://dbpedia.org/ontology/Organisation":
return "ORGANIZATION"
elif v == "http://dbpedia.org/ontology/Location":
return "LOCATION"
except:
pass
return None
# Complete the NER
def ner(grammar, sample_entities, file_count, test=True):
active_path = test_path_untagged if test else untagged_path
# Load the entities from the dbpedia file
with open(dbp_ent_path, 'r') as ef:
dbp_ent_set = ef.read().splitlines()
dbp_ent_set = set(dbp_ent_set)
print("Loaded %d entities from DBPedia entity list" % len(dbp_ent_set))
# Load the entities from more_entities.txt
with open(more_entities, 'r') as ef:
more_ent_set = ef.read().splitlines()
more_ent_set = [(e.split()[1], e.split()[0]) for e in more_ent_set]
more_ent_set = set(more_ent_set)
sample_entities |= more_ent_set
delete_files(active_path)
onlyfiles = [f for f in listdir(active_path) if isfile(join(active_path, f))]
if ".DS_Store" in onlyfiles:
onlyfiles.remove(".DS_Store")
onlyfiles = sorted(onlyfiles)
related_entities = []
non_related_entities = []
file_count = min(file_count, len(onlyfiles))
ieer_dict, ieer_names = get_ieer_entities()
i = 0
for f in onlyfiles[:file_count]:
sys.stdout.write("\r%.2f%%" % (float(i) * 100.0 / float(file_count)))
sys.stdout.flush()
i += 1
text = ""
with open(active_path + f, 'r') as mf:
text += mf.read()
# Split into sentences
sentences_tokenized = nltk.sent_tokenize(text)
sentences = []
for sent in sentences_tokenized:
if "\n" in sent:
sents = sent.split("\n")
for s in sents[:-1]:
sentences += [s + "\n"]
if not sents[-1] == "":
sentences += [sents[-1]]
else:
sentences += [sent]
tagged_sentences = []
for sentence in sentences:
# Tokenize and POS tag the sentence
sentence2 = nltk.word_tokenize(sentence)
sentence3 = nltk.pos_tag(sentence2)
# Parse the sentence using the given grammar
parser = nltk.RegexpParser(grammar)
entities = []
parse_tree = parser.parse(sentence3)
# Extract the entities from the parse tree
for subtree in parse_tree.subtrees():
if subtree.label() in ["PERSON", "ORGANIZATION", "LOCATION"]:
entities += [(subtree.leaves(), subtree.label())]
# Extract the entity names
named_entities = [(" ".join(x[0]), x[1]) for x in [([z[0] for z in y[0]], y[1]) for y in entities]]
tagged_sentence = sentence
for ne in named_entities:
# Get the word prior to the occurence of the entity in the sentence
prev_words = sentence.partition(" %s " % ne[0])
if prev_words[2] == '':
prev_word = None
else:
spl_prev_word = prev_words[0].split()
if spl_prev_word != []:
prev_word = spl_prev_word[-1]
else:
prev_word = None
# Get an initial relation for the entity
rel = get_relation(ne[0], ieer_dict, ieer_names, dbp_ent_set, sample_entities, related_entities[-10:], prev_word)
if rel is not None:
related_entities += [(ne[0], rel)]
tagged_sentence = tagged_sentence.replace(ne[0],
"<ENAMEX TYPE=\"" + rel + "\">" + ne[0] + "</ENAMEX>")
elif " and " in ne[0]:
ne_split = ne[0].split(" and ")
for ne_s in ne_split:
rel_s = get_relation(ne_s, ieer_dict, ieer_names, dbp_ent_set, sample_entities, related_entities[-10:], None)
if rel_s is not None:
related_entities += [(ne_s, rel_s)]
tagged_sentence = tagged_sentence.replace(ne_s,
"<ENAMEX TYPE=\"" + rel_s + "\">" + ne_s + "</ENAMEX>")
else:
non_related_entities += [ne]
tagged_sentences += [tagged_sentence]
with open(active_path + f + ".result", 'w') as fi:
fi.write(" ".join(tagged_sentences))
print("")
print("%d entities successfully extracted and tagged" % len(related_entities))
print("%d recognised entities rejected" % len(non_related_entities))
print("%d files searched" % file_count)
return related_entities, non_related_entities
# Output statistics for the NER
def statistics(training_entities, related_entities, file_count):
training_entities = [(x[0], x[3]) for x in training_entities]
training_entities_set = set(training_entities)
successes = 0
failures = 0
for ent in related_entities:
if ent in training_entities_set:
successes += 1
else:
failures += 1
tp_classified = successes
classified = len(related_entities)
tp_in_corpus = len(training_entities)
precision = tp_classified * 100.0 / classified
recall = tp_classified * 100.0 / tp_in_corpus
success_percentage = float(successes) * 100.0 / float(tp_in_corpus)
print("Using %d files:" % file_count)
print("%d training entities provided" % tp_in_corpus)
print("%d related entities discovered" % classified)
print("%d successful relations identified" % successes)
print("%d relations falsely identified" % failures)
print("%d relations not identified" % (tp_in_corpus - successes))
print("%.2f%% success percentage" % success_percentage)
print("%.2f%% precision" % precision)
print("%.2f%% recall" % recall)
return success_percentage
# Run the NER
def run(file_count=2000, test_training_data=True, test=True, grammar_multiplier=0.7):
print("")
print("Loading Training Entities")
print("-------------------------")
training_entities = get_training_entities(2000)
sample_entities = set([(x[0], x[3]) for x in training_entities])
print("")
print("Creating Grammar")
print("----------------")
grammar = create_grammar(training_entities, grammar_multiplier)
if test_training_data:
print("")
print("Completing NER on training data")
print("-------------------------------")
stime = time.time()
related_entities, failed_entities = ner(grammar, sample_entities, file_count, False)
etime = time.time()
elapsed_time = etime - stime
print("NER on training data completed in %d seconds" % elapsed_time)
success_percentage = None
print("")
print("Training Statistics")
print("-------------------")
sub_training_entities = get_training_entities(file_count)
success_percentage = statistics(sub_training_entities, related_entities, file_count)
if test:
print("")
print("Completing NER on test data")
print("---------------------------")
stime = time.time()
related_test_entities, failed_test_entities = ner(grammar, sample_entities, file_count, True)
etime = time.time()
elapsed_time = etime - stime
print("NER completed in %d seconds" % elapsed_time)
print("")
print("Getting Test Entities")
print("---------------------")
test_entities = get_training_entities(file_count, True)
print("")
print("Training Statistics")
print("-------------------")
test_success_percentage = statistics(test_entities, related_test_entities, file_count)
print("")
print("============")
print("# COMPLETE #")
print("============")
return training_entities, grammar,\
related_test_entities, failed_test_entities, test_success_percentage
# remove all .result.txt files
# for file in files:
# for each sentence:
# POS tag
# identify entities
# relate entity
# replace entity in sentence
# add sentence to text
# write text to file with .result.txt extension