2019-02-08 13:14:49 +00:00
|
|
|
# coding: utf8
|
2019-02-25 14:54:55 +00:00
|
|
|
from ...symbols import ADJ, DET, NOUN, NUM, PRON, PROPN, PUNCT, VERB, POS
|
2019-02-25 14:48:17 +00:00
|
|
|
from ...lemmatizer import Lemmatizer
|
2019-02-08 13:14:49 +00:00
|
|
|
|
2019-02-07 20:05:11 +00:00
|
|
|
|
2019-02-25 14:48:17 +00:00
|
|
|
class UkrainianLemmatizer(Lemmatizer):
|
|
|
|
_morph = None
|
2019-02-07 20:05:11 +00:00
|
|
|
|
2019-10-02 10:04:06 +00:00
|
|
|
def __init__(self, lookups=None):
|
|
|
|
super(UkrainianLemmatizer, self).__init__(lookups)
|
2019-02-07 20:05:11 +00:00
|
|
|
try:
|
2019-02-25 14:48:17 +00:00
|
|
|
from pymorphy2 import MorphAnalyzer
|
2019-02-25 14:54:55 +00:00
|
|
|
|
2019-02-25 14:48:17 +00:00
|
|
|
if UkrainianLemmatizer._morph is None:
|
2019-02-25 14:54:55 +00:00
|
|
|
UkrainianLemmatizer._morph = MorphAnalyzer(lang="uk")
|
2019-02-25 14:48:17 +00:00
|
|
|
except (ImportError, TypeError):
|
2019-02-07 20:05:11 +00:00
|
|
|
raise ImportError(
|
2019-02-27 15:37:03 +00:00
|
|
|
"The Ukrainian lemmatizer requires the pymorphy2 library and "
|
2019-02-25 14:54:55 +00:00
|
|
|
'dictionaries: try to fix it with "pip uninstall pymorphy2" and'
|
|
|
|
'"pip install git+https://github.com/kmike/pymorphy2.git pymorphy2-dicts-uk"'
|
2019-02-08 13:14:49 +00:00
|
|
|
)
|
2019-02-25 14:48:17 +00:00
|
|
|
|
|
|
|
def __call__(self, string, univ_pos, morphology=None):
|
|
|
|
univ_pos = self.normalize_univ_pos(univ_pos)
|
2019-02-25 14:54:55 +00:00
|
|
|
if univ_pos == "PUNCT":
|
2019-02-25 14:48:17 +00:00
|
|
|
return [PUNCT_RULES.get(string, string)]
|
|
|
|
|
2019-02-25 14:54:55 +00:00
|
|
|
if univ_pos not in ("ADJ", "DET", "NOUN", "NUM", "PRON", "PROPN", "VERB"):
|
2019-02-25 14:48:17 +00:00
|
|
|
# Skip unchangeable pos
|
|
|
|
return [string.lower()]
|
|
|
|
|
|
|
|
analyses = self._morph.parse(string)
|
|
|
|
filtered_analyses = []
|
|
|
|
for analysis in analyses:
|
|
|
|
if not analysis.is_known:
|
|
|
|
# Skip suggested parse variant for unknown word for pymorphy
|
|
|
|
continue
|
|
|
|
analysis_pos, _ = oc2ud(str(analysis.tag))
|
2019-02-25 14:54:55 +00:00
|
|
|
if analysis_pos == univ_pos or (
|
|
|
|
analysis_pos in ("NOUN", "PROPN") and univ_pos in ("NOUN", "PROPN")
|
|
|
|
):
|
2019-02-25 14:48:17 +00:00
|
|
|
filtered_analyses.append(analysis)
|
|
|
|
|
|
|
|
if not len(filtered_analyses):
|
|
|
|
return [string.lower()]
|
|
|
|
if morphology is None or (len(morphology) == 1 and POS in morphology):
|
|
|
|
return list(set([analysis.normal_form for analysis in filtered_analyses]))
|
|
|
|
|
2019-02-25 14:54:55 +00:00
|
|
|
if univ_pos in ("ADJ", "DET", "NOUN", "PROPN"):
|
|
|
|
features_to_compare = ["Case", "Number", "Gender"]
|
|
|
|
elif univ_pos == "NUM":
|
|
|
|
features_to_compare = ["Case", "Gender"]
|
|
|
|
elif univ_pos == "PRON":
|
|
|
|
features_to_compare = ["Case", "Number", "Gender", "Person"]
|
2019-02-25 14:48:17 +00:00
|
|
|
else: # VERB
|
2019-02-25 14:54:55 +00:00
|
|
|
features_to_compare = [
|
|
|
|
"Aspect",
|
|
|
|
"Gender",
|
|
|
|
"Mood",
|
|
|
|
"Number",
|
|
|
|
"Tense",
|
|
|
|
"VerbForm",
|
|
|
|
"Voice",
|
|
|
|
]
|
2019-02-25 14:48:17 +00:00
|
|
|
|
|
|
|
analyses, filtered_analyses = filtered_analyses, []
|
|
|
|
for analysis in analyses:
|
|
|
|
_, analysis_morph = oc2ud(str(analysis.tag))
|
|
|
|
for feature in features_to_compare:
|
2019-02-25 14:54:55 +00:00
|
|
|
if (
|
|
|
|
feature in morphology
|
|
|
|
and feature in analysis_morph
|
2019-09-27 16:03:26 +00:00
|
|
|
and morphology[feature].lower() != analysis_morph[feature].lower()
|
2019-02-25 14:54:55 +00:00
|
|
|
):
|
2019-02-25 14:48:17 +00:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
filtered_analyses.append(analysis)
|
|
|
|
|
|
|
|
if not len(filtered_analyses):
|
|
|
|
return [string.lower()]
|
|
|
|
return list(set([analysis.normal_form for analysis in filtered_analyses]))
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def normalize_univ_pos(univ_pos):
|
|
|
|
if isinstance(univ_pos, str):
|
|
|
|
return univ_pos.upper()
|
|
|
|
|
|
|
|
symbols_to_str = {
|
2019-02-25 14:54:55 +00:00
|
|
|
ADJ: "ADJ",
|
|
|
|
DET: "DET",
|
|
|
|
NOUN: "NOUN",
|
|
|
|
NUM: "NUM",
|
|
|
|
PRON: "PRON",
|
|
|
|
PROPN: "PROPN",
|
|
|
|
PUNCT: "PUNCT",
|
|
|
|
VERB: "VERB",
|
2019-02-25 14:48:17 +00:00
|
|
|
}
|
|
|
|
if univ_pos in symbols_to_str:
|
|
|
|
return symbols_to_str[univ_pos]
|
|
|
|
return None
|
|
|
|
|
2019-09-15 20:08:13 +00:00
|
|
|
def lookup(self, string, orth=None):
|
2019-02-25 14:48:17 +00:00
|
|
|
analyses = self._morph.parse(string)
|
|
|
|
if len(analyses) == 1:
|
|
|
|
return analyses[0].normal_form
|
|
|
|
return string
|
|
|
|
|
|
|
|
|
|
|
|
def oc2ud(oc_tag):
|
|
|
|
gram_map = {
|
2019-02-25 14:54:55 +00:00
|
|
|
"_POS": {
|
|
|
|
"ADJF": "ADJ",
|
|
|
|
"ADJS": "ADJ",
|
|
|
|
"ADVB": "ADV",
|
|
|
|
"Apro": "DET",
|
|
|
|
"COMP": "ADJ", # Can also be an ADV - unchangeable
|
|
|
|
"CONJ": "CCONJ", # Can also be a SCONJ - both unchangeable ones
|
|
|
|
"GRND": "VERB",
|
|
|
|
"INFN": "VERB",
|
|
|
|
"INTJ": "INTJ",
|
|
|
|
"NOUN": "NOUN",
|
|
|
|
"NPRO": "PRON",
|
|
|
|
"NUMR": "NUM",
|
|
|
|
"NUMB": "NUM",
|
|
|
|
"PNCT": "PUNCT",
|
|
|
|
"PRCL": "PART",
|
|
|
|
"PREP": "ADP",
|
|
|
|
"PRTF": "VERB",
|
|
|
|
"PRTS": "VERB",
|
|
|
|
"VERB": "VERB",
|
2019-02-25 14:48:17 +00:00
|
|
|
},
|
2019-02-25 14:54:55 +00:00
|
|
|
"Animacy": {"anim": "Anim", "inan": "Inan"},
|
|
|
|
"Aspect": {"impf": "Imp", "perf": "Perf"},
|
|
|
|
"Case": {
|
|
|
|
"ablt": "Ins",
|
|
|
|
"accs": "Acc",
|
|
|
|
"datv": "Dat",
|
|
|
|
"gen1": "Gen",
|
|
|
|
"gen2": "Gen",
|
|
|
|
"gent": "Gen",
|
|
|
|
"loc2": "Loc",
|
|
|
|
"loct": "Loc",
|
|
|
|
"nomn": "Nom",
|
|
|
|
"voct": "Voc",
|
2019-02-25 14:48:17 +00:00
|
|
|
},
|
2019-02-25 14:54:55 +00:00
|
|
|
"Degree": {"COMP": "Cmp", "Supr": "Sup"},
|
|
|
|
"Gender": {"femn": "Fem", "masc": "Masc", "neut": "Neut"},
|
|
|
|
"Mood": {"impr": "Imp", "indc": "Ind"},
|
|
|
|
"Number": {"plur": "Plur", "sing": "Sing"},
|
|
|
|
"NumForm": {"NUMB": "Digit"},
|
|
|
|
"Person": {"1per": "1", "2per": "2", "3per": "3", "excl": "2", "incl": "1"},
|
|
|
|
"Tense": {"futr": "Fut", "past": "Past", "pres": "Pres"},
|
|
|
|
"Variant": {"ADJS": "Brev", "PRTS": "Brev"},
|
|
|
|
"VerbForm": {
|
|
|
|
"GRND": "Conv",
|
|
|
|
"INFN": "Inf",
|
|
|
|
"PRTF": "Part",
|
|
|
|
"PRTS": "Part",
|
|
|
|
"VERB": "Fin",
|
2019-02-25 14:48:17 +00:00
|
|
|
},
|
2019-02-25 14:54:55 +00:00
|
|
|
"Voice": {"actv": "Act", "pssv": "Pass"},
|
|
|
|
"Abbr": {"Abbr": "Yes"},
|
2019-02-25 14:48:17 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 14:54:55 +00:00
|
|
|
pos = "X"
|
2019-02-25 14:48:17 +00:00
|
|
|
morphology = dict()
|
|
|
|
unmatched = set()
|
|
|
|
|
2019-02-25 14:54:55 +00:00
|
|
|
grams = oc_tag.replace(" ", ",").split(",")
|
2019-02-25 14:48:17 +00:00
|
|
|
for gram in grams:
|
|
|
|
match = False
|
|
|
|
for categ, gmap in sorted(gram_map.items()):
|
|
|
|
if gram in gmap:
|
|
|
|
match = True
|
2019-02-25 14:54:55 +00:00
|
|
|
if categ == "_POS":
|
2019-02-25 14:48:17 +00:00
|
|
|
pos = gmap[gram]
|
|
|
|
else:
|
|
|
|
morphology[categ] = gmap[gram]
|
|
|
|
if not match:
|
|
|
|
unmatched.add(gram)
|
|
|
|
|
|
|
|
while len(unmatched) > 0:
|
|
|
|
gram = unmatched.pop()
|
2019-02-25 14:54:55 +00:00
|
|
|
if gram in ("Name", "Patr", "Surn", "Geox", "Orgn"):
|
|
|
|
pos = "PROPN"
|
|
|
|
elif gram == "Auxt":
|
|
|
|
pos = "AUX"
|
|
|
|
elif gram == "Pltm":
|
|
|
|
morphology["Number"] = "Ptan"
|
2019-02-25 14:48:17 +00:00
|
|
|
|
|
|
|
return pos, morphology
|
|
|
|
|
|
|
|
|
2019-02-25 14:54:55 +00:00
|
|
|
PUNCT_RULES = {"«": '"', "»": '"'}
|