-
Notifications
You must be signed in to change notification settings - Fork 0
/
seg_lex.py
463 lines (388 loc) · 17.1 KB
/
seg_lex.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
from collections import namedtuple
from useg import seg_tsv
Lexeme = namedtuple("Lexeme", ["lex_id", "form", "lemma", "pos", "features", "morphemes"])
Morpheme = namedtuple("Morpheme", ["span", "features"])
class SegLex:
"""
A lexicon of segmentations.
Lexemes are identified using their IDs. Each lexeme has a string
form, which can be subdivided into morph(eme)s. Multiple alternative
subdivisions are possible, each identified by an annotation name.
"""
__slots__ = ("_lexemes", "_poses", "_forms")
def __init__(self):
self._lexemes = []
self._poses = {}
self._forms = {}
def load(self, f):
"""
Load the lexicon from `f`, which is either an open file-like
object open for reading text or a string filename, and add all
information contained therein to the internal lexicon of this
object.
"""
# If f is a filename, we open the file ourselves and therefore
# should also close it after reading from it.
actual_f = None
close_at_end = False
try:
if isinstance(f, str):
close_at_end = True
actual_f = open(f, "rt", encoding="utf-8")
else:
# We assume f is already an open file object.
actual_f = f
for line in actual_f:
record = seg_tsv.parse_line(line)
features = {k: v for k, v in record.annot.items() if k not in {"annot_name", "segmentation"}}
if "segmentation" in record.annot:
if "annot_name" not in record.annot:
# Unnamed segmentation. Reject it.
raise ValueError("Line '{}' has unnamed segmentation".format(line))
annot_name = record.annot["annot_name"]
segmentation = record.annot["segmentation"]
else:
segmentation = []
for existing_lexeme in self.iter_lexemes(record.form, record.lemma, record.pos):
existing_annot_names = self.annot_names(existing_lexeme)
if existing_annot_names \
and annot_name not in existing_annot_names \
and self.features(existing_lexeme) == features:
# This is another segmentation of an existing lexeme.
# Avoid adding the lexeme, just add the segmentation.
lexeme = existing_lexeme
break
else:
lexeme = self.add_lexeme(record.form, record.lemma, record.pos, features)
for segment in segmentation:
span = segment["span"]
del segment["span"]
self.add_morpheme(lexeme, annot_name, span, segment)
finally:
if actual_f is not None and close_at_end:
actual_f.close()
def _simple_seg(self, lex_id, annot_name):
simple_seg = []
last_morphemes = self.morphemes(lex_id, annot_name, sort=False, position=0)
morph_str = ""
for i, char in enumerate(self.form(lex_id)):
morphemes = self.morphemes(lex_id, annot_name, sort=False, position=i)
morpheme_lists_identical = True
if len(morphemes) != len(last_morphemes):
morpheme_lists_identical = False
else:
# Check the morpheme lists item by item for equality.
for morpheme, last_morpheme in zip(morphemes, last_morphemes):
if morpheme is not last_morpheme:
morpheme_lists_identical = False
break
if morpheme_lists_identical:
morph_str += char
else:
last_morphemes = morphemes
simple_seg.append(morph_str)
morph_str = char
simple_seg.append(morph_str)
return simple_seg
def _as_records(self):
"""
Iterate over the lexicon as a sequence of SegRecords (not sorted).
"""
for lexeme in self._lexemes:
if not lexeme.morphemes:
# Return a lexeme with no records in it, because the
# loop below is not going to execute.
assert "segmentation" not in lexeme.features and "annot_name" not in lexeme.features
yield seg_tsv.SegRecord(lexeme.form, lexeme.lemma, lexeme.pos, [], lexeme.features)
for annot_name in sorted(lexeme.morphemes.keys()):
annot = lexeme.features.copy()
assert "segmentation" not in annot and "annot_name" not in annot
annot["annot_name"] = annot_name
# TODO Ensure span is not already defined.
annot["segmentation"] = [{**morpheme.features, "span": morpheme.span}
for morpheme in self.morphemes(
lexeme.lex_id,
annot_name,
sort=True
)]
simple_seg = self._simple_seg(lexeme.lex_id, annot_name)
yield seg_tsv.SegRecord(lexeme.form, lexeme.lemma, lexeme.pos, simple_seg, annot)
def save(self, f):
"""
Save the lexicon to `f`, which is either an open file-like
object open for writing or appending text, or a string filename.
"""
# Sort the lexicon and iterate over it, producing the TSV output.
# TODO include the keys and values of features in the sorting as
# well.
records = sorted(
self._as_records(),
key=lambda r: (r.lemma, r.pos, r.form, r.simple_seg, len(r.annot))
)
# If f is a filename, we open the file ourselves and therefore
# should also close it after writing to it.
actual_f = None
close_at_end = False
try:
if isinstance(f, str):
close_at_end = True
actual_f = open(f, "wt", encoding="utf-8", newline="\n")
else:
# We assume f is already an open file object.
actual_f = f
for record in records:
actual_f.write(seg_tsv.format_record(record, False))
finally:
if actual_f is not None:
actual_f.flush()
if close_at_end:
actual_f.close()
def add_lexeme(self, form, lemma, pos, features=None):
"""
Create a new lexeme with the specified form, lemma and
morphological features. Returns an ID of the created lexeme.
"""
if features is None:
features = {}
lex_id = len(self._lexemes)
lexeme = Lexeme(lex_id, form, lemma, pos, features, {})
self._lexemes.append(lexeme)
# Add the lexeme to the lookup dicts.
if pos in self._poses:
if lemma in self._poses[pos]:
self._poses[pos][lemma].append(lex_id)
else:
self._poses[pos][lemma] = [lex_id]
else:
self._poses[pos] = {lemma: [lex_id]}
if form in self._forms:
self._forms[form].append(lex_id)
else:
self._forms[form] = [lex_id]
return lex_id
def iter_lexemes(self, form=None, lemma=None, pos=None):
"""
Find all lexemes with the specified properties. If neither
`form`, `lemma` or `pos` are specified, iterate over all
lexemes in the lexicon.
Return an iterator over IDs of matching lexemes.
"""
if form is not None:
# Use the `form` dict for lookup.
if form in self._forms:
for lex_id in self._forms[form]:
if (lemma is None or self.lemma(lex_id) == lemma) \
and (pos is None or self.pos(lex_id) == pos):
yield lex_id
return
# `form` is None, so use the `pos` dict for lookup.
if pos is None and lemma is None:
# No constraints specified, iterate over the whole lexicon.
# TODO when deleting lexemes, what happens to the
# slot in the _lexemes?
yield from range(len(self._lexemes))
return
# Either POS or lemma is specified. Find all applicable POSes
# (there may be multiple if `pos` is None).
if pos is None:
poses = self._poses.values()
elif pos in self._poses:
poses = (self._poses[pos], )
else:
# A POS was specified, but is not found.
return
if lemma is None:
# Yield all lexemes with the specified POS.
for lemmas in poses:
for lexemes in lemmas.values():
yield from lexemes
else:
# We have a dictionary for the applicable part-of-speech and
# a given lemma to search for.
for lemmas in poses:
if lemma in lemmas:
yield from lemmas[lemma]
def delete_lexeme(self, lex_id):
"""
Deletes the lexeme with the specified ID from the lexicon.
"""
raise NotImplementedError()
def form(self, lex_id):
"""
Return the string form of the lexeme with ID `lex_id`.
"""
return self._lexemes[lex_id].form
def lemma(self, lex_id):
"""
Return the string lemma of the lexeme with ID `lex_id`.
"""
return self._lexemes[lex_id].lemma
def pos(self, lex_id):
"""
Return the part-of-speech tag of the lexeme with ID `lex_id`.
"""
return self._lexemes[lex_id].pos
def features(self, lex_id):
"""
Return the `features` dict of lexeme with ID `lex_id`. You can
freely edit the information therein.
"""
return self._lexemes[lex_id].features
def annot_names(self, lex_id):
"""
Return a set of annotation names of different segmentations of
lexeme with ID `lex_id`.
"""
return set(self._lexemes[lex_id].morphemes.keys())
def print_lexeme(self, lex_id):
"""
Return a stringified form of the lexeme with ID `lex_id`.
This method is meant to be used for pretty-printing the lexeme
in logs, not as a serialization format – for that, use the
method `SegLex.save` instead.
"""
return "{}({}#{})".format(self.form(lex_id), self.lemma(lex_id), self.pos(lex_id))
def print_simple_segmentation(self, lex_id, annot_name):
"""
Returns a simple string representation of the segmentation of the
lexeme.
"""
return " + ".join(self._simple_seg(lex_id, annot_name))
def add_contiguous_morpheme(self, lex_id, annot_name, start, end, features=None):
"""
Subdivide the lexeme with `lex_id` using a new morpheme starting
at 0-indexed position `start` (inclusive) and continuing through
position `end` (exclusive) on annotation layer `annot_name`. The
annotation `features` is saved together with the newly-created
morpheme.
"""
self.add_morpheme(lex_id, annot_name, list(range(start, end)), features)
def add_morpheme(self, lex_id, annot_name, span, features=None):
"""
Subdivide the lexeme with `lex_id` using a new morpheme spanning
integer positions enumerated in `span` on annotation layer
`annot_name`. The annotation `features` is saved together with the
newly-created morpheme.
"""
# Check that the morpheme span actually exists in the lexeme.
span = frozenset(span)
for pos in span:
if pos < 0 or pos >= len(self.form(lex_id)):
raise ValueError(
"Morpheme span position {} is out-of-bounds in lexeme {}".format(
pos,
self.print_lexeme(lex_id)
)
)
if features is None:
features = {}
# Add the morpheme.
morpheme = Morpheme(span, features)
if annot_name not in self._lexemes[lex_id].morphemes:
self._lexemes[lex_id].morphemes[annot_name] = [morpheme]
else:
self._lexemes[lex_id].morphemes[annot_name].append(morpheme)
def add_morphemes_from_list(self, lex_id, annot_name, morphemes):
"""
If `morphemes` is a list of morph strings which, when
concatenated, produce exactly the form of lexeme `lex_id`, add
morphemes corresponding to the morphs.
If any morphs don't correspond to their respective piece
of the form, add no morphemes and raise an error.
"""
form = self.form(lex_id)
to_add = []
end = 0
for morpheme in morphemes:
start = end
if isinstance(morpheme, str):
morph = morpheme
else:
raise NotImplementedError("Only strings are supported as morphemes, {} given.".format(morpheme.__class__))
if form.startswith(morph, start):
end = start + len(morph)
to_add.append((start, end, None))
else:
raise ValueError("Morph '{}' not found in form '{}' at position {} (possibly because an earlier morph blocked it).".format(morph, form, start))
for start, end, features in to_add:
self.add_contiguous_morpheme(lex_id, annot_name, start, end, features)
def morphemes(self, lex_id, annot_name, sort=False, position=None):
"""
Get a list of all morphemes of lexeme with ID `lex_id` on
annotation layer `annot_name`. If `position` is filled in,
return only those covering that position. If there are no
morphemes or none cover the position, an empty list is returned.
If `sort` is True, sort the morphemes by their span.
"""
if annot_name not in self._lexemes[lex_id].morphemes:
# The annotation layer is not present, so there are no
# annotations there.
return []
if position is None:
# Return all morphemes on the annotation layer.
# Return a copy to prevent accidental mangling.
morphemes = self._lexemes[lex_id].morphemes[annot_name]
if sort:
return sorted(morphemes, key=lambda morpheme: tuple(sorted(morpheme.span)))
else:
return list(morphemes)
else:
# Return only morphemes at the specified position.
if position < 0 or position >= len(self.form(lex_id)):
raise ValueError("Invalid position {} in lexeme {}; is out of bounds in the form".format(position, self.print_lexeme(lex_id)))
m = []
for morpheme in self._lexemes[lex_id].morphemes[annot_name]:
if position in morpheme.span:
m.append(morpheme)
if sort:
return sorted(m, key=lambda morpheme: tuple(sorted(morpheme.span)))
else:
return m
def morpheme(self, lex_id, annot_name, position):
"""
Return one of the morphemes found at `position` of annotation
layer `annot_name` in lexeme `lex_id`. If there are multiple
morphemes at this position, silently choose one. Return None
if no such morphemes exist.
"""
if position < 0 or position >= len(self.form(lex_id)):
raise ValueError("Invalid position {} in lexeme {}; is out of bounds in the form".format(position, self.print_lexeme(lex_id)))
if annot_name in self._lexemes[lex_id].morphemes:
for morpheme in self._lexemes[lex_id].morphemes[annot_name]:
if position in morpheme.span:
return morpheme
return None
@staticmethod
def morpheme_to_string(morpheme, form):
"""
Return the string form of the morph of morpheme `morpheme` as
found in lexeme with word form `form`.
"""
if morpheme.span:
span = sorted(morpheme.span)
last_idx = span[0]
morph = form[last_idx]
for idx in span[1:]:
if idx == last_idx + 1:
# This part of the morph is contiguous.
morph += form[idx]
else:
morph += " + " + form[idx]
last_idx = idx
return morph
else:
# Empty morpheme spans are weird, but support them anyway.
return ""
def morph(self, lex_id, annot_name, position):
"""
Return the string form of one of the morphemes found at
`position` of annotation layer `annot_name` in lexeme `lex_id`,
or None if no such morpheme exists.
FIXME this API is really unwieldy, come up with something
better? Maybe introduce morpheme IDs, like we have lex IDs?
"""
morpheme = self.morpheme(lex_id, annot_name, position)
if morpheme is None:
return None
form = self.form(lex_id)
return self.morpheme_to_string(morpheme, form)