Nasty.Statistics.ModelLoader (Nasty v0.3.0)

View Source

Loads statistical models from the filesystem and registers them.

ModelLoader discovers models in the priv/models/ directory and loads them on demand. Models are organized by language and task:

priv/models/
  en/
    pos_hmm_v1.model
    pos_hmm_v1.meta.json
    pos_hmm_v2.model
    pos_hmm_v2.meta.json

Model files use the naming convention: {task}_{model_type}_{version}.model Metadata files use: {task}_{model_type}_{version}.meta.json

Usage

# Load a specific model
{:ok, model} = ModelLoader.load_model(:en, :pos_tagging, "v1")

# Load latest version
{:ok, model} = ModelLoader.load_latest(:en, :pos_tagging)

# Discover all available models
models = ModelLoader.discover_models()

Summary

Functions

Discovers all available models in the models directory.

Gets the path to a model file.

Loads the latest version of a model for the given language and task.

Loads a model from the filesystem and registers it in the ModelRegistry.

Functions

discover_models()

@spec discover_models() :: [
  {atom(), atom(), String.t(), String.t(), String.t() | nil}
]

Discovers all available models in the models directory.

Returns a list of tuples: {language, task, version, model_path, metadata_path}.

Examples

iex> ModelLoader.discover_models()
[
  {:en, :pos_tagging, "v1", "priv/models/en/pos_hmm_v1.model", "priv/models/en/pos_hmm_v1.meta.json"}
]

get_model_path(language, task, version)

@spec get_model_path(atom(), atom(), String.t()) ::
  {:ok, String.t()} | {:error, :not_found}

Gets the path to a model file.

Returns {:ok, path} if the model file exists, {:error, :not_found} otherwise.

Examples

iex> ModelLoader.get_model_path(:en, :pos_tagging, "v1")
{:ok, "/path/to/priv/models/en/pos_hmm_v1.model"}

load_latest(language, task)

@spec load_latest(atom(), atom()) :: {:ok, Model.t()} | {:error, term()}

Loads the latest version of a model for the given language and task.

Returns {:ok, model} if successful, {:error, reason} otherwise.

Examples

iex> ModelLoader.load_latest(:en, :pos_tagging)
{:ok, %Nasty.Statistics.POSTagging.HMMTagger{...}}

load_model(language, task, version)

@spec load_model(atom(), atom(), String.t()) :: {:ok, Model.t()} | {:error, term()}

Loads a model from the filesystem and registers it in the ModelRegistry.

Returns {:ok, model} if successful, {:error, reason} otherwise.

Examples

iex> ModelLoader.load_model(:en, :pos_tagging, "v1")
{:ok, %Nasty.Statistics.POSTagging.HMMTagger{...}}

iex> ModelLoader.load_model(:en, :nonexistent, "v1")
{:error, :not_found}