PocketSphinx
5.0.0
A small speech recognizer
|
N-Gram based language model. More...
#include <pocketsphinx/model.h>
Public Member Functions | |
POCKETSPHINX_EXPORT ngram_model_t * | ngram_model_read (ps_config_t *config, const char *file_name, ngram_file_type_t file_type, logmath_t *lmath) |
POCKETSPHINX_EXPORT int | ngram_model_write (ngram_model_t *model, const char *file_name, ngram_file_type_t format) |
POCKETSPHINX_EXPORT ngram_file_type_t | ngram_file_name_to_type (const char *file_name) |
POCKETSPHINX_EXPORT ngram_file_type_t | ngram_str_to_type (const char *str_name) |
POCKETSPHINX_EXPORT const char * | ngram_type_to_str (int type) |
POCKETSPHINX_EXPORT ngram_model_t * | ngram_model_retain (ngram_model_t *model) |
POCKETSPHINX_EXPORT int | ngram_model_free (ngram_model_t *model) |
POCKETSPHINX_EXPORT int | ngram_model_casefold (ngram_model_t *model, int kase) |
POCKETSPHINX_EXPORT int | ngram_model_apply_weights (ngram_model_t *model, float32 lw, float32 wip) |
POCKETSPHINX_EXPORT float32 | ngram_model_get_weights (ngram_model_t *model, int32 *out_log_wip) |
POCKETSPHINX_EXPORT int32 | ngram_score (ngram_model_t *model, const char *word,...) |
POCKETSPHINX_EXPORT int32 | ngram_tg_score (ngram_model_t *model, int32 w3, int32 w2, int32 w1, int32 *n_used) |
POCKETSPHINX_EXPORT int32 | ngram_bg_score (ngram_model_t *model, int32 w2, int32 w1, int32 *n_used) |
POCKETSPHINX_EXPORT int32 | ngram_ng_score (ngram_model_t *model, int32 wid, int32 *history, int32 n_hist, int32 *n_used) |
POCKETSPHINX_EXPORT int32 | ngram_probv (ngram_model_t *model, const char *word,...) |
POCKETSPHINX_EXPORT int32 | ngram_prob (ngram_model_t *model, const char *const *words, int32 n) |
POCKETSPHINX_EXPORT int32 | ngram_ng_prob (ngram_model_t *model, int32 wid, int32 *history, int32 n_hist, int32 *n_used) |
POCKETSPHINX_EXPORT int32 | ngram_score_to_prob (ngram_model_t *model, int32 score) |
POCKETSPHINX_EXPORT int32 | ngram_wid (ngram_model_t *model, const char *word) |
const POCKETSPHINX_EXPORT char * | ngram_word (ngram_model_t *model, int32 wid) |
POCKETSPHINX_EXPORT int32 | ngram_unknown_wid (ngram_model_t *model) |
POCKETSPHINX_EXPORT int32 | ngram_zero (ngram_model_t *model) |
POCKETSPHINX_EXPORT int32 | ngram_model_get_size (ngram_model_t *model) |
POCKETSPHINX_EXPORT const uint32 * | ngram_model_get_counts (ngram_model_t *model) |
POCKETSPHINX_EXPORT ngram_iter_t * | ngram_model_mgrams (ngram_model_t *model, int m) |
POCKETSPHINX_EXPORT ngram_iter_t * | ngram_iter (ngram_model_t *model, const char *word,...) |
POCKETSPHINX_EXPORT ngram_iter_t * | ngram_ng_iter (ngram_model_t *model, int32 wid, int32 *history, int32 n_hist) |
POCKETSPHINX_EXPORT int32 | ngram_model_add_word (ngram_model_t *model, const char *word, float32 weight) |
POCKETSPHINX_EXPORT int32 | ngram_model_read_classdef (ngram_model_t *model, const char *file_name) |
POCKETSPHINX_EXPORT int32 | ngram_model_add_class (ngram_model_t *model, const char *classname, float32 classweight, char **words, const float32 *weights, int32 n_words) |
POCKETSPHINX_EXPORT int32 | ngram_model_add_class_word (ngram_model_t *model, const char *classname, const char *word, float32 weight) |
POCKETSPHINX_EXPORT ngram_model_t * | ngram_model_set_init (ps_config_t *config, ngram_model_t **models, char **names, const float32 *weights, int32 n_models) |
POCKETSPHINX_EXPORT ngram_model_t * | ngram_model_set_read (ps_config_t *config, const char *lmctlfile, logmath_t *lmath) |
POCKETSPHINX_EXPORT int32 | ngram_model_set_count (ngram_model_t *set) |
POCKETSPHINX_EXPORT ngram_model_set_iter_t * | ngram_model_set_iter (ngram_model_t *set) |
POCKETSPHINX_EXPORT ngram_model_t * | ngram_model_set_select (ngram_model_t *set, const char *name) |
POCKETSPHINX_EXPORT ngram_model_t * | ngram_model_set_lookup (ngram_model_t *set, const char *name) |
const POCKETSPHINX_EXPORT char * | ngram_model_set_current (ngram_model_t *set) |
POCKETSPHINX_EXPORT ngram_model_t * | ngram_model_set_interp (ngram_model_t *set, const char **names, const float32 *weights) |
POCKETSPHINX_EXPORT ngram_model_t * | ngram_model_set_add (ngram_model_t *set, ngram_model_t *model, const char *name, float32 weight, int reuse_widmap) |
POCKETSPHINX_EXPORT ngram_model_t * | ngram_model_set_remove (ngram_model_t *set, const char *name, int reuse_widmap) |
POCKETSPHINX_EXPORT void | ngram_model_set_map_words (ngram_model_t *set, const char **words, int32 n_words) |
POCKETSPHINX_EXPORT int32 | ngram_model_set_current_wid (ngram_model_t *set, int32 set_wid) |
POCKETSPHINX_EXPORT int32 | ngram_model_set_known_wid (ngram_model_t *set, int32 set_wid) |
POCKETSPHINX_EXPORT void | ngram_model_flush (ngram_model_t *lm) |
N-Gram based language model.
POCKETSPHINX_EXPORT ngram_model_t * ngram_model_read | ( | ps_config_t * | config, |
const char * | file_name, | ||
ngram_file_type_t | file_type, | ||
logmath_t * | lmath | ||
) |
Read an N-Gram model from a file on disk.
config | Optional pointer to a set of command-line arguments. Recognized arguments are: |
file_name | path to the file to read. |
file_type | type of the file, or NGRAM_AUTO to determine automatically. |
lmath | Log-math parameters to use for probability calculations. Ownership of this object is assumed by the newly created ngram_model_t, and you should not attempt to free it manually. If you wish to reuse it elsewhere, you must retain it with logmath_retain(). |
POCKETSPHINX_EXPORT int ngram_model_write | ( | ngram_model_t * | model, |
const char * | file_name, | ||
ngram_file_type_t | format | ||
) |
Write an N-Gram model to disk.
POCKETSPHINX_EXPORT ngram_file_type_t ngram_file_name_to_type | ( | const char * | file_name | ) |
Guess the file type for an N-Gram model from the filename.
POCKETSPHINX_EXPORT ngram_file_type_t ngram_str_to_type | ( | const char * | str_name | ) |
Get the N-Gram file type from a string.
POCKETSPHINX_EXPORT const char * ngram_type_to_str | ( | int | type | ) |
Get the canonical name for an N-Gram file type.
POCKETSPHINX_EXPORT ngram_model_t * ngram_model_retain | ( | ngram_model_t * | model | ) |
Retain ownership of an N-Gram model.
POCKETSPHINX_EXPORT int ngram_model_free | ( | ngram_model_t * | model | ) |
Release memory associated with an N-Gram model.
POCKETSPHINX_EXPORT int ngram_model_casefold | ( | ngram_model_t * | model, |
int | kase | ||
) |
Case-fold word strings in an N-Gram model.
WARNING: This is not Unicode aware, so any non-ASCII characters will not be converted.
POCKETSPHINX_EXPORT int ngram_model_apply_weights | ( | ngram_model_t * | model, |
float32 | lw, | ||
float32 | wip | ||
) |
Apply a language weight, insertion penalty, and unigram weight to a language model.
This will change the values output by ngram_score() and friends. This is done for efficiency since in decoding, these are the only values we actually need. Call ngram_prob() if you want the "raw" N-Gram probability estimate.
To remove all weighting, call ngram_apply_weights(model, 1.0, 1.0).
POCKETSPHINX_EXPORT float32 ngram_model_get_weights | ( | ngram_model_t * | model, |
int32 * | out_log_wip | ||
) |
Get the current weights from a language model.
model | The model in question. |
out_log_wip | Output: (optional) logarithm of word insertion penalty. |
POCKETSPHINX_EXPORT int32 ngram_score | ( | ngram_model_t * | model, |
const char * | word, | ||
... | |||
) |
Get the score (scaled, interpolated log-probability) for a general N-Gram.
The argument list consists of the history words (as null-terminated strings) of the N-Gram, in reverse order, followed by NULL. Therefore, if you wanted to get the N-Gram score for "a whole joy", you would call:
score = ngram_score(model, "joy", "whole", "a", NULL);
This is not the function to use in decoding, because it has some overhead for looking up words. Use ngram_ng_score(), ngram_tg_score(), or ngram_bg_score() instead. In the future there will probably be a version that takes a general language model state object, to support suffix-array LM and things like that.
If one of the words is not in the LM's vocabulary, the result will depend on whether this is an open or closed vocabulary language model. For an open-vocabulary model, unknown words are all mapped to the unigram <UNK> which has a non-zero probability and also participates in higher-order N-Grams. Therefore, you will get a score of some sort in this case.
For a closed-vocabulary model, unknown words are impossible and thus have zero probability. Therefore, if word
is unknown, this function will return a "zero" log-probability, i.e. a large negative number. To obtain this number for comparison, call ngram_zero().
POCKETSPHINX_EXPORT int32 ngram_tg_score | ( | ngram_model_t * | model, |
int32 | w3, | ||
int32 | w2, | ||
int32 | w1, | ||
int32 * | n_used | ||
) |
Quick trigram score lookup.
POCKETSPHINX_EXPORT int32 ngram_bg_score | ( | ngram_model_t * | model, |
int32 | w2, | ||
int32 | w1, | ||
int32 * | n_used | ||
) |
Quick bigram score lookup.
POCKETSPHINX_EXPORT int32 ngram_ng_score | ( | ngram_model_t * | model, |
int32 | wid, | ||
int32 * | history, | ||
int32 | n_hist, | ||
int32 * | n_used | ||
) |
Quick general N-Gram score lookup.
POCKETSPHINX_EXPORT int32 ngram_probv | ( | ngram_model_t * | model, |
const char * | word, | ||
... | |||
) |
Get the "raw" log-probability for a general N-Gram.
This returns the log-probability of an N-Gram, as defined in the language model file, before any language weighting, interpolation, or insertion penalty has been applied.
POCKETSPHINX_EXPORT int32 ngram_prob | ( | ngram_model_t * | model, |
const char *const * | words, | ||
int32 | n | ||
) |
Get the "raw" log-probability for a general N-Gram.
This returns the log-probability of an N-Gram, as defined in the language model file, before any language weighting, interpolation, or insertion penalty has been applied.
POCKETSPHINX_EXPORT int32 ngram_ng_prob | ( | ngram_model_t * | model, |
int32 | wid, | ||
int32 * | history, | ||
int32 | n_hist, | ||
int32 * | n_used | ||
) |
Quick "raw" probability lookup for a general N-Gram.
See documentation for ngram_ng_score() and ngram_apply_weights() for an explanation of this.
POCKETSPHINX_EXPORT int32 ngram_score_to_prob | ( | ngram_model_t * | model, |
int32 | score | ||
) |
Convert score to "raw" log-probability.
score
.model | The N-Gram model from which score was obtained. |
score | The N-Gram score to convert |
POCKETSPHINX_EXPORT int32 ngram_wid | ( | ngram_model_t * | model, |
const char * | word | ||
) |
Look up numerical word ID.
const POCKETSPHINX_EXPORT char * ngram_word | ( | ngram_model_t * | model, |
int32 | wid | ||
) |
Look up word string for numerical word ID.
POCKETSPHINX_EXPORT int32 ngram_unknown_wid | ( | ngram_model_t * | model | ) |
Get the unknown word ID for a language model.
Language models can be either "open vocabulary" or "closed vocabulary". The difference is that the former assigns a fixed non-zero unigram probability to unknown words, while the latter does not allow unknown words (or, equivalently, it assigns them zero probability). If this is a closed vocabulary model, this function will return NGRAM_INVALID_WID.
POCKETSPHINX_EXPORT int32 ngram_zero | ( | ngram_model_t * | model | ) |
Get the "zero" log-probability value for a language model.
POCKETSPHINX_EXPORT int32 ngram_model_get_size | ( | ngram_model_t * | model | ) |
Get the order of the N-gram model (i.e. the "N" in "N-gram")
POCKETSPHINX_EXPORT const uint32 * ngram_model_get_counts | ( | ngram_model_t * | model | ) |
Get the counts of the various N-grams in the model.
POCKETSPHINX_EXPORT ngram_iter_t * ngram_model_mgrams | ( | ngram_model_t * | model, |
int | m | ||
) |
Iterate over all M-grams.
model | Language model to query. |
m | Order of the M-Grams requested minus one (i.e. order of the history) |
POCKETSPHINX_EXPORT ngram_iter_t * ngram_iter | ( | ngram_model_t * | model, |
const char * | word, | ||
... | |||
) |
Get an iterator over M-grams pointing to the specified M-gram.
POCKETSPHINX_EXPORT ngram_iter_t * ngram_ng_iter | ( | ngram_model_t * | model, |
int32 | wid, | ||
int32 * | history, | ||
int32 | n_hist | ||
) |
Get an iterator over M-grams pointing to the specified M-gram.
POCKETSPHINX_EXPORT int32 ngram_model_add_word | ( | ngram_model_t * | model, |
const char * | word, | ||
float32 | weight | ||
) |
Add a word (unigram) to the language model.
model | The model to add a word to. |
word | Text of the word to add. |
weight | Weight of this word relative to the uniform distribution. |
POCKETSPHINX_EXPORT int32 ngram_model_read_classdef | ( | ngram_model_t * | model, |
const char * | file_name | ||
) |
Read a class definition file and add classes to a language model.
This function assumes that the class tags have already been defined as unigrams in the language model. All words in the class definition will be added to the vocabulary as special in-class words. For this reason is is necessary that they not have the same names as any words in the general unigram distribution. The convention is to suffix them with ":class_tag", where class_tag is the class tag minus the enclosing square brackets.
POCKETSPHINX_EXPORT int32 ngram_model_add_class | ( | ngram_model_t * | model, |
const char * | classname, | ||
float32 | classweight, | ||
char ** | words, | ||
const float32 * | weights, | ||
int32 | n_words | ||
) |
Add a new class to a language model.
If classname
already exists in the unigram set for model
, then it will be converted to a class tag, and classweight
will be ignored. Otherwise, a new unigram will be created as in ngram_model_add_word().
POCKETSPHINX_EXPORT int32 ngram_model_add_class_word | ( | ngram_model_t * | model, |
const char * | classname, | ||
const char * | word, | ||
float32 | weight | ||
) |
Add a word to a class in a language model.
model | The model to add a word to. |
classname | Name of the class to add this word to. |
word | Text of the word to add. |
weight | Weight of this word relative to the within-class uniform distribution. |
POCKETSPHINX_EXPORT ngram_model_t * ngram_model_set_init | ( | ps_config_t * | config, |
ngram_model_t ** | models, | ||
char ** | names, | ||
const float32 * | weights, | ||
int32 | n_models | ||
) |
Create a set of language models sharing a common space of word IDs.
This function creates a meta-language model which groups together a set of language models, synchronizing word IDs between them. To use this language model, you can either select a submodel to use exclusively using ngram_model_set_select(), or interpolate between scores from all models. To do the latter, you can either pass a non-NULL value of the weights
parameter, or re-activate interpolation later on by calling ngram_model_set_interp().
In order to make this efficient, there are some restrictions on the models that can be grouped together. The most important (and currently the only) one is that they must all share the same log-math parameters.
config | Any configuration parameters to be shared between models. |
models | Array of pointers to previously created language models. |
names | Array of strings to use as unique identifiers for LMs. |
weights | Array of weights to use in interpolating LMs, or NULL for no interpolation. |
n_models | Number of elements in the arrays passed to this function. |
POCKETSPHINX_EXPORT ngram_model_t * ngram_model_set_read | ( | ps_config_t * | config, |
const char * | lmctlfile, | ||
logmath_t * | lmath | ||
) |
Read a set of language models from a control file.
This file creates a language model set from a "control file" of the type used in Sphinx-II and Sphinx-III. File format (optional stuff is indicated by enclosing in []):
[{ LMClassFileName LMClassFilename ... }] TrigramLMFileName LMName [{ LMClassName LMClassName ... }] TrigramLMFileName LMName [{ LMClassName LMClassName ... }] ... (There should be whitespace around the { and } delimiters.)
This is an extension of the older format that had only TrigramLMFilenName and LMName pairs. The new format allows a set of LMClass files to be read in and referred to by the trigram LMs.
No "comments" allowed in this file.
config | Configuration parameters. |
lmctlfile | Path to the language model control file. |
lmath | Log-math parameters to use for probability calculations. Ownership of this object is assumed by the newly created ngram_model_t, and you should not attempt to free it manually. If you wish to reuse it elsewhere, you must retain it with logmath_retain(). |
POCKETSPHINX_EXPORT int32 ngram_model_set_count | ( | ngram_model_t * | set | ) |
Returns the number of language models in a set.
POCKETSPHINX_EXPORT ngram_model_set_iter_t * ngram_model_set_iter | ( | ngram_model_t * | set | ) |
Begin iterating over language models in a set.
POCKETSPHINX_EXPORT ngram_model_t * ngram_model_set_select | ( | ngram_model_t * | set, |
const char * | name | ||
) |
Select a single language model from a set for scoring.
POCKETSPHINX_EXPORT ngram_model_t * ngram_model_set_lookup | ( | ngram_model_t * | set, |
const char * | name | ||
) |
Look up a language model by name from a set.
name
, or NULL if no language model by that name exists. const POCKETSPHINX_EXPORT char * ngram_model_set_current | ( | ngram_model_t * | set | ) |
Get the current language model name, if any.
POCKETSPHINX_EXPORT ngram_model_t * ngram_model_set_interp | ( | ngram_model_t * | set, |
const char ** | names, | ||
const float32 * | weights | ||
) |
Set interpolation weights for a set and enables interpolation.
If weights
is NULL, any previously initialized set of weights will be used. If no weights were specified to ngram_model_set_init(), then a uniform distribution will be used.
POCKETSPHINX_EXPORT ngram_model_t * ngram_model_set_add | ( | ngram_model_t * | set, |
ngram_model_t * | model, | ||
const char * | name, | ||
float32 | weight, | ||
int | reuse_widmap | ||
) |
Add a language model to a set.
set | The language model set to add to. |
model | The language model to add. |
name | The name to associate with this model. |
weight | Interpolation weight for this model, relative to the uniform distribution. 1.0 is a safe value. |
reuse_widmap | Reuse the existing word-ID mapping in set . Any new words present in model will not be added to the word-ID mapping in this case. |
POCKETSPHINX_EXPORT ngram_model_t * ngram_model_set_remove | ( | ngram_model_t * | set, |
const char * | name, | ||
int | reuse_widmap | ||
) |
Remove a language model from a set.
set | The language model set to remove from. |
name | The name associated with the model to remove. |
reuse_widmap | Reuse the existing word-ID mapping in set . |
POCKETSPHINX_EXPORT void ngram_model_set_map_words | ( | ngram_model_t * | set, |
const char ** | words, | ||
int32 | n_words | ||
) |
Set the word-to-ID mapping for this model set.
POCKETSPHINX_EXPORT int32 ngram_model_set_current_wid | ( | ngram_model_t * | set, |
int32 | set_wid | ||
) |
Query the word-ID mapping for the current language model.
set_wid
is invalid or interpolation is enabled. POCKETSPHINX_EXPORT int32 ngram_model_set_known_wid | ( | ngram_model_t * | set, |
int32 | set_wid | ||
) |
Test whether a word ID corresponds to a known word in the current state of the language model set.
set_wid
corresponds to a known word in that language model. Otherwise, returns non-zero if set_wid
corresponds to a known word in any language model. POCKETSPHINX_EXPORT void ngram_model_flush | ( | ngram_model_t * | lm | ) |
Flush any cached N-Gram information