banhxeo.model.base module

class banhxeo.model.base.BaseLanguageModel(model_config: ModelConfig, vocab: Vocabulary)[source]

Bases: object

Abstract base class for all language models in the Banhxeo library.

This class defines a common interface for language models, including configuration management, saving/loading, and basic summary functionalities. It is intended to be subclassed by specific model types (e.g., N-gram, neural models).

Variables:
  • config (ModelConfig) – The configuration object for the model.

  • vocab (Vocabulary) – The vocabulary associated with the model.

  • _is_trained_or_fitted (bool) – A flag indicating whether the model has been trained (for neural models) or fitted (for statistical models).

__init__(model_config: ModelConfig, vocab: Vocabulary)[source]

Initializes the BaseLanguageModel.

Parameters:
  • model_config – The configuration object for the model. It should be an instance of ModelConfig or its subclass.

  • vocab – The Vocabulary instance to be used by the model.

get_config() ModelConfig[source]

Returns the configuration object of the model.

Returns:

The ModelConfig (or subclass) instance associated with this model.

summary() None[source]

Prints a human-readable summary of the model.

This typically includes the model class name, its configuration, and its training/fitted status. Subclasses (like NeuralLanguageModel) may override this to provide more detailed summaries (e.g., layer structure).

save_model(save_directory: Path | str) None[source]

Saves the model’s state and configuration.

The specific format and content depend on the model type. For neural models, this typically involves saving weights and architecture config. For statistical models, it might involve saving learned parameters or data structures.

Parameters:

save_path – The file path or directory path where the model should be saved. Conventionally, for neural models, this might be a directory. For simpler models, it could be a single file.

Raises:

NotImplementedError – If the subclass does not implement this method.

classmethod load_model(load_directory: Path | str, vocab: Vocabulary | None = None, **kwargs)[source]

Loads a model from a saved state.

Parameters:
  • cls – The class itself.

  • load_path – The file path or directory path from which to load the model.

  • vocab (#) – Optionally, a Vocabulary instance if not saved with the model.

  • **kwargs – Additional arguments that might be needed for loading, such as a Vocabulary instance if not bundled.

Returns:

An instance of the language model class.

Raises:

NotImplementedError – If the subclass does not implement this method.