Nasty.Language.Resources.LexiconLoader (Nasty v0.3.0)

View Source

Loads 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

in_lexicon?(language, lexicon_name, word)

@spec in_lexicon?(atom(), atom(), String.t()) :: boolean()

Checks if a word is in the specified lexicon.

Parameters

  • language - Language code
  • lexicon_name - Name of the lexicon
  • word - Word to check (case-sensitive)

Returns

true if word is in lexicon, false otherwise.

lexicon_path(language, lexicon_name)

@spec lexicon_path(atom(), atom()) :: String.t()

Returns the full path to a lexicon file.

Parameters

  • language - Language code
  • lexicon_name - Name of the lexicon

Returns

Absolute path to the lexicon file.

list_lexicons(language)

@spec list_lexicons(atom()) :: [atom()]

Lists all available lexicons for a language.

Parameters

  • language - Language code

Returns

List of lexicon names (as atoms) available for the language.

load(language, lexicon_name)

@spec load(atom(), atom()) :: [String.t()]

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", ...]

preload_lexicons(language, lexicon_names)

(macro)

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