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
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
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
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__ = ["_data", "_load_dictionary_from_disk"]

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

        Args:
            cache_max_size (int): The maximum size of the cache for loaded dictionaries.
                Defaults to `8`.
        """
        self._data: Dict[str, Dict[str, str]] = {}
        self._load_dictionary_from_disk = lru_cache(maxsize=cache_max_size)(
            _load_dictionary_from_disk
        )

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

        Args:
            lang (str): The language code.

        Returns:
            Dict[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 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
 98
 99
100
101
102
103
104
105
106
107
108
109
def __init__(self, cache_max_size: int = 8):
    """
    Initialize the DefaultDictionaryFactory.

    Args:
        cache_max_size (int): The maximum size of the cache for loaded dictionaries.
            Defaults to `8`.
    """
    self._data: Dict[str, Dict[str, str]] = {}
    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
Dict[str, str]

Dict[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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
def get_dictionary(
    self,
    lang: str,
) -> Dict[str, str]:
    """
    Get the dictionary for a specific language.

    Args:
        lang (str): The language code.

    Returns:
        Dict[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 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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,
    ) -> Dict[str, str]:
        """
        Get the dictionary for a specific language.

        Args:
            lang (str): The language code.

        Returns:
            Dict[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
Dict[str, str]

Dict[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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
@abstractmethod
def get_dictionary(
    self,
    lang: str,
) -> Dict[str, str]:
    """
    Get the dictionary for a specific language.

    Args:
        lang (str): The language code.

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

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