Skip to content

Commit

Permalink
Global cache for each view
Browse files Browse the repository at this point in the history
  • Loading branch information
xfenix committed Jun 15, 2022
1 parent 75ab49c commit ecf55f0
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions whole_app/spell.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@
from .settings import SETTINGS


@functools.lru_cache(maxsize=SETTINGS.cache_size)
def _made_suggestions(engine: enchant.Dict, ready_word: str) -> list[str] | None:
# skip to short words
if len(ready_word) < SETTINGS.minimum_length_for_correction:
return None
# skip correct words
if engine.check(ready_word):
return None
return engine.suggest(ready_word)


class SpellCheckService:
"""Spellcheck service class."""

Expand All @@ -24,24 +35,14 @@ def prepare(self) -> "SpellCheckService":
self._spellcheck_engine: enchant.Dict = enchant.Dict(self._language)
return self

@functools.lru_cache(maxsize=SETTINGS.cache_size)
def _made_suggestions(self, ready_word: str) -> list[str] | None:
# skip to short words
if len(ready_word) < SETTINGS.minimum_length_for_correction:
return None
# skip correct words
if self._spellcheck_engine.check(ready_word):
return None
return self._spellcheck_engine.suggest(ready_word)

def _make_one_correction_and_append_to_output(self, index: int, one_word_buf: list[str]) -> None:
ready_word: str = "".join(one_word_buf)
possible_candidates: list[str] | None = self._made_suggestions(ready_word)
possible_candidates: list[str] | None = _made_suggestions(self._spellcheck_engine, ready_word)
if not possible_candidates:
return
self._user_corrections.append(
models.OneCorrection(
first_position=index - len(one_word_buf),
first_position=index - len(ready_word),
last_position=index - 1,
word=ready_word,
suggestions=possible_candidates[: SETTINGS.max_suggestions]
Expand Down

0 comments on commit ecf55f0

Please sign in to comment.