Nasty.Translation.LexiconLoader (Nasty v0.3.0)

View Source

Loads 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

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

loaded?(source_lang, target_lang)

@spec loaded?(atom(), atom()) :: boolean()

Checks if a lexicon for a language pair is loaded.

Examples

iex> LexiconLoader.loaded?(:en, :es)
true

iex> LexiconLoader.loaded?(:en, :fr)
false

lookup(word, source_lang, target_lang)

@spec lookup(String.t(), atom(), atom()) :: {:ok, term()} | :not_found

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

reload()

@spec reload() :: :ok

Reloads all lexicons from disk.

Useful during development or if lexicons are updated.

start_link(opts \\ [])

Starts the Lexicon Loader GenServer.

stats()

@spec stats() :: map()

Returns statistics about loaded lexicons.

Examples

iex> LexiconLoader.stats()
%{
  en_es: %{entries: 308, loaded: true},
  es_en: %{entries: 352, loaded: true},
  ...
}