-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexicon.py
executable file
·294 lines (202 loc) · 7.76 KB
/
lexicon.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
"""
Created by diesel
12/23/19
"""
from collections import defaultdict
import nltk
import json
import spacy
nlp = spacy.load('en_core_web_sm')
class Lexicon(object):
_save_attrs = ["name", "terms", "_id2term", "info", "_id_counter",
"_standard_2_id", "_all_refs"]
def __init__(self, name):
self.name = name
self.terms = []
self._id2term = {}
self.info = {}
self._id_counter = 0
self._standard_2_id = {}
self._all_refs = defaultdict(list)
self._phrases = None
def _clean_up(self):
for record in self._id2term.values():
record["referential_forms"] = list(set(record["referential_forms"]))
def to_dict(self):
self._clean_up()
d = {}
for k in self._save_attrs:
d[k] = getattr(self, k)
return d
def to_lines(self , delim="; ", num_spaces=2):
self._clean_up()
all_lines = []
for entry in self.terms:
info = self.info[entry["id"]]
category = self.name
if category == "characters":
category = "character"
category = category.upper()
lines = [category, "standard_form" + ": "+ entry["standard_form"]]
forms = entry.get("referential_forms", [])
if forms:
forms = delim.join(forms)
lines.append("forms: " + forms)
for attr, val in info.items():
if attr in {"forms", "full_name", "standard_form"}:
continue
if isinstance(val, list):
val = delim.join(val)
lines.append(attr + ": " + str(val))
lines.extend([""] * num_spaces)
all_lines.extend(lines)
return all_lines
def to_df(self , delim="; ", num_spaces=2):
self._clean_up()
all_lines = []
for entry in self.terms:
info = self.info[entry["id"]]
category = self.name
if category == "characters":
category = "character"
category = category.upper()
e = {
"category": category,
"standard_form": entry["standard_form"],
"forms": json.dumps(entry.get("referential_forms", []))
}
for attr, val in info.items():
if attr in {"forms", "full_name", "standard_form"}:
continue
if isinstance(val, list):
val = json.dumps(val)
e[attr] = val
all_lines.append(e)
return all_lines
@classmethod
def from_dict(cls, d):
assert "name" in d
lex = cls(d["name"])
for k in cls._save_attrs:
if d.get(k):
setattr(lex, k, d[k])
lex._id2term = {}
_terms = []
if len(lex.terms) > 0:
for t in lex.terms:
if t["id"] not in lex._id2term:
lex._id2term[t["id"]] = t
_terms.append(t)
lex.terms = _terms
assert len(lex.terms) == len(lex._id2term), "{} {}".format(len(lex.terms), len(lex._id2term))
return lex
def get_info(self, tid_or_standard_form, default_val=None):
info = self.info.get(tid_or_standard_form)
if info is None:
key = self._standard_2_id.get(tid_or_standard_form)
info = self.info.get(key)
if info is None:
info = default_val
return info
def new_id(self):
self._id_counter += 1
return self._id_counter
def new_term(self, standard_form, info=None, referential_forms=None, tid=None, standard_form_is_id=True):
if standard_form in self._standard_2_id:
the_id = self._standard_2_id[standard_form]
for ref in referential_forms:
self._add_new_ref(ref, the_id)
else:
new_term = {
"standard_form": standard_form,
"referential_forms": referential_forms if referential_forms else [],
"id": (standard_form if standard_form_is_id else self.new_id()) if tid is None else tid
}
self.terms.append(new_term)
self._id2term[new_term["id"]] = new_term
self.info[new_term["id"]] = info
for ref in referential_forms:
self._add_new_ref(ref, new_term["id"])
self._standard_2_id[standard_form] = new_term["id"]
def update_info(self, tid, new_info):
tinfo = self.info.get(tid, {})
tinfo.update(new_info)
def _add_new_ref(self, ref, tid):
if ref not in self._all_refs or tid not in self._all_refs[ref]:
if ref not in self._all_refs:
self._all_refs[ref] = []
self._all_refs[ref].append(tid)
def new_referential_phrase(self, refs, standard_form=None, tid=None):
assert standard_form or tid
if tid is None:
tid = standard_form
if not isinstance(refs, list):
refs = [refs]
#print("tid:", [tid])
record = self._id2term.get(tid)
#print("record:", record)
record["referential_forms"].extend([r for r in refs if r not in record["referential_forms"]])
for ref in refs:
self._add_new_ref(ref, tid)
print(" * add {} => {}".format(ref, tid))
@property
def phrases(self):
if self._phrases is None:
self._phrases = set(self._all_refs.keys())
return self._phrases
def ref2standard(self, ref, get_tid=False):
ids = self._all_refs.get(ref)
if ids:
if not isinstance(ids, list):
ids = [ids]
standard_forms = [self._id2term[_i].get("standard_form") for _i in ids]
else:
standard_forms = None
retval = standard_forms, ids if get_tid else standard_forms
return retval
class LexBuilder(object):
def __init__(self):
self._entries = defaultdict(list)
self._lexicons = None
@property
def lexicons(self):
return self._lexicons
def add_entry(self, e, etype=None):
if etype is None:
etype = e["category"]
self._entries[etype].append(e)
def get_entry_key_set(self):
return set([k for etype, ents in self._entries.items() for e in ents for k in e.keys()])
def lex_as_dict(self):
return {lex.name: lex.to_dict() for lex in self.lexicons.values()}
def build_lexes(self):
lexicons = {}
for ent_type, entries in self._entries.items():
lex = Lexicon(ent_type)
for entry in entries:
if isinstance(entry, str):
entry = entries[entry]
if not entry.get("full_name") or entry["full_name"] == "none none":
continue
refs = self.process_ref(entry["forms"])
lex.new_term(entry["full_name"], info=entry, referential_forms=refs)
#refs = [process_ref(player["full_name"])]
#player_lex.new_term(player["full_name"], info=player, referential_forms=refs)
lexicons[ent_type] = lex
self._lexicons = lexicons
def save_lexes(self, outfile):
with open(outfile, "w") as fout:
json.dump(self.lex_as_dict(), fout, indent=2)
@staticmethod
def process_ref(text):
def _process(ref):
doc = nlp(ref)
toks = " ".join(getattr(tok, "text", None) for tok in doc).lower()
#toks = " ".join(nltk.word_tokenize(ref)).lower()
return toks
retval = [_process(ref) for ref in text] if isinstance(text, list) else _process(text)
return retval
def main():
pass
if __name__ == "__main__":
main()