models
Models
MarkovCombineError
Bases: Exception
Exception raised when attempt to combine incompatible model chains.
MarkovEmptyError
Bases: Exception
Raised when attempting to do model comparison and inspection on empty models.
MarkovTextModel
Bases: Model
Stores a compiled markov text model.
Attributes:
Name | Type | Description |
---|---|---|
created |
datetime
|
Date and time when the model was created. |
modified |
datetime
|
Date and time when the model was last modified. |
data |
JSON
|
The text model as JSON. |
is_ready
property
Flag to indicate if the model is initialized and ready to generate sentences.
aadd_new_corpus_data_to_model
async
aadd_new_corpus_data_to_model(
corpus_entries: list[str],
*,
char_limit: int | None = None,
weights: list[float] | None = None
) -> None
Takes a list of new corpus entries and incorporates them into the model.
Unlike aupdate_model_from_corpus
, this method is additive. This works by
first creating a text model based on the new entries, and then uses
markovify.combine
to add them to the existing text model. Note that
this will fail if the stored model is compiled.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
corpus_entries |
list[str]
|
A list of text sentences to add. |
required |
char_limit |
int | None
|
The character limit to use for the new corpus.
Use |
None
|
weights |
list[float] | None
|
The weighting to use for combine operation, the first value representing the saved model, and the second representing the new entries. |
None
|
Raises:
Type | Description |
---|---|
MarkovCombineError
|
If the stored model is already compiled. |
MarkovEmptyError
|
If the new models are empty. |
Source code in src/django_markov/models.py
acombine_models
async
classmethod
acombine_models(
models: list[MarkovTextModel],
*,
return_type: Literal[
"model_instance", "text_model"
] = "model_instance",
mode: Literal["strict", "permissive"] = "strict",
weights: list[float] | None = None
) -> tuple[MarkovTextModel | POSifiedText, int]
Combine multiple MarkovTextModels into a single model.
Models cannot be combined if any of the following is true
- They are empty of data.
- They are stored in compiled state.
- The state size between models is not the same.
- The underlying text models are not the same type (if you subclass).
- You supply weights, but not the same number as the models to combine or if you use permissive mode.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
models |
list[MarkovTextModel]
|
A list of MarkovTextModel instances to combine. |
required |
return_type |
Literal['model_instance', 'text_model']
|
The desired result type. |
'model_instance'
|
mode |
Literal['strict', 'permissive']
|
strict indicates that an exception should be raised if any of the candidate models are incompatible, or if those specific instances should simply be dropped from the operation. |
'strict'
|
weights |
list[float] | None
|
A list of floats representing the relative weights to put on each source. Optional, but can only be used with mode='strict'. |
None
|
Returns:
Type | Description |
---|---|
tuple[MarkovTextModel | POSifiedText, int]
|
Either a new MarkovTextModel instance persisted to the database or a POSifiedText object to manipulate at a low level, and the total number of models combined. |
Raises:
Type | Description |
---|---|
ValueError
|
If any of the parameter combinations is invalid |
MarkovCombineError
|
If models are incompatible for combining or a markovify error is raised. |
Source code in src/django_markov/models.py
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
|
add_new_corpus_data_to_model
add_new_corpus_data_to_model(
corpus_entries: list[str],
*,
char_limit: int | None = None,
weights: list[float] | None = None
) -> None
Sync wrapper for aadd_new_corpus_data_to_model
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
corpus_entries |
list[str]
|
A list of text sentences to add. |
required |
char_limit |
int | None
|
The character limit to use for the new corpus.
Use |
None
|
weights |
list[float] | None
|
The weighting to use for combine operation, the first value representing the saved model, and the second representing the new entries. |
None
|
Raises:
Type | Description |
---|---|
MarkovCombineError
|
If the stored model is already compiled. |
MarkovEmptyError
|
If the new models are empty. |
ValueError
|
If weights are supplied, and they do not have a length of two. |
Source code in src/django_markov/models.py
agenerate_sentence
async
Generates a random sentence within the character limit based on the model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
char_limit |
int
|
Maximum characters to use. If zero, no limit. |
0
|
tries |
int
|
Number of attempts to make a sentence. |
10
|
Returns: str: Random sentence
Source code in src/django_markov/models.py
aupdate_model_from_corpus
async
aupdate_model_from_corpus(
corpus_entries: list[str],
*,
char_limit: int | None = None,
store_compiled: bool | None = None
) -> None
Takes the a list of entries as the new full corpus and recreates the model, saving it. The corpus must not exceed the char_limit.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
corpus_entries |
list[str]
|
The corpus as a list of text sentences. |
required |
char_limit |
int | None
|
The maximum number of characters to allow in the corpus. |
None
|
store_compiled |
bool | None
|
Whether to store the model in it's compiled state. If None, defaults to settings.MARKOV_STORE_COMPILED_MODELS or False. |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
If the corpus is beyond the maximum character limit. |
Source code in src/django_markov/models.py
combine_models
classmethod
combine_models(
models: list[MarkovTextModel],
*,
return_type: Literal[
"model_instance", "text_model"
] = "model_instance",
mode: Literal["strict", "permissive"] = "strict",
weights: list[float] | None = None
) -> tuple[MarkovTextModel | POSifiedText, int]
Sync wrapper of acombine_models.
Combine multiple MarkovTextModels into a single model.
Models cannot be combined if any of the following is true
- They are empty of data.
- They are stored in compiled state.
- The state size between models is not the same.
- The underlying text models are not the same type (if you subclass).
- You supply weights, but not the same number as the models to combine or if you use permissive mode.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
models |
list[MarkovTextModel]
|
A list of MarkovTextModel instances to combine. |
required |
return_type |
Literal['model_instance', 'text_model']
|
The desired result type. |
'model_instance'
|
mode |
Literal['strict', 'permissive']
|
strict indicates that an exception should be raised if any of the candidate models are incompatible, or if those specific instances should simply be dropped from the operation. |
'strict'
|
weights |
list[float] | None
|
A list of floats representing the relative weights to put on each source. Optional, but can only be used with mode='strict'. |
None
|
Returns:
Type | Description |
---|---|
tuple[MarkovTextModel | POSifiedText, int]
|
Either a new MarkovTextModel instance persisted to the database or a POSifiedText object to manipulate at a low level, and the total number of models combined. |
Raises:
Type | Description |
---|---|
ValueError
|
If any of the parameter combinations is invalid |
MarkovCombineError
|
If models are incompatible for combining or a markovify error is raised. |
Source code in src/django_markov/models.py
generate_sentence
Sync wrapper for agenerate_sentence.
is_compiled_model
Checks if the stored data for the text mile is compiled.
Raises:
Type | Description |
---|---|
MarkovEmptyError
|
if the model data is null. |
Source code in src/django_markov/models.py
refresh_from_db
Remove the value of the cached properties before refreshing the data.
Source code in src/django_markov/models.py
update_model_from_corpus
update_model_from_corpus(
corpus_entries: list[str],
*,
char_limit: int | None = None,
store_compiled: bool | None = None
) -> None
Sync wrapper for the async version Takes the a list of entries as the new full corpus and recreates the model, saving it. The corpus must not exceed the char_limit.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
corpus_entries |
list[str]
|
The corpus as a list of text sentences. |
required |
char_limit |
int | None
|
The maximum number of characters to allow in the corpus. |
None
|
store_compiled |
bool | None
|
Whether to store the model in it's compiled state. If None, defaults to settings.MARKOV_STORE_COMPILED_MODELS or False. |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
If the corpus is beyond the maximum character limit. |