Nasty.Translation.LexiconLoader (Nasty v0.3.0)
View SourceLoads and caches bilingual lexicons for translation.
Uses ETS (Erlang Term Storage) for fast in-memory lookups of word translations. Lexicons are loaded from .exs files in priv/translation/lexicons/.
Supported Language Pairs
- en_es (English → Spanish)
- es_en (Spanish → English)
- en_ca (English → Catalan)
- ca_en (Catalan → English)
- es_ca (Spanish → Catalan)
- ca_es (Catalan → Spanish)
Usage
# Start the loader (usually done by application supervisor)
LexiconLoader.start_link()
# Lookup a word
LexiconLoader.lookup("cat", :en, :es)
# => {:ok, %{translations: ["gato", "gata"], gender: :m}}
# Check if lexicon is loaded
LexiconLoader.loaded?(:en, :es)
# => true
Summary
Functions
Returns a specification to start this module under a supervisor.
Checks if a lexicon for a language pair is loaded.
Looks up a word translation in the lexicon.
Reloads all lexicons from disk.
Starts the Lexicon Loader GenServer.
Returns statistics about loaded lexicons.
Functions
Returns a specification to start this module under a supervisor.
See Supervisor.
Checks if a lexicon for a language pair is loaded.
Examples
iex> LexiconLoader.loaded?(:en, :es)
true
iex> LexiconLoader.loaded?(:en, :fr)
false
Looks up a word translation in the lexicon.
Returns {:ok, translation} if found, :not_found otherwise.
Examples
iex> LexiconLoader.lookup("cat", :en, :es)
{:ok, %{translations: ["gato", "gata"], gender: :m}}
iex> LexiconLoader.lookup("gato", :es, :en)
{:ok, %{base: "cat", type: :noun}}
iex> LexiconLoader.lookup("nonexistent", :en, :es)
:not_found
@spec reload() :: :ok
Reloads all lexicons from disk.
Useful during development or if lexicons are updated.
Starts the Lexicon Loader GenServer.
@spec stats() :: map()
Returns statistics about loaded lexicons.
Examples
iex> LexiconLoader.stats()
%{
en_es: %{entries: 308, loaded: true},
es_en: %{entries: 352, loaded: true},
...
}