Nasty.Language.Registry (Nasty v0.3.0)
View SourceRegistry for managing natural language implementations.
The registry maps language codes to their implementation modules and provides language detection and validation utilities.
Summary
Functions
Returns metadata for all registered languages.
Returns a specification to start this module under a supervisor.
Clears all registered languages.
Detects the language of the given text.
Gets the implementation module for a language code.
Gets the implementation module for a language code, raising on error.
Registers a language implementation module.
Checks if a language is registered.
Returns all registered language codes.
Starts the language registry.
Unregisters a language implementation.
Types
Functions
@spec all_metadata() :: %{required(language_code()) => map()}
Returns metadata for all registered languages.
Examples
iex> Nasty.Language.Registry.all_metadata()
%{
en: %{version: "1.0.0", features: [...]},
es: %{version: "1.0.0", features: [...]}
}
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec clear() :: :ok
Clears all registered languages.
Primarily for testing purposes.
Examples
iex> Nasty.Language.Registry.clear()
:ok
@spec detect_language(String.t()) :: {:ok, language_code()} | {:error, term()}
Detects the language of the given text.
Uses heuristics:
- Character set analysis (Latin, Cyrillic, Arabic, etc.)
- Common word frequency analysis
- Statistical language models
Returns the most likely language code from registered languages. If no registered language matches, returns {:error, :no_match}.
Examples
iex> Nasty.Language.Registry.detect_language("Hello world")
{:ok, :en}
iex> Nasty.Language.Registry.detect_language("你好世界")
{:error, :no_match}
@spec get(language_code()) :: {:ok, language_module()} | {:error, :language_not_found}
Gets the implementation module for a language code.
Examples
iex> Nasty.Language.Registry.get(:en)
{:ok, Nasty.Language.English}
iex> Nasty.Language.Registry.get(:fr)
{:error, :language_not_found}
@spec get!(language_code()) :: language_module() | no_return()
Gets the implementation module for a language code, raising on error.
Examples
iex> Nasty.Language.Registry.get!(:en)
Nasty.Language.English
iex> Nasty.Language.Registry.get!(:fr)
** (RuntimeError) Language not found: :fr
@spec register(language_module()) :: :ok | {:error, String.t()}
Registers a language implementation module.
Validates that the module implements the Language.Behaviour correctly before registration.
Examples
iex> Nasty.Language.Registry.register(Nasty.Language.English)
:ok
iex> Nasty.Language.Registry.register(InvalidModule)
{:error, "Module does not implement Nasty.Language.Behaviour"}
@spec registered?(language_code()) :: boolean()
Checks if a language is registered.
Examples
iex> Nasty.Language.Registry.registered?(:en)
true
iex> Nasty.Language.Registry.registered?(:fr)
false
@spec registered_languages() :: [language_code()]
Returns all registered language codes.
Examples
iex> Nasty.Language.Registry.registered_languages()
[:en, :es, :ca]
@spec start_link(keyword()) :: Agent.on_start()
Starts the language registry.
Automatically called when the application starts.
@spec unregister(language_code()) :: :ok
Unregisters a language implementation.
Examples
iex> Nasty.Language.Registry.unregister(:en)
:ok