Skip to content

Dictionary Factory

This module defines the DictionaryFactory protocol and the DefaultDictionaryFactory class. It provides functionality for loading and accessing dictionaries for supported languages.

  • DictionaryFactory: The Protocol class for all dictionary factories.
  • DefaultDictionaryFactory: Default dictionary factory. It loads the dictionaries that are shipped with simplemma and caches them as configured.

Classes

DefaultDictionaryFactory

Bases: DictionaryFactory

Default Dictionary Factory.

This class is a concrete implementation of the DictionaryFactory protocol. It provides functionality for loading and caching dictionaries from disk that are included in Simplemma.

Source code in simplemma/strategies/dictionaries/dictionary_factory.py
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
class DefaultDictionaryFactory(DictionaryFactory):
    """
    Default Dictionary Factory.

    This class is a concrete implementation of the `DictionaryFactory` protocol.
    It provides functionality for loading and caching dictionaries from disk that are included in Simplemma.
    """

    __slots__ = ["_load_dictionary_from_disk"]

    def __init__(self, cache_max_size: int = 8) -> None:
        """
        Initialize the DefaultDictionaryFactory.

        Args:
            cache_max_size (int): The maximum size of the cache for loaded dictionaries.
                Defaults to `8`.
        """
        self._load_dictionary_from_disk = lru_cache(maxsize=cache_max_size)(
            _load_dictionary_from_disk
        )

    def get_dictionary(
        self,
        lang: str,
    ) -> Mapping[str, str]:
        """
        Get the dictionary for a specific language.

        Args:
            lang (str): The language code.

        Returns:
            Mapping[str, str]: The dictionary for the specified language.

        Raises:
            ValueError: If the specified language is not supported.
        """
        if lang not in SUPPORTED_LANGUAGES:
            raise ValueError(f"Unsupported language: {lang}")
        return MappingStrToByteString(self._load_dictionary_from_disk(lang))

Functions

__init__(cache_max_size=8)

Initialize the DefaultDictionaryFactory.

Parameters:

Name Type Description Default
cache_max_size int

The maximum size of the cache for loaded dictionaries. Defaults to 8.

8
Source code in simplemma/strategies/dictionaries/dictionary_factory.py
110
111
112
113
114
115
116
117
118
119
120
def __init__(self, cache_max_size: int = 8) -> None:
    """
    Initialize the DefaultDictionaryFactory.

    Args:
        cache_max_size (int): The maximum size of the cache for loaded dictionaries.
            Defaults to `8`.
    """
    self._load_dictionary_from_disk = lru_cache(maxsize=cache_max_size)(
        _load_dictionary_from_disk
    )
get_dictionary(lang)

Get the dictionary for a specific language.

Parameters:

Name Type Description Default
lang str

The language code.

required

Returns:

Type Description
Mapping[str, str]

Mapping[str, str]: The dictionary for the specified language.

Raises:

Type Description
ValueError

If the specified language is not supported.

Source code in simplemma/strategies/dictionaries/dictionary_factory.py
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
def get_dictionary(
    self,
    lang: str,
) -> Mapping[str, str]:
    """
    Get the dictionary for a specific language.

    Args:
        lang (str): The language code.

    Returns:
        Mapping[str, str]: The dictionary for the specified language.

    Raises:
        ValueError: If the specified language is not supported.
    """
    if lang not in SUPPORTED_LANGUAGES:
        raise ValueError(f"Unsupported language: {lang}")
    return MappingStrToByteString(self._load_dictionary_from_disk(lang))

DictionaryFactory

Bases: Protocol

This protocol defines the interface for a dictionary factory, which is responsible for loading and providing access to dictionaries for different languages.

Note

This protocol should be implemented by concrete dictionary factories. Concrete implementations of this protocol should provide a concrete implementation for the get_dictionary method.

Source code in simplemma/strategies/dictionaries/dictionary_factory.py
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
class DictionaryFactory(Protocol):
    """
    This protocol defines the interface for a dictionary factory, which is responsible for loading and providing access to dictionaries for different languages.

    Note:
        This protocol should be implemented by concrete dictionary factories.
        Concrete implementations of this protocol should provide a concrete implementation for the `get_dictionary` method.
    """

    @abstractmethod
    def get_dictionary(
        self,
        lang: str,
    ) -> Mapping[str, str]:
        """
        Get the dictionary for a specific language.

        Args:
            lang (str): The language code.

        Returns:
            Mapping[str, str]: The dictionary for the specified language.

        Raises:
            ValueError: If the specified language is not supported.
        """
        raise NotImplementedError

Functions

get_dictionary(lang) abstractmethod

Get the dictionary for a specific language.

Parameters:

Name Type Description Default
lang str

The language code.

required

Returns:

Type Description
Mapping[str, str]

Mapping[str, str]: The dictionary for the specified language.

Raises:

Type Description
ValueError

If the specified language is not supported.

Source code in simplemma/strategies/dictionaries/dictionary_factory.py
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
@abstractmethod
def get_dictionary(
    self,
    lang: str,
) -> Mapping[str, str]:
    """
    Get the dictionary for a specific language.

    Args:
        lang (str): The language code.

    Returns:
        Mapping[str, str]: The dictionary for the specified language.

    Raises:
        ValueError: If the specified language is not supported.
    """
    raise NotImplementedError

MappingStrToByteString

Bases: Mapping[str, str]

Wrapper around ByString dict to make them behave like str dict.

Source code in simplemma/strategies/dictionaries/dictionary_factory.py
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
class MappingStrToByteString(Mapping[str, str]):
    """Wrapper around ByString dict to make them behave like str dict."""

    __slots__ = ["_dict"]

    def __init__(self, dictionary: Dict[bytes, bytes]) -> None:
        self._dict = dictionary

    def __getitem__(self, item: str) -> str:
        return self._dict[item.encode()].decode()

    def __iter__(self) -> Iterator[str]:
        for key in self._dict:
            yield key.decode()

    def __len__(self) -> int:
        return len(self._dict)