kaldi.decoder

Functions

grammar_to_vector_fst Converts a GrammarFST to a StdVectorFst.
prepare_for_grammar_fst Prepares input FST for use in GrammarFST.

Classes

BiglmFasterDecoder Faster decoder for decoding with big language models.
BiglmFasterDecoderOptions Options for BiglmFasterDecoder.
DecodableMapped Decodable for mapping the indices of another decodable object.
DecodableMatrixMapped Decodable returning mapped log-likelihoods from a matrix.
DecodableMatrixMappedOffset Decodable returning mapped log-likelihoods from a matrix.
DecodableMatrixScaled Decodable returning log-likelihoods from a matrix.
DecodableMatrixScaledMapped Decodable scaling and returning mapped log-likelihoods from a matrix.
DecodableSum Decodable for summing scores of other decodable objects.
DecodableSumScaled Decodable for summing and scaling scores of other decodable objects.
FasterDecoder Faster decoder.
FasterDecoderOptions Options for FasterDecoder.
GrammarFst Grammar FST.
GrammarFstArc Grammar FST arc.
GrammarFstArcIterator Grammar FST arc iterator.
LatticeBiglmFasterDecoder Lattice generating faster decoder for decoding with big language models.
LatticeFasterDecoder Lattice generating faster decoder.
LatticeFasterDecoderOptions Options for LatticeFasterDecoder.
LatticeFasterGrammarDecoder Lattice generating faster grammar decoder.
LatticeFasterOnlineDecoder Lattice generating faster online decoder.
LatticeFasterOnlineGrammarDecoder Lattice generating faster online grammar decoder.
TrainingGraphCompiler Training graph compiler.
TrainingGraphCompilerOptions Options for training graph compiler.
class kaldi.decoder.BiglmFasterDecoder(fst, opts, lm_diff_fst)[source]

Faster decoder for decoding with big language models.

This is as LatticeFasterDecoder, but does online composition between decoding graph fst and the difference language model lm_diff_fst.

Parameters:
  • fst (StdFst) – Decoding graph.
  • opts (BiglmFasterDecoderOptions) – Decoder options.
  • lm_diff_fst (StdDeterministicOnDemandFst) – The deterministic on-demand FST representing the difference in scores between the LM to decode with and the LM the decoding graph fst was compiled with.
decode(decodable:DecodableInterface)

Decodes all available frames in the decodable object.

Parameters:decodable (DecodableInterface) – The decodable object.
get_best_path(use_final_probs=True)

Gets best path as a lattice.

Parameters:use_final_probs (bool) – If True and a final state of the graph is reached, then the output will include final probabilities given by the graph. Otherwise all final probabilities are treated as one.
Returns:The best path.
Return type:LatticeVectorFst
Raises:RuntimeError – In the unusual circumstances where no tokens survive.
reached_final() → bool

Checks if a final state was active on the last decoded frame.

Returns:True if a final state was active on the last decoded frame.
set_options(opts:BiglmFasterDecoderOptions)

Sets decoder options.

Parameters:opts (BiglmFasterDecoderOptions) – Decoder options.
class kaldi.decoder.BiglmFasterDecoderOptions

Options for BiglmFasterDecoder.

beam

Decoding beam. Larger -> slower but more accurate.

beam_delta

Increment used for increasing decoder beam. Obscure option.

hash_ratio

Setting used in decoder to control hash behavior.

max_active

Max number of active states. Larger -> slower but more accurate.

min_active

Min number of active states. No pruning if active < min_active.

register(opts:OptionsItf, full:bool)

Registers options with an object implementing the options interface.

Parameters:
  • opts (OptionsItf) – An object implementing the options interface. Typically a command-line option parser.
  • full (bool) – If True, registers obscure options too.
class kaldi.decoder.DecodableMapped(index_map, d)

Decodable for mapping the indices of another decodable object.

Parameters:
  • index_map (List[int]) – The index mapping.
  • d (DecodableInterface) – The other decodable object.
is_last_frame(frame:int) → bool

Checks if given frame is the last frame.

log_likelihood(frame:int, index:int) → float

Returns the log-likehood of the given index for the given frame.

loglikelihood(frame:int, state_index:int) → float

Returns the log-likehood of the mapped index for the given frame.

num_frames_ready() → int

Returns number of frames ready for decoding.

num_indices() → int

Returns number of indices.

class kaldi.decoder.DecodableMatrixMapped(tm, likes)

Decodable returning mapped log-likelihoods from a matrix.

This decodable maps transition IDs to PDF IDs when queried.

Parameters:
  • tm (TransitionModel) – The transition model for mapping transition IDs to PDF IDs.
  • likes (Matrix) – The matrix of log-likelihoods.
  • frame_offset (int) – Frame offset into the log-likelihoods matrix.
is_last_frame(frame:int) → bool

Checks if given frame is the last frame.

log_likelihood(frame:int, tid:int) → float

Returns the log-likehood of the transition ID for the given frame.

num_frames_ready() → int

Returns number of frames ready for decoding.

num_indices() → int

Returns number of indices.

class kaldi.decoder.DecodableMatrixMappedOffset(tm)

Decodable returning mapped log-likelihoods from a matrix.

This decodable maps transition IDs to pdf IDs when queried. It supports repeatedly writing to the matrix and setting a time-offset representing the frame-index of the first row of the matrix. It’s intended for use in multi-threaded decoding; mutex and semaphores are not included. External code will call accept_log_likes() each time more log-likelihods are available. If you try to access a log-likelihood that’s no longer available because the frame index is less than the current offset, it is an error.

Parameters:tm (TransitionModel) – The transition model for mapping transition IDs to PDF IDs.
accept_log_likes(loglikes:Matrix, frames_to_discard:int)

Appends given log-likelihoods to existing log-likehoods.

This method is destructive of the input “loglikes” because it may under some circumstances do a shallow copy by swapping data pointers. It appends loglikes to any existing likelihoods that have been previously supplied. frames_to_discard, if nonzero, will discard that number of previously available frames, from the left, advancing first_available_frame() by frames_to_discard frames. Users should only set frames_to_discard to nonzero if the decoder won’t access the loglikes for older frames going forward.

first_available_frame() → int

Returns the 0-based index of the first available frame.

input_is_finished()

Signals that no more likelihoods will be appended.

This method should only be called after all likelihoods are appended to mark the end.

is_last_frame(frame:int) → bool

Checks if given frame is the last frame.

log_likelihood(frame:int, tid:int) → float

Returns the log-likehood of the transition ID for the given frame.

num_frames_ready() → int

Returns number of frames ready for decoding.

num_indices() → int

Returns number of indices.

class kaldi.decoder.DecodableMatrixScaled(likes, scale)

Decodable returning log-likelihoods from a matrix.

Parameters:
  • likes (Matrix) – The matrix of log-likelihoods.
  • scale (float) – The scalar multiplier.
is_last_frame(frame:int) → bool

Checks if given frame is the last frame.

log_likelihood(frame:int, index:int) → float

Returns the log-likehood of the index for the given frame.

num_frames_ready() → int

Returns number of frames ready for decoding.

num_indices() → int

Returns number of indices.

class kaldi.decoder.DecodableMatrixScaledMapped(tm, likes, scale)

Decodable scaling and returning mapped log-likelihoods from a matrix.

This decodable maps transition IDs to PDF IDs when queried. Likelihoods read from the matrix are scaled with the scale argument provided when instantiating the object.

Parameters:
  • tm (TransitionModel) – The transition model for mapping transition IDs to PDF IDs.
  • likes (Matrix) – The matrix of log-likelihoods.
  • scale (float) – The scalar multiplier.
is_last_frame(frame:int) → bool

Checks if given frame is the last frame.

log_likelihood(frame:int, tid:int) → float

Returns the log-likehood of the transition ID for the given frame.

num_frames_ready() → int

Returns number of frames ready for decoding.

num_indices() → int

Returns number of indices.

class kaldi.decoder.DecodableSum(d1, w1, d2, w2)

Decodable for summing scores of other decodable objects.

Parameters:
  • d1 (DecodableInterface) – The first decodable object.
  • w1 (float) – The weight for the scores obtained from d1.
  • d2 (DecodableInterface) – The second decodable object.
  • w2 (float) – The weight for the scores obtained from d2.
check_sizes()

Checks the sizes of underlying decodables.

is_last_frame(frame:int) → bool

Checks if given frame is the last frame.

log_likelihood(frame:int, state_index:int) → float

Returns the log-likehood of the given index for the given frame.

new(decodables:list<tuple<DecodableInterface, float>>) → DecodableSum

Creates a new decodable from given (decodable, weight) pairs.

Parameters:decodables (List[Tuple[DecodableInterface,float]]) – The list of (decodable, weight) pairs.
num_frames_ready() → int

Returns number of frames ready for decoding.

num_indices() → int

Returns number of indices.

class kaldi.decoder.DecodableSumScaled(d1, w1, d2, w2, scale)

Decodable for summing and scaling scores of other decodable objects.

Parameters:
  • d1 (DecodableInterface) – The first decodable object.
  • w1 (float) – The weight for the scores obtained from d1.
  • d2 (DecodableInterface) – The second decodable object.
  • w2 (float) – The weight for the scores obtained from d2.
  • scale (float) – The scalar multiplier for the sum.
check_sizes()

Checks the sizes of underlying decodables.

is_last_frame(frame:int) → bool

Checks if given frame is the last frame.

log_likelihood(frame:int, state_index:int) → float

Returns the log-likehood of the given index for the given frame.

new(decodables:list<tuple<DecodableInterface, float>>, scale:float) → DecodableSumScaled

Creates a new decodable from given (decodable, weight) pairs.

Parameters:
  • decodables (List[Tuple[DecodableInterface,float]]) – The list of (decodable, weight) pairs.
  • scale (float) – The scalar multiplier for the sum.
num_frames_ready() → int

Returns number of frames ready for decoding.

num_indices() → int

Returns number of indices.

class kaldi.decoder.FasterDecoder(fst, opts)[source]

Faster decoder.

Parameters:
advance_decoding(decodable:DecodableInterface, max_num_frames:int=default)

Advances decoding.

This will decode until there are no more frames ready in the decodable object, but if max_num_frames >= 0 it will decode no more than that many frames.

Parameters:
  • decodable (DecodableInterface) – The decodable object.
  • max_num_frames (int) – Max number of frames to decode. Defaults to -1 which means decode all available frames.
decode(decodable:DecodableInterface)

Decodes all available frames in the decodable object.

Parameters:decodable (DecodableInterface) – The decodable object.
get_best_path(use_final_probs=True)

Gets best path as a lattice.

Parameters:use_final_probs (bool) – If True and a final state of the graph is reached, then the output will include final probabilities given by the graph. Otherwise all final probabilities are treated as one.
Returns:The best path.
Return type:LatticeVectorFst
Raises:RuntimeError – In the unusual circumstances where no tokens survive.
init_decoding()

Initializes decoding.

As an alternative to decode(), you can call init_decoding() and then call advance_decoding() (possibly multiple times).

num_frames_decoded() → int

Queries the number of frames already decoded.

Returns:The number of frames already decoded.
reached_final() → bool

Checks if a final state was active on the last decoded frame.

Returns:True if a final state was active on the last decoded frame.
set_options(opts:FasterDecoderOptions)

Sets decoder options.

Parameters:opts (FasterDecoderOptions) – Decoder options.
class kaldi.decoder.FasterDecoderOptions

Options for FasterDecoder.

beam

Decoding beam. Larger -> slower but more accurate.

beam_delta

Increment used for increasing decoder beam. Obscure option.

hash_ratio

Setting used in decoder to control hash behavior.

max_active

Max number of active states. Larger -> slower but more accurate.

min_active

Min number of active states. No pruning if active < min_active.

register(opts:OptionsItf, full:bool)

Registers options with an object implementing the options interface.

Parameters:
  • opts (OptionsItf) – An object implementing the options interface. Typically a command-line option parser.
  • full (bool) – If True, registers obscure options too.
class kaldi.decoder.GrammarFst

Grammar FST.

final(state:int) → TropicalWeight

Returns the final weight of the given state.

from_fsts(top_fst:StdConstFst, ifsts:list<tuple<int, StdConstFst>>, nonterm_phones_offset:int) → GrammarFst

Creates a new grammar FST from input FSTs.

Parameters:
  • top_fst (StdConstFst) – The top-level FST of the grammar.
  • ifsts (List[Tuple[int,StdConstFst]]) – The list of (non-terminal symbol, decoding graph) pairs.
  • nonterm_phones_offset (int) – The integer index of the first non-terminal symbol.
num_input_epsilons(state:int) → int

Returns number of input epsilon arcs originating from given state.

read(is:istream, binary:bool)

Reads object from input stream.

Supports only binary mode.

start() → int

Returns the start state index.

type() → str

Returns the FST type.

write(os:ostream, binary:bool)

Writes object to output stream.

Supports only binary mode.

class kaldi.decoder.GrammarFstArc

Grammar FST arc.

Attributes of the arc can be accessed and mutated.

GrammarFstArc():
Creates an uninitialized GrammarFstArc.
from_attrs(ilabel:int, olabel:int, weight:TropicalWeight, nextstate:int) → GrammarFstArc

Creates a new arc with the given attributes.

Parameters:
  • ilabel (int) – The input label.
  • olabel (int) – The output label.
  • weight (TropicalWeight) – The arc weight.
  • nextstate (int) – The destination state for the arc.
ilabel

int – The input label.

nextstate

int – The destination state for the arc.

olabel

int – The output label.

weight

TropicalWeight – The arc weight.

class kaldi.decoder.GrammarFstArcIterator

Grammar FST arc iterator.

Parameters:
  • fst – The grammar fst.
  • state – The state index.
done() → bool

Indicates whether the iterator is exhausted or not.

Returns:True if the iterator is exhausted, False otherwise.
next()

Advances the iterator.

value() → GrammarFstArc

Returns the current arc.

class kaldi.decoder.LatticeBiglmFasterDecoder(fst, opts, lm_diff_fst)[source]

Lattice generating faster decoder for decoding with big language models.

This is as LatticeFasterDecoder, but does online composition between decoding graph fst and the difference language model lm_diff_fst.

Parameters:
  • fst (StdFst) – Decoding graph HCLG.
  • opts (LatticeFasterDecoderOptions) – Decoder options.
  • lm_diff_fst (StdDeterministicOnDemandFst) – The deterministic on-demand FST representing the difference in scores between the LM to decode with and the LM the decoding graph fst was compiled with.
decode(decodable:DecodableInterface) → bool

Decodes all available frames in the decodable object.

This may block waiting for input if the decodable object blocks.

Parameters:decodable (DecodableInterface) – The decodable object.
Returns:True if any kind of traceback is available (not necessarily from a final state).
get_best_path(use_final_probs=True)

Gets best path as a lattice.

Parameters:use_final_probs (bool) – If True and a final state of the graph is reached, then the output will include final probabilities given by the graph. Otherwise all final probabilities are treated as one.
Returns:The best path.
Return type:LatticeVectorFst
Raises:RuntimeError – In the unusual circumstances where no tokens survive.
get_lattice(use_final_probs=True)

Gets the lattice-determinized compact lattice.

The output is a deterministic compact lattice with a unique path for each word sequence.

Parameters:use_final_probs (bool) – If True and a final state of the graph is reached, then the output will include final probabilities given by the graph. Otherwise all final probabilities are treated as one.
Returns:The lattice-determinized compact lattice.
Return type:CompactLatticeVectorFst
Raises:RuntimeError – In the unusual circumstances where no tokens survive.
get_raw_lattice(use_final_probs=True)

Gets raw state-level lattice.

The output raw lattice will be topologically sorted.

Parameters:use_final_probs (bool) – If True and a final state of the graph is reached, then the output will include final probabilities given by the graph. Otherwise all final probabilities are treated as one.
Returns:The state-level lattice.
Return type:LatticeVectorFst
Raises:RuntimeError – In the unusual circumstances where no tokens survive.
reached_final() → bool

Checks if a final state was active on the last decoded frame.

Returns:True if a final state was active on the last decoded frame.
set_options(opts:LatticeFasterDecoderOptions)

Sets decoder options.

Parameters:opts (LatticeFasterDecoderOptions) – Decoder options.
class kaldi.decoder.LatticeFasterDecoder(fst, opts)[source]

Lattice generating faster decoder.

Parameters:
advance_decoding(decodable:DecodableInterface, max_num_frames:int=default)

Advances decoding.

This will decode until there are no more frames ready in the decodable object, but if max_num_frames >= 0 it will decode no more than that many frames.

Parameters:
  • decodable (DecodableInterface) – The decodable object.
  • max_num_frames (int) – Max number of frames to decode. Defaults to -1 which means decode all available frames.
decode(decodable:DecodableInterface) → bool

Decodes all available frames in the decodable object.

This may block waiting for input if the decodable object blocks.

Parameters:decodable (DecodableInterface) – The decodable object.
Returns:True if any kind of traceback is available (not necessarily from a final state).
final_relative_cost() → float

Queries the final relative cost.

This method serves the same purpose as reached_final(), but gives more information. It returns the difference between the best (final-cost plus cost) of any token on the final frame, and the best cost of any token on the final frame. If it is infinity it means no final-states were present on the final frame. It will usually be nonnegative. If it not too positive (e.g. < 5) you can take it as an indication that we reached the final-state with reasonable likelihood.

Returns:The final relative cost.
finalize_decoding()

Finalizes decoding.

This function may be optionally called after the last call to advance_decoding(). It does an extra pruning step to prune the lattices output by get_lattice() and get_raw_lattice(). more accurately.

get_best_path(use_final_probs=True)

Gets best path as a lattice.

Parameters:use_final_probs (bool) – If True and a final state of the graph is reached, then the output will include final probabilities given by the graph. Otherwise all final probabilities are treated as one.
Returns:The best path.
Return type:LatticeVectorFst
Raises:RuntimeError – In the unusual circumstances where no tokens survive.
get_lattice(use_final_probs=True)

Gets the lattice-determinized compact lattice.

The output is a deterministic compact lattice with a unique path for each word sequence.

Parameters:use_final_probs (bool) – If True and a final state of the graph is reached, then the output will include final probabilities given by the graph. Otherwise all final probabilities are treated as one.
Returns:The lattice-determinized compact lattice.
Return type:CompactLatticeVectorFst
Raises:RuntimeError – In the unusual circumstances where no tokens survive.
get_options() → LatticeFasterDecoderOptions

Gets decoder options.

Returns:The decoder options.
get_raw_lattice(use_final_probs=True)

Gets raw state-level lattice.

The output raw lattice will be topologically sorted.

Parameters:use_final_probs (bool) – If True and a final state of the graph is reached, then the output will include final probabilities given by the graph. Otherwise all final probabilities are treated as one.
Returns:The state-level lattice.
Return type:LatticeVectorFst
Raises:RuntimeError – In the unusual circumstances where no tokens survive.
init_decoding()

Initializes decoding.

As an alternative to decode(), you can call init_decoding() and then call advance_decoding() (possibly multiple times).

num_frames_decoded() → int

Queries the number of frames already decoded.

Returns:The number of frames already decoded.
reached_final() → bool

Checks if a final state was active on the last decoded frame.

Returns:True if a final state was active on the last decoded frame.
set_options(opts:LatticeFasterDecoderOptions)

Sets decoder options.

Parameters:opts (LatticeFasterDecoderOptions) – Decoder options.
class kaldi.decoder.LatticeFasterDecoderOptions

Options for LatticeFasterDecoder.

beam

Decoding beam. Larger -> slower but more accurate.

beam_delta

Increment used for increasing decoder beam. Obscure option.

det_opts

Options for lattice-determinization.

determinize_lattice

Whether to run lattice-determinization on output lattices.

hash_ratio

Setting used in decoder to control hash behavior.

lattice_beam

Lattice generation beam. Larger -> slower but deeper lattices.

max_active

Max number of active states. Larger -> slower but more accurate.

min_active

Min number of active states. No pruning if active < min_active.

prune_interval

Token pruning interval (in frames).

prune_scale

Pruning scale. It affects the algorithm used for pruning tokens.

register(opts:OptionsItf)

Registers options with an object implementing the options interface.

Parameters:opts (OptionsItf) – An object implementing the options interface. Typically a command-line option parser.
class kaldi.decoder.LatticeFasterGrammarDecoder(fst, opts)[source]

Lattice generating faster grammar decoder.

Parameters:
advance_decoding(decodable:DecodableInterface, max_num_frames:int=default)

Advances decoding.

This will decode until there are no more frames ready in the decodable object, but if max_num_frames >= 0 it will decode no more than that many frames.

Parameters:
  • decodable (DecodableInterface) – The decodable object.
  • max_num_frames (int) – Max number of frames to decode. Defaults to -1 which means decode all available frames.
decode(decodable:DecodableInterface) → bool

Decodes all available frames in the decodable object.

This may block waiting for input if the decodable object blocks.

Parameters:decodable (DecodableInterface) – The decodable object.
Returns:True if any kind of traceback is available (not necessarily from a final state).
final_relative_cost() → float

Queries the final relative cost.

This method serves the same purpose as reached_final(), but gives more information. It returns the difference between the best (final-cost plus cost) of any token on the final frame, and the best cost of any token on the final frame. If it is infinity it means no final-states were present on the final frame. It will usually be nonnegative. If it not too positive (e.g. < 5) you can take it as an indication that we reached the final-state with reasonable likelihood.

Returns:The final relative cost.
finalize_decoding()

Finalizes decoding.

This function may be optionally called after the last call to advance_decoding(). It does an extra pruning step to prune the lattices output by get_lattice() and get_raw_lattice(). more accurately.

get_best_path(use_final_probs=True)

Gets best path as a lattice.

Parameters:use_final_probs (bool) – If True and a final state of the graph is reached, then the output will include final probabilities given by the graph. Otherwise all final probabilities are treated as one.
Returns:The best path.
Return type:LatticeVectorFst
Raises:RuntimeError – In the unusual circumstances where no tokens survive.
get_lattice(use_final_probs=True)

Gets the lattice-determinized compact lattice.

The output is a deterministic compact lattice with a unique path for each word sequence.

Parameters:use_final_probs (bool) – If True and a final state of the graph is reached, then the output will include final probabilities given by the graph. Otherwise all final probabilities are treated as one.
Returns:The lattice-determinized compact lattice.
Return type:CompactLatticeVectorFst
Raises:RuntimeError – In the unusual circumstances where no tokens survive.
get_options() → LatticeFasterDecoderOptions

Gets decoder options.

Returns:The decoder options.
get_raw_lattice(use_final_probs=True)

Gets raw state-level lattice.

The output raw lattice will be topologically sorted.

Parameters:use_final_probs (bool) – If True and a final state of the graph is reached, then the output will include final probabilities given by the graph. Otherwise all final probabilities are treated as one.
Returns:The state-level lattice.
Return type:LatticeVectorFst
Raises:RuntimeError – In the unusual circumstances where no tokens survive.
init_decoding()

Initializes decoding.

As an alternative to decode(), you can call init_decoding() and then call advance_decoding() (possibly multiple times).

num_frames_decoded() → int

Queries the number of frames already decoded.

Returns:The number of frames already decoded.
reached_final() → bool

Checks if a final state was active on the last decoded frame.

Returns:True if a final state was active on the last decoded frame.
set_options(opts:LatticeFasterDecoderOptions)

Sets decoder options.

Parameters:opts (LatticeFasterDecoderOptions) – Decoder options.
class kaldi.decoder.LatticeFasterOnlineDecoder(fst, opts)[source]

Lattice generating faster online decoder.

Similar to LatticeFasterDecoder but computes the best path without generating the entire raw lattice and finding the best path through it. Instead, it traces back through the lattice.

Parameters:
advance_decoding(decodable:DecodableInterface, max_num_frames:int=default)

Advances decoding.

This will decode until there are no more frames ready in the decodable object, but if max_num_frames >= 0 it will decode no more than that many frames.

Parameters:
  • decodable (DecodableInterface) – The decodable object.
  • max_num_frames (int) – Max number of frames to decode. Defaults to -1 which means decode all available frames.
decode(decodable:DecodableInterface) → bool

Decodes all available frames in the decodable object.

This may block waiting for input if the decodable object blocks.

Parameters:decodable (DecodableInterface) – The decodable object.
Returns:True if any kind of traceback is available (not necessarily from a final state).
final_relative_cost() → float

Queries the final relative cost.

This method serves the same purpose as reached_final(), but gives more information. It returns the difference between the best (final-cost plus cost) of any token on the final frame, and the best cost of any token on the final frame. If it is infinity it means no final-states were present on the final frame. It will usually be nonnegative. If it not too positive (e.g. < 5) you can take it as an indication that we reached the final-state with reasonable likelihood.

Returns:The final relative cost.
finalize_decoding()

Finalizes decoding.

This function may be optionally called after the last call to advance_decoding(). It does an extra pruning step to prune the lattices output by get_lattice() and get_raw_lattice(). more accurately.

get_best_path(use_final_probs=True)

Gets best path as a lattice.

Parameters:use_final_probs (bool) – If True and a final state of the graph is reached, then the output will include final probabilities given by the graph. Otherwise all final probabilities are treated as one.
Returns:The best path.
Return type:LatticeVectorFst
Raises:RuntimeError – In the unusual circumstances where no tokens survive.
get_lattice(use_final_probs=True)

Gets the lattice-determinized compact lattice.

The output is a deterministic compact lattice with a unique path for each word sequence.

Parameters:use_final_probs (bool) – If True and a final state of the graph is reached, then the output will include final probabilities given by the graph. Otherwise all final probabilities are treated as one.
Returns:The lattice-determinized compact lattice.
Return type:CompactLatticeVectorFst
Raises:RuntimeError – In the unusual circumstances where no tokens survive.
get_options() → LatticeFasterDecoderOptions

Gets decoder options.

Returns:The decoder options.
get_raw_lattice(use_final_probs=True)

Gets raw state-level lattice.

The output raw lattice will be topologically sorted.

Parameters:use_final_probs (bool) – If True and a final state of the graph is reached, then the output will include final probabilities given by the graph. Otherwise all final probabilities are treated as one.
Returns:The state-level lattice.
Return type:LatticeVectorFst
Raises:RuntimeError – In the unusual circumstances where no tokens survive.
get_raw_lattice_pruned(beam, use_final_probs=True)

Prunes and returns raw state-level lattice.

Behaves like get_raw_lattice() but only processes tokens whose extra-cost is smaller than the best-cost plus the specified beam. It is worthwhile to call this function only if beam is less than the lattice-beam specified in the decoder options. Otherwise, it returns essentially the same thing as get_raw_lattice(), but more slowly.

The output raw lattice will be topologically sorted.

Parameters:
  • beam (float) – Pruning beam.
  • use_final_probs (bool) – If True and a final state of the graph is reached, then the output will include final probabilities given by the graph. Otherwise all final probabilities are treated as one.
Returns:

The state-level lattice.

Return type:

LatticeVectorFst

Raises:

RuntimeError – In the unusual circumstances where no tokens survive.

init_decoding()

Initializes decoding.

As an alternative to decode(), you can call init_decoding() and then call advance_decoding() (possibly multiple times).

num_frames_decoded() → int

Queries the number of frames already decoded.

Returns:The number of frames already decoded.
reached_final() → bool

Checks if a final state was active on the last decoded frame.

Returns:True if a final state was active on the last decoded frame.
set_options(opts:LatticeFasterDecoderOptions)

Sets decoder options.

Parameters:opts (LatticeFasterDecoderOptions) – Decoder options.
class kaldi.decoder.LatticeFasterOnlineGrammarDecoder(fst, opts)[source]

Lattice generating faster online grammar decoder.

Similar to LatticeFasterGrammarDecoder but computes the best path without generating the entire raw lattice and finding the best path through it. Instead, it traces back through the lattice.

Parameters:
advance_decoding(decodable:DecodableInterface, max_num_frames:int=default)

Advances decoding.

This will decode until there are no more frames ready in the decodable object, but if max_num_frames >= 0 it will decode no more than that many frames.

Parameters:
  • decodable (DecodableInterface) – The decodable object.
  • max_num_frames (int) – Max number of frames to decode. Defaults to -1 which means decode all available frames.
decode(decodable:DecodableInterface) → bool

Decodes all available frames in the decodable object.

This may block waiting for input if the decodable object blocks.

Parameters:decodable (DecodableInterface) – The decodable object.
Returns:True if any kind of traceback is available (not necessarily from a final state).
final_relative_cost() → float

Queries the final relative cost.

This method serves the same purpose as reached_final(), but gives more information. It returns the difference between the best (final-cost plus cost) of any token on the final frame, and the best cost of any token on the final frame. If it is infinity it means no final-states were present on the final frame. It will usually be nonnegative. If it not too positive (e.g. < 5) you can take it as an indication that we reached the final-state with reasonable likelihood.

Returns:The final relative cost.
finalize_decoding()

Finalizes decoding.

This function may be optionally called after the last call to advance_decoding(). It does an extra pruning step to prune the lattices output by get_lattice() and get_raw_lattice(). more accurately.

get_best_path(use_final_probs=True)

Gets best path as a lattice.

Parameters:use_final_probs (bool) – If True and a final state of the graph is reached, then the output will include final probabilities given by the graph. Otherwise all final probabilities are treated as one.
Returns:The best path.
Return type:LatticeVectorFst
Raises:RuntimeError – In the unusual circumstances where no tokens survive.
get_lattice(use_final_probs=True)

Gets the lattice-determinized compact lattice.

The output is a deterministic compact lattice with a unique path for each word sequence.

Parameters:use_final_probs (bool) – If True and a final state of the graph is reached, then the output will include final probabilities given by the graph. Otherwise all final probabilities are treated as one.
Returns:The lattice-determinized compact lattice.
Return type:CompactLatticeVectorFst
Raises:RuntimeError – In the unusual circumstances where no tokens survive.
get_options() → LatticeFasterDecoderOptions

Gets decoder options.

Returns:The decoder options.
get_raw_lattice(use_final_probs=True)

Gets raw state-level lattice.

The output raw lattice will be topologically sorted.

Parameters:use_final_probs (bool) – If True and a final state of the graph is reached, then the output will include final probabilities given by the graph. Otherwise all final probabilities are treated as one.
Returns:The state-level lattice.
Return type:LatticeVectorFst
Raises:RuntimeError – In the unusual circumstances where no tokens survive.
get_raw_lattice_pruned(beam, use_final_probs=True)

Prunes and returns raw state-level lattice.

Behaves like get_raw_lattice() but only processes tokens whose extra-cost is smaller than the best-cost plus the specified beam. It is worthwhile to call this function only if beam is less than the lattice-beam specified in the decoder options. Otherwise, it returns essentially the same thing as get_raw_lattice(), but more slowly.

The output raw lattice will be topologically sorted.

Parameters:
  • beam (float) – Pruning beam.
  • use_final_probs (bool) – If True and a final state of the graph is reached, then the output will include final probabilities given by the graph. Otherwise all final probabilities are treated as one.
Returns:

The state-level lattice.

Return type:

LatticeVectorFst

Raises:

RuntimeError – In the unusual circumstances where no tokens survive.

init_decoding()

Initializes decoding.

As an alternative to decode(), you can call init_decoding() and then call advance_decoding() (possibly multiple times).

num_frames_decoded() → int

Queries the number of frames already decoded.

Returns:The number of frames already decoded.
reached_final() → bool

Checks if a final state was active on the last decoded frame.

Returns:True if a final state was active on the last decoded frame.
set_options(opts:LatticeFasterDecoderOptions)

Sets decoder options.

Parameters:opts (LatticeFasterDecoderOptions) – Decoder options.
class kaldi.decoder.TrainingGraphCompiler(trans_model, ctx_dep, lex_fst, disambig_syms, opts)[source]

Training graph compiler.

Parameters:
compile_graph(word_fst)[source]

Compiles a single training graph from a weighted acceptor.

Parameters:word_fst (StdVectorFst) – Weighted acceptor G at the word level.
Returns:The training graph HCLG.
Return type:StdVectorFst
compile_graph_from_text(transcript)[source]

Compiles a single training graph from a transcript.

Parameters:transcript (List[int]) – The input transcript.
Returns:The training graph HCLG.
Return type:StdVectorFst
compile_graphs(word_fsts)[source]

Compiles training graphs from weighted acceptors.

Parameters:word_fsts (List[StdVectorFst]) – Weighted acceptors at the word level.
Returns:The training graphs.
Return type:List[StdVectorFst]
compile_graphs_from_text(transcripts)[source]

Compiles training graphs from transcripts.

Parameters:transcripts (List[List[int]]) – The input transcripts.
Returns:The training graphs.
Return type:List[StdVectorFst]
class kaldi.decoder.TrainingGraphCompilerOptions

Options for training graph compiler.

register(opts:OptionsItf)

Registers options with an object implementing the options interface.

Parameters:opts (OptionsItf) – An object implementing the options interface. Typically a command-line option parser.
reorder

Reorder transition IDs for greater decoding efficiency.

rm_eps

Remove (most) epsilons before minimization.

Applicable only if disambiguation symbols are present.

self_loop_scale

Scale of self-loop vs. non-self-loop probability mass.

transition_scale

Scale of transition probabilities (excluding self-loops).

kaldi.decoder.grammar_to_vector_fst(grammar_fst:GrammarFst) → StdVectorFst

Converts a GrammarFST to a StdVectorFst.

kaldi.decoder.prepare_for_grammar_fst(nonterm_phones_offset:int, fst:StdVectorFst)

Prepares input FST for use in GrammarFST.