-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrule_based_ner.py
executable file
·363 lines (276 loc) · 11.7 KB
/
rule_based_ner.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
"""
Created by diesel
12/23/19
"""
#import scaffold
#print(vars(scaffold))
from lexicon import Lexicon
import json
import nltk
from collections import Counter
"""
# Penn Tagset
# Optional DT: (\S+/DT\s*)?
# Adjetive can be JJ,JJR,JJS: (\S+/JJ\w?\s*)*
# Noun can be NN,NNS,NNP,NNPS: (\S+/NN\w*\s*)+
# ?P<> named group
# ?: non-capturing group
# See https://docs.python.org/3/howto/regex.html#non-capturing-and-named-groups
# index/word/pos/ner
# capital determiner 'A' or 'The' followed by nouns
# The man
# (The Markell family) always
generic = "((?:(?:[0-9]+)/(?:The|A)/DT/\S+\s)(?:\S+/NN\S*/\S+\s*)+)"
# possesive relations people
# our son; his girlfriend
generic2 = "((?:(?:[0-9]+)/\S+/PRP\$/\S+\s)(?:\S+/NN\S*/\S+\s*)+)"
# Mister Deals saw
proper = "((?:^(?:[0-9]+)/\S+/NN\S*/\S+\s)(?:\S+/NN\S*/\S+)*)"
person = "((?:(?:[0-9]+)/\S+/NN\S*/\S+\s+)*(?:\S+/NN\S*/PERSON\s*))"
speaker = "((?:[0-9]+/SPEAKER/\S+/\S+\s+))"
#char_patterns = "|".join([generic, proper, person, speaker])
re_patterns = {
"animate_NN_wn": generic,
"proper_NN": proper,
"person_NER": person,
"speaker": speaker,
"poss_rel_wn": generic2
}
#pattern = re.compile(r'(?P<CHAR>(?:\S+/DT\s*)?(?:\S+/JJ\w?\s*)*(?:\S+/NN\w*\s*)+)')
#pattern = re.compile("(?P<CHAR>{})".format(char_patterns))
regex = {k: re.compile("(?P<CHAR>{})".format(char_patterns)) for k, char_patterns in re_patterns.items()}
#found_entities = defaultdict(list)
for pat_name, pattern in regex.items():
#print("pattern_name:", pat_name)
for m in pattern.finditer(tokens):
mention = [feats.split("/") for feats in m.group("CHAR").rstrip().split()]
"""
class RuleBasedNER():
def __init__(self, lexicons):
self._lexicons = lexicons
def tokenize_text(self, text):
return [t.lower() for t in nltk.word_tokenize(text)]
def tag_tokens(self, tokens):
#print("tokens:", tokens)
indexed_grams = {}
for n in [4, 3, 2, 1]:
ngrams = nltk.ngrams(tokens, n)
for j, gram in enumerate(ngrams):
g = " ".join(gram)
if g in indexed_grams:
if not isinstance(indexed_grams[g], list):
indexed_grams[g] = [indexed_grams[g]]
indexed_grams[g].append(j)
else:
indexed_grams[g] = j
#print("all_grams:", list(indexed_grams.keys()))
mentions = []
_mention_idxs = set()
for lex_name, lex in self._lexicons.items():
#print("phrases:", lex.phrases)
found = set(indexed_grams.keys()) & lex.phrases
#print("found:", found)
found = sorted([(len(f),f) for f in found], reverse=True)
if len(found) > 0:
for length, gram in found:
#print("gram:", gram)
idxs = indexed_grams.get(gram)
#print("idxs:", idxs)
if not isinstance(idxs, list):
idxs = [idxs]
for start_idx in idxs:
#print("start_idx:", start_idx)
#print("len:", len(gram.split()))
end_idx = start_idx + len(gram.split())
span_idxs = set(range(start_idx, end_idx))
#print("span_idxs:", span_idxs)
#print("start,end:", start_idx, end_idx)
standard_form, ent_id = lex.ref2standard(gram, get_tid=True)
#print("_mention_idxs:", _mention_idxs)
if len(_mention_idxs & span_idxs) < 1:
mention = {
"ent_id": ent_id,
"standard_form": standard_form,
"words": tokens[start_idx:end_idx],
"start_idx": start_idx,
"end_idx": end_idx,
"ent_type": lex.name
}
mentions.append(mention)
for _idx_ in span_idxs:
_mention_idxs.add(_idx_)
#print("mentions:", mentions)
return mentions
def player_analysis():
dm = DataManager(
season_dir_path="../data/season_info",
players_path="../data/nba-static-data/nba-players.json",
teams_path="../data/nba-static-data/nba-teams.json",
fun_facts_path="../data/ready-facts.json",
questions_path="../data/ready-questions.json",
standings_path="../data/nba-playoff-picture",
)
print("All Players:")
full_names = [player["full_name"] for player in dm.players]
print("num_names:", len(full_names))
print("num unique names:", len(set(full_names)))
dups = []
_all = set()
for name in full_names:
if name in _all:
dups.append(name)
_all.add(name)
for d in dups:
print(" * ", d)
print("Active Players:")
full_names = [player["full_name"] for player in dm.players if player["is_active"]]
print("num_names:", len(full_names))
print("num unique names:", len(set(full_names)))
def process_ref(text):
return " ".join(nltk.word_tokenize(text)).lower()
def setup_lexicons():
dm = DataManager(
season_dir_path="../data/season_info",
players_path="../data/nba-static-data/nba-players.json",
teams_path="../data/nba-static-data/nba-teams.json",
fun_facts_path="../data/ready-facts.json",
questions_path="../data/ready-questions.json",
standings_path="../data/nba-playoff-picture",
)
# set up lexicons of know entities
player_lex = Lexicon("player")
for player in dm.players:
refs = [process_ref(player["full_name"])]
player_lex.new_term(player["full_name"], info=player, referential_forms=refs)
team_lex = Lexicon("team")
for team in dm.teams:
refs = [process_ref(team[k]) for k in ["full_name", "city", "nickname", ]] # "abbreviation"
team_lex.new_term(team["full_name"], info=team, referential_forms=refs)
lexicons = {
lex.name: lex.to_dict() for lex in [player_lex, team_lex]
}
with open("../data/lexicons/lex.json", "w") as fout:
json.dump(lexicons, fout, indent=2)
with open("../data/lexicons/lex.json", "r") as fin:
lexicons2 = json.load(fin)
lex2 = {
name: Lexicon.from_dict(d) for name, d in lexicons2.items()
}
prepared_lexicons = "../data/lexicons/lex.json"
with open(prepared_lexicons, "r") as fin:
lexicons2 = json.load(fin)
lexicons = {
name: Lexicon.from_dict(d) for name, d in lexicons2.items()
}
def team_ner_demo(fact, ner_tagger, results, lex):
print(("-" * 20) + "\n\n")
text = fact["text"]
team = fact["team"]
toks = [t.lower() for t in nltk.word_tokenize(text)]
mentions = ner_tagger.tag_tokens(toks)
#player_mentions = [men for men in mentions if men["ent_type"] == "player"]
team_mentions = [men for men in mentions if men["ent_type"] == "team"]
nicknames = [lex.get_info(men["standard_form"][0])["nickname"] for men in team_mentions]
nicknames = "; ".join(nicknames)
print("\ntext:", text)
print("fact team:", team)
print("team_mention:", "; ".join([" ".join(men["words"]) for men in team_mentions]))
print("nickname:", nicknames)
if len(team_mentions) == 1:
words = " ".join(team_mentions[0]["words"])
standard = team_mentions[0]["standard_form"][0]
else:
words = "; ".join([" ".join(men["words"]) for men in team_mentions])
standard = "; ".join([men["standard_form"][0] for men in team_mentions])
#print("player_mention: ", words)
print("{} == {}".format(team, nicknames))
if team.lower() == nicknames.lower():
result = "PASS"
else:
result = "FAIL"
print(result)
results.append(result)
def player_ner_demo(fact, ner_tagger, results):
print(("-" * 20) + "\n\n")
text = fact["text"]
player = fact["player"]
toks = [t.lower() for t in nltk.word_tokenize(text)]
mentions = ner_tagger.tag_tokens(toks)
player_mentions = [men for men in mentions if men["ent_type"] == "player"]
team_mentions = [men for men in mentions if men["ent_type"] == "team"]
print("\ntext:", text)
print("player:", player)
print("team_mention:", "; ".join([" ".join(men["words"]) for men in team_mentions]))
if len(player_mentions) == 1:
words = " ".join(player_mentions[0]["words"])
standard = player_mentions[0]["standard_form"][0]
else:
words = "; ".join([" ".join(men["words"]) for men in player_mentions])
standard = "; ".join([men["standard_form"][0] for men in player_mentions])
print("player_mention: ", words)
print("{} == {}".format(player, standard))
if player.lower() == standard.lower():
result = "PASS"
else:
result = "FAIL"
print(result)
results.append(result)
def load_lexicons(prepared_lexicons="../data/lexicons/lex.json"):
with open(prepared_lexicons, "r") as fin:
lexicons = json.load(fin)
lexicons = {
name: Lexicon.from_dict(d) for name, d in lexicons.items()
}
return lexicons
def main():
dm = DataManager(
season_dir_path="../data/season_info",
players_path="../data/nba-static-data/nba-players.json",
teams_path="../data/nba-static-data/nba-teams.json",
fun_facts_path="../data/ready-facts.json",
questions_path="../data/ready-questions.json",
standings_path="../data/nba-playoff-picture",
templates_path="../data/season-templates/templates.json"
)
aka_list = [
{"full_name": "Kareem Abdul-Jabbar", "aka": ["Kareem Abdul-Jabaar", "Kareem Abdul-Jabbar"]},
{"full_name": "Shaquille O'Neal", "aka": ["Shaq"]}
]
player_lex = Lexicon("player")
for player in dm.players:
refs = [process_ref(player["full_name"])]
player_lex.new_term(player["full_name"], info=player, referential_forms=refs)
for aka in aka_list:
player_lex.new_referential_phrase(
[process_ref(ref) for ref in aka["aka"]],
aka["full_name"])
team_lex = Lexicon("team")
for team in dm.teams:
refs = [process_ref(team[k]) for k in ["full_name", "city", "nickname"]] # "abbreviation"
team_lex.new_term(team["full_name"], info=team, referential_forms=refs)
lexicons = {
lex.name: lex for lex in [player_lex, team_lex]
}
ner_tagger = RuleBasedNER(lexicons)
results = []
for fact in dm.fun_facts["fun-facts"]["players"]["facts"]:
player_ner_demo(fact, ner_tagger, results)
results = Counter(results)
print("results:")
for k,v in results.items():
print("{}: {}".format(k, v))
print("accuracy:", results["PASS"]/ (results["PASS"] + results["FAIL"]))
results = []
for fact in dm.fun_facts["fun-facts"]["teams"]["facts"]:
team_ner_demo(fact, ner_tagger, results, lexicons["team"])
results = Counter(results)
print("results:")
for k,v in results.items():
print("{}: {}".format(k, v))
print("accuracy:", results["PASS"]/ (results["PASS"] + results["FAIL"]))
prepared_lexicons = "../data/lexicons/lex.json"
lexicons = {name: lex.to_dict() for name, lex in lexicons.items()}
with open(prepared_lexicons, "w") as fout:
json.dump(lexicons, fout, indent=2)
if __name__ == "__main__":
main()