-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/better dictionaries #135
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
120 changes: 120 additions & 0 deletions
120
simplemma/strategies/dictionaries/trie_dictionary_factory.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
from functools import lru_cache | ||
from pathlib import Path | ||
import tempfile | ||
from typing import ByteString, Dict, Mapping, Optional | ||
|
||
from marisa_trie import RecordTrie, HUGE_CACHE # type: ignore[import-not-found] | ||
|
||
from simplemma import __version__ as SIMPLEMMA_VERSION | ||
from simplemma.strategies.dictionaries.dictionary_factory import ( | ||
SUPPORTED_LANGUAGES, | ||
DictionaryFactory, | ||
DefaultDictionaryFactory, | ||
) | ||
|
||
|
||
class TrieWrapDict(Mapping[str, str]): | ||
"""Wrapper around RecordTrie to make them behave like dicts.""" | ||
|
||
__slots__ = ["_trie"] | ||
|
||
def __init__(self, trie: RecordTrie): | ||
self._trie = trie | ||
|
||
def __getitem__(self, item: str) -> str: | ||
return self._trie[item][0] | ||
|
||
def __iter__(self): | ||
for key in self._trie.iterkeys(): | ||
yield key | ||
|
||
def __len__(self): | ||
return len(self._trie) | ||
|
||
|
||
class TrieDictionaryFactory(DictionaryFactory): | ||
"""Memory optimized DictionaryFactory backed by MARISA-tries. | ||
|
||
This dictionary factory creates dictionaries, which are backed by a | ||
MARISA-trie instead of a dict, to make them consume very little | ||
memory compared to the DefaultDictionaryFactory. Trade-offs are that | ||
lookup performance isn't as good as with dicts. | ||
""" | ||
|
||
__slots__ = ["_cache_dir", "_defaultDictionaryFactory", "_get_dictionary"] | ||
|
||
def __init__( | ||
self, | ||
cache_max_size: int = 8, | ||
use_disk_cache: bool = True, | ||
disk_cache_dir: Optional[str] = None, | ||
) -> None: | ||
"""Initialize the TrieDictionaryFactory. | ||
|
||
Args: | ||
cache_max_size (int): The maximum number dictionaries to | ||
keep in memory. Defaults to `8`. | ||
use_disk_cache (bool): Whether to cache the tries on disk to | ||
speed up loading time. Defaults to `True`. | ||
disk_cache_dir (Optional[str]): Path where the generated | ||
tries should be stored in. Defaults to a Simplemma- | ||
specific subdirectory of the user's cache directory. | ||
""" | ||
|
||
self._defaultDictionaryFactory = DefaultDictionaryFactory(cache_max_size=0) | ||
if use_disk_cache: | ||
self._cache_dir: Optional[Path] = ( | ||
Path(disk_cache_dir) | ||
if disk_cache_dir is not None | ||
else (Path(tempfile.gettempdir()) / "simplemma" / SIMPLEMMA_VERSION) | ||
) | ||
else: | ||
self._cache_dir = None | ||
|
||
self._get_dictionary = lru_cache(maxsize=cache_max_size)( | ||
self._get_dictionary_uncached | ||
) | ||
|
||
def _try_read_trie_from_disk(self, lang: str) -> bool: | ||
"""Check if a trie for the given language is available on disk.""" | ||
if self._cache_dir is None: | ||
return False | ||
try: | ||
return RecordTrie().load(str(self._cache_dir / f"{lang}.pkl")) | ||
except FileNotFoundError: | ||
return False | ||
|
||
def _write_trie_to_disk(self, lang: str, trie: RecordTrie) -> None: | ||
"""Persist the trie to disk for later usage. | ||
|
||
The persisted trie can be loaded by subsequent runs to speed up | ||
loading times. | ||
""" | ||
if self._cache_dir is None: | ||
return | ||
|
||
trie.save(self._cache_dir / f"{lang}.pkl") | ||
|
||
def _get_dictionary_uncached(self, lang: str) -> Mapping[str, str]: | ||
"""Get the dictionary for the given language.""" | ||
if lang not in SUPPORTED_LANGUAGES: | ||
raise ValueError(f"Unsupported language: {lang}") | ||
|
||
if self._cache_dir: | ||
trie = RecordTrie().load(str(self._cache_dir / f"{lang}.pkl")) | ||
|
||
if trie is None: | ||
trie = RecordTrie( | ||
self._defaultDictionaryFactory.get_dictionary(lang).items(), | ||
cache_size=HUGE_CACHE, | ||
) | ||
if self._cache_dir: | ||
self._write_trie_to_disk(lang, trie) | ||
|
||
return TrieWrapDict(trie) | ||
|
||
def get_dictionary( | ||
self, | ||
lang: str, | ||
) -> Mapping[str, str]: | ||
return self._get_dictionary(lang) |
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Unused import Note