Last active
May 26, 2022 11:26
-
-
Save dayyass/1127652b1a3db629b0badff5f81d6117 to your computer and use it in GitHub Desktop.
Pymorphy2 lemmatizer class.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pymorphy2 | |
class Lemmatizer: | |
""" | |
Pymorphy2 lemmatizer class. | |
""" | |
def __init__(self): | |
""" | |
Init lemmatizer. | |
""" | |
self.morph = pymorphy2.MorphAnalyzer() | |
def __call__(self, word: str) -> str: | |
""" | |
Lemmatize word. | |
""" | |
lemma = self.morph.parse(word)[0].normal_form | |
return lemma | |
class LemmatizerCached: | |
""" | |
Pymorphy2 lemmatizer class with caching. | |
""" | |
def __init__(self): | |
""" | |
Init lemmatizer. | |
""" | |
self.cache = {} | |
self.morph = pymorphy2.MorphAnalyzer() | |
def __call__(self, word: str) -> str: | |
""" | |
Lemmatize word. | |
""" | |
if word in self.cache: | |
lemma = self.cache[word] | |
else: | |
lemma = self.morph.parse(word)[0].normal_form | |
self.cache[word] = lemma | |
return lemma | |
lemmatizer = Lemmatizer() | |
lemmatizer_cached = LemmatizerCached() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment