|
|
|
@ -1,7 +1,7 @@
|
|
|
|
|
import re
|
|
|
|
|
from enum import Enum
|
|
|
|
|
|
|
|
|
|
from luscenje_struktur.codes_tagset import CODES, TAGSET
|
|
|
|
|
from luscenje_struktur.codes_tagset import CODES, TAGSET, CODES_UD
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RestrictionType(Enum):
|
|
|
|
@ -9,6 +9,21 @@ class RestrictionType(Enum):
|
|
|
|
|
Lexis = 1
|
|
|
|
|
MatchAll = 2
|
|
|
|
|
Space = 3
|
|
|
|
|
MorphologyUD = 4
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def determine_ppb_ud(rgxs):
|
|
|
|
|
if len(rgxs) != 1:
|
|
|
|
|
return 0
|
|
|
|
|
rgx = rgxs[0]
|
|
|
|
|
if rgx in ("ADJ", "NOUN", "ADV"):
|
|
|
|
|
return 0
|
|
|
|
|
elif rgx == "AUX":
|
|
|
|
|
return 3
|
|
|
|
|
elif rgx == "VERB":
|
|
|
|
|
return 2
|
|
|
|
|
else:
|
|
|
|
|
return 4
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def determine_ppb(rgxs):
|
|
|
|
@ -112,6 +127,78 @@ class MorphologyRegex:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MorphologyUDRegex:
|
|
|
|
|
def __init__(self, restriction):
|
|
|
|
|
# self.min_msd_length = 1
|
|
|
|
|
|
|
|
|
|
restr_dict = {}
|
|
|
|
|
for feature in restriction:
|
|
|
|
|
feature_dict = dict(feature.items())
|
|
|
|
|
|
|
|
|
|
match_type = True
|
|
|
|
|
# if "filter" in feature_dict:
|
|
|
|
|
# assert feature_dict['filter'] == "negative"
|
|
|
|
|
# match_type = False
|
|
|
|
|
# del feature_dict['filter']
|
|
|
|
|
|
|
|
|
|
assert len(feature_dict) == 1
|
|
|
|
|
key, value = next(iter(feature_dict.items()))
|
|
|
|
|
restr_dict[key] = (value, match_type)
|
|
|
|
|
|
|
|
|
|
assert 'POS' in restr_dict
|
|
|
|
|
|
|
|
|
|
# handle multiple word types
|
|
|
|
|
if '|' in restr_dict['POS'][0]:
|
|
|
|
|
categories = restr_dict['POS'][0].split('|')
|
|
|
|
|
else:
|
|
|
|
|
categories = [restr_dict['POS'][0]]
|
|
|
|
|
|
|
|
|
|
self.rgxs = []
|
|
|
|
|
self.re_objects = []
|
|
|
|
|
self.min_msd_lengths = []
|
|
|
|
|
|
|
|
|
|
del restr_dict['POS']
|
|
|
|
|
|
|
|
|
|
for category in categories:
|
|
|
|
|
min_msd_length = 1
|
|
|
|
|
category = category.upper()
|
|
|
|
|
assert category in CODES_UD
|
|
|
|
|
cat_code = category
|
|
|
|
|
rgx = category
|
|
|
|
|
|
|
|
|
|
# for attribute, (value, typ) in restr_dict.items():
|
|
|
|
|
# if attribute.lower() not in TAGSET[cat_code]:
|
|
|
|
|
# continue
|
|
|
|
|
# index = TAGSET[cat_code].index(attribute.lower())
|
|
|
|
|
# assert index >= 0
|
|
|
|
|
#
|
|
|
|
|
# if '|' in value:
|
|
|
|
|
# match = "".join(CODES[val] for val in value.split('|'))
|
|
|
|
|
# else:
|
|
|
|
|
# match = CODES[value]
|
|
|
|
|
#
|
|
|
|
|
# match = "[{}{}]".format("" if typ else "^", match)
|
|
|
|
|
# rgx[index + 1] = match
|
|
|
|
|
#
|
|
|
|
|
# if typ:
|
|
|
|
|
# min_msd_length = max(index + 1, min_msd_length)
|
|
|
|
|
|
|
|
|
|
# strip rgx
|
|
|
|
|
# for i in reversed(range(len(rgx))):
|
|
|
|
|
# if rgx[i] == '.':
|
|
|
|
|
# rgx = rgx[:-1]
|
|
|
|
|
# else:
|
|
|
|
|
# break
|
|
|
|
|
|
|
|
|
|
# self.re_objects.append([re.compile(r) for r in rgx])
|
|
|
|
|
self.rgxs.append(rgx)
|
|
|
|
|
self.min_msd_lengths.append(min_msd_length)
|
|
|
|
|
|
|
|
|
|
def __call__(self, text):
|
|
|
|
|
assert len(self.rgxs) == 1
|
|
|
|
|
return self.rgxs[0] == text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LexisRegex:
|
|
|
|
|
def __init__(self, restriction):
|
|
|
|
|
restr_dict = {}
|
|
|
|
@ -150,8 +237,11 @@ class SpaceRegex:
|
|
|
|
|
|
|
|
|
|
return match
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Restriction:
|
|
|
|
|
def __init__(self, restriction_tag):
|
|
|
|
|
def __init__(self, restriction_tag, system_type='JOS'):
|
|
|
|
|
self.ppb = 4 # polnopomenska beseda (0-4)
|
|
|
|
|
|
|
|
|
|
if restriction_tag is None:
|
|
|
|
@ -162,9 +252,15 @@ class Restriction:
|
|
|
|
|
|
|
|
|
|
restriction_type = restriction_tag.get('type')
|
|
|
|
|
if restriction_type == "morphology":
|
|
|
|
|
self.type = RestrictionType.Morphology
|
|
|
|
|
self.matcher = MorphologyRegex(list(restriction_tag))
|
|
|
|
|
self.ppb = determine_ppb(self.matcher.rgxs)
|
|
|
|
|
if system_type == 'JOS':
|
|
|
|
|
self.type = RestrictionType.Morphology
|
|
|
|
|
self.matcher = MorphologyRegex(list(restriction_tag))
|
|
|
|
|
self.ppb = determine_ppb(self.matcher.rgxs)
|
|
|
|
|
# UD system is handled based on deprel
|
|
|
|
|
elif system_type == 'UD':
|
|
|
|
|
self.type = RestrictionType.MorphologyUD
|
|
|
|
|
self.matcher = MorphologyUDRegex(list(restriction_tag))
|
|
|
|
|
# self.ppb = determine_ppb_ud(self.matcher.rgxs)
|
|
|
|
|
|
|
|
|
|
elif restriction_type == "lexis":
|
|
|
|
|
self.type = RestrictionType.Lexis
|
|
|
|
@ -177,7 +273,7 @@ class Restriction:
|
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
|
|
def match(self, word):
|
|
|
|
|
if self.type == RestrictionType.Morphology:
|
|
|
|
|
if self.type == RestrictionType.Morphology or self.type == RestrictionType.MorphologyUD:
|
|
|
|
|
match_to = word.msd
|
|
|
|
|
elif self.type == RestrictionType.Lexis:
|
|
|
|
|
match_to = word.lemma
|
|
|
|
|