Refactoring lexis/morphology matchers, now "pickable".
This commit is contained in:
parent
ad0f9b0956
commit
3be4118dc0
37
wani.py
37
wani.py
|
@ -442,7 +442,10 @@ def determine_ppb(rgx):
|
||||||
else:
|
else:
|
||||||
return 4
|
return 4
|
||||||
|
|
||||||
def build_morphology_regex(restriction):
|
class MorphologyRegex:
|
||||||
|
def __init__(self, restriction):
|
||||||
|
self.min_msd_length = 1
|
||||||
|
|
||||||
restr_dict = {}
|
restr_dict = {}
|
||||||
for feature in restriction:
|
for feature in restriction:
|
||||||
feature_dict = dict(feature.items())
|
feature_dict = dict(feature.items())
|
||||||
|
@ -463,7 +466,6 @@ def build_morphology_regex(restriction):
|
||||||
rgx = [cat_code] + CATEGORY_BASES[cat_code]
|
rgx = [cat_code] + CATEGORY_BASES[cat_code]
|
||||||
|
|
||||||
del restr_dict['POS']
|
del restr_dict['POS']
|
||||||
min_msd_length = 1
|
|
||||||
|
|
||||||
for attribute, (value, typ) in restr_dict.items():
|
for attribute, (value, typ) in restr_dict.items():
|
||||||
index = TAGSET[cat_code].index(attribute.lower())
|
index = TAGSET[cat_code].index(attribute.lower())
|
||||||
|
@ -478,30 +480,32 @@ def build_morphology_regex(restriction):
|
||||||
rgx[index + 1] = match
|
rgx[index + 1] = match
|
||||||
|
|
||||||
if typ:
|
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]
|
self.re_objects = [re.compile(r) for r in rgx]
|
||||||
def matcher(text):
|
self.rgx = rgx
|
||||||
if len(text) <= min_msd_length:
|
|
||||||
|
def __call__(self, text):
|
||||||
|
if len(text) <= self.min_msd_length:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
for c, r in zip(text, re_objects):
|
for c, r in zip(text, self.re_objects):
|
||||||
if not r.match(c):
|
if not r.match(c):
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
return rgx, matcher
|
|
||||||
|
|
||||||
|
class LexisRegex:
|
||||||
def build_lexis_regex(restriction):
|
def __init__(self, restriction):
|
||||||
restr_dict = {}
|
restr_dict = {}
|
||||||
for feature in restriction:
|
for feature in restriction:
|
||||||
restr_dict.update(feature.items())
|
restr_dict.update(feature.items())
|
||||||
|
|
||||||
assert "lemma" in restr_dict
|
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:
|
class Restriction:
|
||||||
|
@ -517,13 +521,12 @@ class Restriction:
|
||||||
restriction_type = restriction_tag.get('type')
|
restriction_type = restriction_tag.get('type')
|
||||||
if restriction_type == "morphology":
|
if restriction_type == "morphology":
|
||||||
self.type = RestrictionType.Morphology
|
self.type = RestrictionType.Morphology
|
||||||
present, self.matcher = build_morphology_regex(list(restriction_tag))
|
self.matcher = MorphologyRegex(list(restriction_tag))
|
||||||
self.present = " ".join(present)
|
self.ppb = determine_ppb(self.matcher.rgx)
|
||||||
self.ppb = determine_ppb(present)
|
|
||||||
|
|
||||||
elif restriction_type == "lexis":
|
elif restriction_type == "lexis":
|
||||||
self.type = RestrictionType.Lexis
|
self.type = RestrictionType.Lexis
|
||||||
self.present, self.matcher = build_lexis_regex(list(restriction_tag))
|
self.matcher = LexisRegex(list(restriction_tag))
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
@ -802,7 +805,7 @@ def get_lemma_features(et):
|
||||||
|
|
||||||
result = {}
|
result = {}
|
||||||
for pos in lf.iter('POS'):
|
for pos in lf.iter('POS'):
|
||||||
rgx_list, _ = build_morphology_regex(pos)
|
rgx_list = MorphologyRegex(pos).rgx
|
||||||
rgx_str = ""
|
rgx_str = ""
|
||||||
for position in rgx_list:
|
for position in rgx_list:
|
||||||
if position == ".":
|
if position == ".":
|
||||||
|
|
Loading…
Reference in New Issue
Block a user