Refactoring lexis/morphology matchers, now "pickable".

This commit is contained in:
Ozbolt Menegatti 2019-06-11 10:02:24 +02:00
parent ad0f9b0956
commit 3be4118dc0

37
wani.py
View File

@ -442,7 +442,10 @@ def determine_ppb(rgx):
else:
return 4
def build_morphology_regex(restriction):
class MorphologyRegex:
def __init__(self, restriction):
self.min_msd_length = 1
restr_dict = {}
for feature in restriction:
feature_dict = dict(feature.items())
@ -463,7 +466,6 @@ def build_morphology_regex(restriction):
rgx = [cat_code] + CATEGORY_BASES[cat_code]
del restr_dict['POS']
min_msd_length = 1
for attribute, (value, typ) in restr_dict.items():
index = TAGSET[cat_code].index(attribute.lower())
@ -478,30 +480,32 @@ def build_morphology_regex(restriction):
rgx[index + 1] = match
if typ:
min_msd_length = max(index + 1, min_msd_length)
self.min_msd_length = max(index + 1, self.min_msd_length)
re_objects = [re.compile(r) for r in rgx]
def matcher(text):
if len(text) <= min_msd_length:
self.re_objects = [re.compile(r) for r in rgx]
self.rgx = rgx
def __call__(self, text):
if len(text) <= self.min_msd_length:
return False
for c, r in zip(text, re_objects):
for c, r in zip(text, self.re_objects):
if not r.match(c):
return False
return True
return rgx, matcher
def build_lexis_regex(restriction):
class LexisRegex:
def __init__(self, restriction):
restr_dict = {}
for feature in restriction:
restr_dict.update(feature.items())
assert "lemma" in restr_dict
match_list = restr_dict['lemma'].split('|')
self.match_list = restr_dict['lemma'].split('|')
return match_list, lambda text: text in match_list
def __call__(self, text):
return text in self.match_list
class Restriction:
@ -517,13 +521,12 @@ class Restriction:
restriction_type = restriction_tag.get('type')
if restriction_type == "morphology":
self.type = RestrictionType.Morphology
present, self.matcher = build_morphology_regex(list(restriction_tag))
self.present = " ".join(present)
self.ppb = determine_ppb(present)
self.matcher = MorphologyRegex(list(restriction_tag))
self.ppb = determine_ppb(self.matcher.rgx)
elif restriction_type == "lexis":
self.type = RestrictionType.Lexis
self.present, self.matcher = build_lexis_regex(list(restriction_tag))
self.matcher = LexisRegex(list(restriction_tag))
else:
raise NotImplementedError()
@ -802,7 +805,7 @@ def get_lemma_features(et):
result = {}
for pos in lf.iter('POS'):
rgx_list, _ = build_morphology_regex(pos)
rgx_list = MorphologyRegex(pos).rgx
rgx_str = ""
for position in rgx_list:
if position == ".":