fix problem from claude
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
from functools import lru_cache
|
||||
|
||||
import pymorphy3
|
||||
|
||||
|
||||
@@ -5,11 +7,21 @@ class Lemmatizer:
|
||||
|
||||
def __init__(self):
|
||||
self.morph = pymorphy3.MorphAnalyzer()
|
||||
# Разбор pymorphy — самое дорогое в детекторе, а слова повторяются
|
||||
self._normalize_cached = lru_cache(maxsize=50_000)(self._normalize)
|
||||
self._is_known_cached = lru_cache(maxsize=50_000)(self.morph.word_is_known)
|
||||
|
||||
def is_known(self, word: str) -> bool:
|
||||
"""Есть ли слово в словаре pymorphy (обычное слово, а не сленг/опечатка)."""
|
||||
return self._is_known_cached(word)
|
||||
|
||||
def _normalize(self, word: str) -> str:
|
||||
result = self.morph.parse(word)
|
||||
|
||||
return result[0].normal_form
|
||||
|
||||
def normalize(
|
||||
self,
|
||||
word: str
|
||||
) -> str:
|
||||
result = self.morph.parse(word)
|
||||
|
||||
return result[0].normal_form
|
||||
return self._normalize_cached(word)
|
||||
|
||||
Reference in New Issue
Block a user