Nasty.Language.Resources.LexiconLoader (Nasty v0.3.0)
View SourceLoads and caches lexicon files from the priv/languages directory.
Lexicons are loaded at compile time and cached as module attributes for fast runtime access.
File Format
Lexicon files should be Elixir term files (.exs) that evaluate to a list of strings:
# Example: priv/languages/english/lexicons/determiners.exs
~w(the a an this that these those)Usage
# Get a lexicon
determiners = LexiconLoader.load(:en, :determiners)
# Check if word is in lexicon
LexiconLoader.in_lexicon?(:en, :determiners, "the") # => true
Summary
Functions
Checks if a word is in the specified lexicon.
Returns the full path to a lexicon file.
Lists all available lexicons for a language.
Loads a lexicon for the given language and name.
Preloads all lexicons for a language at compile time.
Functions
Checks if a word is in the specified lexicon.
Parameters
language- Language codelexicon_name- Name of the lexiconword- Word to check (case-sensitive)
Returns
true if word is in lexicon, false otherwise.
Returns the full path to a lexicon file.
Parameters
language- Language codelexicon_name- Name of the lexicon
Returns
Absolute path to the lexicon file.
Lists all available lexicons for a language.
Parameters
language- Language code
Returns
List of lexicon names (as atoms) available for the language.
Loads a lexicon for the given language and name.
Parameters
language- Language code (:en,:es,:ca, etc.)lexicon_name- Name of the lexicon (:determiners,:verbs, etc.)
Returns
List of words in the lexicon, or raises if file not found.
Examples
iex> LexiconLoader.load(:en, :determiners)
["the", "a", "an", ...]
Preloads all lexicons for a language at compile time.
This macro can be used in a module to preload lexicons as module attributes:
defmodule MyModule do
require Nasty.Language.Resources.LexiconLoader
LexiconLoader.preload_lexicons(:en, [:determiners, :verbs])
@determiners LexiconLoader.load(:en, :determiners)
@verbs LexiconLoader.load(:en, :verbs)
end