2020-07-22 11:42:59 +00:00
|
|
|
from typing import Optional, List, Dict
|
|
|
|
|
2020-05-14 16:23:19 +00:00
|
|
|
from ...lemmatizer import Lemmatizer
|
|
|
|
from ...parts_of_speech import NAMES
|
|
|
|
|
|
|
|
|
|
|
|
class PolishLemmatizer(Lemmatizer):
|
2020-05-26 07:56:12 +00:00
|
|
|
# This lemmatizer implements lookup lemmatization based on the Morfeusz
|
|
|
|
# dictionary (morfeusz.sgjp.pl/en) by Institute of Computer Science PAS.
|
|
|
|
# It utilizes some prefix based improvements for verb and adjectives
|
|
|
|
# lemmatization, as well as case-sensitive lemmatization for nouns.
|
2020-07-22 11:42:59 +00:00
|
|
|
def __call__(
|
|
|
|
self, string: str, univ_pos: str, morphology: Optional[dict] = None
|
|
|
|
) -> List[str]:
|
2020-05-14 16:23:19 +00:00
|
|
|
if isinstance(univ_pos, int):
|
|
|
|
univ_pos = NAMES.get(univ_pos, "X")
|
|
|
|
univ_pos = univ_pos.upper()
|
2020-05-26 07:56:12 +00:00
|
|
|
lookup_pos = univ_pos.lower()
|
|
|
|
if univ_pos == "PROPN":
|
|
|
|
lookup_pos = "noun"
|
|
|
|
lookup_table = self.lookups.get_table("lemma_lookup_" + lookup_pos, {})
|
2020-05-14 16:23:19 +00:00
|
|
|
if univ_pos == "NOUN":
|
2020-05-26 07:56:12 +00:00
|
|
|
return self.lemmatize_noun(string, morphology, lookup_table)
|
2020-05-14 16:23:19 +00:00
|
|
|
if univ_pos != "PROPN":
|
|
|
|
string = string.lower()
|
|
|
|
if univ_pos == "ADJ":
|
2020-05-26 07:56:12 +00:00
|
|
|
return self.lemmatize_adj(string, morphology, lookup_table)
|
2020-05-14 16:23:19 +00:00
|
|
|
elif univ_pos == "VERB":
|
2020-05-26 07:56:12 +00:00
|
|
|
return self.lemmatize_verb(string, morphology, lookup_table)
|
|
|
|
return [lookup_table.get(string, string.lower())]
|
2020-05-14 16:23:19 +00:00
|
|
|
|
2020-07-22 11:42:59 +00:00
|
|
|
def lemmatize_adj(
|
|
|
|
self, string: str, morphology: dict, lookup_table: Dict[str, str]
|
|
|
|
) -> List[str]:
|
2020-05-14 16:23:19 +00:00
|
|
|
# this method utilizes different procedures for adjectives
|
|
|
|
# with 'nie' and 'naj' prefixes
|
|
|
|
if string[:3] == "nie":
|
|
|
|
search_string = string[3:]
|
|
|
|
if search_string[:3] == "naj":
|
|
|
|
naj_search_string = search_string[3:]
|
2020-05-26 07:56:12 +00:00
|
|
|
if naj_search_string in lookup_table:
|
|
|
|
return [lookup_table[naj_search_string]]
|
|
|
|
if search_string in lookup_table:
|
|
|
|
return [lookup_table[search_string]]
|
2020-05-14 16:23:19 +00:00
|
|
|
if string[:3] == "naj":
|
|
|
|
naj_search_string = string[3:]
|
2020-05-26 07:56:12 +00:00
|
|
|
if naj_search_string in lookup_table:
|
|
|
|
return [lookup_table[naj_search_string]]
|
|
|
|
return [lookup_table.get(string, string)]
|
2020-05-14 16:23:19 +00:00
|
|
|
|
2020-07-22 11:42:59 +00:00
|
|
|
def lemmatize_verb(
|
|
|
|
self, string: str, morphology: dict, lookup_table: Dict[str, str]
|
|
|
|
) -> List[str]:
|
2020-05-14 16:23:19 +00:00
|
|
|
# this method utilizes a different procedure for verbs
|
|
|
|
# with 'nie' prefix
|
|
|
|
if string[:3] == "nie":
|
|
|
|
search_string = string[3:]
|
2020-05-26 07:56:12 +00:00
|
|
|
if search_string in lookup_table:
|
|
|
|
return [lookup_table[search_string]]
|
|
|
|
return [lookup_table.get(string, string)]
|
2020-05-14 16:23:19 +00:00
|
|
|
|
2020-07-22 11:42:59 +00:00
|
|
|
def lemmatize_noun(
|
|
|
|
self, string: str, morphology: dict, lookup_table: Dict[str, str]
|
|
|
|
) -> List[str]:
|
2020-05-14 16:23:19 +00:00
|
|
|
# this method is case-sensitive, in order to work
|
|
|
|
# for incorrectly tagged proper names
|
|
|
|
if string != string.lower():
|
2020-05-26 07:56:12 +00:00
|
|
|
if string.lower() in lookup_table:
|
|
|
|
return [lookup_table[string.lower()]]
|
|
|
|
elif string in lookup_table:
|
|
|
|
return [lookup_table[string]]
|
2020-05-14 16:23:19 +00:00
|
|
|
return [string.lower()]
|
2020-05-26 07:56:12 +00:00
|
|
|
return [lookup_table.get(string, string)]
|
2020-05-14 16:23:19 +00:00
|
|
|
|
2020-07-22 11:42:59 +00:00
|
|
|
def lookup(self, string: str, orth: Optional[int] = None) -> str:
|
2020-05-14 16:23:19 +00:00
|
|
|
return string.lower()
|
|
|
|
|
2020-07-22 11:42:59 +00:00
|
|
|
def lemmatize(
|
|
|
|
self,
|
|
|
|
string: str,
|
|
|
|
index: Dict[str, List[str]],
|
|
|
|
exceptions: Dict[str, Dict[str, List[str]]],
|
|
|
|
rules: Dict[str, List[List[str]]],
|
|
|
|
) -> List[str]:
|
2020-05-14 16:23:19 +00:00
|
|
|
raise NotImplementedError
|