2018-10-29 10:29:51 +00:00
|
|
|
from xml.etree import ElementTree
|
|
|
|
import re
|
|
|
|
from enum import Enum
|
|
|
|
from collections import defaultdict
|
2019-01-08 20:13:36 +00:00
|
|
|
import sys
|
2019-01-19 21:42:51 +00:00
|
|
|
import logging
|
2018-10-29 10:29:51 +00:00
|
|
|
|
|
|
|
from msd_translate import MSD_TRANSLATE
|
|
|
|
|
|
|
|
|
2019-01-25 10:58:40 +00:00
|
|
|
MAX_NUM_COMPONENTS = 5
|
|
|
|
|
|
|
|
|
2019-01-08 20:13:36 +00:00
|
|
|
STAVKI = sys.argv[1]
|
2019-01-19 21:42:51 +00:00
|
|
|
STRUKTURE = sys.argv[2]
|
|
|
|
FILE_OUT = sys.argv[3]
|
2018-10-29 10:29:51 +00:00
|
|
|
|
|
|
|
CODES = {
|
|
|
|
"Noun": "N",
|
|
|
|
"Verb": "V",
|
|
|
|
"Adjective": "A",
|
|
|
|
"Adverb": "R",
|
|
|
|
"Pronoun": "P",
|
|
|
|
"Numeral": "M",
|
|
|
|
"Preposition": "S",
|
|
|
|
"Conjunction": "C",
|
|
|
|
"Particle": "Q",
|
|
|
|
"Interjection": "I",
|
|
|
|
"Abbreviation": "Y",
|
|
|
|
"Residual": "X",
|
|
|
|
|
|
|
|
'common': 'c',
|
|
|
|
'proper': 'p',
|
|
|
|
'masculine': 'm',
|
|
|
|
'feminine': 'f',
|
|
|
|
'neuter': 'n',
|
|
|
|
"singular": "s",
|
|
|
|
"dual": "d",
|
|
|
|
"plural": "p",
|
|
|
|
"nominative": "n",
|
|
|
|
"genitive": "g",
|
|
|
|
"dative": "d",
|
|
|
|
"accusative": "a",
|
|
|
|
"locative": "l",
|
|
|
|
"instrumental": "i",
|
|
|
|
"no": "n",
|
|
|
|
"yes": "y",
|
|
|
|
"main": "m",
|
|
|
|
"auxiliary": "a",
|
|
|
|
"perfective": "e",
|
|
|
|
"progressive": "p",
|
|
|
|
"biaspectual": "b",
|
|
|
|
"infinitive": "n",
|
|
|
|
"supine": "u",
|
|
|
|
"participle": "p",
|
|
|
|
"present": "r",
|
|
|
|
"future": "f",
|
|
|
|
"conditional": "c",
|
|
|
|
"imperative": "m",
|
|
|
|
"first": "1",
|
|
|
|
"second": "2",
|
|
|
|
"third": "3",
|
|
|
|
"general": "g",
|
|
|
|
"possessive": "s",
|
|
|
|
"positive": "p",
|
|
|
|
"comparative": "c",
|
|
|
|
"superlative": "s",
|
|
|
|
"personal": "p",
|
|
|
|
"demonstrative": "d",
|
|
|
|
"relative": "r",
|
|
|
|
"reflexive": "x",
|
|
|
|
"interrogative": "q",
|
|
|
|
"indefinite": "i",
|
|
|
|
"negative": "z",
|
|
|
|
"bound": "b",
|
|
|
|
"digit": "d",
|
|
|
|
"roman": "r",
|
|
|
|
"letter": "l",
|
|
|
|
"cardinal": "c",
|
|
|
|
"ordinal": "o",
|
|
|
|
"pronominal": "p",
|
|
|
|
"special": "s",
|
|
|
|
"coordinating": "c",
|
|
|
|
"subordinating": "s",
|
|
|
|
"foreign": "f",
|
|
|
|
"typo": "t",
|
|
|
|
"program": "p",
|
|
|
|
}
|
|
|
|
|
|
|
|
TAGSET = {
|
|
|
|
"N": ['type', 'gender', 'number', 'case', 'animate'],
|
|
|
|
"V": ['type', 'aspect', 'vform', 'person', 'number', 'gender', 'negative'],
|
|
|
|
"A": ['type', 'degree', 'gender', 'number', 'case', 'definiteness'],
|
|
|
|
"R": ['type', 'degree'],
|
|
|
|
"P": ['type', 'person', 'gender', 'number', 'case', 'owner_number', 'owned_gender', 'clitic'],
|
|
|
|
"M": ['form', 'type', 'gender', 'number', 'case', 'definiteness'],
|
|
|
|
"S": ['case'],
|
|
|
|
"C": ['type'],
|
|
|
|
"Q": [],
|
|
|
|
"I": [],
|
|
|
|
"Y": [],
|
|
|
|
"X": ['type']
|
|
|
|
}
|
|
|
|
|
|
|
|
CATEGORY_BASES = {
|
2019-01-19 21:42:51 +00:00
|
|
|
"N": ['.'] * 5,
|
|
|
|
"V": ['.'] * 7,
|
|
|
|
"A": ['.'] * 6,
|
|
|
|
"R": ['.'] * 2,
|
|
|
|
"P": ['.'] * 6,
|
|
|
|
"M": ['.'] * 6,
|
|
|
|
"S": ['.'] * 1,
|
|
|
|
"C": ['.'] * 1,
|
2018-10-29 10:29:51 +00:00
|
|
|
"Q": [],
|
|
|
|
"I": [],
|
|
|
|
"Y": [],
|
2019-01-19 21:42:51 +00:00
|
|
|
"X": ['.'] * 1
|
2018-10-29 10:29:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class RestrictionType(Enum):
|
|
|
|
Morphology = 0
|
|
|
|
Lexis = 1
|
2019-01-19 21:42:51 +00:00
|
|
|
MatchAll = 2
|
2018-10-29 10:29:51 +00:00
|
|
|
|
|
|
|
|
2019-01-19 21:42:51 +00:00
|
|
|
class Rendition(Enum):
|
2018-10-29 10:29:51 +00:00
|
|
|
Lemma = 0
|
|
|
|
WordForm = 1
|
2019-01-19 21:42:51 +00:00
|
|
|
Unknown = 2
|
|
|
|
|
|
|
|
class ComponentRendition:
|
|
|
|
def __init__(self, rendition=Rendition.Unknown):
|
|
|
|
self.word_form = {}
|
|
|
|
self.rendition = rendition
|
|
|
|
|
|
|
|
def render(self, word):
|
|
|
|
if self.rendition == Rendition.Lemma:
|
|
|
|
return word.lemma
|
|
|
|
elif self.rendition == Rendition.WordForm:
|
|
|
|
return word.text
|
|
|
|
elif self.rendition == Rendition.Unknown:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
raise RuntimeError("Unknown rendition: {}".format(self.rendition))
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return str(self.rendition)
|
|
|
|
|
|
|
|
|
|
|
|
# dont know...
|
|
|
|
class StructureSelection(Enum):
|
|
|
|
All = 0
|
|
|
|
Frequency = 1
|
|
|
|
|
|
|
|
class ComponentRepresentation:
|
|
|
|
def new(s):
|
|
|
|
if 'rendition' in s:
|
|
|
|
if s['rendition'] == "lemma":
|
|
|
|
return ComponentRendition(Rendition.Lemma)
|
|
|
|
elif s['rendition'] == "word_form":
|
|
|
|
return ComponentRendition(Rendition.WordForm)
|
|
|
|
else:
|
|
|
|
raise NotImplementedError("Rendition: {}".format(s))
|
|
|
|
elif 'selection' in s:
|
|
|
|
if s['selection'] == "frequency":
|
|
|
|
return StructureSelection.Frequency
|
|
|
|
elif s['selection'] == "all":
|
|
|
|
return StructureSelection.All
|
|
|
|
else:
|
|
|
|
return {s['selection']: s['value']}
|
|
|
|
else:
|
|
|
|
raise NotImplementedError("Representation: {}".format(s))
|
|
|
|
|
|
|
|
|
|
|
|
class ComponentStatus(Enum):
|
|
|
|
Optional = 0
|
|
|
|
Required = 1
|
|
|
|
Forbidden = 2
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
if self == ComponentStatus.Optional:
|
|
|
|
return "?"
|
|
|
|
elif self == ComponentStatus.Required:
|
|
|
|
return "!"
|
|
|
|
else: #Forbidden
|
|
|
|
return "X"
|
2018-10-29 10:29:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_level(restriction):
|
|
|
|
for feature in restriction:
|
|
|
|
if "level" in feature.keys():
|
|
|
|
lvl = feature.get("level")
|
2019-01-19 21:42:51 +00:00
|
|
|
else:
|
|
|
|
continue
|
2018-10-29 10:29:51 +00:00
|
|
|
|
|
|
|
raise RuntimeError("Unreachable!")
|
|
|
|
|
|
|
|
|
|
|
|
def build_morphology_regex(restriction):
|
|
|
|
restr_dict = {}
|
|
|
|
for feature in restriction:
|
2019-01-19 21:42:51 +00:00
|
|
|
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)
|
2018-10-29 10:29:51 +00:00
|
|
|
|
|
|
|
assert('POS' in restr_dict)
|
2019-01-19 21:42:51 +00:00
|
|
|
category = restr_dict['POS'][0].capitalize()
|
2018-10-29 10:29:51 +00:00
|
|
|
cat_code = CODES[category]
|
|
|
|
rgx = [cat_code] + CATEGORY_BASES[cat_code]
|
|
|
|
|
|
|
|
del restr_dict['POS']
|
2019-01-25 10:58:40 +00:00
|
|
|
min_msd_length = 1
|
2018-10-29 10:29:51 +00:00
|
|
|
|
2019-01-19 21:42:51 +00:00
|
|
|
for attribute, (value, typ) in restr_dict.items():
|
2018-10-29 10:29:51 +00:00
|
|
|
index = TAGSET[cat_code].index(attribute.lower())
|
|
|
|
assert(index >= 0)
|
|
|
|
|
|
|
|
if '|' in value:
|
2019-01-19 21:42:51 +00:00
|
|
|
match = "".join(CODES[val] for val in value.split('|'))
|
2018-10-29 10:29:51 +00:00
|
|
|
else:
|
|
|
|
match = CODES[value]
|
|
|
|
|
2019-01-19 21:42:51 +00:00
|
|
|
match = "[{}{}]".format("" if typ else "^", match)
|
2018-10-29 10:29:51 +00:00
|
|
|
rgx[index + 1] = match
|
|
|
|
|
2019-01-25 10:58:40 +00:00
|
|
|
if typ:
|
|
|
|
min_msd_length = max(index + 1, min_msd_length)
|
|
|
|
|
2019-01-19 21:42:51 +00:00
|
|
|
def matcher(text):
|
2019-01-25 10:58:40 +00:00
|
|
|
if len(text) <= min_msd_length:
|
|
|
|
return False
|
|
|
|
|
2019-01-19 21:42:51 +00:00
|
|
|
for c, r in zip(text, rgx):
|
|
|
|
if not re.match(r, c):
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
return " ".join(rgx), matcher
|
2018-10-29 10:29:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
def build_lexis_regex(restriction):
|
|
|
|
restr_dict = {}
|
|
|
|
for feature in restriction:
|
|
|
|
restr_dict.update(feature.items())
|
|
|
|
|
2019-01-19 21:42:51 +00:00
|
|
|
assert("lemma" in restr_dict)
|
|
|
|
match_list = restr_dict['lemma'].split('|')
|
|
|
|
|
|
|
|
return match_list, lambda text: text in match_list
|
2018-10-29 10:29:51 +00:00
|
|
|
|
|
|
|
|
2018-10-29 11:16:42 +00:00
|
|
|
class Restriction:
|
|
|
|
def __init__(self, restriction_tag):
|
2019-01-19 21:42:51 +00:00
|
|
|
if restriction_tag is None:
|
|
|
|
self.type = RestrictionType.MatchAll
|
|
|
|
self.matcher = None
|
|
|
|
self.present = None
|
|
|
|
return
|
|
|
|
|
2018-10-29 11:16:42 +00:00
|
|
|
restriction_type = restriction_tag.get('type')
|
|
|
|
if restriction_type == "morphology":
|
|
|
|
self.type = RestrictionType.Morphology
|
2019-01-19 21:42:51 +00:00
|
|
|
self.present, self.matcher = build_morphology_regex(list(restriction_tag))
|
2018-10-29 11:16:42 +00:00
|
|
|
elif restriction_type == "lexis":
|
|
|
|
self.type = RestrictionType.Lexis
|
2019-01-19 21:42:51 +00:00
|
|
|
self.present, self.matcher = build_lexis_regex(list(restriction_tag))
|
2018-10-29 11:16:42 +00:00
|
|
|
else:
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def match(self, word):
|
|
|
|
if self.type == RestrictionType.Morphology:
|
|
|
|
match_to = word.msd
|
|
|
|
elif self.type == RestrictionType.Lexis:
|
|
|
|
match_to = word.lemma
|
2019-01-19 21:42:51 +00:00
|
|
|
elif self.type == RestrictionType.MatchAll:
|
|
|
|
return True
|
2018-10-29 11:16:42 +00:00
|
|
|
else:
|
|
|
|
raise RuntimeError("Unreachable!")
|
|
|
|
|
2019-01-19 21:42:51 +00:00
|
|
|
return self.matcher(match_to)
|
2018-10-29 11:16:42 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2019-01-19 21:42:51 +00:00
|
|
|
return "({:s} {})".format(str(self.type).split('.')[1], self.present)
|
2018-10-29 11:16:42 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return str(self)
|
|
|
|
|
|
|
|
|
2018-10-29 10:29:51 +00:00
|
|
|
class Component:
|
2019-01-19 21:42:51 +00:00
|
|
|
def __init__(self, info):
|
|
|
|
idx = info['cid']
|
|
|
|
name = info['name'] if 'name' in info else None
|
|
|
|
|
|
|
|
if 'status' not in info:
|
|
|
|
status = ComponentStatus.Required
|
|
|
|
elif info['status'] == 'forbidden':
|
|
|
|
status = ComponentStatus.Forbidden
|
|
|
|
elif info['status'] == 'obligatory':
|
|
|
|
status = ComponentStatus.Required
|
|
|
|
elif info['status'] == 'optional':
|
|
|
|
status = ComponentStatus.Optional
|
|
|
|
else:
|
|
|
|
raise NotImplementedError("strange status: {}".format(info['status']))
|
2018-10-30 12:33:08 +00:00
|
|
|
|
2019-01-19 21:42:51 +00:00
|
|
|
self.status = status
|
|
|
|
self.name = name
|
2018-10-30 12:33:08 +00:00
|
|
|
self.idx = idx
|
2018-10-29 10:29:51 +00:00
|
|
|
self.restriction = None
|
2018-10-30 12:33:08 +00:00
|
|
|
self.next_element = []
|
2019-01-19 21:42:51 +00:00
|
|
|
self.rendition = ComponentRendition()
|
|
|
|
self.selection = {}
|
2018-10-29 10:29:51 +00:00
|
|
|
|
2018-10-30 12:33:08 +00:00
|
|
|
self.iter_ctr = 0
|
|
|
|
|
2019-01-19 21:42:51 +00:00
|
|
|
def render_word(self, word):
|
|
|
|
return self.rendition.render(word)
|
2018-10-29 10:29:51 +00:00
|
|
|
|
2018-10-30 12:33:08 +00:00
|
|
|
def add_next(self, next_component, link_label):
|
|
|
|
self.next_element.append((next_component, link_label))
|
2018-10-29 10:29:51 +00:00
|
|
|
|
2018-10-29 11:16:42 +00:00
|
|
|
def set_restriction(self, restrictions_tag):
|
2019-01-19 21:42:51 +00:00
|
|
|
if restrictions_tag is None:
|
|
|
|
self.restriction = Restriction(None)
|
|
|
|
|
|
|
|
elif restrictions_tag.tag == "restriction":
|
2018-10-29 11:16:42 +00:00
|
|
|
self.restriction = Restriction(restrictions_tag)
|
2018-10-29 10:29:51 +00:00
|
|
|
|
2018-10-29 11:16:42 +00:00
|
|
|
elif restrictions_tag.tag == "restriction_or":
|
|
|
|
self.restriction = [Restriction(el) for el in restrictions_tag]
|
|
|
|
|
|
|
|
else:
|
|
|
|
raise RuntimeError("Unreachable")
|
2018-10-29 10:29:51 +00:00
|
|
|
|
2019-01-19 21:42:51 +00:00
|
|
|
def set_representation(self, representation):
|
|
|
|
cr = None
|
|
|
|
if representation is not None:
|
|
|
|
self.representation = []
|
|
|
|
|
|
|
|
for feature in representation:
|
|
|
|
f = ComponentRepresentation.new(dict(feature.attrib))
|
|
|
|
|
|
|
|
if type(f) is StructureSelection:
|
|
|
|
assert(cr is None)
|
|
|
|
cr = f
|
|
|
|
elif type(f) is ComponentRendition:
|
|
|
|
self.rendition = f
|
|
|
|
elif type(f) is dict:
|
|
|
|
self.selection.update(f)
|
|
|
|
else:
|
|
|
|
raise RuntimeError("Unreachable: {}".format(f))
|
|
|
|
|
|
|
|
return cr
|
|
|
|
|
|
|
|
def find_next(self, deps, comps, restrs, reprs):
|
|
|
|
representation = StructureSelection.All
|
|
|
|
|
|
|
|
to_ret = []
|
2018-10-30 12:33:08 +00:00
|
|
|
for d in deps:
|
|
|
|
if d[0] == self.idx:
|
|
|
|
_, idx, dep_label = d
|
|
|
|
|
2019-01-19 21:42:51 +00:00
|
|
|
next_component = Component(comps[idx])
|
2018-10-30 12:33:08 +00:00
|
|
|
next_component.set_restriction(restrs[idx])
|
2019-01-19 21:42:51 +00:00
|
|
|
r1 = next_component.set_representation(reprs[idx])
|
|
|
|
to_ret.append(next_component)
|
2018-10-30 12:33:08 +00:00
|
|
|
|
|
|
|
self.add_next(next_component, dep_label)
|
2019-01-19 21:42:51 +00:00
|
|
|
others, r2 = next_component.find_next(deps, comps, restrs, reprs)
|
|
|
|
to_ret.extend(others)
|
|
|
|
|
|
|
|
if StructureSelection.Frequency in (r1, r2):
|
|
|
|
representation = StructureSelection.Frequency
|
|
|
|
|
|
|
|
return to_ret, representation
|
|
|
|
|
|
|
|
def name_str(self):
|
|
|
|
return "_" if self.name is None else self.name
|
|
|
|
|
2018-10-30 12:33:08 +00:00
|
|
|
|
2018-10-29 10:29:51 +00:00
|
|
|
def __str__(self):
|
2019-01-19 21:42:51 +00:00
|
|
|
n = self.name_str()
|
|
|
|
return "{:s}) {:7s}:{} [{}] :{}".format(
|
|
|
|
self.idx, n, self.status, self.restriction, self.rendition)
|
|
|
|
|
|
|
|
def tree(self):
|
|
|
|
el = []
|
|
|
|
for next, link in self.next_element:
|
|
|
|
el.append("{:3} -- {:5} --> {:3}".format(self.idx, link, next.idx))
|
|
|
|
el.extend(next.tree())
|
2018-10-29 10:29:51 +00:00
|
|
|
return el
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return str(self)
|
|
|
|
|
|
|
|
def match(self, word):
|
2019-01-19 21:42:51 +00:00
|
|
|
m1 = self._match_self(word)
|
|
|
|
if m1 is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
mn = self._match_next(word)
|
|
|
|
if mn is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
to_ret = [m1]
|
|
|
|
for cmatch in mn:
|
|
|
|
# if good match but nothing to add, just continue
|
|
|
|
if len(cmatch) == 0:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# if more than one match found for particular component
|
|
|
|
elif len(cmatch) > 1:
|
|
|
|
logging.debug("MULTIPLE: {}, {}".format(self.idx, cmatch))
|
|
|
|
# if more than one match in multiple components, NOPE!
|
|
|
|
if len(to_ret) > 1:
|
|
|
|
logging.warning("Strange multiple match: {}".format(
|
|
|
|
str([w.id for w in cmatch[0].values()])))
|
|
|
|
|
|
|
|
for tr in to_ret:
|
|
|
|
tr.update(cmatch[0])
|
|
|
|
continue
|
|
|
|
|
|
|
|
# yeah, so we have found more than one match, =>
|
|
|
|
# more than one element in to_ret
|
|
|
|
to_ret = [{**dict(to_ret[0]), **m} for m in cmatch]
|
|
|
|
|
|
|
|
else:
|
|
|
|
for tr in to_ret:
|
|
|
|
tr.update(cmatch[0])
|
|
|
|
|
|
|
|
logging.debug("MA: {}".format(str(to_ret)))
|
|
|
|
return to_ret
|
|
|
|
|
|
|
|
def _match_self(self, word):
|
2018-10-29 11:16:42 +00:00
|
|
|
matched = None
|
|
|
|
|
|
|
|
# matching
|
|
|
|
if type(self.restriction) is list:
|
|
|
|
for restr in self.restriction:
|
|
|
|
matched = restr.match(word)
|
2019-01-19 21:42:51 +00:00
|
|
|
if matched: # match either
|
2018-10-29 11:16:42 +00:00
|
|
|
break
|
2018-10-29 10:29:51 +00:00
|
|
|
else:
|
2018-10-29 11:16:42 +00:00
|
|
|
matched = self.restriction.match(word)
|
2018-10-29 10:29:51 +00:00
|
|
|
|
2019-01-19 21:42:51 +00:00
|
|
|
logging.debug("SELF MATCH({}: {} -> {}".format(self.idx, word.text, matched))
|
|
|
|
|
2018-10-29 11:16:42 +00:00
|
|
|
# recurse to next
|
2019-01-19 21:42:51 +00:00
|
|
|
if not matched:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return {self.idx: word}
|
|
|
|
|
|
|
|
def _match_next(self, word):
|
|
|
|
# matches for every component in links from this component
|
|
|
|
to_ret = []
|
|
|
|
|
|
|
|
# need to get all links that match
|
|
|
|
for next, link in self.next_element:
|
2019-01-25 10:58:40 +00:00
|
|
|
next_links = word.get_links(link)
|
|
|
|
logging.debug("FIND LINKS FOR: {} -> {}: #{}".format(self.idx, next.idx, len(next_links)))
|
2019-01-19 21:42:51 +00:00
|
|
|
to_ret.append([])
|
|
|
|
|
|
|
|
# good flag
|
|
|
|
good = next.status != ComponentStatus.Required
|
2019-01-25 10:58:40 +00:00
|
|
|
for next_word in next_links:
|
2019-01-19 21:42:51 +00:00
|
|
|
logging.debug("link: {}: {} -> {}".format(link, word.id, next_word.id))
|
|
|
|
match = next.match(next_word)
|
|
|
|
|
|
|
|
if match is not None:
|
|
|
|
# special treatement for forbidden
|
|
|
|
if next.status == ComponentStatus.Forbidden:
|
|
|
|
good = False
|
2018-10-30 12:33:08 +00:00
|
|
|
break
|
|
|
|
|
2019-01-19 21:42:51 +00:00
|
|
|
else:
|
|
|
|
assert(type(match) is list)
|
|
|
|
to_ret[-1].extend(match)
|
|
|
|
good = True
|
2018-10-29 10:29:51 +00:00
|
|
|
|
2019-01-19 21:42:51 +00:00
|
|
|
# if none matched, nothing found!
|
|
|
|
if not good:
|
|
|
|
logging.debug("BAD")
|
|
|
|
return None
|
2018-10-29 10:29:51 +00:00
|
|
|
|
2019-01-19 21:42:51 +00:00
|
|
|
return to_ret
|
2018-10-29 10:29:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SyntacticStructure:
|
|
|
|
def __init__(self):
|
|
|
|
self.id = None
|
|
|
|
self.lbs = None
|
2019-01-19 21:42:51 +00:00
|
|
|
self.agreements = []
|
|
|
|
self.components = []
|
|
|
|
self.selection = StructureSelection.All
|
2018-10-29 10:29:51 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def from_xml(xml):
|
|
|
|
st = SyntacticStructure()
|
2018-10-29 11:16:42 +00:00
|
|
|
st.id = xml.get('id')
|
2018-10-29 10:29:51 +00:00
|
|
|
st.lbs = xml.get('LBS')
|
2019-01-19 21:42:51 +00:00
|
|
|
|
|
|
|
if float(st.id.replace('-','.')) >= 17:
|
|
|
|
return None
|
2018-10-29 10:29:51 +00:00
|
|
|
|
2019-01-19 21:42:51 +00:00
|
|
|
assert(len(list(xml)) == 1)
|
|
|
|
system = next(iter(xml))
|
2018-10-29 10:29:51 +00:00
|
|
|
|
|
|
|
assert(system.get('type') == 'JOS')
|
2019-01-19 21:42:51 +00:00
|
|
|
components, dependencies, definitions = list(system)
|
2018-10-29 10:29:51 +00:00
|
|
|
|
2018-10-30 12:33:08 +00:00
|
|
|
deps = [ (dep.get('from'), dep.get('to'), dep.get('label')) for dep in dependencies ]
|
2019-01-19 21:42:51 +00:00
|
|
|
comps = { comp.get('cid'): dict(comp.items()) for comp in components }
|
|
|
|
|
|
|
|
restrs, forms = {}, {}
|
2018-10-29 10:29:51 +00:00
|
|
|
|
2019-01-19 21:42:51 +00:00
|
|
|
for comp in definitions:
|
|
|
|
n = comp.get('cid')
|
|
|
|
restrs[n] = None
|
|
|
|
forms[n] = None
|
2018-10-29 10:29:51 +00:00
|
|
|
|
2019-01-19 21:42:51 +00:00
|
|
|
for el in comp:
|
|
|
|
if el.tag.startswith("restriction"):
|
|
|
|
assert(restrs[n] is None)
|
|
|
|
restrs[n] = el
|
|
|
|
elif el.tag.startswith("representation"):
|
|
|
|
st.add_representation(n, el, forms)
|
|
|
|
else:
|
|
|
|
raise NotImplementedError("definition??")
|
|
|
|
|
|
|
|
fake_root_component = Component({'cid': '#', 'type': 'other'})
|
|
|
|
st.components, st.selection = fake_root_component.find_next(deps, comps, restrs, forms)
|
2018-10-29 10:29:51 +00:00
|
|
|
return st
|
|
|
|
|
2019-01-19 21:42:51 +00:00
|
|
|
def add_representation(self, n, el, forms):
|
|
|
|
if el.tag == "representation":
|
|
|
|
els = [el]
|
|
|
|
elif el.tag == "representation_and":
|
|
|
|
els = list(el)
|
|
|
|
else:
|
|
|
|
raise NotImplementedError("repr what?: {}".format(el.tag))
|
|
|
|
|
|
|
|
for el in els:
|
|
|
|
if el.get('basic') == 'form':
|
|
|
|
assert(forms[n] is None)
|
|
|
|
forms[n] = el
|
|
|
|
elif el.get('basic') == "agreement":
|
|
|
|
self.add_agreement(n, el)
|
|
|
|
else:
|
|
|
|
raise NotImplementedError("representation?: {}".format(el.tag))
|
|
|
|
|
|
|
|
def add_agreement(self, n, el):
|
|
|
|
assert(el.get('head')[:4] == 'cid_')
|
|
|
|
|
|
|
|
n1 = n
|
|
|
|
n2 = el.get('head')[4:]
|
|
|
|
agreement_str = next(iter(el)).get('agreement')
|
|
|
|
|
|
|
|
self.agreements.append({
|
|
|
|
'n1': n1,
|
|
|
|
'n2': n2,
|
|
|
|
'match': agreement_str.split('|')})
|
|
|
|
|
2018-10-29 10:29:51 +00:00
|
|
|
def __str__(self):
|
2019-01-19 21:42:51 +00:00
|
|
|
comp_str = "\n".join(str(comp) for comp in self.components)
|
|
|
|
|
|
|
|
agrs = "\n".join("({} -[{}]- {}) ".format(
|
|
|
|
a['n1'], "|".join(a['match']), a['n2']) for a in self.agreements)
|
|
|
|
|
|
|
|
links_str = "\n".join(self.components[0].tree())
|
|
|
|
|
|
|
|
return "{} LBS {}\nCOMPONENTS\n{}\nAGREEMENTS\n{}\nLINKS\n{}\n{}".format(
|
|
|
|
self.id, self.lbs, comp_str, agrs, links_str, "-" * 40)
|
|
|
|
|
|
|
|
def get_component(self, idx):
|
|
|
|
for c in self.components:
|
|
|
|
if c.idx == idx:
|
|
|
|
return c
|
|
|
|
raise RuntimeError("Unknown component id: {}".format(idx))
|
|
|
|
|
|
|
|
def check_agreements(self, match):
|
|
|
|
for agr in self.agreements:
|
|
|
|
w1 = match[agr['n1']]
|
|
|
|
w2 = match[agr['n2']]
|
|
|
|
|
|
|
|
for agr_case in agr['match']:
|
|
|
|
t1 = w1.msd[0]
|
|
|
|
v1 = TAGSET[t1].index(agr_case)
|
|
|
|
assert(v1 >= 0)
|
|
|
|
# if none specified: nedolocnik, always agrees
|
|
|
|
if v1 + 1 >= len(w1.msd):
|
|
|
|
continue
|
|
|
|
# first is uppercase, not in TAGSET
|
|
|
|
m1 = w1.msd[v1 + 1]
|
|
|
|
|
|
|
|
# REPEAT (not DRY!)
|
|
|
|
t2 = w2.msd[0]
|
|
|
|
v2 = TAGSET[t2].index(agr_case)
|
|
|
|
assert(v2 >= 0)
|
|
|
|
if v2 + 1 >= len(w2.msd):
|
|
|
|
continue
|
|
|
|
m2 = w2.msd[v2 + 1]
|
|
|
|
|
|
|
|
# match!
|
|
|
|
if '-' not in [m1, m2] and m1 != m2:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def check_form(self, match):
|
|
|
|
for midx, w in match.items():
|
|
|
|
c = self.get_component(midx)
|
|
|
|
for key, value in c.selection.items():
|
|
|
|
t = w.msd[0]
|
|
|
|
v = TAGSET[t].index(key.lower())
|
|
|
|
f1 = w.msd[v + 1]
|
|
|
|
f2 = CODES[value]
|
|
|
|
|
|
|
|
if '-' not in [f1, f2] and f1 != f2:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
2018-10-29 10:29:51 +00:00
|
|
|
|
|
|
|
def match(self, word):
|
2019-01-19 21:42:51 +00:00
|
|
|
matches = self.components[0].match(word)
|
|
|
|
if matches is None:
|
|
|
|
return []
|
|
|
|
|
|
|
|
to_ret = []
|
|
|
|
for m in matches:
|
|
|
|
if not self.check_agreements(m):
|
|
|
|
bad = "Agreement"
|
|
|
|
elif not self.check_form(m):
|
|
|
|
bad = "Form"
|
|
|
|
else:
|
|
|
|
bad = "OK"
|
|
|
|
|
|
|
|
to_ret.append((m, bad))
|
|
|
|
|
|
|
|
return to_ret
|
2018-10-29 10:29:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
def build_structures(filename):
|
|
|
|
structures = []
|
|
|
|
with open(filename, 'r') as fp:
|
|
|
|
et = ElementTree.XML(fp.read())
|
2018-10-29 11:16:42 +00:00
|
|
|
for structure in et.iter('syntactic_structure'):
|
2019-01-19 21:42:51 +00:00
|
|
|
to_append = SyntacticStructure.from_xml(structure)
|
|
|
|
if to_append is None:
|
|
|
|
continue
|
|
|
|
structures.append(to_append)
|
2018-10-29 10:29:51 +00:00
|
|
|
return structures
|
|
|
|
|
|
|
|
|
2019-01-19 21:42:51 +00:00
|
|
|
def get_msd(comp):
|
|
|
|
d = dict(comp.items())
|
|
|
|
if 'msd' in d:
|
|
|
|
return d['msd']
|
|
|
|
elif 'ana' in d:
|
|
|
|
return d['ana'][4:]
|
|
|
|
else:
|
|
|
|
logging.error(d, file=sys.stderr)
|
|
|
|
raise NotImplementedError("MSD?")
|
|
|
|
|
2018-10-29 10:29:51 +00:00
|
|
|
class Word:
|
|
|
|
def __init__(self, xml):
|
|
|
|
self.lemma = xml.get('lemma')
|
2019-01-19 21:42:51 +00:00
|
|
|
self.msd = MSD_TRANSLATE[get_msd(xml)]
|
2018-10-29 10:29:51 +00:00
|
|
|
self.id = xml.get('id')
|
|
|
|
self.text = xml.text
|
|
|
|
self.links = defaultdict(list)
|
|
|
|
|
|
|
|
assert(None not in (self.id, self.lemma, self.msd))
|
|
|
|
|
2019-01-25 10:58:40 +00:00
|
|
|
@staticmethod
|
|
|
|
def pcWord(pc):
|
|
|
|
pc.set('lemma', pc.text)
|
|
|
|
return Word(pc)
|
|
|
|
|
2018-10-29 10:29:51 +00:00
|
|
|
def add_link(self, link, to):
|
|
|
|
self.links[link].append(to)
|
|
|
|
|
|
|
|
def get_links(self, link):
|
2018-10-29 11:43:07 +00:00
|
|
|
if link not in self.links and "|" in link:
|
|
|
|
for l in link.split('|'):
|
|
|
|
self.links[link].extend(self.links[l])
|
|
|
|
|
2018-10-29 10:29:51 +00:00
|
|
|
return self.links[link]
|
|
|
|
|
|
|
|
|
2019-01-19 21:42:51 +00:00
|
|
|
def is_root_id(id_):
|
|
|
|
return len(id_.split('.')) == 3
|
|
|
|
|
|
|
|
|
2018-10-29 10:29:51 +00:00
|
|
|
def load_corpus(filename):
|
|
|
|
with open(filename, 'r') as fp:
|
|
|
|
xmlstring = re.sub(' xmlns="[^"]+"', '', fp.read(), count=1)
|
|
|
|
xmlstring = xmlstring.replace(' xml:', ' ')
|
|
|
|
et = ElementTree.XML(xmlstring)
|
|
|
|
|
|
|
|
words = {}
|
|
|
|
for w in et.iter("w"):
|
|
|
|
words[w.get('id')] = Word(w)
|
2019-01-19 21:42:51 +00:00
|
|
|
for pc in et.iter("pc"):
|
2019-01-25 10:58:40 +00:00
|
|
|
words[pc.get('id')] = Word.pcWord(pc)
|
2019-01-19 21:42:51 +00:00
|
|
|
|
2018-10-29 10:29:51 +00:00
|
|
|
for l in et.iter("link"):
|
2019-01-19 21:42:51 +00:00
|
|
|
if 'dep' in l.keys():
|
|
|
|
ana = l.get('afun')
|
|
|
|
lfrom = l.get('from')
|
|
|
|
dest = l.get('dep')
|
|
|
|
else:
|
|
|
|
ana = l.get('ana')
|
|
|
|
if ana[:4] != 'syn:': # dont bother...
|
|
|
|
continue
|
|
|
|
ana = ana[4:]
|
|
|
|
lfrom, dest = l.get('target').replace('#', '').split()
|
2018-10-29 10:29:51 +00:00
|
|
|
|
|
|
|
if lfrom in words:
|
2019-01-19 21:42:51 +00:00
|
|
|
if is_root_id(lfrom):
|
2019-01-25 10:58:40 +00:00
|
|
|
logging.error("NOO: ", lfrom)
|
2019-01-19 21:42:51 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
if dest in words:
|
|
|
|
next_word = words[dest]
|
|
|
|
words[lfrom].add_link(ana, next_word)
|
2019-01-25 10:58:40 +00:00
|
|
|
else:
|
|
|
|
logging.error("Unknown id: {}".format(dest))
|
|
|
|
sys.exit(1)
|
2019-01-08 20:17:15 +00:00
|
|
|
|
2019-01-08 18:37:28 +00:00
|
|
|
else:
|
|
|
|
# strange errors, just skip...
|
|
|
|
pass
|
2019-01-08 20:17:15 +00:00
|
|
|
|
2018-10-29 10:29:51 +00:00
|
|
|
return list(words.values())
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
import time
|
|
|
|
t = time.time()
|
|
|
|
|
|
|
|
structures = build_structures(STRUKTURE)
|
|
|
|
for s in structures:
|
|
|
|
print(s)
|
|
|
|
|
2019-01-19 21:42:51 +00:00
|
|
|
# words = load_corpus(STAVKI)
|
|
|
|
import pickle
|
|
|
|
# with open("words.p", "wb") as fp:
|
|
|
|
# pickle.dump(words, fp)
|
|
|
|
with open("words.p", "rb") as fp:
|
|
|
|
words = pickle.load(fp)
|
|
|
|
|
|
|
|
print("MATCHES...")
|
|
|
|
matches = {s.id: [] for s in structures}
|
|
|
|
|
|
|
|
for idx, s in enumerate(structures):
|
|
|
|
print("\r{}/{}: {:7s}".format(idx, len(structures), s.id)) #, end="")
|
|
|
|
for w in words:
|
|
|
|
mhere = s.match(w)
|
|
|
|
logging.debug(" GOT: {}".format(len(mhere)))
|
|
|
|
for match, reason in mhere:
|
|
|
|
matches[s.id].append((match, reason))
|
2019-01-25 10:58:40 +00:00
|
|
|
|
2019-01-19 21:42:51 +00:00
|
|
|
print("")
|
|
|
|
|
2019-01-25 10:58:40 +00:00
|
|
|
header = ["Structure_ID"]
|
|
|
|
for i in range(MAX_NUM_COMPONENTS):
|
|
|
|
header.extend("C{}_{}".format(i + 1, thd) for thd in
|
|
|
|
["Token_ID", "Word_form", "Lemma", "Msd", "Representative_form"])
|
|
|
|
header.extend(["Collocation_ID", "Joint_representative_form"])
|
2019-01-19 21:42:51 +00:00
|
|
|
|
2019-01-25 10:58:40 +00:00
|
|
|
csv = [", ".join(header)]
|
2019-01-19 21:42:51 +00:00
|
|
|
colocation_ids = {}
|
2018-10-29 10:29:51 +00:00
|
|
|
|
2019-01-19 21:42:51 +00:00
|
|
|
for s in structures:
|
|
|
|
ms = matches[s.id]
|
2018-10-29 10:29:51 +00:00
|
|
|
|
2019-01-19 21:42:51 +00:00
|
|
|
for m, reason in ms:
|
|
|
|
colocation_id = [s.id]
|
2019-01-25 10:58:40 +00:00
|
|
|
to_print = []
|
2018-10-29 10:29:51 +00:00
|
|
|
|
2019-01-19 21:42:51 +00:00
|
|
|
m_sorted = defaultdict(lambda: None, m.items())
|
|
|
|
for idx, comp in enumerate(s.components):
|
|
|
|
idx = str(idx + 1)
|
|
|
|
if idx not in m_sorted:
|
2019-01-25 10:58:40 +00:00
|
|
|
to_print.extend(["", "", "", "", ""])
|
2019-01-19 21:42:51 +00:00
|
|
|
else:
|
|
|
|
w = m_sorted[idx]
|
|
|
|
# if comp.render_word(m_sorted[idx]) is not None:
|
|
|
|
if True:
|
2019-01-25 10:58:40 +00:00
|
|
|
to_print.extend([w.id, w.text, w.lemma, w.msd, ""])
|
2019-01-19 21:42:51 +00:00
|
|
|
colocation_id.append(w.lemma)
|
|
|
|
|
|
|
|
colocation_id = tuple(colocation_id)
|
|
|
|
if colocation_id in colocation_ids:
|
|
|
|
cid = colocation_ids[colocation_id]
|
|
|
|
else:
|
2019-01-25 10:58:40 +00:00
|
|
|
cid = len(colocation_ids) + 1
|
2019-01-19 21:42:51 +00:00
|
|
|
colocation_ids[colocation_id] = cid
|
2019-01-25 10:58:40 +00:00
|
|
|
|
|
|
|
to_print = [s.id] + to_print
|
|
|
|
length = 1 + MAX_NUM_COMPONENTS * 5
|
|
|
|
# make them equal size
|
|
|
|
to_print.extend([""] * (length - len(to_print)))
|
2019-01-19 21:42:51 +00:00
|
|
|
to_print.extend([str(cid), ""])
|
2019-01-25 10:58:40 +00:00
|
|
|
|
2019-01-19 21:42:51 +00:00
|
|
|
csv.append(", ".join(to_print))
|
|
|
|
|
|
|
|
|
|
|
|
with open(FILE_OUT, "w") as fp:
|
|
|
|
print("\n".join(csv), file=fp)
|
2018-10-29 10:29:51 +00:00
|
|
|
|
2019-01-19 21:42:51 +00:00
|
|
|
# groups = defaultdict(int)
|
|
|
|
# for m, reason in ms:
|
|
|
|
# if reason != "OK":
|
|
|
|
# continue
|
|
|
|
# lemmas = [(n, w.lemma) for n, w in m.items()]
|
|
|
|
# lemmas = tuple(sorted(lemmas, key=lambda x: x[0]))
|
|
|
|
# groups[lemmas] += 1
|
2018-10-29 10:29:51 +00:00
|
|
|
|
2019-01-19 21:42:51 +00:00
|
|
|
# print(s.id)
|
|
|
|
# print(groups)
|
2018-10-29 10:29:51 +00:00
|
|
|
|
|
|
|
|
2019-01-19 21:42:51 +00:00
|
|
|
print("")
|
|
|
|
print("TIME", time.time() - t)
|
|
|
|
print([(k, len(v)) for k, v in matches.items()])
|
|
|
|
print(sum(len(v) for _, v in matches.items()))
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
main()
|
|
|
|
|
|
|
|
# 6, 7 primeri laznih zadetkov?
|