Skip to content

Instantly share code, notes, and snippets.

@dayyass
Last active May 26, 2022 11:26
Show Gist options
  • Save dayyass/1127652b1a3db629b0badff5f81d6117 to your computer and use it in GitHub Desktop.
Save dayyass/1127652b1a3db629b0badff5f81d6117 to your computer and use it in GitHub Desktop.
Pymorphy2 lemmatizer class.
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