fst Namespace Reference

For an extended explanation of the framework of which grammar-fsts are a part, please see Support for grammars and graphs with on-the-fly parts. (i.e. More...

Namespaces

 internal
 
 pre_determinize_helpers
 

Classes

class  ArcIterator< GrammarFst >
 This is the overridden template for class ArcIterator for GrammarFst. More...
 
class  ArcIterator< TrivialFactorWeightFst< A, F > >
 
class  ArcticWeightTpl
 
class  BackoffDeterministicOnDemandFst
 This class wraps an Fst, representing a language model, using the interface for "BackoffDeterministicOnDemandFst". More...
 
class  CacheDeterministicOnDemandFst
 
class  CompactLatticeMinimizer
 
class  CompactLatticePusher
 
class  CompactLatticeWeightCommonDivisorTpl
 
class  CompactLatticeWeightTpl
 
class  ComposeDeterministicOnDemandFst
 
class  DeterministicOnDemandFst
 class DeterministicOnDemandFst is an "FST-like" base-class. More...
 
struct  DeterminizeLatticeOptions
 
struct  DeterminizeLatticePhonePrunedOptions
 
struct  DeterminizeLatticePrunedOptions
 
class  DeterminizerStar
 
class  DfsOrderVisitor
 
class  GrammarFst
 GrammarFst is an FST that is 'stitched together' from multiple FSTs, that can recursively incorporate each other. More...
 
struct  GrammarFstArc
 
class  GrammarFstPreparer
 
struct  IdentityFunction
 
class  InverseContextFst
 
class  InverseLeftBiphoneContextFst
 
class  LatticeDeterminizer
 
class  LatticeDeterminizerPruned
 
class  LatticeStringRepository
 
class  LatticeToStdMapper
 Class LatticeToStdMapper maps a LatticeArc to a normal arc (StdArc) by adding the elements of the LatticeArc weight. More...
 
class  LatticeWeightTpl
 
class  LmExampleDeterministicOnDemandFst
 This class is for didactic purposes, it does not really do anything. More...
 
class  MapInputSymbolsMapper
 
class  NaturalLess< CompactLatticeWeightTpl< LatticeWeightTpl< double >, int32 > >
 
class  NaturalLess< CompactLatticeWeightTpl< LatticeWeightTpl< float >, int32 > >
 
class  NaturalLess< CompactLatticeWeightTpl< LatticeWeightTpl< FloatType >, IntType > >
 
class  NaturalLess< LatticeWeightTpl< double > >
 
class  NaturalLess< LatticeWeightTpl< float > >
 
class  NaturalLess< LatticeWeightTpl< FloatType > >
 
class  PruneSpecialClass
 This class is used to implement the function PruneSpecial. More...
 
class  PushSpecialClass
 
struct  RandFstOptions
 
class  RemoveEpsLocalClass
 
class  RemoveSomeInputSymbolsMapper
 
struct  ReweightPlusDefault
 
struct  ReweightPlusLogArc
 
class  ScaleDeterministicOnDemandFst
 Class ScaleDeterministicOnDemandFst takes another DeterministicOnDemandFst and scales the weights (like applying a language-model scale). More...
 
class  StateIterator< TrivialFactorWeightFst< A, F > >
 
class  StdToLatticeMapper
 Class StdToLatticeMapper maps a normal arc (StdArc) to a LatticeArc by putting the StdArc weight as the first element of the LatticeWeight. More...
 
class  StringRepository
 
struct  TableComposeCache
 TableComposeCache lets us do multiple compositions while caching the same matcher. More...
 
struct  TableComposeOptions
 
class  TableMatcher
 
class  TableMatcherImpl
 
struct  TableMatcherOptions
 TableMatcher is a matcher specialized for the case where the output side of the left FST always has either all-epsilons coming out of a state, or a majority of the symbol table. More...
 
struct  TestFunctor
 
class  TrivialFactorWeightFst
 TrivialFactorWeightFst takes as template parameter a FactorIterator as defined above. More...
 
struct  TrivialFactorWeightOptions
 
class  UnweightedNgramFst
 The class UnweightedNgramFst is a DeterministicOnDemandFst whose states encode an n-gram history. More...
 
class  VectorFstTplHolder
 

Typedefs

typedef fst::StdArc StdArc
 
typedef fst::StdArc::Label Label
 
typedef fst::StdArc::StateId StateId
 
typedef fst::StdVectorFst StdVectorFst
 
typedef fst::StdArc::Weight Weight
 
typedef unsigned char StatePropertiesType
 
typedef VectorFstTplHolder< StdArcVectorFstHolder
 
typedef float BaseFloat
 
typedef LatticeWeightTpl< BaseFloatLatticeWeight
 
typedef CompactLatticeWeightTpl< LatticeWeight, int32 > CompactLatticeWeight
 
typedef CompactLatticeWeightCommonDivisorTpl< LatticeWeight, int32 > CompactLatticeWeightCommonDivisor
 
typedef ArcticWeightTpl< floatArcticWeight
 

Enumerations

enum  { kStateHasEpsilonArcsEntering = 0x1, kStateHasNonEpsilonArcsEntering = 0x2, kStateHasEpsilonArcsLeaving = 0x4, kStateHasNonEpsilonArcsLeaving = 0x8 }
 
enum  StatePropertiesEnum {
  kStateFinal = 0x1, kStateInitial = 0x2, kStateArcsIn = 0x4, kStateMultipleArcsIn = 0x8,
  kStateArcsOut = 0x10, kStateMultipleArcsOut = 0x20, kStateOlabelsOut = 0x40, kStateIlabelsOut = 0x80
}
 
enum  NonterminalValues {
  kNontermBos = 0, kNontermBegin = 1, kNontermEnd = 2, kNontermReenter = 3,
  kNontermUserDefined = 4, kNontermMediumNumber = 1000, kNontermBigNumber = 10000000
}
 An anonymous enum to define some values for symbols used in our grammar-fst framework. More...
 

Functions

template<class Arc >
static VectorFst< Arc > * GenAcceptorFromSequence (const vector< typename Arc::Label > &symbols, float cost)
 
template<class Arc >
static float CheckPhones (const VectorFst< Arc > &linear_fst, const vector< typename Arc::Label > &phone_ids, const vector< typename Arc::Label > &disambig_ids, const vector< typename Arc::Label > &phone_seq, const vector< vector< typename Arc::Label > > &ilabel_info, int N, int P)
 
template<class Arc >
static VectorFst< Arc > * GenRandPhoneSeq (vector< typename Arc::Label > &phone_syms, vector< typename Arc::Label > &disambig_syms, typename Arc::Label subsequential_symbol, int num_subseq_syms, float seq_prob, vector< typename Arc::Label > *phoneseq_out)
 
static void TestContextFst (bool verbose, bool use_matcher)
 
void ComposeContext (const vector< int32 > &disambig_syms_in, int32 context_width, int32 central_position, VectorFst< StdArc > *ifst, VectorFst< StdArc > *ofst, vector< vector< int32 > > *ilabels_out, bool project_ifst)
 Used in the command-line tool fstcomposecontext. More...
 
void AddSubsequentialLoop (StdArc::Label subseq_symbol, MutableFst< StdArc > *fst)
 Modifies an FST so that it transuces the same paths, but the input side of the paths can all have the subsequential symbol '$' appended to them any number of times (we could easily specify the number of times, but accepting any number of repetitions is just more convenient). More...
 
void WriteILabelInfo (std::ostream &os, bool binary, const vector< vector< int32 > > &info)
 Utility function for writing ilabel-info vectors to disk. More...
 
void ReadILabelInfo (std::istream &is, bool binary, vector< vector< int32 > > *info)
 Utility function for reading ilabel-info vectors from disk. More...
 
SymbolTable * CreateILabelInfoSymbolTable (const vector< vector< int32 > > &info, const SymbolTable &phones_symtab, std::string separator, std::string initial_disambig)
 The following function is mainly of use for printing and debugging. More...
 
void WriteILabelInfo (std::ostream &os, bool binary, const std::vector< std::vector< int32 > > &ilabel_info)
 Utility function for writing ilabel-info vectors to disk. More...
 
void ReadILabelInfo (std::istream &is, bool binary, std::vector< std::vector< int32 > > *ilabel_info)
 Utility function for reading ilabel-info vectors from disk. More...
 
SymbolTable * CreateILabelInfoSymbolTable (const std::vector< std::vector< int32 > > &ilabel_info, const SymbolTable &phones_symtab, std::string separator, std::string disambig_prefix)
 The following function is mainly of use for printing and debugging. More...
 
void ComposeContext (const std::vector< int32 > &disambig_syms, int32 context_width, int32 central_position, VectorFst< StdArc > *ifst, VectorFst< StdArc > *ofst, std::vector< std::vector< int32 > > *ilabels_out, bool project_ifst=false)
 Used in the command-line tool fstcomposecontext. More...
 
template<class Arc >
void ComposeDeterministicOnDemand (const Fst< Arc > &fst1, DeterministicOnDemandFst< Arc > *fst2, MutableFst< Arc > *fst_composed)
 
template<class Arc >
void ComposeDeterministicOnDemandInverse (const Fst< Arc > &fst1, DeterministicOnDemandFst< Arc > *fst2, MutableFst< Arc > *fst_composed)
 This function does '*fst_composed = Compose(Inverse(*fst2), fst1)' Note that the arguments are reversed; this is unfortunate but it's because the fst2 argument needs to be non-const and non-const arguments must follow const ones. More...
 
bool FileExists (std::string strFilename)
 
StdVectorFstCreateBackoffFst ()
 
StdVectorFstCreateResultFst ()
 
void DeleteTestFst (StdVectorFst *fst)
 
Weight WalkSinglePath (StdVectorFst *ifst, DeterministicOnDemandFst< StdArc > *dfst)
 
void TestBackoffAndCache ()
 
void TestCompose ()
 
template<class Weight , class IntType >
bool DeterminizeLattice (const Fst< ArcTpl< Weight > > &ifst, MutableFst< ArcTpl< Weight > > *ofst, DeterminizeLatticeOptions opts=DeterminizeLatticeOptions(), bool *debug_ptr=NULL)
 This function implements the normal version of DeterminizeLattice, in which the output strings are represented using sequences of arcs, where all but the first one has an epsilon on the input side. More...
 
template<class Weight , class IntType >
bool DeterminizeLattice (const Fst< ArcTpl< Weight > > &ifst, MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, IntType > > > *ofst, DeterminizeLatticeOptions opts, bool *debug_ptr)
 
void TestLatticeStringRepository ()
 
template<class Arc >
void TestDeterminizeLattice ()
 
template<class Arc >
void TestDeterminizeLattice2 ()
 
template<class F >
bool DeterminizeStar (F &ifst, MutableFst< typename F::Arc > *ofst, float delta=kDelta, bool *debug_ptr=NULL, int max_states=-1, bool allow_partial=false)
 This function implements the normal version of DeterminizeStar, in which the output strings are represented using sequences of arcs, where all but the first one has an epsilon on the input side. More...
 
template<class F >
bool DeterminizeStar (F &ifst, MutableFst< GallicArc< typename F::Arc > > *ofst, float delta, bool *debug_ptr, int max_states, bool allow_partial)
 
template<class Arc >
void TestDeterminizeGeneral ()
 
template<class Arc >
void TestDeterminize ()
 
template<class Arc >
void TestDeterminize2 ()
 
template<class Arc >
void TestPush ()
 
template<class Arc >
void TestMinimize ()
 
template<class Arc , class inttype >
void TestStringRepository ()
 
template<class Arc >
void ComputeStateInfo (const VectorFst< Arc > &fst, std::vector< char > *epsilon_info)
 This function will set epsilon_info to have size equal to the NumStates() of the FST, containing a logical-or of the enum values kStateHasEpsilonArcsEntering, kStateHasNonEpsilonArcsEntering, kStateHasEpsilonArcsLeaving, and kStateHasNonEpsilonArcsLeaving. More...
 
template<class Arc >
void EnsureEpsilonProperty (VectorFst< Arc > *fst)
 This function modifies the fst (while maintaining equivalence) in such a way that, after the modification, all states of the FST which have epsilon-arcs entering them, have no non-epsilon arcs entering them, and all states which have epsilon-arcs leaving them, have no non-epsilon arcs leaving them. More...
 
void TestEnsureEpsilonProperty ()
 
template<class Arc >
void GetStateProperties (const Fst< Arc > &fst, typename Arc::StateId max_state, std::vector< StatePropertiesType > *props)
 This function works out various properties of the states in the FST, using the bit properties defined in StatePropertiesEnum. More...
 
template<class Arc , class I >
void Factor (const Fst< Arc > &fst, MutableFst< Arc > *ofst, std::vector< std::vector< I > > *symbols)
 Factor identifies linear chains of states with an olabel (if any) only on the first arc of the chain, and possibly a sequence of ilabels; it outputs an FST with different symbols on the input that represent sequences of the original input symbols; it outputs the mapping from the new symbol to sequences of original symbols, as "symbols" [zero is reserved for epsilon]. More...
 
template<class Arc >
void Factor (const Fst< Arc > &fst, MutableFst< Arc > *ofst1, MutableFst< Arc > *ofst2)
 This is a more conventional interface of Factor that outputs the result as two FSTs. More...
 
template<class Arc , class I >
void ExpandInputSequences (const std::vector< std::vector< I > > &sequences, MutableFst< Arc > *fst)
 ExpandInputSequences expands out the input symbols into sequences of input symbols. More...
 
template<class Arc , class I >
void CreateFactorFst (const std::vector< std::vector< I > > &sequences, MutableFst< Arc > *fst)
 The function CreateFactorFst will create an FST that expands out the "factors" that are the indices of the "sequences" array, into linear sequences of symbols. More...
 
template<class Arc , class I >
void CreateMapFst (const std::vector< I > &symbol_map, MutableFst< Arc > *fst)
 CreateMapFst will create an FST representing this symbol_map. More...
 
template<class Arc >
static void TestFactor ()
 
template<class Arc >
Arc::Label HighestNumberedOutputSymbol (const Fst< Arc > &fst)
 Returns the highest numbered output symbol id of the FST (or zero for an empty FST. More...
 
template<class Arc >
Arc::Label HighestNumberedInputSymbol (const Fst< Arc > &fst)
 Returns the highest numbered input symbol id of the FST (or zero for an empty FST. More...
 
template<class Arc >
Arc::StateId NumArcs (const ExpandedFst< Arc > &fst)
 Returns the total number of arcs in an FST. More...
 
template<class Arc , class I >
void GetOutputSymbols (const Fst< Arc > &fst, bool include_eps, std::vector< I > *symbols)
 GetOutputSymbols gets the list of symbols on the output of fst (including epsilon, if include_eps == true) More...
 
template<class Arc , class I >
void GetInputSymbols (const Fst< Arc > &fst, bool include_eps, std::vector< I > *symbols)
 GetInputSymbols gets the list of symbols on the input of fst (including epsilon, if include_eps == true), as a sorted, unique list. More...
 
template<class Arc , class I >
void RemoveSomeInputSymbols (const std::vector< I > &to_remove, MutableFst< Arc > *fst)
 RemoveSomeInputSymbols removes any symbol that appears in "to_remove", from the input side of the FST, replacing them with epsilon. More...
 
template<class Arc , class I >
void MapInputSymbols (const std::vector< I > &symbol_mapping, MutableFst< Arc > *fst)
 
template<class Arc , class I >
bool GetLinearSymbolSequence (const Fst< Arc > &fst, std::vector< I > *isymbols_out, std::vector< I > *osymbols_out, typename Arc::Weight *tot_weight_out)
 GetLinearSymbolSequence gets the symbol sequence from a linear FST. More...
 
template<class Arc >
void ConvertNbestToVector (const Fst< Arc > &fst, std::vector< VectorFst< Arc > > *fsts_out)
 This function converts an FST with a special structure, which is output by the OpenFst functions ShortestPath and RandGen, and converts them into a std::vector of separate FSTs. More...
 
template<class Arc >
void NbestAsFsts (const Fst< Arc > &fst, size_t n, std::vector< VectorFst< Arc > > *fsts_out)
 Takes the n-shortest-paths (using ShortestPath), but outputs the result as a vector of up to n fsts. More...
 
template<class Arc , class I >
void MakeLinearAcceptorWithAlternatives (const std::vector< std::vector< I > > &labels, MutableFst< Arc > *ofst)
 Creates an unweighted acceptor with a linear structure, with alternatives at each position. More...
 
template<class Arc , class I >
void MakeLinearAcceptor (const std::vector< I > &labels, MutableFst< Arc > *ofst)
 Creates unweighted linear acceptor from symbol sequence. More...
 
template<class I >
void GetSymbols (const SymbolTable &symtab, bool include_eps, std::vector< I > *syms_out)
 
template<class Arc >
void SafeDeterminizeWrapper (MutableFst< Arc > *ifst, MutableFst< Arc > *ofst, float delta=kDelta)
 Does PreDeterminize and DeterminizeStar and then removes the disambiguation symbols. More...
 
template<class Arc >
void SafeDeterminizeMinimizeWrapper (MutableFst< Arc > *ifst, VectorFst< Arc > *ofst, float delta=kDelta)
 SafeDeterminizeMinimizeWapper is as SafeDeterminizeWrapper except that it also minimizes (encoded minimization, which is safe). More...
 
void DeterminizeStarInLog (VectorFst< StdArc > *fst, float delta, bool *debug_ptr, int max_states)
 
void DeterminizeInLog (VectorFst< StdArc > *fst)
 
void SafeDeterminizeMinimizeWrapperInLog (VectorFst< StdArc > *ifst, VectorFst< StdArc > *ofst, float delta=kDelta)
 SafeDeterminizeMinimizeWapperInLog is as SafeDeterminizeMinimizeWrapper except it first casts tothe log semiring. More...
 
void SafeDeterminizeWrapperInLog (VectorFst< StdArc > *ifst, VectorFst< StdArc > *ofst, float delta)
 
template<class Arc >
void RemoveWeights (MutableFst< Arc > *ifst)
 
template<class Arc >
bool PrecedingInputSymbolsAreSame (bool start_is_epsilon, const Fst< Arc > &fst)
 Returns true if and only if the FST is such that the input symbols on arcs entering any given state all have the same value. More...
 
template<class Arc , class F >
bool PrecedingInputSymbolsAreSameClass (bool start_is_epsilon, const Fst< Arc > &fst, const F &f)
 This is as PrecedingInputSymbolsAreSame, but with a functor f that maps labels to classes. More...
 
template<class Arc >
bool FollowingInputSymbolsAreSame (bool end_is_epsilon, const Fst< Arc > &fst)
 Returns true if and only if the FST is such that the input symbols on arcs exiting any given state all have the same value. More...
 
template<class Arc , class F >
bool FollowingInputSymbolsAreSameClass (bool end_is_epsilon, const Fst< Arc > &fst, const F &f)
 
template<class Arc >
void MakePrecedingInputSymbolsSame (bool start_is_epsilon, MutableFst< Arc > *fst)
 MakePrecedingInputSymbolsSame ensures that all arcs entering any given fst state have the same input symbol. More...
 
template<class Arc , class F >
void MakePrecedingInputSymbolsSameClass (bool start_is_epsilon, MutableFst< Arc > *fst, const F &f)
 As MakePrecedingInputSymbolsSame, but takes a functor object that maps labels to classes. More...
 
template<class Arc >
void MakeFollowingInputSymbolsSame (bool end_is_epsilon, MutableFst< Arc > *fst)
 MakeFollowingInputSymbolsSame ensures that all arcs exiting any given fst state have the same input symbol. More...
 
template<class Arc , class F >
void MakeFollowingInputSymbolsSameClass (bool end_is_epsilon, MutableFst< Arc > *fst, const F &f)
 As MakeFollowingInputSymbolsSame, but takes a functor object that maps labels to classes. More...
 
template<class Arc >
VectorFst< Arc > * MakeLoopFst (const std::vector< const ExpandedFst< Arc > * > &fsts)
 MakeLoopFst creates an FST that has a state that is both initial and final (weight == Weight::One()), and for each non-NULL pointer fsts[i], it has an arc out whose output-symbol is i and which goes to a sub-graph whose input language is equivalent to fsts[i], where the final-state becomes a transition to the loop-state. More...
 
template<class Arc >
void ClearSymbols (bool clear_input, bool clear_output, MutableFst< Arc > *fst)
 ClearSymbols sets all the symbols on the input and/or output side of the FST to zero, as specified. More...
 
template<class Arc >
void ApplyProbabilityScale (float scale, MutableFst< Arc > *fst)
 ApplyProbabilityScale is applicable to FSTs in the log or tropical semiring. More...
 
template<class Arc >
ssize_t FindSelfLoopWithILabel (const Fst< Arc > &fst, typename Arc::StateId s)
 
template<class Arc >
bool EqualAlign (const Fst< Arc > &ifst, typename Arc::StateId length, int rand_seed, MutableFst< Arc > *ofst, int num_retries=10)
 EqualAlign is similar to RandGen, but it generates a sequence with exactly "length" input symbols. More...
 
template<class Arc >
void RemoveUselessArcs (MutableFst< Arc > *fst)
 
template<class Arc >
void PhiCompose (const Fst< Arc > &fst1, const Fst< Arc > &fst2, typename Arc::Label phi_label, MutableFst< Arc > *ofst)
 
template<class Arc >
void PropagateFinalInternal (typename Arc::Label phi_label, typename Arc::StateId s, MutableFst< Arc > *fst)
 
template<class Arc >
void PropagateFinal (typename Arc::Label phi_label, MutableFst< Arc > *fst)
 
template<class Arc >
void RhoCompose (const Fst< Arc > &fst1, const Fst< Arc > &fst2, typename Arc::Label rho_label, MutableFst< Arc > *ofst)
 
template<>
bool IsStochasticFst (const Fst< LogArc > &fst, float delta, LogArc::Weight *min_sum, LogArc::Weight *max_sum)
 
template<class Arc >
bool IsStochasticFst (const Fst< Arc > &fst, float delta=kDelta, typename Arc::Weight *min_sum=NULL, typename Arc::Weight *max_sum=NULL)
 This function returns true if, in the semiring of the FST, the sum (within the semiring) of all the arcs out of each state in the FST is one, to within delta. More...
 
bool IsStochasticFstInLog (const Fst< StdArc > &fst, float delta, StdArc::Weight *min_sum, StdArc::Weight *max_sum)
 
template<class Arc , class I >
void TestMakeLinearAcceptor ()
 
template<class Arc >
void TestDeterminizeStarInLog ()
 
template<class Arc >
void TestSafeDeterminizeWrapper ()
 
void TestPushInLog ()
 
template<class Arc >
void TestAcceptorMinimize ()
 
template<class Arc >
void TestMakeSymbolsSame ()
 
template<class Arc >
void TestMakeSymbolsSameClass ()
 
template<class Arc >
VectorFst< Arc > * MakeLoopFstCompare (const vector< const ExpandedFst< Arc > *> &fsts)
 
template<class Arc >
void TestMakeLoopFst ()
 
template<class Arc >
void TestEqualAlign ()
 
template<class Arc >
void Print (const Fst< Arc > &fst, std::string message)
 
template<class Arc >
void TestRemoveUselessArcs ()
 
template<ReweightType rtype>
void PushInLog (VectorFst< StdArc > *fst, uint32 ptype, float delta=kDelta)
 
template<class Arc >
void MinimizeEncoded (VectorFst< Arc > *fst, float delta=kDelta)
 
void ComposeContextLeftBiphone (int32 nonterm_phones_offset, const vector< int32 > &disambig_syms_in, const VectorFst< StdArc > &ifst, VectorFst< StdArc > *ofst, std::vector< std::vector< int32 > > *ilabels)
 This is a variant of the function ComposeContext() which is to be used with our "grammar FST" framework (see The ContextFst object, i.e. More...
 
int32 GetEncodingMultiple (int32 nonterm_phones_offset)
 
void ComposeContextLeftBiphone (int32 nonterm_phones_offset, const std::vector< int32 > &disambig_syms, const VectorFst< StdArc > &ifst, VectorFst< StdArc > *ofst, std::vector< std::vector< int32 > > *ilabels)
 This is a variant of the function ComposeContext() which is to be used with our "grammar FST" framework (see The ContextFst object, i.e. More...
 
template<class Arc >
void WriteFstKaldi (std::ostream &os, bool binary, const VectorFst< Arc > &t)
 
template<class W >
bool StrToWeight (const std::string &s, bool allow_zero, W *w)
 
template<class Arc >
void ReadFstKaldi (std::istream &is, bool binary, VectorFst< Arc > *fst)
 
VectorFst< StdArc > * ReadFstKaldi (std::string rxfilename)
 
Fst< StdArc > * ReadFstKaldiGeneric (std::string rxfilename, bool throw_on_err)
 
VectorFst< StdArc > * CastOrConvertToVectorFst (Fst< StdArc > *fst)
 
void ReadFstKaldi (std::string rxfilename, fst::StdVectorFst *ofst)
 
void WriteFstKaldi (const VectorFst< StdArc > &fst, std::string wxfilename)
 
fst::VectorFst< fst::StdArc > * ReadAndPrepareLmFst (std::string rxfilename)
 
void ReadFstKaldi (std::string rxfilename, VectorFst< StdArc > *ofst)
 
template<class Weight , class Int >
void ConvertLattice (const ExpandedFst< ArcTpl< Weight > > &ifst, MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, Int > > > *ofst, bool invert=true)
 Convert lattice from a normal FST to a CompactLattice FST. More...
 
template<class Weight , class Int >
void ConvertLattice (const ExpandedFst< ArcTpl< CompactLatticeWeightTpl< Weight, Int > > > &ifst, MutableFst< ArcTpl< Weight > > *ofst, bool invert=true)
 Convert lattice CompactLattice format to Lattice. More...
 
template<class WeightIn , class WeightOut >
void ConvertLattice (const ExpandedFst< ArcTpl< WeightIn > > &ifst, MutableFst< ArcTpl< WeightOut > > *ofst)
 Convert between CompactLattices and Lattices of different floating point types... More...
 
template<class Weight , class ScaleFloat >
void ScaleLattice (const std::vector< std::vector< ScaleFloat > > &scale, MutableFst< ArcTpl< Weight > > *fst)
 Scales the pairs of weights in LatticeWeight or CompactLatticeWeight by viewing the pair (a, b) as a 2-vector and pre-multiplying by the 2x2 matrix in "scale". More...
 
template<class Weight , class Int >
void RemoveAlignmentsFromCompactLattice (MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, Int > > > *fst)
 Removes state-level alignments (the strings that are part of the weights). More...
 
template<class Weight , class Int >
bool CompactLatticeHasAlignment (const ExpandedFst< ArcTpl< CompactLatticeWeightTpl< Weight, Int > > > &fst)
 Returns true if lattice has alignments, i.e. More...
 
template<class Real >
void ConvertFstToLattice (const ExpandedFst< ArcTpl< TropicalWeight > > &ifst, MutableFst< ArcTpl< LatticeWeightTpl< Real > > > *ofst)
 Converts TropicalWeight to LatticeWeight (puts all the weight on the first float in the lattice's pair). More...
 
template<class Weight , class Int >
void TestConvert (bool invert)
 
template<class Weight , class Int >
void TestShortestPath ()
 
template<class Int >
void TestConvert2 ()
 
template<class Weight , class Int >
void TestConvertPair (bool invert)
 
template<class Weight , class Int >
void TestScalePair (bool invert)
 
template<class Int >
void ConvertLattice (const ExpandedFst< ArcTpl< LatticeWeightTpl< float > > > &ifst, MutableFst< ArcTpl< CompactLatticeWeightTpl< LatticeWeightTpl< double >, Int > > > *ofst)
 
template<class Int >
void ConvertLattice (const ExpandedFst< ArcTpl< LatticeWeightTpl< double > > > &ifst, MutableFst< ArcTpl< CompactLatticeWeightTpl< LatticeWeightTpl< float >, Int > > > *ofst)
 
template<class Int >
void ConvertLattice (const ExpandedFst< ArcTpl< CompactLatticeWeightTpl< LatticeWeightTpl< double >, Int > > > &ifst, MutableFst< ArcTpl< LatticeWeightTpl< float > > > *ofst)
 Converts CompactLattice with double to Lattice with float. More...
 
template<class Int >
void ConvertLattice (const ExpandedFst< ArcTpl< CompactLatticeWeightTpl< LatticeWeightTpl< float >, Int > > > &ifst, MutableFst< ArcTpl< LatticeWeightTpl< double > > > *ofst)
 Converts CompactLattice with float to Lattice with double. More...
 
std::vector< std::vector< double > > DefaultLatticeScale ()
 Returns a default 2x2 matrix scaling factor for LatticeWeight. More...
 
std::vector< std::vector< double > > AcousticLatticeScale (double acwt)
 
std::vector< std::vector< double > > GraphLatticeScale (double lmwt)
 
std::vector< std::vector< double > > LatticeScale (double lmwt, double acwt)
 
template<class Weight , class Int >
void PruneCompactLattice (Weight beam, MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, Int > > > *fst)
 
LatticeWeight RandomLatticeWeight ()
 
CompactLatticeWeight RandomCompactLatticeWeight ()
 
void LatticeWeightTest ()
 
void CompactLatticeWeightTest ()
 
template<class FloatType >
std::ostream & operator<< (std::ostream &strm, const LatticeWeightTpl< FloatType > &w)
 
template<class FloatType >
std::istream & operator>> (std::istream &strm, LatticeWeightTpl< FloatType > &w)
 
template<class FloatType , class ScaleFloatType >
LatticeWeightTpl< FloatType > ScaleTupleWeight (const LatticeWeightTpl< FloatType > &w, const std::vector< std::vector< ScaleFloatType > > &scale)
 
template<class FloatType , class ScaleFloatType >
PairWeight< TropicalWeightTpl< FloatType >, TropicalWeightTpl< FloatType > > ScaleTupleWeight (const PairWeight< TropicalWeightTpl< FloatType >, TropicalWeightTpl< FloatType > > &w, const std::vector< std::vector< ScaleFloatType > > &scale)
 
template<class FloatType >
bool operator== (const LatticeWeightTpl< FloatType > &wa, const LatticeWeightTpl< FloatType > &wb)
 
template<class FloatType >
bool operator!= (const LatticeWeightTpl< FloatType > &wa, const LatticeWeightTpl< FloatType > &wb)
 
template<class FloatType >
int Compare (const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2)
 Compare returns -1 if w1 < w2, +1 if w1 > w2, and 0 if w1 == w2. More...
 
template<class FloatType >
LatticeWeightTpl< FloatType > Plus (const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2)
 
template<class FloatType >
LatticeWeightTpl< FloatType > Times (const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2)
 
template<class FloatType >
LatticeWeightTpl< FloatType > Divide (const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2, DivideType typ=DIVIDE_ANY)
 
template<class FloatType >
bool ApproxEqual (const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2, float delta=kDelta)
 
template<class WeightType , class IntType >
bool operator== (const CompactLatticeWeightTpl< WeightType, IntType > &w1, const CompactLatticeWeightTpl< WeightType, IntType > &w2)
 
template<class WeightType , class IntType >
bool operator!= (const CompactLatticeWeightTpl< WeightType, IntType > &w1, const CompactLatticeWeightTpl< WeightType, IntType > &w2)
 
template<class WeightType , class IntType >
bool ApproxEqual (const CompactLatticeWeightTpl< WeightType, IntType > &w1, const CompactLatticeWeightTpl< WeightType, IntType > &w2, float delta=kDelta)
 
template<class WeightType , class IntType >
int Compare (const CompactLatticeWeightTpl< WeightType, IntType > &w1, const CompactLatticeWeightTpl< WeightType, IntType > &w2)
 
int Compare (const TropicalWeight &w1, const TropicalWeight &w2)
 
template<class WeightType , class IntType >
CompactLatticeWeightTpl< WeightType, IntType > Plus (const CompactLatticeWeightTpl< WeightType, IntType > &w1, const CompactLatticeWeightTpl< WeightType, IntType > &w2)
 
template<class WeightType , class IntType >
CompactLatticeWeightTpl< WeightType, IntType > Times (const CompactLatticeWeightTpl< WeightType, IntType > &w1, const CompactLatticeWeightTpl< WeightType, IntType > &w2)
 
template<class WeightType , class IntType >
CompactLatticeWeightTpl< WeightType, IntType > Divide (const CompactLatticeWeightTpl< WeightType, IntType > &w1, const CompactLatticeWeightTpl< WeightType, IntType > &w2, DivideType div=DIVIDE_ANY)
 
template<class WeightType , class IntType >
std::ostream & operator<< (std::ostream &strm, const CompactLatticeWeightTpl< WeightType, IntType > &w)
 
template<class WeightType , class IntType >
std::istream & operator>> (std::istream &strm, CompactLatticeWeightTpl< WeightType, IntType > &w)
 
template<class Weight , class IntType , class ScaleFloatType >
CompactLatticeWeightTpl< Weight, IntType > ScaleTupleWeight (const CompactLatticeWeightTpl< Weight, IntType > &w, const std::vector< std::vector< ScaleFloatType > > &scale)
 Scales the pair (a, b) of floating-point weights inside a CompactLatticeWeight by premultiplying it (viewed as a vector) by a 2x2 matrix "scale". More...
 
template<class Float1 , class Float2 >
void ConvertLatticeWeight (const LatticeWeightTpl< Float1 > &w_in, LatticeWeightTpl< Float2 > *w_out)
 Define some ConvertLatticeWeight functions that are used in various lattice conversions... More...
 
template<class Float1 , class Float2 , class Int >
void ConvertLatticeWeight (const CompactLatticeWeightTpl< LatticeWeightTpl< Float1 >, Int > &w_in, CompactLatticeWeightTpl< LatticeWeightTpl< Float2 >, Int > *w_out)
 
template<class Float1 , class Float2 >
void ConvertLatticeWeight (const LatticeWeightTpl< Float1 > &w_in, TropicalWeightTpl< Float2 > *w_out)
 
template<class Float >
double ConvertToCost (const LatticeWeightTpl< Float > &w)
 
template<class Float , class Int >
double ConvertToCost (const CompactLatticeWeightTpl< LatticeWeightTpl< Float >, Int > &w)
 
template<class Float >
double ConvertToCost (const TropicalWeightTpl< Float > &w)
 
template<class Arc , class Int >
void PreDeterminize (MutableFst< Arc > *fst, typename Arc::Label first_new_sym, std::vector< Int > *symsOut)
 
template<class Label >
void CreateNewSymbols (SymbolTable *input_sym_table, int nSym, std::string prefix, std::vector< Label > *symsOut)
 
template<class Arc >
void AddSelfLoops (MutableFst< Arc > *fst, std::vector< typename Arc::Label > &isyms, std::vector< typename Arc::Label > &osyms)
 AddSelfLoops is a function you will probably want to use alongside PreDeterminize, to add self-loops to any FSTs that you compose on the left hand side of the one modified by PreDeterminize. More...
 
template<class Arc >
int64 DeleteISymbols (MutableFst< Arc > *fst, std::vector< typename Arc::Label > isyms)
 
template<class Arc >
Arc::StateId CreateSuperFinal (MutableFst< Arc > *fst)
 
template<class Arc >
void TestPreDeterminize ()
 
template<class Arc >
void TestAddSelfLoops ()
 
template<class Arc >
void PruneSpecial (const Fst< Arc > &ifst, VectorFst< Arc > *ofst, typename Arc::Weight beam, size_t max_states=0)
 The function PruneSpecial is like the standard OpenFst function "prune", except it does not expand the entire "ifst"- this is useful for cases where ifst is an on-demand FST such as a ComposeFst and we don't want to visit it all. More...
 
static void TestPruneSpecial ()
 
static void TestPushSpecial ()
 
void PushSpecial (VectorFst< StdArc > *fst, float delta)
 
template<class Arc >
VectorFst< Arc > * RandFst (RandFstOptions opts=RandFstOptions())
 Returns a random FST. More...
 
template<class Arc >
VectorFst< Arc > * RandPairFst (RandFstOptions opts=RandFstOptions())
 Returns a random FST. More...
 
template<class Arc >
void RemoveEpsLocal (MutableFst< Arc > *fst)
 RemoveEpsLocal remove some (but not necessarily all) epsilons in an FST, using an algorithm that is guaranteed to never increase the number of arcs in the FST (and will also never increase the number of states). More...
 
void RemoveEpsLocalSpecial (MutableFst< StdArc > *fst)
 As RemoveEpsLocal but takes care to preserve stochasticity when cast to LogArc. More...
 
template<class Arc >
static void TestRemoveEpsLocal ()
 
static void TestRemoveEpsLocalSpecial ()
 
template<class Arc >
void TestTableMatcher (bool connect, bool left)
 
template<class Arc >
void TestTableMatcherCacheLeft (bool connect)
 
template<class Arc >
void TestTableMatcherCacheRight (bool connect)
 
template<class Arc >
void TableCompose (const Fst< Arc > &ifst1, const Fst< Arc > &ifst2, MutableFst< Arc > *ofst, const TableComposeOptions &opts=TableComposeOptions())
 
template<class Arc >
void TableCompose (const Fst< Arc > &ifst1, const Fst< Arc > &ifst2, MutableFst< Arc > *ofst, TableComposeCache< Fst< Arc > > *cache)
 
template<class Arc >
void TestFactor ()
 
static ConstFst< StdArc > * ReadConstFstFromStream (std::istream &is)
 
static void InputDeterminizeSingleState (StdArc::StateId s, VectorFst< StdArc > *fst)
 This utility function input-determinizes a specified state s of the FST 'fst'. More...
 
void PrepareForGrammarFst (int32 nonterm_phones_offset, VectorFst< StdArc > *fst)
 This function prepares 'ifst' for use in GrammarFst: it ensures that it has the expected properties, changing it slightly as needed. More...
 
void CopyToVectorFst (GrammarFst *grammar_fst, VectorFst< StdArc > *vector_fst)
 This function copies a GrammarFst to a VectorFst (intended mostly for testing and comparison purposes). More...
 
template<class T >
ArcticWeightTpl< T > Plus (const ArcticWeightTpl< T > &w1, const ArcticWeightTpl< T > &w2)
 
ArcticWeightTpl< floatPlus (const ArcticWeightTpl< float > &w1, const ArcticWeightTpl< float > &w2)
 
ArcticWeightTpl< double > Plus (const ArcticWeightTpl< double > &w1, const ArcticWeightTpl< double > &w2)
 
template<class T >
ArcticWeightTpl< T > Times (const ArcticWeightTpl< T > &w1, const ArcticWeightTpl< T > &w2)
 
ArcticWeightTpl< floatTimes (const ArcticWeightTpl< float > &w1, const ArcticWeightTpl< float > &w2)
 
ArcticWeightTpl< double > Times (const ArcticWeightTpl< double > &w1, const ArcticWeightTpl< double > &w2)
 
template<class T >
ArcticWeightTpl< T > Divide (const ArcticWeightTpl< T > &w1, const ArcticWeightTpl< T > &w2, DivideType typ=DIVIDE_ANY)
 
ArcticWeightTpl< floatDivide (const ArcticWeightTpl< float > &w1, const ArcticWeightTpl< float > &w2, DivideType typ=DIVIDE_ANY)
 
ArcticWeightTpl< double > Divide (const ArcticWeightTpl< double > &w1, const ArcticWeightTpl< double > &w2, DivideType typ=DIVIDE_ANY)
 
template<class Arc >
void TestDeterminizeLatticePruned ()
 
template<class Arc >
void TestDeterminizeLatticePruned2 ()
 
template<class Weight , class IntType >
bool DeterminizeLatticePruned (const ExpandedFst< ArcTpl< Weight > > &ifst, double beam, MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, IntType > > > *ofst, DeterminizeLatticePrunedOptions opts)
 
template<class Weight >
bool DeterminizeLatticePruned (const ExpandedFst< ArcTpl< Weight > > &ifst, double prune, MutableFst< ArcTpl< Weight > > *ofst, DeterminizeLatticePrunedOptions opts=DeterminizeLatticePrunedOptions())
 This function implements the normal version of DeterminizeLattice, in which the output strings are represented using sequences of arcs, where all but the first one has an epsilon on the input side. More...
 
template<class Weight >
ArcTpl< Weight >::Label DeterminizeLatticeInsertPhones (const kaldi::TransitionModel &trans_model, MutableFst< ArcTpl< Weight > > *fst)
 This function takes in lattices and inserts phones at phone boundaries. More...
 
template<class Weight >
void DeterminizeLatticeDeletePhones (typename ArcTpl< Weight >::Label first_phone_label, MutableFst< ArcTpl< Weight > > *fst)
 This function takes in lattices and deletes "phones" from them. More...
 
template void DeterminizeLatticeDeletePhones (ArcTpl< kaldi::LatticeWeight >::Label first_phone_label, MutableFst< ArcTpl< kaldi::LatticeWeight > > *fst)
 
template<class Weight , class IntType >
bool DeterminizeLatticePhonePrunedFirstPass (const kaldi::TransitionModel &trans_model, double beam, MutableFst< ArcTpl< Weight > > *fst, const DeterminizeLatticePrunedOptions &opts)
 This function does a first pass determinization with phone symbols inserted at phone boundary. More...
 
template<class Weight , class IntType >
bool DeterminizeLatticePhonePruned (const kaldi::TransitionModel &trans_model, MutableFst< ArcTpl< Weight > > *ifst, double prune, MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, IntType > > > *ofst, DeterminizeLatticePhonePrunedOptions opts=DeterminizeLatticePhonePrunedOptions())
 "Destructive" version of DeterminizeLatticePhonePruned() where the input lattice might be changed. More...
 
template<class Weight , class IntType >
bool DeterminizeLatticePhonePruned (const kaldi::TransitionModel &trans_model, const ExpandedFst< ArcTpl< Weight > > &ifst, double prune, MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, IntType > > > *ofst, DeterminizeLatticePhonePrunedOptions opts=DeterminizeLatticePhonePrunedOptions())
 This function is a wrapper of DeterminizeLatticePhonePrunedFirstPass() and DeterminizeLatticePruned(). More...
 
bool DeterminizeLatticePhonePrunedWrapper (const kaldi::TransitionModel &trans_model, MutableFst< kaldi::LatticeArc > *ifst, double prune, MutableFst< kaldi::CompactLatticeArc > *ofst, DeterminizeLatticePhonePrunedOptions opts=DeterminizeLatticePhonePrunedOptions())
 This function is a wrapper of DeterminizeLatticePhonePruned() that works for Lattice type FSTs. More...
 
template bool DeterminizeLatticePruned< kaldi::LatticeWeight > (const ExpandedFst< kaldi::LatticeArc > &ifst, double prune, MutableFst< kaldi::CompactLatticeArc > *ofst, DeterminizeLatticePrunedOptions opts)
 
template bool DeterminizeLatticePruned< kaldi::LatticeWeight > (const ExpandedFst< kaldi::LatticeArc > &ifst, double prune, MutableFst< kaldi::LatticeArc > *ofst, DeterminizeLatticePrunedOptions opts)
 
template bool DeterminizeLatticePhonePruned< kaldi::LatticeWeight, kaldi::int32 > (const kaldi::TransitionModel &trans_model, const ExpandedFst< kaldi::LatticeArc > &ifst, double prune, MutableFst< kaldi::CompactLatticeArc > *ofst, DeterminizeLatticePhonePrunedOptions opts)
 
template bool DeterminizeLatticePhonePruned< kaldi::LatticeWeight, kaldi::int32 > (const kaldi::TransitionModel &trans_model, MutableFst< kaldi::LatticeArc > *ifst, double prune, MutableFst< kaldi::CompactLatticeArc > *ofst, DeterminizeLatticePhonePrunedOptions opts)
 
template<class Weight , class IntType >
bool MinimizeCompactLattice (MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, IntType > > > *clat, float delta=fst::kDelta)
 This function minimizes the compact lattice. More...
 
template bool MinimizeCompactLattice< kaldi::LatticeWeight, kaldi::int32 > (MutableFst< kaldi::CompactLatticeArc > *clat, float delta)
 
template<class Weight , class IntType >
bool PushCompactLatticeStrings (MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, IntType > > > *clat)
 This function pushes the transition-ids as far towards the start as they will go. More...
 
template<class Weight , class IntType >
bool PushCompactLatticeWeights (MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, IntType > > > *clat)
 This function pushes the weights in the CompactLattice so that all states except possibly the start state, have Weight components (of type LatticeWeight) that "sum to one" in the LatticeWeight (i.e. More...
 
template bool PushCompactLatticeStrings< kaldi::LatticeWeight, kaldi::int32 > (MutableFst< kaldi::CompactLatticeArc > *clat)
 
template bool PushCompactLatticeWeights< kaldi::LatticeWeight, kaldi::int32 > (MutableFst< kaldi::CompactLatticeArc > *clat)
 
static void InputDeterminizeSingleState (StdArc::StateId s, VectorFst< StdArc > *fst)
 
template<class Arc , class I >
void RemoveArcsWithSomeInputSymbols (const std::vector< I > &symbols_in, VectorFst< Arc > *fst)
 
template<class Arc , class I >
void PenalizeArcsWithSomeInputSymbols (const std::vector< I > &symbols_in, float penalty, VectorFst< Arc > *fst)
 
ConstFst< StdArc > * ReadAsConstFst (std::string rxfilename)
 
bool PrintProxyFstPath (const VectorFst< StdArc > &proxy, vector< vector< StdArc::Label > > *path, vector< StdArc::Weight > *weight, StdArc::StateId cur_state, vector< StdArc::Label > cur_path, StdArc::Weight cur_weight)
 

Detailed Description

For an extended explanation of the framework of which grammar-fsts are a part, please see Support for grammars and graphs with on-the-fly parts. (i.e.

../doc/grammar.dox).

This header implements a special FST type which we use in that framework; it is a lightweight wrapper which stitches together several FSTs and makes them look, to the decoder code, like a single FST. It is a bit like OpenFst's Replace() function, but with support for left-biphone context.

Typedef Documentation

◆ ArcticWeight

Definition at line 84 of file arctic-weight.h.

◆ BaseFloat

typedef float BaseFloat

Definition at line 28 of file lattice-weight-test.cc.

◆ CompactLatticeWeight

◆ CompactLatticeWeightCommonDivisor

◆ Label

typedef fst::StdArc::Label Label

Definition at line 57 of file deterministic-fst-test.cc.

◆ LatticeWeight

Definition at line 30 of file lattice-weight-test.cc.

◆ StateId

typedef fst::StdArc::StateId StateId

Definition at line 58 of file deterministic-fst-test.cc.

◆ StatePropertiesType

typedef unsigned char StatePropertiesType

Definition at line 122 of file factor.h.

◆ StdArc

Definition at line 56 of file deterministic-fst-test.cc.

◆ StdVectorFst

Definition at line 59 of file deterministic-fst-test.cc.

◆ VectorFstHolder

Definition at line 151 of file kaldi-fst-io.h.

◆ Weight

typedef fst::StdArc::Weight Weight

Definition at line 60 of file deterministic-fst-test.cc.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
kStateHasEpsilonArcsEntering 
kStateHasNonEpsilonArcsEntering 
kStateHasEpsilonArcsLeaving 
kStateHasNonEpsilonArcsLeaving 

Definition at line 27 of file epsilon-property.h.

◆ NonterminalValues

An anonymous enum to define some values for symbols used in our grammar-fst framework.

Please understand this with reference to the documentation in Support for grammars and graphs with on-the-fly parts. (../doc/grammar.dox). This enum defines the values of nonterminal-related symbols in phones.txt. They are not the actual values– they will be shifted by adding the value nonterm_phones_offset which is passed in by the command-line flag –nonterm-phones-offset.

Enumerator
kNontermBos 
kNontermBegin 
kNontermEnd 
kNontermReenter 
kNontermUserDefined 
kNontermMediumNumber 
kNontermBigNumber 

Definition at line 68 of file grammar-context-fst.h.

68  {
69  kNontermBos = 0, // #nonterm_bos
70  kNontermBegin = 1, // #nonterm_begin
71  kNontermEnd = 2, // #nonterm_end
72  kNontermReenter = 3, // #nonterm_reenter
73  kNontermUserDefined = 4, // the lowest-numbered user-defined nonterminal, e.g. #nonterm:foo
74  // kNontermMediumNumber and kNontermBigNumber come into the encoding of
75  // nonterminal-related symbols in HCLG.fst. The only hard constraint on them
76  // is that kNontermBigNumber must be bigger than the biggest transition-id in
77  // your system, and kNontermMediumNumber must be >0. These values were chosen
78  // for ease of human inspection of numbers encoded with them.
79  kNontermMediumNumber = 1000,
80  kNontermBigNumber = 10000000
81 };

◆ StatePropertiesEnum

Enumerator
kStateFinal 
kStateInitial 
kStateArcsIn 
kStateMultipleArcsIn 
kStateArcsOut 
kStateMultipleArcsOut 
kStateOlabelsOut 
kStateIlabelsOut 

Definition at line 112 of file factor.h.

Function Documentation

◆ AcousticLatticeScale()

std::vector<std::vector<double> > fst::AcousticLatticeScale ( double  acwt)
inline

◆ AddSelfLoops()

void AddSelfLoops ( MutableFst< Arc > *  fst,
std::vector< typename Arc::Label > &  isyms,
std::vector< typename Arc::Label > &  osyms 
)

AddSelfLoops is a function you will probably want to use alongside PreDeterminize, to add self-loops to any FSTs that you compose on the left hand side of the one modified by PreDeterminize.

This function inserts loops with "special symbols" [e.g. #0, #1] into an FST. This is done at each final state and each state with non-epsilon output symbols on at least one arc out of it. This is to ensure that these symbols, when inserted into the input side of an FST we will compose with on the right, can "pass through" this FST.

At input, isyms and osyms must be vectors of the same size n, corresponding to symbols that currently do not exist in 'fst'. For each state in n that has non-epsilon symbols on the output side of arcs leaving it, or which is a final state, this function inserts n self-loops with unit weight and one of the n pairs of symbols on its input and output.

Definition at line 599 of file pre-determinize-inl.h.

References rnnlm::i, and rnnlm::n.

Referenced by TestAddSelfLoops().

600  {
601  assert(fst != NULL);
602  assert(isyms.size() == osyms.size());
603  typedef typename Arc::Label Label;
604  typedef typename Arc::StateId StateId;
605  typedef typename Arc::Weight Weight;
606  size_t n = isyms.size();
607  if (n == 0) return; // Nothing to do.
608 
609  // {
610  // the following declarations and statements are for quick detection of these
611  // symbols, which is purely for debugging/checking purposes.
612  Label isyms_min = *std::min_element(isyms.begin(), isyms.end()),
613  isyms_max = *std::max_element(isyms.begin(), isyms.end()),
614  osyms_min = *std::min_element(osyms.begin(), osyms.end()),
615  osyms_max = *std::max_element(osyms.begin(), osyms.end());
616  std::set<Label> isyms_set, osyms_set;
617  for (size_t i = 0; i < isyms.size(); i++) {
618  assert(isyms[i] > 0 && osyms[i] > 0); // should not have epsilon or invalid symbols.
619  isyms_set.insert(isyms[i]);
620  osyms_set.insert(osyms[i]);
621  }
622  assert(isyms_set.size() == n && osyms_set.size() == n);
623  // } end block.
624 
625  for (StateIterator<MutableFst<Arc> > siter(*fst); ! siter.Done(); siter.Next()) {
626  StateId state = siter.Value();
627  bool this_state_needs_self_loops = (fst->Final(state) != Weight::Zero());
628  for (ArcIterator<MutableFst<Arc> > aiter(*fst, state); ! aiter.Done(); aiter.Next()) {
629  const Arc &arc = aiter.Value();
630  // If one of the following asserts fails, it means that the input FST already had the symbols
631  // we are inserting. This is contrary to the preconditions of this algorithm.
632  assert(!(arc.ilabel>=isyms_min && arc.ilabel<=isyms_max && isyms_set.count(arc.ilabel) != 0));
633  assert(!(arc.olabel>=osyms_min && arc.olabel<=osyms_max && osyms_set.count(arc.olabel) != 0));
634  if (arc.olabel != 0) // Has non-epsilon output label -> need self loops.
635  this_state_needs_self_loops = true;
636  }
637  if (this_state_needs_self_loops) {
638  for (size_t i = 0;i < n;i++) {
639  Arc arc;
640  arc.ilabel = isyms[i];
641  arc.olabel = osyms[i];
642  arc.weight = Weight::One();
643  arc.nextstate = state;
644  fst->AddArc(state, arc);
645  }
646  }
647  }
648 }
fst::StdArc::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
struct rnnlm::@11::@12 n
fst::StdArc::Label Label
fst::StdArc::Weight Weight

◆ AddSubsequentialLoop()

void AddSubsequentialLoop ( StdArc::Label  subseq_symbol,
MutableFst< StdArc > *  fst 
)

Modifies an FST so that it transuces the same paths, but the input side of the paths can all have the subsequential symbol '$' appended to them any number of times (we could easily specify the number of times, but accepting any number of repetitions is just more convenient).

The actual way we do this is for each final state, we add a transition with weight equal to the final-weight of that state, with input-symbol '$' and output-symbols <eps>, and ending in a new super-final state that has unit final-probability and a unit-weight self-loop with '$' on its input and <eps> on its output. The reason we don't just add a loop to each final-state has to do with preserving stochasticity (see Preserving stochasticity and testing it). We keep the final-probability in all the original final-states rather than setting them to zero, so the resulting FST can accept zero '$' symbols at the end (in case we had no right context).

Definition at line 297 of file context-fst.cc.

References rnnlm::i.

Referenced by ComposeContext(), main(), and TrainingGraphCompiler::TrainingGraphCompiler().

298  {
299  typedef StdArc Arc;
300  typedef typename Arc::StateId StateId;
301  typedef typename Arc::Weight Weight;
302 
303  vector<StateId> final_states;
304  for (StateIterator<MutableFst<Arc> > siter(*fst); !siter.Done(); siter.Next()) {
305  StateId s = siter.Value();
306  if (fst->Final(s) != Weight::Zero()) final_states.push_back(s);
307  }
308 
309  StateId superfinal = fst->AddState();
310  Arc arc(subseq_symbol, 0, Weight::One(), superfinal);
311  fst->AddArc(superfinal, arc); // loop at superfinal.
312  fst->SetFinal(superfinal, Weight::One());
313 
314  for (size_t i = 0; i < final_states.size(); i++) {
315  StateId s = final_states[i];
316  fst->AddArc(s, Arc(subseq_symbol, 0, fst->Final(s), superfinal));
317  // No, don't remove the final-weights of the original states..
318  // this is so we can add the subsequential loop in cases where
319  // there is no context, and it won't hurt.
320  // fst->SetFinal(s, Weight::Zero());
321  arc.nextstate = final_states[i];
322  }
323 }
fst::StdArc::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
fst::StdArc StdArc
fst::StdArc::Weight Weight

◆ ApplyProbabilityScale()

void ApplyProbabilityScale ( float  scale,
MutableFst< Arc > *  fst 
)

ApplyProbabilityScale is applicable to FSTs in the log or tropical semiring.

It multiplies the arc and final weights by "scale" [this is not the Mul operation of the semiring, it's actual multiplication, which is equivalent to taking a power in the semiring].

Definition at line 771 of file fstext-utils-inl.h.

Referenced by kaldi::GetHmmAsFsa(), main(), and MinimizeEncoded().

771  {
772  typedef typename Arc::Weight Weight;
773  typedef typename Arc::StateId StateId;
774  for (StateIterator<MutableFst<Arc> > siter(*fst);
775  !siter.Done();
776  siter.Next()) {
777  StateId s = siter.Value();
778  for (MutableArcIterator<MutableFst<Arc> > aiter(fst, s);
779  !aiter.Done();
780  aiter.Next()) {
781  Arc arc = aiter.Value();
782  arc.weight = Weight(arc.weight.Value() * scale);
783  aiter.SetValue(arc);
784  }
785  if (fst->Final(s) != Weight::Zero())
786  fst->SetFinal(s, Weight(fst->Final(s).Value() * scale));
787  }
788 }
fst::StdArc::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
fst::StdArc::Weight Weight

◆ ApproxEqual() [1/2]

◆ ApproxEqual() [2/2]

bool fst::ApproxEqual ( const CompactLatticeWeightTpl< WeightType, IntType > &  w1,
const CompactLatticeWeightTpl< WeightType, IntType > &  w2,
float  delta = kDelta 
)
inline

Definition at line 570 of file lattice-weight.h.

References ApproxEqual(), CompactLatticeWeightTpl< WeightType, IntType >::String(), and CompactLatticeWeightTpl< WeightType, IntType >::Weight().

572  {
573  return (ApproxEqual(w1.Weight(), w2.Weight(), delta) && w1.String() == w2.String());
574 }
bool ApproxEqual(const CompactLatticeWeightTpl< WeightType, IntType > &w1, const CompactLatticeWeightTpl< WeightType, IntType > &w2, float delta=kDelta)

◆ CastOrConvertToVectorFst()

VectorFst< StdArc > * CastOrConvertToVectorFst ( Fst< StdArc > *  fst)

Definition at line 94 of file kaldi-fst-io.cc.

References KALDI_ASSERT.

Referenced by main().

94  {
95  // This version currently supports ConstFst<StdArc> or VectorFst<StdArc>
96  std::string real_type = fst->Type();
97  KALDI_ASSERT(real_type == "vector" || real_type == "const");
98  if (real_type == "vector") {
99  return dynamic_cast<VectorFst<StdArc> *>(fst);
100  } else {
101  // As the 'fst' can't cast to VectorFst, we create a new
102  // VectorFst<StdArc> initialized by 'fst', and delete 'fst'.
103  VectorFst<StdArc> *new_fst = new VectorFst<StdArc>(*fst);
104  delete fst;
105  return new_fst;
106  }
107 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ CheckPhones()

static float fst::CheckPhones ( const VectorFst< Arc > &  linear_fst,
const vector< typename Arc::Label > &  phone_ids,
const vector< typename Arc::Label > &  disambig_ids,
const vector< typename Arc::Label > &  phone_seq,
const vector< vector< typename Arc::Label > > &  ilabel_info,
int  N,
int  P 
)
static

Definition at line 71 of file context-fst-test.cc.

References GetLinearSymbolSequence(), rnnlm::i, kaldi::IsSorted(), and rnnlm::j.

76  {
77  typedef typename Arc::Label Label;
78  typedef typename Arc::StateId StateId;
79  typedef typename Arc::Weight Weight;
80 
81  assert(kaldi::IsSorted(phone_ids)); // so we can do binary_search.
82 
83 
84  vector<int32> input_syms;
85  vector<int32> output_syms;
86  Weight tot_cost;
87  bool ans = GetLinearSymbolSequence(linear_fst, &input_syms,
88  &output_syms, &tot_cost);
89  assert(ans); // should be linear.
90 
91  vector<int32> phone_seq_check;
92  for (size_t i = 0; i < output_syms.size(); i++)
93  if (std::binary_search(phone_ids.begin(), phone_ids.end(), output_syms[i]))
94  phone_seq_check.push_back(output_syms[i]);
95 
96  assert(phone_seq_check == phone_seq);
97 
98  vector<vector<int32> > input_syms_long;
99  for (size_t i = 0; i < input_syms.size(); i++) {
100  Label isym = input_syms[i];
101  if (ilabel_info[isym].size() == 0) continue; // epsilon.
102  if ( (ilabel_info[isym].size() == 1 &&
103  ilabel_info[isym][0] <= 0) ) continue; // disambig.
104  input_syms_long.push_back(ilabel_info[isym]);
105  }
106 
107  for (size_t i = 0; i < input_syms_long.size(); i++) {
108  vector<int32> phone_context_window(N); // phone at pos i will be at pos P in this window.
109  int pos = ((int)i) - P; // pos of first phone in window [ may be out of range] .
110  for (int j = 0; j < N; j++, pos++) {
111  if (static_cast<size_t>(pos) < phone_seq.size()) phone_context_window[j] = phone_seq[pos];
112  else phone_context_window[j] = 0; // 0 is a special symbol that context-dep-itf expects to see
113  // when no phone is present due to out-of-window. context-fst knows about this too.
114  }
115  assert(input_syms_long[i] == phone_context_window);
116  }
117  return tot_cost.Value();
118 }
fst::StdArc::StateId StateId
bool GetLinearSymbolSequence(const Fst< Arc > &fst, std::vector< I > *isymbols_out, std::vector< I > *osymbols_out, typename Arc::Weight *tot_weight_out)
GetLinearSymbolSequence gets the symbol sequence from a linear FST.
fst::StdArc::Label Label
fst::StdArc::Weight Weight
bool IsSorted(const std::vector< T > &vec)
Returns true if the vector is sorted.
Definition: stl-utils.h:47

◆ ClearSymbols()

void ClearSymbols ( bool  clear_input,
bool  clear_output,
MutableFst< Arc > *  fst 
)

ClearSymbols sets all the symbols on the input and/or output side of the FST to zero, as specified.

It does not alter the symbol tables.

Definition at line 742 of file fstext-utils-inl.h.

Referenced by MakeLoopFstCompare().

744  {
745  for (StateIterator<MutableFst<Arc> > siter(*fst);
746  !siter.Done();
747  siter.Next()) {
748  typename Arc::StateId s = siter.Value();
749  for (MutableArcIterator<MutableFst<Arc> > aiter(fst, s);
750  !aiter.Done();
751  aiter.Next()) {
752  Arc arc = aiter.Value();
753  bool change = false;
754  if (clear_input && arc.ilabel != 0) {
755  arc.ilabel = 0;
756  change = true;
757  }
758  if (clear_output && arc.olabel != 0) {
759  arc.olabel = 0;
760  change = true;
761  }
762  if (change) {
763  aiter.SetValue(arc);
764  }
765  }
766  }
767 }
fst::StdArc::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21

◆ CompactLatticeHasAlignment()

bool CompactLatticeHasAlignment ( const ExpandedFst< ArcTpl< CompactLatticeWeightTpl< Weight, Int > > > &  fst)

Returns true if lattice has alignments, i.e.

it has any nonempty strings inside its weights.

Definition at line 244 of file lattice-utils-inl.h.

Referenced by LatticeScale().

245  {
246  typedef CompactLatticeWeightTpl<Weight, Int> W;
247  typedef ArcTpl<W> Arc;
248  typedef ExpandedFst<Arc> Fst;
249  typedef typename Arc::StateId StateId;
250  StateId num_states = fst.NumStates();
251  for (StateId s = 0; s < num_states; s++) {
252  for (ArcIterator<Fst> aiter(fst, s);
253  !aiter.Done();
254  aiter.Next()) {
255  const Arc &arc = aiter.Value();
256  if (!arc.weight.String().empty()) return true;
257  }
258  W final_weight = fst.Final(s);
259  if (!final_weight.String().empty()) return true;
260  }
261  return false;
262 }
fst::StdArc::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21

◆ CompactLatticeWeightTest()

void fst::CompactLatticeWeightTest ( )

Definition at line 126 of file lattice-weight-test.cc.

References ApproxEqual(), Compare(), Divide(), rnnlm::i, KALDI_ASSERT, CompactLatticeWeightTpl< WeightType, IntType >::Member(), CompactLatticeWeightTpl< WeightType, IntType >::One(), Plus(), CompactLatticeWeightTpl< WeightType, IntType >::Quantize(), RandomCompactLatticeWeight(), CompactLatticeWeightTpl< WeightType, IntType >::Read(), Times(), and CompactLatticeWeightTpl< WeightType, IntType >::Zero().

Referenced by main().

126  {
127  for(int32 i = 0; i < 100; i++) {
129  CompactLatticeWeight l3 = Plus(l1, l2);
130  CompactLatticeWeight l4 = Times(l1, l2);
131 
132  KALDI_ASSERT(Plus(l3, l3) == l3);
133  KALDI_ASSERT(Plus(l1, l2) == Plus(l2, l1)); // commutativity of plus
134  KALDI_ASSERT(Plus(l3, CompactLatticeWeight::Zero()) == l3); // x + 0 = x
135  KALDI_ASSERT(Times(l3, CompactLatticeWeight::One()) == l3); // x * 1 = x
136  KALDI_ASSERT(Times(l3, CompactLatticeWeight::Zero()) == CompactLatticeWeight::Zero()); // x * 0 = 0
137  NaturalLess<CompactLatticeWeight> nl;
138  bool a = nl(l1, l2);
139  bool b = (Plus(l1, l2) == l1 && l1 != l2);
140  KALDI_ASSERT(a == b);
141 
142  KALDI_ASSERT(Compare(l1, Plus(l1, l2)) != 1); // so do not have l1 > l1 + l2
144  KALDI_ASSERT(Times(Plus(l1, l2), Plus(l5, l6)) ==
145  Plus(Times(l1, l5), Plus(Times(l1, l6),
146  Plus(Times(l2, l5), Times(l2, l6))))); // * distributes over +
147  KALDI_ASSERT(l1.Member() && l2.Member() && l3.Member() && l4.Member()
148  && l5.Member() && l6.Member());
149  if (l2 != CompactLatticeWeight::Zero()) {
150  KALDI_ASSERT(ApproxEqual(Divide(Times(l1, l2), l2, DIVIDE_RIGHT), l1)); // (a*b) / b = a if b != 0
151  KALDI_ASSERT(ApproxEqual(Divide(Times(l2, l1), l2, DIVIDE_LEFT), l1)); // (a*b) / b = a if b != 0
152  }
153  KALDI_ASSERT(ApproxEqual(l1, l1.Quantize()));
154 
155  std::ostringstream s1;
156  s1 << l1;
157  std::istringstream s2(s1.str());
158  s2 >> l2;
159  KALDI_ASSERT(ApproxEqual(l1, l2));
160  std::cout << s1.str() << '\n';
161 
162  {
163  std::ostringstream s1b;
164  l1.Write(s1b);
165  std::istringstream s2b(s1b.str());
166  l3.Read(s2b);
167  KALDI_ASSERT(l1 == l3);
168  }
169 
171  std::cout << "l5 = " << l5 << '\n';
172  std::cout << "l6 = " << l6 << '\n';
173  l1 = divisor(l5, l6);
174  std::cout << "div = " << l1 << '\n';
175  if (l1 != CompactLatticeWeight::Zero()) {
176  l2 = Divide(l5, l1, DIVIDE_LEFT);
177  l3 = Divide(l6, l1, DIVIDE_LEFT);
178  std::cout << "l2 = " << l2 << '\n';
179  std::cout << "l3 = " << l3 << '\n';
180  l4 = divisor(l2, l3); // make sure l2 is now one.
181  std::cout << "l4 = " << l4 << '\n';
182  KALDI_ASSERT(ApproxEqual(l4, CompactLatticeWeight::One()));
183  } else {
184  KALDI_ASSERT(l5 == CompactLatticeWeight::Zero()
185  && l6 == CompactLatticeWeight::Zero());
186  }
187  }
188 }
CompactLatticeWeight RandomCompactLatticeWeight()
LatticeWeightTpl< FloatType > Divide(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2, DivideType typ=DIVIDE_ANY)
LatticeWeightTpl< FloatType > Plus(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2)
kaldi::int32 int32
LatticeWeightTpl< FloatType > Times(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2)
int Compare(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2)
Compare returns -1 if w1 < w2, +1 if w1 > w2, and 0 if w1 == w2.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
static bool ApproxEqual(float a, float b, float relative_tolerance=0.001)
return abs(a - b) <= relative_tolerance * (abs(a)+abs(b)).
Definition: kaldi-math.h:265
CompactLatticeWeightTpl< LatticeWeight, int32 > CompactLatticeWeight
CompactLatticeWeightCommonDivisorTpl< LatticeWeight, int32 > CompactLatticeWeightCommonDivisor

◆ Compare() [1/3]

int fst::Compare ( const LatticeWeightTpl< FloatType > &  w1,
const LatticeWeightTpl< FloatType > &  w2 
)
inline

Compare returns -1 if w1 < w2, +1 if w1 > w2, and 0 if w1 == w2.

Definition at line 294 of file lattice-weight.h.

References LatticeWeightTpl< FloatType >::Value1(), and LatticeWeightTpl< FloatType >::Value2().

Referenced by CompactLatticeWeightTest(), Compare(), LatticeDeterminizerPruned< Weight, IntType >::Compare(), LatticeDeterminizer< Weight, IntType >::Compare(), PruneSpecialClass< Arc >::Done(), LatticeDeterminizerPruned< Weight, IntType >::EpsilonClosure(), LatticeDeterminizer< Weight, IntType >::EpsilonClosure(), LatticeWeightTest(), LatticeDeterminizer< Weight, IntType >::MakeSubsetUnique(), NaturalLess< LatticeWeightTpl< FloatType > >::operator()(), NaturalLess< LatticeWeightTpl< float > >::operator()(), NaturalLess< LatticeWeightTpl< double > >::operator()(), NaturalLess< CompactLatticeWeightTpl< LatticeWeightTpl< FloatType >, IntType > >::operator()(), NaturalLess< CompactLatticeWeightTpl< LatticeWeightTpl< float >, int32 > >::operator()(), NaturalLess< CompactLatticeWeightTpl< LatticeWeightTpl< double >, int32 > >::operator()(), PruneSpecialClass< Arc >::Task::operator<(), Plus(), LatticeDeterminizer< Weight, IntType >::ProcessFinal(), and PruneSpecialClass< Arc >::ProcessState().

295  {
296  FloatType f1 = w1.Value1() + w1.Value2(),
297  f2 = w2.Value1() + w2.Value2();
298  if (f1 < f2) { return 1; } // having smaller cost means you're larger
299  // in the semiring [higher probability]
300  else if (f1 > f2) { return -1; }
301  // mathematically we should be comparing (w1.value1_-w1.value2_ < w2.value1_-w2.value2_)
302  // in the next line, but add w1.value1_+w1.value2_ = w2.value1_+w2.value2_ to both sides and
303  // divide by two, and we get the simpler equivalent form w1.value1_ < w2.value1_.
304  else if (w1.Value1() < w2.Value1()) { return 1; }
305  else if (w1.Value1() > w2.Value1()) { return -1; }
306  else { return 0; }
307 }

◆ Compare() [2/3]

int fst::Compare ( const CompactLatticeWeightTpl< WeightType, IntType > &  w1,
const CompactLatticeWeightTpl< WeightType, IntType > &  w2 
)
inline

Definition at line 591 of file lattice-weight.h.

References Compare(), rnnlm::i, CompactLatticeWeightTpl< WeightType, IntType >::String(), and CompactLatticeWeightTpl< WeightType, IntType >::Weight().

592  {
593  int c1 = Compare(w1.Weight(), w2.Weight());
594  if (c1 != 0) return c1;
595  int l1 = w1.String().size(), l2 = w2.String().size();
596  // Use opposite order on the string lengths, so that if the costs are the same,
597  // the shorter string wins.
598  if (l1 > l2) return -1;
599  else if (l1 < l2) return 1;
600  for(int i = 0; i < l1; i++) {
601  if (w1.String()[i] < w2.String()[i]) return -1;
602  else if (w1.String()[i] > w2.String()[i]) return 1;
603  }
604  return 0;
605 }
int Compare(const TropicalWeight &w1, const TropicalWeight &w2)

◆ Compare() [3/3]

int fst::Compare ( const TropicalWeight &  w1,
const TropicalWeight &  w2 
)
inline

Definition at line 653 of file lattice-weight.h.

654  {
655  float f1 = w1.Value(), f2 = w2.Value();
656  if (f1 == f2) return 0;
657  else if (f1 > f2) return -1;
658  else return 1;
659 }

◆ ComposeContext() [1/2]

void fst::ComposeContext ( const std::vector< int32 > &  disambig_syms,
int32  context_width,
int32  central_position,
VectorFst< StdArc > *  ifst,
VectorFst< StdArc > *  ofst,
std::vector< std::vector< int32 > > *  ilabels_out,
bool  project_ifst = false 
)

Used in the command-line tool fstcomposecontext.

It creates a context FST and composes it on the left with "ifst" to make "ofst". It outputs the label information to ilabels_out. "ifst" is mutable because we need to add the subsequential loop.

Parameters
[in]disambig_symsList of disambiguation symbols, e.g. the integer ids of #0, #1, #2 ... in the phones.txt.
[in]context_widthSize of context window, e.g. 3 for triphone.
[in]central_positionCentral position in phonetic context window (zero-based index), e.g. 1 for triphone.
[in,out]ifstThe FST we are composing with C (e.g. LG.fst), mustable because we need to add the subsequential loop to it.
[out]ofstComposed output FST (would be CLG.fst).
[out]ilabels_outVector, indexed by ilabel of CLG.fst, providing information about the meaning of that ilabel; see "http://kaldi-asr.org/doc/tree_externals.html#tree_ilabel".
[in]project_ifstThis is intended only to be set to true in the program 'fstmakecontextfst'... if true, it will project on the input after adding the subsequential loop to 'ifst', which allows us to reconstruct the context fst C.fst.

Definition at line 246 of file context-fst.cc.

References AddSubsequentialLoop(), ComposeDeterministicOnDemandInverse(), GetInputSymbols(), rnnlm::i, KALDI_ASSERT, and InverseContextFst::SwapIlabelInfo().

Referenced by main().

251  {
252  KALDI_ASSERT(ifst != NULL && ofst != NULL);
253  KALDI_ASSERT(context_width > 0);
254  KALDI_ASSERT(central_position >= 0);
255  KALDI_ASSERT(central_position < context_width);
256 
257  vector<int32> disambig_syms(disambig_syms_in);
258  std::sort(disambig_syms.begin(), disambig_syms.end());
259 
260  vector<int32> all_syms;
261  GetInputSymbols(*ifst, false/*no eps*/, &all_syms);
262  std::sort(all_syms.begin(), all_syms.end());
263  vector<int32> phones;
264  for (size_t i = 0; i < all_syms.size(); i++)
265  if (!std::binary_search(disambig_syms.begin(),
266  disambig_syms.end(), all_syms[i]))
267  phones.push_back(all_syms[i]);
268 
269  // Get subsequential symbol that does not clash with
270  // any disambiguation symbol or symbol in the FST.
271  int32 subseq_sym = 1;
272  if (!all_syms.empty())
273  subseq_sym = std::max(subseq_sym, all_syms.back() + 1);
274  if (!disambig_syms.empty())
275  subseq_sym = std::max(subseq_sym, disambig_syms.back() + 1);
276 
277  // if central_position == context_width-1, it's left-context, and no
278  // subsequential symbol is needed.
279  if (central_position != context_width-1) {
280  AddSubsequentialLoop(subseq_sym, ifst);
281  if (project_ifst) {
282  fst::Project(ifst, fst::PROJECT_INPUT);
283  }
284  }
285 
286  InverseContextFst inv_c(subseq_sym, phones, disambig_syms,
287  context_width, central_position);
288 
289  // The following statement is equivalent to the following
290  // (if FSTs had the '*' operator for composition):
291  // (*ofst) = inv(inv_c) * (*ifst)
292  ComposeDeterministicOnDemandInverse(*ifst, &inv_c, ofst);
293 
294  inv_c.SwapIlabelInfo(ilabels_out);
295 }
kaldi::int32 int32
void GetInputSymbols(const Fst< Arc > &fst, bool include_eps, std::vector< I > *symbols)
GetInputSymbols gets the list of symbols on the input of fst (including epsilon, if include_eps == tr...
void ComposeDeterministicOnDemandInverse(const Fst< Arc > &right, DeterministicOnDemandFst< Arc > *left, MutableFst< Arc > *fst_composed)
This function does &#39;*fst_composed = Compose(Inverse(*fst2), fst1)&#39; Note that the arguments are revers...
void AddSubsequentialLoop(StdArc::Label subseq_symbol, MutableFst< StdArc > *fst)
Modifies an FST so that it transuces the same paths, but the input side of the paths can all have the...
Definition: context-fst.cc:297
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ ComposeContext() [2/2]

void fst::ComposeContext ( const std::vector< int32 > &  disambig_syms,
int32  context_width,
int32  central_position,
VectorFst< StdArc > *  ifst,
VectorFst< StdArc > *  ofst,
std::vector< std::vector< int32 > > *  ilabels_out,
bool  project_ifst = false 
)

Used in the command-line tool fstcomposecontext.

It creates a context FST and composes it on the left with "ifst" to make "ofst". It outputs the label information to ilabels_out. "ifst" is mutable because we need to add the subsequential loop.

Parameters
[in]disambig_symsList of disambiguation symbols, e.g. the integer ids of #0, #1, #2 ... in the phones.txt.
[in]context_widthSize of context window, e.g. 3 for triphone.
[in]central_positionCentral position in phonetic context window (zero-based index), e.g. 1 for triphone.
[in,out]ifstThe FST we are composing with C (e.g. LG.fst), mustable because we need to add the subsequential loop to it.
[out]ofstComposed output FST (would be CLG.fst).
[out]ilabels_outVector, indexed by ilabel of CLG.fst, providing information about the meaning of that ilabel; see "http://kaldi-asr.org/doc/tree_externals.html#tree_ilabel".
[in]project_ifstThis is intended only to be set to true in the program 'fstmakecontextfst'... if true, it will project on the input after adding the subsequential loop to 'ifst', which allows us to reconstruct the context fst C.fst.

Definition at line 246 of file context-fst.cc.

References AddSubsequentialLoop(), ComposeDeterministicOnDemandInverse(), GetInputSymbols(), rnnlm::i, KALDI_ASSERT, and InverseContextFst::SwapIlabelInfo().

Referenced by main().

251  {
252  KALDI_ASSERT(ifst != NULL && ofst != NULL);
253  KALDI_ASSERT(context_width > 0);
254  KALDI_ASSERT(central_position >= 0);
255  KALDI_ASSERT(central_position < context_width);
256 
257  vector<int32> disambig_syms(disambig_syms_in);
258  std::sort(disambig_syms.begin(), disambig_syms.end());
259 
260  vector<int32> all_syms;
261  GetInputSymbols(*ifst, false/*no eps*/, &all_syms);
262  std::sort(all_syms.begin(), all_syms.end());
263  vector<int32> phones;
264  for (size_t i = 0; i < all_syms.size(); i++)
265  if (!std::binary_search(disambig_syms.begin(),
266  disambig_syms.end(), all_syms[i]))
267  phones.push_back(all_syms[i]);
268 
269  // Get subsequential symbol that does not clash with
270  // any disambiguation symbol or symbol in the FST.
271  int32 subseq_sym = 1;
272  if (!all_syms.empty())
273  subseq_sym = std::max(subseq_sym, all_syms.back() + 1);
274  if (!disambig_syms.empty())
275  subseq_sym = std::max(subseq_sym, disambig_syms.back() + 1);
276 
277  // if central_position == context_width-1, it's left-context, and no
278  // subsequential symbol is needed.
279  if (central_position != context_width-1) {
280  AddSubsequentialLoop(subseq_sym, ifst);
281  if (project_ifst) {
282  fst::Project(ifst, fst::PROJECT_INPUT);
283  }
284  }
285 
286  InverseContextFst inv_c(subseq_sym, phones, disambig_syms,
287  context_width, central_position);
288 
289  // The following statement is equivalent to the following
290  // (if FSTs had the '*' operator for composition):
291  // (*ofst) = inv(inv_c) * (*ifst)
292  ComposeDeterministicOnDemandInverse(*ifst, &inv_c, ofst);
293 
294  inv_c.SwapIlabelInfo(ilabels_out);
295 }
kaldi::int32 int32
void GetInputSymbols(const Fst< Arc > &fst, bool include_eps, std::vector< I > *symbols)
GetInputSymbols gets the list of symbols on the input of fst (including epsilon, if include_eps == tr...
void ComposeDeterministicOnDemandInverse(const Fst< Arc > &right, DeterministicOnDemandFst< Arc > *left, MutableFst< Arc > *fst_composed)
This function does &#39;*fst_composed = Compose(Inverse(*fst2), fst1)&#39; Note that the arguments are revers...
void AddSubsequentialLoop(StdArc::Label subseq_symbol, MutableFst< StdArc > *fst)
Modifies an FST so that it transuces the same paths, but the input side of the paths can all have the...
Definition: context-fst.cc:297
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ ComposeContextLeftBiphone() [1/2]

void fst::ComposeContextLeftBiphone ( int32  nonterm_phones_offset,
const std::vector< int32 > &  disambig_syms,
const VectorFst< StdArc > &  ifst,
VectorFst< StdArc > *  ofst,
std::vector< std::vector< int32 > > *  ilabels 
)

This is a variant of the function ComposeContext() which is to be used with our "grammar FST" framework (see The ContextFst object, i.e.

../doc/grammar.dox, for more details). This does not take the 'context_width' and 'central_position' arguments because they are assumed to be 2 and 1 respectively (meaning, left-biphone phonetic context).

This function creates a context FST and composes it on the left with "ifst" to make "ofst".

Parameters
[in]nonterm_phones_offsetThe integer id of the symbol #nonterm_bos in the phones.txt file. You can just set this to a large value (like 1 million) if you are not actually using nonterminals (e.g. for testing purposes).
[in]disambig_symsList of disambiguation symbols, e.g. the integer ids of #0, #1, #2 ... in the phones.txt.
[in,out]ifstThe FST we are composing with C (e.g. LG.fst).
[out]ofstComposed output FST (would be CLG.fst).
[out]ilabelsVector, indexed by ilabel of CLG.fst, providing information about the meaning of that ilabel; see The ilabel_info object (http://kaldi-asr.org/doc/tree_externals.html#tree_ilabel) and also Special symbols in CLG.fst (http://kaldi-asr.org/doc/grammar#grammar_special_clg).

Definition at line 196 of file grammar-context-fst.cc.

References ComposeDeterministicOnDemandInverse(), GetInputSymbols(), rnnlm::i, and InverseLeftBiphoneContextFst::SwapIlabelInfo().

Referenced by GetEncodingMultiple(), and main().

201  {
202 
203  vector<int32> disambig_syms(disambig_syms_in);
204  std::sort(disambig_syms.begin(), disambig_syms.end());
205 
206  vector<int32> all_syms;
207  GetInputSymbols(ifst, false/*no eps*/, &all_syms);
208  std::sort(all_syms.begin(), all_syms.end());
209  vector<int32> phones;
210  for (size_t i = 0; i < all_syms.size(); i++)
211  if (!std::binary_search(disambig_syms.begin(),
212  disambig_syms.end(), all_syms[i]) &&
213  all_syms[i] < nonterm_phones_offset)
214  phones.push_back(all_syms[i]);
215 
216 
217  InverseLeftBiphoneContextFst inv_c(nonterm_phones_offset,
218  phones, disambig_syms);
219 
220  // The following statement is equivalent to the following
221  // (if FSTs had the '*' operator for composition):
222  // (*ofst) = inv(inv_c) * (*ifst)
223  ComposeDeterministicOnDemandInverse(ifst, &inv_c, ofst);
224 
225  inv_c.SwapIlabelInfo(ilabels);
226 }
void GetInputSymbols(const Fst< Arc > &fst, bool include_eps, std::vector< I > *symbols)
GetInputSymbols gets the list of symbols on the input of fst (including epsilon, if include_eps == tr...
void ComposeDeterministicOnDemandInverse(const Fst< Arc > &right, DeterministicOnDemandFst< Arc > *left, MutableFst< Arc > *fst_composed)
This function does &#39;*fst_composed = Compose(Inverse(*fst2), fst1)&#39; Note that the arguments are revers...

◆ ComposeContextLeftBiphone() [2/2]

void fst::ComposeContextLeftBiphone ( int32  nonterm_phones_offset,
const std::vector< int32 > &  disambig_syms,
const VectorFst< StdArc > &  ifst,
VectorFst< StdArc > *  ofst,
std::vector< std::vector< int32 > > *  ilabels 
)

This is a variant of the function ComposeContext() which is to be used with our "grammar FST" framework (see The ContextFst object, i.e.

../doc/grammar.dox, for more details). This does not take the 'context_width' and 'central_position' arguments because they are assumed to be 2 and 1 respectively (meaning, left-biphone phonetic context).

This function creates a context FST and composes it on the left with "ifst" to make "ofst".

Parameters
[in]nonterm_phones_offsetThe integer id of the symbol #nonterm_bos in the phones.txt file. You can just set this to a large value (like 1 million) if you are not actually using nonterminals (e.g. for testing purposes).
[in]disambig_symsList of disambiguation symbols, e.g. the integer ids of #0, #1, #2 ... in the phones.txt.
[in,out]ifstThe FST we are composing with C (e.g. LG.fst).
[out]ofstComposed output FST (would be CLG.fst).
[out]ilabelsVector, indexed by ilabel of CLG.fst, providing information about the meaning of that ilabel; see The ilabel_info object (http://kaldi-asr.org/doc/tree_externals.html#tree_ilabel) and also Special symbols in CLG.fst (http://kaldi-asr.org/doc/grammar#grammar_special_clg).

Definition at line 196 of file grammar-context-fst.cc.

References ComposeDeterministicOnDemandInverse(), GetInputSymbols(), rnnlm::i, and InverseLeftBiphoneContextFst::SwapIlabelInfo().

Referenced by GetEncodingMultiple(), and main().

201  {
202 
203  vector<int32> disambig_syms(disambig_syms_in);
204  std::sort(disambig_syms.begin(), disambig_syms.end());
205 
206  vector<int32> all_syms;
207  GetInputSymbols(ifst, false/*no eps*/, &all_syms);
208  std::sort(all_syms.begin(), all_syms.end());
209  vector<int32> phones;
210  for (size_t i = 0; i < all_syms.size(); i++)
211  if (!std::binary_search(disambig_syms.begin(),
212  disambig_syms.end(), all_syms[i]) &&
213  all_syms[i] < nonterm_phones_offset)
214  phones.push_back(all_syms[i]);
215 
216 
217  InverseLeftBiphoneContextFst inv_c(nonterm_phones_offset,
218  phones, disambig_syms);
219 
220  // The following statement is equivalent to the following
221  // (if FSTs had the '*' operator for composition):
222  // (*ofst) = inv(inv_c) * (*ifst)
223  ComposeDeterministicOnDemandInverse(ifst, &inv_c, ofst);
224 
225  inv_c.SwapIlabelInfo(ilabels);
226 }
void GetInputSymbols(const Fst< Arc > &fst, bool include_eps, std::vector< I > *symbols)
GetInputSymbols gets the list of symbols on the input of fst (including epsilon, if include_eps == tr...
void ComposeDeterministicOnDemandInverse(const Fst< Arc > &right, DeterministicOnDemandFst< Arc > *left, MutableFst< Arc > *fst_composed)
This function does &#39;*fst_composed = Compose(Inverse(*fst2), fst1)&#39; Note that the arguments are revers...

◆ ComputeStateInfo()

void ComputeStateInfo ( const VectorFst< Arc > &  fst,
std::vector< char > *  epsilon_info 
)

This function will set epsilon_info to have size equal to the NumStates() of the FST, containing a logical-or of the enum values kStateHasEpsilonArcsEntering, kStateHasNonEpsilonArcsEntering, kStateHasEpsilonArcsLeaving, and kStateHasNonEpsilonArcsLeaving.

The meaning should be obvious. Note: an epsilon arc is defined as an arc where ilabel == olabel == 0.

Definition at line 28 of file epsilon-property-inl.h.

References kStateHasEpsilonArcsEntering, kStateHasEpsilonArcsLeaving, kStateHasNonEpsilonArcsEntering, and kStateHasNonEpsilonArcsLeaving.

Referenced by kaldi::CreateFactorTransducer(), EnsureEpsilonProperty(), and TestEnsureEpsilonProperty().

29  {
30  typedef typename Arc::StateId StateId;
31  typedef VectorFst<Arc> Fst;
32  epsilon_info->clear();
33  epsilon_info->resize(fst.NumStates(), static_cast<char>(0));
34  for (StateId s = 0; s < fst.NumStates(); s++) {
35  for (ArcIterator<Fst> aiter(fst, s); !aiter.Done(); aiter.Next()) {
36  const Arc &arc = aiter.Value();
37  if (arc.ilabel == 0 && arc.olabel == 0) {
38  (*epsilon_info)[arc.nextstate] |= static_cast<char>(kStateHasEpsilonArcsEntering);
39  (*epsilon_info)[s] |= static_cast<char>(kStateHasEpsilonArcsLeaving);
40  } else {
41  (*epsilon_info)[arc.nextstate] |= static_cast<char>(kStateHasNonEpsilonArcsEntering);
42  (*epsilon_info)[s] |= static_cast<char>(kStateHasNonEpsilonArcsLeaving);
43  }
44  }
45  }
46 }
fst::StdArc::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21

◆ ConvertFstToLattice()

void ConvertFstToLattice ( const ExpandedFst< ArcTpl< TropicalWeight > > &  ifst,
MutableFst< ArcTpl< LatticeWeightTpl< Real > > > *  ofst 
)

Converts TropicalWeight to LatticeWeight (puts all the weight on the first float in the lattice's pair).

Definition at line 266 of file lattice-utils-inl.h.

Referenced by ConvertLattice().

268  {
269  int32 num_states_cache = 50000;
270  fst::CacheOptions cache_opts(true, num_states_cache);
271  fst::MapFstOptions mapfst_opts(cache_opts);
272  StdToLatticeMapper<Real> mapper;
273  MapFst<StdArc, ArcTpl<LatticeWeightTpl<Real> >,
274  StdToLatticeMapper<Real> > map_fst(ifst, mapper, mapfst_opts);
275  *ofst = map_fst;
276 }
kaldi::int32 int32

◆ ConvertLattice() [1/7]

void ConvertLattice ( const ExpandedFst< ArcTpl< Weight > > &  ifst,
MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, Int > > > *  ofst,
bool  invert = true 
)

Convert lattice from a normal FST to a CompactLattice FST.

This is a bit like converting to the Gallic semiring, except the semiring behaves in a different way (designed to take the best path). Note: the ilabels end up as the symbols on the arcs of the output acceptor, and the olabels go to the strings. To make it the other way around (useful for the speech-recognition application), set invert=true [the default].

Definition at line 33 of file lattice-utils-inl.h.

References Factor(), and KALDI_PARANOID_ASSERT.

Referenced by ConvertLattice(), kaldi::ConvertLatticeToUnweightedAcceptor(), kaldi::ConvertToCompactLattice(), kaldi::ConvertToLattice(), DiscriminativeExampleSplitter::CreateOutputLattice(), kaldi::DecodeUtteranceLatticeIncremental(), DiscriminativeExampleSplitter::DoExcise(), kaldi::nnet2::ExampleToPdfPost(), kaldi::GetDiagnosticsAndPrintOutput(), NnetDiscriminativeUpdater::LatticeComputations(), kaldi::LatticeToString(), main(), MinimumBayesRisk::MinimumBayesRisk(), DiscriminativeExampleSplitter::PrepareLattice(), kaldi::RandCompactLattice(), kaldi::SentenceLevelConfidence(), kaldi::TestCompactLatticeTableCross(), TestConvert2(), kaldi::TestLatticeTableCross(), kaldi::TestWordAlignedLattice(), and kaldi::TestWordAlignLatticeLexicon().

36  {
37  typedef ArcTpl<Weight> Arc;
38  typedef typename Arc::StateId StateId;
39  typedef CompactLatticeWeightTpl<Weight, Int> CompactWeight;
40  typedef ArcTpl<CompactWeight> CompactArc;
41 
42  VectorFst<ArcTpl<Weight> > ffst;
43  std::vector<std::vector<Int> > labels;
44  if (invert) // normal case: want the ilabels as sequences on the arcs of
45  Factor(ifst, &ffst, &labels); // the output... Factor makes seqs of
46  // ilabels.
47  else {
48  VectorFst<ArcTpl<Weight> > invfst(ifst);
49  Invert(&invfst);
50  Factor(invfst, &ffst, &labels);
51  }
52 
53  TopSort(&ffst); // Put the states in ffst in topological order, which is
54  // easier on the eye when reading the text-form lattices and corresponds to
55  // what we get when we generate the lattices in the decoder.
56 
57  ofst->DeleteStates();
58 
59  // The states will be numbered exactly the same as the original FST.
60  // Add the states to the new FST.
61  StateId num_states = ffst.NumStates();
62  for (StateId s = 0; s < num_states; s++) {
63  StateId news = ofst->AddState();
64  assert(news == s);
65  }
66  ofst->SetStart(ffst.Start());
67  for (StateId s = 0; s < num_states; s++) {
68  Weight final_weight = ffst.Final(s);
69  if (final_weight != Weight::Zero()) {
70  CompactWeight final_compact_weight(final_weight, std::vector<Int>());
71  ofst->SetFinal(s, final_compact_weight);
72  }
73  for (ArcIterator<ExpandedFst<Arc> > iter(ffst, s);
74  !iter.Done();
75  iter.Next()) {
76  const Arc &arc = iter.Value();
77  KALDI_PARANOID_ASSERT(arc.weight != Weight::Zero());
78  // note: zero-weight arcs not allowed anyway so weight should not be zero,
79  // but no harm in checking.
80  CompactArc compact_arc(arc.olabel, arc.olabel,
81  CompactWeight(arc.weight, labels[arc.ilabel]),
82  arc.nextstate);
83  ofst->AddArc(s, compact_arc);
84  }
85  }
86 }
fst::StdArc::StateId StateId
#define KALDI_PARANOID_ASSERT(cond)
Definition: kaldi-error.h:206
void Factor(const Fst< Arc > &fst, MutableFst< Arc > *ofst, std::vector< std::vector< I > > *symbols_out)
Factor identifies linear chains of states with an olabel (if any) only on the first arc of the chain...
Definition: factor-inl.h:69
fst::StdArc::Weight Weight

◆ ConvertLattice() [2/7]

void fst::ConvertLattice ( const ExpandedFst< ArcTpl< LatticeWeightTpl< float > > > &  ifst,
MutableFst< ArcTpl< CompactLatticeWeightTpl< LatticeWeightTpl< double >, Int > > > *  ofst 
)

Definition at line 87 of file lattice-utils.h.

References ConvertLattice().

88  {
89  VectorFst<ArcTpl<CompactLatticeWeightTpl<LatticeWeightTpl<float>, Int> > > fst;
90  ConvertLattice(ifst, &fst);
91  ConvertLattice(fst, ofst);
92 }
void ConvertLattice(const ExpandedFst< ArcTpl< CompactLatticeWeightTpl< LatticeWeightTpl< float >, Int > > > &ifst, MutableFst< ArcTpl< LatticeWeightTpl< double > > > *ofst)
Converts CompactLattice with float to Lattice with double.
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21

◆ ConvertLattice() [3/7]

void ConvertLattice ( const ExpandedFst< ArcTpl< CompactLatticeWeightTpl< Weight, Int > > > &  ifst,
MutableFst< ArcTpl< Weight > > *  ofst,
bool  invert = true 
)

Convert lattice CompactLattice format to Lattice.

This is a bit like converting from the Gallic semiring. As for any CompactLattice, "ifst" must be an acceptor (i.e., ilabels and olabels should be identical). If invert=false, the labels on "ifst" become the ilabels on "ofst" and the strings in the weights of "ifst" becomes the olabels. If invert=true [default], this is reversed (useful for speech recognition lattices; our standard non-compact format has the words on the output side to match HCLG).

Definition at line 89 of file lattice-utils-inl.h.

References rnnlm::n, and kaldi::swap().

92  {
93  typedef ArcTpl<Weight> Arc;
94  typedef typename Arc::StateId StateId;
95  typedef typename Arc::Label Label;
96  typedef CompactLatticeWeightTpl<Weight, Int> CompactWeight;
97  typedef ArcTpl<CompactWeight> CompactArc;
98  ofst->DeleteStates();
99  // make the states in the new FST have the same numbers as
100  // the original ones, and add chains of states as necessary
101  // to encode the string-valued weights.
102  StateId num_states = ifst.NumStates();
103  for (StateId s = 0; s < num_states; s++) {
104  StateId news = ofst->AddState();
105  assert(news == s);
106  }
107  ofst->SetStart(ifst.Start());
108  for (StateId s = 0; s < num_states; s++) {
109  CompactWeight final_weight = ifst.Final(s);
110  if (final_weight != CompactWeight::Zero()) {
111  StateId cur_state = s;
112  size_t string_length = final_weight.String().size();
113  for (size_t n = 0; n < string_length; n++) {
114  StateId next_state = ofst->AddState();
115  Label ilabel = 0;
116  Arc arc(ilabel, final_weight.String()[n],
117  (n == 0 ? final_weight.Weight() : Weight::One()),
118  next_state);
119  if (invert) std::swap(arc.ilabel, arc.olabel);
120  ofst->AddArc(cur_state, arc);
121  cur_state = next_state;
122  }
123  ofst->SetFinal(cur_state,
124  string_length > 0 ? Weight::One() : final_weight.Weight());
125  }
126  for (ArcIterator<ExpandedFst<CompactArc> > iter(ifst, s);
127  !iter.Done();
128  iter.Next()) {
129  const CompactArc &arc = iter.Value();
130  size_t string_length = arc.weight.String().size();
131  StateId cur_state = s;
132  // for all but the last element in the string--
133  // add a temporary state.
134  for (size_t n = 0 ; n+1 < string_length; n++) {
135  StateId next_state = ofst->AddState();
136  Label ilabel = (n == 0 ? arc.ilabel : 0),
137  olabel = static_cast<Label>(arc.weight.String()[n]);
138  Weight weight = (n == 0 ? arc.weight.Weight() : Weight::One());
139  Arc new_arc(ilabel, olabel, weight, next_state);
140  if (invert) std::swap(new_arc.ilabel, new_arc.olabel);
141  ofst->AddArc(cur_state, new_arc);
142  cur_state = next_state;
143  }
144  Label ilabel = (string_length <= 1 ? arc.ilabel : 0),
145  olabel = (string_length > 0 ? arc.weight.String()[string_length-1] : 0);
146  Weight weight = (string_length <= 1 ? arc.weight.Weight() : Weight::One());
147  Arc new_arc(ilabel, olabel, weight, arc.nextstate);
148  if (invert) std::swap(new_arc.ilabel, new_arc.olabel);
149  ofst->AddArc(cur_state, new_arc);
150  }
151  }
152 }
fst::StdArc::StateId StateId
void swap(basic_filebuf< CharT, Traits > &x, basic_filebuf< CharT, Traits > &y)
struct rnnlm::@11::@12 n
fst::StdArc::Label Label
fst::StdArc::Weight Weight

◆ ConvertLattice() [4/7]

void fst::ConvertLattice ( const ExpandedFst< ArcTpl< LatticeWeightTpl< double > > > &  ifst,
MutableFst< ArcTpl< CompactLatticeWeightTpl< LatticeWeightTpl< float >, Int > > > *  ofst 
)

Definition at line 96 of file lattice-utils.h.

References ConvertLattice().

97  {
98  VectorFst<ArcTpl<CompactLatticeWeightTpl<LatticeWeightTpl<double>, Int> > > fst;
99  ConvertLattice(ifst, &fst);
100  ConvertLattice(fst, ofst);
101 }
void ConvertLattice(const ExpandedFst< ArcTpl< CompactLatticeWeightTpl< LatticeWeightTpl< float >, Int > > > &ifst, MutableFst< ArcTpl< LatticeWeightTpl< double > > > *ofst)
Converts CompactLattice with float to Lattice with double.
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21

◆ ConvertLattice() [5/7]

void fst::ConvertLattice ( const ExpandedFst< ArcTpl< CompactLatticeWeightTpl< LatticeWeightTpl< double >, Int > > > &  ifst,
MutableFst< ArcTpl< LatticeWeightTpl< float > > > *  ofst 
)

Converts CompactLattice with double to Lattice with float.

Definition at line 105 of file lattice-utils.h.

References ConvertLattice().

106  {
107  VectorFst<ArcTpl<CompactLatticeWeightTpl<LatticeWeightTpl<float>, Int> > > fst;
108  ConvertLattice(ifst, &fst);
109  ConvertLattice(fst, ofst);
110 }
void ConvertLattice(const ExpandedFst< ArcTpl< CompactLatticeWeightTpl< LatticeWeightTpl< float >, Int > > > &ifst, MutableFst< ArcTpl< LatticeWeightTpl< double > > > *ofst)
Converts CompactLattice with float to Lattice with double.
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21

◆ ConvertLattice() [6/7]

void fst::ConvertLattice ( const ExpandedFst< ArcTpl< CompactLatticeWeightTpl< LatticeWeightTpl< float >, Int > > > &  ifst,
MutableFst< ArcTpl< LatticeWeightTpl< double > > > *  ofst 
)

Converts CompactLattice with float to Lattice with double.

Definition at line 114 of file lattice-utils.h.

References ConvertFstToLattice(), and ConvertLattice().

115  {
116  VectorFst<ArcTpl<CompactLatticeWeightTpl<LatticeWeightTpl<double>, Int> > > fst;
117  ConvertLattice(ifst, &fst);
118  ConvertLattice(fst, ofst);
119 }
void ConvertLattice(const ExpandedFst< ArcTpl< CompactLatticeWeightTpl< LatticeWeightTpl< float >, Int > > > &ifst, MutableFst< ArcTpl< LatticeWeightTpl< double > > > *ofst)
Converts CompactLattice with float to Lattice with double.
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21

◆ ConvertLattice() [7/7]

void ConvertLattice ( const ExpandedFst< ArcTpl< WeightIn > > &  ifst,
MutableFst< ArcTpl< WeightOut > > *  ofst 
)

Convert between CompactLattices and Lattices of different floating point types...

this works between any pair of weight types for which ConvertLatticeWeight is defined (c.f. lattice-weight.h), and also includes conversion from LatticeWeight to TropicalWeight.

Definition at line 157 of file lattice-utils-inl.h.

References ConvertLatticeWeight(), and KALDI_PARANOID_ASSERT.

159  {
160  typedef ArcTpl<WeightIn> ArcIn;
161  typedef ArcTpl<WeightOut> ArcOut;
162  typedef typename ArcIn::StateId StateId;
163  ofst->DeleteStates();
164  // The states will be numbered exactly the same as the original FST.
165  // Add the states to the new FST.
166  StateId num_states = ifst.NumStates();
167  for (StateId s = 0; s < num_states; s++) {
168  StateId news = ofst->AddState();
169  assert(news == s);
170  }
171  ofst->SetStart(ifst.Start());
172  for (StateId s = 0; s < num_states; s++) {
173  WeightIn final_iweight = ifst.Final(s);
174  if (final_iweight != WeightIn::Zero()) {
175  WeightOut final_oweight;
176  ConvertLatticeWeight(final_iweight, &final_oweight);
177  ofst->SetFinal(s, final_oweight);
178  }
179  for (ArcIterator<ExpandedFst<ArcIn> > iter(ifst, s);
180  !iter.Done();
181  iter.Next()) {
182  ArcIn arc = iter.Value();
183  KALDI_PARANOID_ASSERT(arc.weight != WeightIn::Zero());
184  ArcOut oarc;
185  ConvertLatticeWeight(arc.weight, &oarc.weight);
186  oarc.ilabel = arc.ilabel;
187  oarc.olabel = arc.olabel;
188  oarc.nextstate = arc.nextstate;
189  ofst->AddArc(s, oarc);
190  }
191  }
192 }
fst::StdArc::StateId StateId
void ConvertLatticeWeight(const LatticeWeightTpl< Float1 > &w_in, LatticeWeightTpl< Float2 > *w_out)
Define some ConvertLatticeWeight functions that are used in various lattice conversions...
#define KALDI_PARANOID_ASSERT(cond)
Definition: kaldi-error.h:206

◆ ConvertLatticeWeight() [1/3]

void fst::ConvertLatticeWeight ( const LatticeWeightTpl< Float1 > &  w_in,
LatticeWeightTpl< Float2 > *  w_out 
)
inline

Define some ConvertLatticeWeight functions that are used in various lattice conversions...

make them all templates, some with no arguments, since some must be templates.

Definition at line 819 of file lattice-weight.h.

References LatticeWeightTpl< FloatType >::SetValue1(), LatticeWeightTpl< FloatType >::SetValue2(), LatticeWeightTpl< FloatType >::Value1(), and LatticeWeightTpl< FloatType >::Value2().

Referenced by ConvertLattice().

821  {
822  w_out->SetValue1(w_in.Value1());
823  w_out->SetValue2(w_in.Value2());
824 }

◆ ConvertLatticeWeight() [2/3]

void fst::ConvertLatticeWeight ( const CompactLatticeWeightTpl< LatticeWeightTpl< Float1 >, Int > &  w_in,
CompactLatticeWeightTpl< LatticeWeightTpl< Float2 >, Int > *  w_out 
)
inline

Definition at line 827 of file lattice-weight.h.

829  {
830  LatticeWeightTpl<Float2> weight2(w_in.Weight().Value1(),
831  w_in.Weight().Value2());
832  w_out->SetWeight(weight2);
833  w_out->SetString(w_in.String());
834 }

◆ ConvertLatticeWeight() [3/3]

void fst::ConvertLatticeWeight ( const LatticeWeightTpl< Float1 > &  w_in,
TropicalWeightTpl< Float2 > *  w_out 
)
inline

Definition at line 838 of file lattice-weight.h.

References Times(), LatticeWeightTpl< FloatType >::Value1(), and LatticeWeightTpl< FloatType >::Value2().

840  {
841  TropicalWeightTpl<Float2> w1(w_in.Value1());
842  TropicalWeightTpl<Float2> w2(w_in.Value2());
843  *w_out = Times(w1, w2);
844 }
CompactLatticeWeightTpl< WeightType, IntType > Times(const CompactLatticeWeightTpl< WeightType, IntType > &w1, const CompactLatticeWeightTpl< WeightType, IntType > &w2)

◆ ConvertNbestToVector()

void ConvertNbestToVector ( const Fst< Arc > &  fst,
std::vector< VectorFst< Arc > > *  fsts_out 
)

This function converts an FST with a special structure, which is output by the OpenFst functions ShortestPath and RandGen, and converts them into a std::vector of separate FSTs.

This special structure is that the only state that has more than one (arcs-out or final-prob) is the start state. fsts_out is resized to the appropriate size.

Definition at line 221 of file fstext-utils-inl.h.

References KALDI_ASSERT.

Referenced by main(), MinimizeEncoded(), NbestAsFsts(), and kaldi::TestWordAlignLatticeLexicon().

222  {
223  typedef typename Arc::Weight Weight;
224  typedef typename Arc::StateId StateId;
225  fsts_out->clear();
226  StateId start_state = fst.Start();
227  if (start_state == kNoStateId) return; // No output.
228  size_t n_arcs = fst.NumArcs(start_state);
229  bool start_is_final = (fst.Final(start_state) != Weight::Zero());
230  fsts_out->reserve(n_arcs + (start_is_final ? 1 : 0));
231 
232  if (start_is_final) {
233  fsts_out->resize(fsts_out->size() + 1);
234  StateId start_state_out = fsts_out->back().AddState();
235  fsts_out->back().SetFinal(start_state_out, fst.Final(start_state));
236  }
237 
238  for (ArcIterator<Fst<Arc> > start_aiter(fst, start_state);
239  !start_aiter.Done();
240  start_aiter.Next()) {
241  fsts_out->resize(fsts_out->size() + 1);
242  VectorFst<Arc> &ofst = fsts_out->back();
243  const Arc &first_arc = start_aiter.Value();
244  StateId cur_state = start_state,
245  cur_ostate = ofst.AddState();
246  ofst.SetStart(cur_ostate);
247  StateId next_ostate = ofst.AddState();
248  ofst.AddArc(cur_ostate, Arc(first_arc.ilabel, first_arc.olabel,
249  first_arc.weight, next_ostate));
250  cur_state = first_arc.nextstate;
251  cur_ostate = next_ostate;
252  while (1) {
253  size_t this_n_arcs = fst.NumArcs(cur_state);
254  KALDI_ASSERT(this_n_arcs <= 1); // or it violates our assumptions
255  // about the input.
256  if (this_n_arcs == 1) {
257  KALDI_ASSERT(fst.Final(cur_state) == Weight::Zero());
258  // or problem with ShortestPath.
259  ArcIterator<Fst<Arc> > aiter(fst, cur_state);
260  const Arc &arc = aiter.Value();
261  next_ostate = ofst.AddState();
262  ofst.AddArc(cur_ostate, Arc(arc.ilabel, arc.olabel,
263  arc.weight, next_ostate));
264  cur_state = arc.nextstate;
265  cur_ostate = next_ostate;
266  } else {
267  KALDI_ASSERT(fst.Final(cur_state) != Weight::Zero());
268  // or problem with ShortestPath.
269  ofst.SetFinal(cur_ostate, fst.Final(cur_state));
270  break;
271  }
272  }
273  }
274 }
fst::StdArc::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
fst::StdArc::Weight Weight
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ ConvertToCost() [1/3]

◆ ConvertToCost() [2/3]

double fst::ConvertToCost ( const CompactLatticeWeightTpl< LatticeWeightTpl< Float >, Int > &  w)
inline

Definition at line 852 of file lattice-weight.h.

852  {
853  return static_cast<double>(w.Weight().Value1()) + static_cast<double>(w.Weight().Value2());
854 }

◆ ConvertToCost() [3/3]

double fst::ConvertToCost ( const TropicalWeightTpl< Float > &  w)
inline

Definition at line 857 of file lattice-weight.h.

857  {
858  return w.Value();
859 }

◆ CopyToVectorFst()

void CopyToVectorFst ( GrammarFst grammar_fst,
VectorFst< StdArc > *  vector_fst 
)

This function copies a GrammarFst to a VectorFst (intended mostly for testing and comparison purposes).

GrammarFst doesn't actually inherit from class Fst, so we can't just construct an FST from the GrammarFst.

grammar_fst gets expanded by this call, and although we could make it a const reference (because the ArcIterator does actually use const_cast), we make it a non-const pointer to emphasize that this call does change grammar_fst.

Definition at line 988 of file grammar-fst.cc.

References ArcIterator< GrammarFst >::Done(), GrammarFst::Final(), GrammarFstArc::ilabel, ArcIterator< GrammarFst >::Next(), GrammarFstArc::nextstate, GrammarFstArc::olabel, GrammarFst::Start(), ArcIterator< GrammarFst >::Value(), and GrammarFstArc::weight.

Referenced by main().

989  {
990  typedef GrammarFstArc::StateId GrammarStateId; // int64
991  typedef StdArc::StateId StdStateId; // int
992  typedef StdArc::Label Label;
993  typedef StdArc::Weight Weight;
994 
995  std::vector<std::pair<GrammarStateId, StdStateId> > queue;
996  std::unordered_map<GrammarStateId, StdStateId> state_map;
997 
998  vector_fst->DeleteStates();
999  state_map[grammar_fst->Start()] = vector_fst->AddState(); // state 0.
1000  vector_fst->SetStart(0);
1001 
1002  queue.push_back(
1003  std::pair<GrammarStateId, StdStateId>(grammar_fst->Start(), 0));
1004 
1005  while (!queue.empty()) {
1006  std::pair<GrammarStateId, StdStateId> p = queue.back();
1007  queue.pop_back();
1008  GrammarStateId grammar_state = p.first;
1009  StdStateId std_state = p.second;
1010  vector_fst->SetFinal(std_state, grammar_fst->Final(grammar_state));
1011  ArcIterator<GrammarFst> aiter(*grammar_fst, grammar_state);
1012  for (; !aiter.Done(); aiter.Next()) {
1013  const GrammarFstArc &grammar_arc = aiter.Value();
1014  StdArc std_arc;
1015  std_arc.ilabel = grammar_arc.ilabel;
1016  std_arc.olabel = grammar_arc.olabel;
1017  std_arc.weight = grammar_arc.weight;
1018  GrammarStateId next_grammar_state = grammar_arc.nextstate;
1019  StdStateId next_std_state;
1020  std::unordered_map<GrammarStateId, StdStateId>::const_iterator
1021  state_iter = state_map.find(next_grammar_state);
1022  if (state_iter == state_map.end()) {
1023  next_std_state = vector_fst->AddState();
1024  state_map[next_grammar_state] = next_std_state;
1025  queue.push_back(std::pair<GrammarStateId, StdStateId>(
1026  next_grammar_state, next_std_state));
1027  } else {
1028  next_std_state = state_iter->second;
1029  }
1030  std_arc.nextstate = next_std_state;
1031  vector_fst->AddArc(std_state, std_arc);
1032  }
1033  }
1034 }
fst::StdArc::StateId StateId
fst::StdArc StdArc
fst::StdArc::Label Label
fst::StdArc::Weight Weight

◆ CreateBackoffFst()

StdVectorFst* fst::CreateBackoffFst ( )

Definition at line 64 of file deterministic-fst-test.cc.

Referenced by TestBackoffAndCache(), and TestCompose().

64  {
65  StdVectorFst *fst = new StdVectorFst();
66  fst->AddState(); // state 0
67  fst->SetStart(0);
68  fst->AddArc(0, StdArc(10, 10, 0.0, 1));
69 
70  fst->AddState(); // state 1
71  fst->AddArc(1, StdArc(12, 12, 0.0, 4));
72  fst->AddArc(1, StdArc(0,0, 0.1,2)); // backoff from 1 to 2
73 
74  fst->AddState(); // state 2
75  fst->AddArc(2, StdArc(13, 13, 0.2, 4));
76  fst->AddArc(2, StdArc(0,0, 0.3,3)); // backoff from 2 to 3
77 
78  fst->AddState(); // state 3
79  fst->AddArc(3, StdArc(14, 14, 0.4, 4));
80 
81  fst->AddState(); // state 4
82  fst->AddArc(4, StdArc(15, 15, 0.5, 5));
83 
84  fst->AddState(); // state 5
85  fst->SetFinal(5, 0.6);
86 
87  return fst;
88 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
fst::StdArc StdArc
fst::StdVectorFst StdVectorFst

◆ CreateFactorFst()

void CreateFactorFst ( const std::vector< std::vector< I > > &  sequences,
MutableFst< Arc > *  fst 
)

The function CreateFactorFst will create an FST that expands out the "factors" that are the indices of the "sequences" array, into linear sequences of symbols.

There is a single start and end state (state 0), and for each nonzero index i into the array "sequences", there is an arc from state 0 that has output-label i, and enters a chain of states with output epsilons and input labels corresponding to the remaining elements of the sequences, terminating again in state 0. This FST is output-deterministic and sorted on olabel. Composing an FST on the left with the output of this function, should be the same as calling "ExpandInputSequences". Use TableCompose (see table-matcher.h) for efficiency.

Definition at line 250 of file factor-inl.h.

References rnnlm::i, and KALDI_ASSERT_IS_INTEGER_TYPE.

Referenced by Factor(), and TestFactor().

251  {
253  typedef typename Arc::StateId StateId;
254  typedef typename Arc::Label Label;
255  typedef typename Arc::Weight Weight;
256 
257  assert(fst != NULL);
258  fst->DeleteStates();
259  StateId loopstate = fst->AddState();
260  assert(loopstate == 0);
261  fst->SetStart(0);
262  fst->SetFinal(0, Weight::One());
263  if (sequences.size() != 0) assert(sequences[0].size() == 0); // can't replace epsilon...
264 
265  for (Label olabel = 1; olabel < static_cast<Label>(sequences.size()); olabel++) {
266  size_t len = sequences[olabel].size();
267  if (len == 0) {
268  Arc arc(0, olabel, Weight::One(), loopstate);
269  fst->AddArc(loopstate, arc);
270  } else {
271  StateId curstate = loopstate;
272  for (size_t i = 0; i < len; i++) {
273  StateId nextstate = (i == len-1 ? loopstate : fst->AddState());
274  Arc arc(sequences[olabel][i], (i == 0 ? olabel : 0), Weight::One(), nextstate);
275  fst->AddArc(curstate, arc);
276  curstate = nextstate;
277  }
278  }
279  }
280  fst->SetProperties(kOLabelSorted, kOLabelSorted);
281 }
fst::StdArc::StateId StateId
#define KALDI_ASSERT_IS_INTEGER_TYPE(I)
Definition: kaldi-utils.h:133
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
fst::StdArc::Label Label
fst::StdArc::Weight Weight

◆ CreateILabelInfoSymbolTable() [1/2]

SymbolTable* fst::CreateILabelInfoSymbolTable ( const std::vector< std::vector< int32 > > &  ilabel_info,
const SymbolTable &  phones_symtab,
std::string  separator,
std::string  disambig_prefix 
)

The following function is mainly of use for printing and debugging.

Definition at line 345 of file context-fst.cc.

References rnnlm::i, rnnlm::j, KALDI_ASSERT, and KALDI_ERR.

Referenced by main().

348  { // e.g. separator = "/", initial-disambig="#-1"
349  KALDI_ASSERT(!info.empty() && info[0].empty());
350  SymbolTable *ans = new SymbolTable("ilabel-info-symtab");
351  int64 s = ans->AddSymbol(phones_symtab.Find(static_cast<int64>(0)));
352  assert(s == 0);
353  for (size_t i = 1; i < info.size(); i++) {
354  if (info[i].size() == 0) {
355  KALDI_ERR << "Invalid ilabel-info";
356  }
357  if (info[i].size() == 1 &&
358  info[i][0] <= 0) {
359  if (info[i][0] == 0) { // special symbol at start that we want to call #-1.
360  s = ans->AddSymbol(initial_disambig);
361  if (s != i) {
362  KALDI_ERR << "Disambig symbol " << initial_disambig
363  << " already in vocab";
364  }
365  } else {
366  std::string disambig_sym = phones_symtab.Find(-info[i][0]);
367  if (disambig_sym == "") {
368  KALDI_ERR << "Disambig symbol " << -info[i][0]
369  << " not in phone symbol-table";
370  }
371  s = ans->AddSymbol(disambig_sym);
372  if (s != i) {
373  KALDI_ERR << "Disambig symbol " << disambig_sym
374  << " already in vocab";
375  }
376  }
377  } else {
378  // is a phone-context-window.
379  std::string newsym;
380  for (size_t j = 0; j < info[i].size(); j++) {
381  std::string phonesym = phones_symtab.Find(info[i][j]);
382  if (phonesym == "") {
383  KALDI_ERR << "Symbol " << info[i][j]
384  << " not in phone symbol-table";
385  }
386  if (j != 0) newsym += separator;
387  newsym += phonesym;
388  }
389  int64 s = ans->AddSymbol(newsym);
390  if (s != static_cast<int64>(i)) {
391  KALDI_ERR << "Some problem with duplicate symbols";
392  }
393  }
394  }
395  return ans;
396 }
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ CreateILabelInfoSymbolTable() [2/2]

SymbolTable* fst::CreateILabelInfoSymbolTable ( const vector< vector< int32 > > &  info,
const SymbolTable &  phones_symtab,
std::string  separator,
std::string  initial_disambig 
)

The following function is mainly of use for printing and debugging.

Definition at line 345 of file context-fst.cc.

References rnnlm::i, rnnlm::j, KALDI_ASSERT, and KALDI_ERR.

Referenced by main().

348  { // e.g. separator = "/", initial-disambig="#-1"
349  KALDI_ASSERT(!info.empty() && info[0].empty());
350  SymbolTable *ans = new SymbolTable("ilabel-info-symtab");
351  int64 s = ans->AddSymbol(phones_symtab.Find(static_cast<int64>(0)));
352  assert(s == 0);
353  for (size_t i = 1; i < info.size(); i++) {
354  if (info[i].size() == 0) {
355  KALDI_ERR << "Invalid ilabel-info";
356  }
357  if (info[i].size() == 1 &&
358  info[i][0] <= 0) {
359  if (info[i][0] == 0) { // special symbol at start that we want to call #-1.
360  s = ans->AddSymbol(initial_disambig);
361  if (s != i) {
362  KALDI_ERR << "Disambig symbol " << initial_disambig
363  << " already in vocab";
364  }
365  } else {
366  std::string disambig_sym = phones_symtab.Find(-info[i][0]);
367  if (disambig_sym == "") {
368  KALDI_ERR << "Disambig symbol " << -info[i][0]
369  << " not in phone symbol-table";
370  }
371  s = ans->AddSymbol(disambig_sym);
372  if (s != i) {
373  KALDI_ERR << "Disambig symbol " << disambig_sym
374  << " already in vocab";
375  }
376  }
377  } else {
378  // is a phone-context-window.
379  std::string newsym;
380  for (size_t j = 0; j < info[i].size(); j++) {
381  std::string phonesym = phones_symtab.Find(info[i][j]);
382  if (phonesym == "") {
383  KALDI_ERR << "Symbol " << info[i][j]
384  << " not in phone symbol-table";
385  }
386  if (j != 0) newsym += separator;
387  newsym += phonesym;
388  }
389  int64 s = ans->AddSymbol(newsym);
390  if (s != static_cast<int64>(i)) {
391  KALDI_ERR << "Some problem with duplicate symbols";
392  }
393  }
394  }
395  return ans;
396 }
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ CreateMapFst()

void CreateMapFst ( const std::vector< I > &  symbol_map,
MutableFst< Arc > *  fst 
)

CreateMapFst will create an FST representing this symbol_map.

The FST has a single loop state with single-arc loops with isymbol = symbol_map[i], osymbol = i. The resulting FST applies this map to the input symbols of something we compose with it on the right. Must have symbol_map[0] == 0.

Definition at line 285 of file factor-inl.h.

References KALDI_ASSERT_IS_INTEGER_TYPE.

Referenced by main().

286  {
288  typedef typename Arc::StateId StateId;
289  typedef typename Arc::Label Label;
290  typedef typename Arc::Weight Weight;
291 
292  assert(fst != NULL);
293  fst->DeleteStates();
294  StateId loopstate = fst->AddState();
295  assert(loopstate == 0);
296  fst->SetStart(0);
297  fst->SetFinal(0, Weight::One());
298  assert(symbol_map.empty() || symbol_map[0] == 0); // FST cannot map epsilon to something else.
299  for (Label olabel = 1; olabel < static_cast<Label>(symbol_map.size()); olabel++) {
300  Arc arc(symbol_map[olabel], olabel, Weight::One(), loopstate);
301  fst->AddArc(loopstate, arc);
302  }
303 }
fst::StdArc::StateId StateId
#define KALDI_ASSERT_IS_INTEGER_TYPE(I)
Definition: kaldi-utils.h:133
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
fst::StdArc::Label Label
fst::StdArc::Weight Weight

◆ CreateNewSymbols()

void CreateNewSymbols ( SymbolTable *  input_sym_table,
int  nSym,
std::string  prefix,
std::vector< Label > *  symsOut 
)

Definition at line 581 of file pre-determinize-inl.h.

References rnnlm::i.

Referenced by TestAddSelfLoops().

582  {
583  // Creates nSym new symbols named (prefix)0, (prefix)1 and so on.
584  // Crashes if it cannot create them because one or more of them were in the symbol
585  // table already.
586  assert(symsOut && symsOut->size() == 0);
587  for (int i = 0;i < nSym;i++) {
588  std::stringstream ss; ss << prefix << i;
589  std::string str = ss.str();
590  if (input_sym_table->Find(str) != -1) { // should not be present.
591  }
592  assert(symsOut);
593  symsOut->push_back( (Label) input_sym_table->AddSymbol(str));
594  }
595 }
fst::StdArc::Label Label

◆ CreateResultFst()

StdVectorFst* fst::CreateResultFst ( )

Definition at line 91 of file deterministic-fst-test.cc.

Referenced by TestBackoffAndCache(), and TestCompose().

91  {
92  StdVectorFst *fst = new StdVectorFst();
93  fst->AddState(); // state 0
94  fst->SetStart(0);
95  fst->AddArc(0, StdArc(10, 10, 0.0, 1));
96 
97  fst->AddState(); // state 1
98  fst->AddArc(1, StdArc(12, 12, 0.0, 4));
99  fst->AddArc(1, StdArc(13,13,0.3,4)); // went through 1 backoff
100  fst->AddArc(1, StdArc(14,14,0.8,4)); // went through 2 backoffs
101 
102  fst->AddState(); // state 2
103  fst->AddState(); // state 3
104 
105  fst->AddState(); // state 4
106  fst->AddArc(4, StdArc(15, 15, 0.5, 5));
107 
108  fst->AddState(); // state 5
109  fst->SetFinal(5, 0.6);
110 
111  return fst;
112 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
fst::StdArc StdArc
fst::StdVectorFst StdVectorFst

◆ CreateSuperFinal()

Arc::StateId CreateSuperFinal ( MutableFst< Arc > *  fst)

Definition at line 687 of file pre-determinize-inl.h.

Referenced by LatticeLexiconWordAligner::LatticeLexiconWordAligner(), LatticePhoneAligner::LatticePhoneAligner(), LatticeWordAligner::LatticeWordAligner(), main(), PreDeterminize(), and MinimumBayesRisk::PrepareLatticeAndInitStats().

687  {
688  typedef typename Arc::StateId StateId;
689  typedef typename Arc::Weight Weight;
690  assert(fst != NULL);
691  StateId num_states = fst->NumStates();
692  StateId num_final = 0;
693  std::vector<StateId> final_states;
694  for (StateId s = 0; s < num_states; s++) {
695  if (fst->Final(s) != Weight::Zero()) {
696  num_final++;
697  final_states.push_back(s);
698  }
699  }
700  if (final_states.size() == 1) {
701  if (fst->Final(final_states[0]) == Weight::One()) {
702  ArcIterator<MutableFst<Arc> > iter(*fst, final_states[0]);
703  if (iter.Done()) {
704  // We already have a final state w/ no transitions out and unit weight.
705  // So we're done.
706  return final_states[0];
707  }
708  }
709  }
710 
711  StateId final_state = fst->AddState();
712  fst->SetFinal(final_state, Weight::One());
713  for (size_t idx = 0; idx < final_states.size(); idx++) {
714  StateId s = final_states[idx];
715  Weight weight = fst->Final(s);
716  fst->SetFinal(s, Weight::Zero());
717  Arc arc;
718  arc.ilabel = 0;
719  arc.olabel = 0;
720  arc.nextstate = final_state;
721  arc.weight = weight;
722  fst->AddArc(s, arc);
723  }
724  return final_state;
725 }
fst::StdArc::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
fst::StdArc::Weight Weight

◆ DefaultLatticeScale()

std::vector<std::vector<double> > fst::DefaultLatticeScale ( )
inline

Returns a default 2x2 matrix scaling factor for LatticeWeight.

Definition at line 130 of file lattice-utils.h.

Referenced by ScaleLattice(), and TestScalePair().

130  {
131  std::vector<std::vector<double> > ans(2);
132  ans[0].resize(2, 0.0);
133  ans[1].resize(2, 0.0);
134  ans[0][0] = ans[1][1] = 1.0;
135  return ans;
136 }

◆ DeleteISymbols()

int64 DeleteISymbols ( MutableFst< Arc > *  fst,
std::vector< typename Arc::Label >  isyms 
)

Definition at line 651 of file pre-determinize-inl.h.

References rnnlm::i.

Referenced by TestDeterminize(), TestMinimize(), and TestPreDeterminize().

651  {
652 
653  // We could do this using the Mapper concept, but this is much easier to understand.
654 
655  typedef typename Arc::Label Label;
656  typedef typename Arc::StateId StateId;
657 
658  int64 num_deleted = 0;
659 
660  if (isyms.size() == 0) return 0;
661  Label isyms_min = *std::min_element(isyms.begin(), isyms.end()),
662  isyms_max = *std::max_element(isyms.begin(), isyms.end());
663  bool isyms_consecutive = (isyms_max+1-isyms_min == static_cast<Label>(isyms.size()));
664  std::set<Label> isyms_set;
665  if (!isyms_consecutive)
666  for (size_t i = 0;i < isyms.size();i++)
667  isyms_set.insert(isyms[i]);
668 
669  for (StateIterator<MutableFst<Arc> > siter(*fst); ! siter.Done(); siter.Next()) {
670  StateId state = siter.Value();
671  for (MutableArcIterator<MutableFst<Arc> > aiter(fst, state); ! aiter.Done(); aiter.Next()) {
672  const Arc &arc = aiter.Value();
673  if (arc.ilabel >= isyms_min && arc.ilabel <= isyms_max) {
674  if (isyms_consecutive || isyms_set.count(arc.ilabel) != 0) {
675  num_deleted++;
676  Arc mod_arc (arc);
677  mod_arc.ilabel = 0; // change label to epsilon.
678  aiter.SetValue(mod_arc);
679  }
680  }
681  }
682  }
683  return num_deleted;
684 }
fst::StdArc::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
fst::StdArc::Label Label

◆ DeleteTestFst()

void fst::DeleteTestFst ( StdVectorFst fst)

Definition at line 114 of file deterministic-fst-test.cc.

114  {
115  delete fst;
116 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21

◆ DeterminizeInLog()

void fst::DeterminizeInLog ( VectorFst< StdArc > *  fst)
inline

Definition at line 388 of file fstext-utils-inl.h.

Referenced by main().

388  {
389  // DeterminizeInLog determinizes 'fst' in the log semiring.
390 
391  ArcSort(fst, ILabelCompare<StdArc>()); // helps DeterminizeStar to be faster.
392  VectorFst<LogArc> *fst_log = new VectorFst<LogArc>; // Want to determinize in log semiring.
393  Cast(*fst, fst_log);
394  VectorFst<StdArc> tmp;
395  *fst = tmp; // make fst empty to free up memory. [actually may make no difference..]
396  VectorFst<LogArc> *fst_det_log = new VectorFst<LogArc>;
397  Determinize(*fst_log, fst_det_log);
398  Cast(*fst_det_log, fst);
399  delete fst_log;
400  delete fst_det_log;
401 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21

◆ DeterminizeLatticeDeletePhones()

template void fst::DeterminizeLatticeDeletePhones ( ArcTpl< kaldi::LatticeWeight >::Label  first_phone_label,
MutableFst< ArcTpl< kaldi::LatticeWeight > > *  fst 
)

◆ DeterminizeLatticePhonePruned< kaldi::LatticeWeight, kaldi::int32 >() [1/2]

template bool fst::DeterminizeLatticePhonePruned< kaldi::LatticeWeight, kaldi::int32 > ( const kaldi::TransitionModel trans_model,
const ExpandedFst< kaldi::LatticeArc > &  ifst,
double  prune,
MutableFst< kaldi::CompactLatticeArc > *  ofst,
DeterminizeLatticePhonePrunedOptions  opts 
)

◆ DeterminizeLatticePhonePruned< kaldi::LatticeWeight, kaldi::int32 >() [2/2]

template bool fst::DeterminizeLatticePhonePruned< kaldi::LatticeWeight, kaldi::int32 > ( const kaldi::TransitionModel trans_model,
MutableFst< kaldi::LatticeArc > *  ifst,
double  prune,
MutableFst< kaldi::CompactLatticeArc > *  ofst,
DeterminizeLatticePhonePrunedOptions  opts 
)

◆ DeterminizeLatticePhonePrunedFirstPass()

bool fst::DeterminizeLatticePhonePrunedFirstPass ( const kaldi::TransitionModel trans_model,
double  beam,
MutableFst< ArcTpl< Weight > > *  fst,
const DeterminizeLatticePrunedOptions opts 
)

This function does a first pass determinization with phone symbols inserted at phone boundary.

It uses a transition model to work out the transition-id to phone map. First, phones will be inserted into the word level lattice. Second, determinization will be applied on top of the phone + word lattice. Finally, the inserted phones will be removed, converting the lattice back to a word level lattice. The output lattice of this pass is not deterministic, since we remove the phone symbols as a last step. It is supposed to be followed by another pass of determinization at the word level. It could also be useful for some other applications such as fMLLR estimation, confidence estimation, discriminative training, etc.

Definition at line 1393 of file determinize-lattice-pruned.cc.

References DeterminizeLatticeDeletePhones(), and DeterminizeLatticeInsertPhones().

1397  {
1398  // First, insert the phones.
1399  typename ArcTpl<Weight>::Label first_phone_label =
1400  DeterminizeLatticeInsertPhones(trans_model, fst);
1401  TopSort(fst);
1402 
1403  // Second, do determinization with phone inserted.
1404  bool ans = DeterminizeLatticePruned<Weight>(*fst, beam, fst, opts);
1405 
1406  // Finally, remove the inserted phones.
1407  DeterminizeLatticeDeletePhones(first_phone_label, fst);
1408  TopSort(fst);
1409 
1410  return ans;
1411 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
template void DeterminizeLatticeDeletePhones(ArcTpl< kaldi::LatticeWeight >::Label first_phone_label, MutableFst< ArcTpl< kaldi::LatticeWeight > > *fst)
fst::StdArc::Label Label
ArcTpl< Weight >::Label DeterminizeLatticeInsertPhones(const kaldi::TransitionModel &trans_model, MutableFst< ArcTpl< Weight > > *fst)
This function takes in lattices and inserts phones at phone boundaries.

◆ DeterminizeLatticePruned< kaldi::LatticeWeight >() [1/2]

template bool fst::DeterminizeLatticePruned< kaldi::LatticeWeight > ( const ExpandedFst< kaldi::LatticeArc > &  ifst,
double  prune,
MutableFst< kaldi::CompactLatticeArc > *  ofst,
DeterminizeLatticePrunedOptions  opts 
)

◆ DeterminizeLatticePruned< kaldi::LatticeWeight >() [2/2]

template bool fst::DeterminizeLatticePruned< kaldi::LatticeWeight > ( const ExpandedFst< kaldi::LatticeArc > &  ifst,
double  prune,
MutableFst< kaldi::LatticeArc > *  ofst,
DeterminizeLatticePrunedOptions  opts 
)

◆ DeterminizeStarInLog()

void DeterminizeStarInLog ( VectorFst< StdArc > *  fst,
float  delta,
bool debug_ptr,
int  max_states 
)
inline

Definition at line 371 of file fstext-utils-inl.h.

References DeterminizeStar().

Referenced by TrainingGraphCompiler::CompileGraph(), TrainingGraphCompiler::CompileGraphs(), and main().

371  {
372  // DeterminizeStarInLog determinizes 'fst' in the log semiring, using
373  // the DeterminizeStar algorithm (which also removes epsilons).
374 
375  ArcSort(fst, ILabelCompare<StdArc>()); // helps DeterminizeStar to be faster.
376  VectorFst<LogArc> *fst_log = new VectorFst<LogArc>; // Want to determinize in log semiring.
377  Cast(*fst, fst_log);
378  VectorFst<StdArc> tmp;
379  *fst = tmp; // make fst empty to free up memory. [actually may make no difference..]
380  VectorFst<LogArc> *fst_det_log = new VectorFst<LogArc>;
381  DeterminizeStar(*fst_log, fst_det_log, delta, debug_ptr, max_states);
382  Cast(*fst_det_log, fst);
383  delete fst_log;
384  delete fst_det_log;
385 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
bool DeterminizeStar(F &ifst, MutableFst< typename F::Arc > *ofst, float delta, bool *debug_ptr, int max_states, bool allow_partial)
This function implements the normal version of DeterminizeStar, in which the output strings are repre...

◆ Divide() [1/5]

ArcticWeightTpl<T> fst::Divide ( const ArcticWeightTpl< T > &  w1,
const ArcticWeightTpl< T > &  w2,
DivideType  typ = DIVIDE_ANY 
)
inline

Definition at line 125 of file arctic-weight.h.

127  {
128  T f1 = w1.Value(), f2 = w2.Value();
129  if (f2 == -std::numeric_limits<T>::infinity())
130  return std::numeric_limits<T>::quiet_NaN();
131  else if (f1 == -std::numeric_limits<T>::infinity())
132  return -std::numeric_limits<T>::infinity();
133  else
134  return ArcticWeightTpl<T>(f1 - f2);
135 }

◆ Divide() [2/5]

ArcticWeightTpl<float> fst::Divide ( const ArcticWeightTpl< float > &  w1,
const ArcticWeightTpl< float > &  w2,
DivideType  typ = DIVIDE_ANY 
)
inline

Definition at line 137 of file arctic-weight.h.

139  {
140  return Divide<float>(w1, w2, typ);
141 }

◆ Divide() [3/5]

ArcticWeightTpl<double> fst::Divide ( const ArcticWeightTpl< double > &  w1,
const ArcticWeightTpl< double > &  w2,
DivideType  typ = DIVIDE_ANY 
)
inline

Definition at line 143 of file arctic-weight.h.

145  {
146  return Divide<double>(w1, w2, typ);
147 }

◆ Divide() [4/5]

LatticeWeightTpl<FloatType> fst::Divide ( const LatticeWeightTpl< FloatType > &  w1,
const LatticeWeightTpl< FloatType > &  w2,
DivideType  typ = DIVIDE_ANY 
)
inline

Definition at line 370 of file lattice-weight.h.

References KALDI_WARN, LatticeWeightTpl< FloatType >::Value1(), LatticeWeightTpl< FloatType >::Value2(), and LatticeWeightTpl< FloatType >::Zero().

Referenced by CompactLatticeWeightTest(), Divide(), LatticeWeightTest(), LatticeDeterminizerPruned< Weight, IntType >::NormalizeSubset(), LatticeDeterminizer< Weight, IntType >::NormalizeSubset(), DeterminizerStar< F >::ProcessTransition(), PushCompactLatticeWeights(), RemoveEpsLocalClass< Arc, ReweightPlus >::RemoveEpsPattern1(), RemoveEpsLocalClass< Arc, ReweightPlus >::Reweight(), and TestRemoveEpsLocalSpecial().

372  {
373  typedef FloatType T;
374  T a = w1.Value1() - w2.Value1(), b = w1.Value2() - w2.Value2();
375  if (a != a || b != b || a == -std::numeric_limits<T>::infinity()
376  || b == -std::numeric_limits<T>::infinity()) {
377  KALDI_WARN << "LatticeWeightTpl::Divide, NaN or invalid number produced. "
378  << "[dividing by zero?] Returning zero";
379  return LatticeWeightTpl<T>::Zero();
380  }
381  if (a == std::numeric_limits<T>::infinity() ||
382  b == std::numeric_limits<T>::infinity())
383  return LatticeWeightTpl<T>::Zero(); // not a valid number if only one is infinite.
384  return LatticeWeightTpl<T>(a, b);
385 }
#define KALDI_WARN
Definition: kaldi-error.h:150

◆ Divide() [5/5]

CompactLatticeWeightTpl<WeightType, IntType> fst::Divide ( const CompactLatticeWeightTpl< WeightType, IntType > &  w1,
const CompactLatticeWeightTpl< WeightType, IntType > &  w2,
DivideType  div = DIVIDE_ANY 
)
inline

Definition at line 689 of file lattice-weight.h.

References Divide(), KALDI_ERR, CompactLatticeWeightTpl< WeightType, IntType >::String(), CompactLatticeWeightTpl< WeightType, IntType >::Weight(), and CompactLatticeWeightTpl< WeightType, IntType >::Zero().

691  {
692  if (w1.Weight() == WeightType::Zero()) {
693  if (w2.Weight() != WeightType::Zero()) {
694  return CompactLatticeWeightTpl<WeightType, IntType>::Zero();
695  } else {
696  KALDI_ERR << "Division by zero [0/0]";
697  }
698  } else if (w2.Weight() == WeightType::Zero()) {
699  KALDI_ERR << "Error: division by zero";
700  }
701  WeightType w = Divide(w1.Weight(), w2.Weight());
702 
703  const std::vector<IntType> v1 = w1.String(), v2 = w2.String();
704  if (v2.size() > v1.size()) {
705  KALDI_ERR << "Cannot divide, length mismatch";
706  }
707  typename std::vector<IntType>::const_iterator v1b = v1.begin(),
708  v1e = v1.end(), v2b = v2.begin(), v2e = v2.end();
709  if (div == DIVIDE_LEFT) {
710  if (!std::equal(v2b, v2e, v1b)) { // v2 must be identical to first part of v1.
711  KALDI_ERR << "Cannot divide, data mismatch";
712  }
713  return CompactLatticeWeightTpl<WeightType, IntType>(
714  w, std::vector<IntType>(v1b+(v2e-v2b), v1e)); // return last part of v1.
715  } else if (div == DIVIDE_RIGHT) {
716  if (!std::equal(v2b, v2e, v1e-(v2e-v2b))) { // v2 must be identical to last part of v1.
717  KALDI_ERR << "Cannot divide, data mismatch";
718  }
719  return CompactLatticeWeightTpl<WeightType, IntType>(
720  w, std::vector<IntType>(v1b, v1e-(v2e-v2b))); // return first part of v1.
721 
722  } else {
723  KALDI_ERR << "Cannot divide CompactLatticeWeightTpl with DIVIDE_ANY";
724  }
725  return CompactLatticeWeightTpl<WeightType,IntType>::Zero(); // keep compiler happy.
726 }
#define KALDI_ERR
Definition: kaldi-error.h:147
CompactLatticeWeightTpl< WeightType, IntType > Divide(const CompactLatticeWeightTpl< WeightType, IntType > &w1, const CompactLatticeWeightTpl< WeightType, IntType > &w2, DivideType div=DIVIDE_ANY)

◆ EnsureEpsilonProperty()

void EnsureEpsilonProperty ( VectorFst< Arc > *  fst)

This function modifies the fst (while maintaining equivalence) in such a way that, after the modification, all states of the FST which have epsilon-arcs entering them, have no non-epsilon arcs entering them, and all states which have epsilon-arcs leaving them, have no non-epsilon arcs leaving them.

It does this by creating extra states and adding extra epsilon transitions. An epsilon arc is defined as an arc where both the ilabel and the olabel are epsilons. This function may fail with KALDI_ASSERT for certain cyclic FSTs, but is safe in the acyclic case.

new_state_vec is for those states that have both epsilon and non-epsilon arcs entering. For these states, we'll create a new state for the non-epsilon arcs to enter and put it in this array, and we'll put an epsilon transition from the new state to the old state.

First modify arcs to point to states in new_state_vec when necessary.

Now handle the situation where states have both epsilon and non-epsilon arcs leaving.

Definition at line 49 of file epsilon-property-inl.h.

References ComputeStateInfo(), kStateHasEpsilonArcsEntering, kStateHasEpsilonArcsLeaving, kStateHasNonEpsilonArcsEntering, and kStateHasNonEpsilonArcsLeaving.

Referenced by main(), and TestEnsureEpsilonProperty().

49  {
50 
51  typedef typename Arc::StateId StateId;
52  typedef typename Arc::Weight Weight;
53  typedef VectorFst<Arc> Fst;
54  std::vector<char> epsilon_info;
55  ComputeStateInfo(*fst, &epsilon_info);
56 
57 
58  StateId num_states_old = fst->NumStates();
59  StateId non_coaccessible_state = fst->AddState();
60 
65  std::vector<StateId> new_state_vec(num_states_old, kNoStateId);
66  for (StateId s = 0; s < num_states_old; s++) {
67  if ((epsilon_info[s] & kStateHasEpsilonArcsEntering) != 0 &&
68  (epsilon_info[s] & kStateHasNonEpsilonArcsEntering) != 0) {
69  assert(s != fst->Start()); // a type of cyclic FST we can't handle
70  // easily.
71  StateId new_state = fst->AddState();
72  new_state_vec[s] = new_state;
73  fst->AddArc(new_state, Arc(0, 0, Weight::One(), s));
74  }
75  }
76 
79  for (StateId s = 0; s < num_states_old; s++) {
80  for (MutableArcIterator<Fst> aiter(fst, s);
81  !aiter.Done(); aiter.Next()) {
82  Arc arc = aiter.Value();
83  if (arc.ilabel != 0 || arc.olabel != 0) { // non-epsilon arc
84  StateId replacement_state;
85  if (arc.nextstate >= 0 && arc.nextstate < num_states_old &&
86  (replacement_state = new_state_vec[arc.nextstate]) !=
87  kNoStateId) {
88  arc.nextstate = replacement_state;
89  aiter.SetValue(arc);
90  }
91  }
92  }
93  }
94 
97  for (StateId s = 0; s < num_states_old; s++) {
98  if ((epsilon_info[s] & kStateHasEpsilonArcsLeaving) != 0 &&
99  (epsilon_info[s] & kStateHasNonEpsilonArcsLeaving) != 0) {
100  // state has non-epsilon and epsilon arcs leaving.
101  // create a new state and move the non-epsilon arcs to leave
102  // from there instead.
103  StateId new_state = fst->AddState();
104  for (MutableArcIterator<Fst> aiter(fst, s); !aiter.Done();
105  aiter.Next()) {
106  Arc arc = aiter.Value();
107  if (arc.ilabel != 0 || arc.olabel != 0) { // non-epsilon arc.
108  assert(arc.nextstate != s); // we don't handle cyclic FSTs.
109  // move this arc to leave from the new state:
110  fst->AddArc(new_state, arc);
111  arc.nextstate = non_coaccessible_state;
112  aiter.SetValue(arc); // invalidate the arc, Connect() will remove it.
113  }
114  }
115  // Create an epsilon arc to the new state.
116  fst->AddArc(s, Arc(0, 0, Weight::One(), new_state));
117  }
118  }
119  Connect(fst); // Removes arcs to the non-coaccessible state.
120 }
fst::StdArc::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
void ComputeStateInfo(const VectorFst< Arc > &fst, std::vector< char > *epsilon_info)
This function will set epsilon_info to have size equal to the NumStates() of the FST, containing a logical-or of the enum values kStateHasEpsilonArcsEntering, kStateHasNonEpsilonArcsEntering, kStateHasEpsilonArcsLeaving, and kStateHasNonEpsilonArcsLeaving.
fst::StdArc::Weight Weight

◆ EqualAlign()

bool EqualAlign ( const Fst< Arc > &  ifst,
typename Arc::StateId  length,
int  rand_seed,
MutableFst< Arc > *  ofst,
int  num_retries = 10 
)

EqualAlign is similar to RandGen, but it generates a sequence with exactly "length" input symbols.

It returns true on success, false on failure (failure is partly random but should never happen in practice for normal speech models.) It generates a random path through the input FST, finds out which subset of the states it visits along the way have self-loops with inupt symbols on them, and outputs a path with exactly enough self-loops to have the requested number of input symbols. Note that EqualAlign does not use the probabilities on the FST. It just uses equal probabilities in the first stage of selection (since the output will anyway not be a truly random sample from the FST). The input fst "ifst" must be connected or this may enter an infinite loop.

Definition at line 803 of file fstext-utils-inl.h.

References FindSelfLoopWithILabel(), rnnlm::i, rnnlm::j, KALDI_ASSERT, KALDI_WARN, and kaldi::RandInt().

Referenced by main(), MinimizeEncoded(), and TestEqualAlign().

807  {
808  srand(rand_seed);
809  KALDI_ASSERT(ofst->NumStates() == 0); // make sure ofst empty.
810  // make sure all states can reach final-state (or this algorithm may enter
811  // infinite loop.
812  KALDI_ASSERT(ifst.Properties(kCoAccessible, true) == kCoAccessible);
813 
814  typedef typename Arc::StateId StateId;
815  typedef typename Arc::Weight Weight;
816 
817  if (ifst.Start() == kNoStateId) {
818  KALDI_WARN << "Empty input fst.";
819  return false;
820  }
821  // First select path through ifst.
822  std::vector<StateId> path;
823  std::vector<size_t> arc_offsets; // arc taken out of each state.
824  std::vector<int> nof_ilabels;
825 
826  StateId num_ilabels = 0;
827  int retry_no = 0;
828 
829  // Under normal circumstances, this will be one-pass-only process
830  // Multiple tries might be needed in special cases, typically when
831  // the number of frames is close to number of transitions from
832  // the start node to the final node. It usually happens for really
833  // short utterances
834  do {
835  num_ilabels = 0;
836  arc_offsets.clear();
837  path.clear();
838  path.push_back(ifst.Start());
839 
840  while (1) {
841  // Select either an arc or final-prob.
842  StateId s = path.back();
843  size_t num_arcs = ifst.NumArcs(s);
844  size_t num_arcs_tot = num_arcs;
845  if (ifst.Final(s) != Weight::Zero()) num_arcs_tot++;
846  // kaldi::RandInt is a bit like Rand(), but gets around situations
847  // where RAND_MAX is very small.
848  // Change this to Rand() % num_arcs_tot if compile issues arise
849  size_t arc_offset = static_cast<size_t>(kaldi::RandInt(0, num_arcs_tot-1));
850 
851  if (arc_offset < num_arcs) { // an actual arc.
852  ArcIterator<Fst<Arc> > aiter(ifst, s);
853  aiter.Seek(arc_offset);
854  const Arc &arc = aiter.Value();
855  if (arc.nextstate == s) {
856  continue; // don't take this self-loop arc
857  } else {
858  arc_offsets.push_back(arc_offset);
859  path.push_back(arc.nextstate);
860  if (arc.ilabel != 0) num_ilabels++;
861  }
862  } else {
863  break; // Chose final-prob.
864  }
865  }
866 
867  nof_ilabels.push_back(num_ilabels);
868  } while (( ++retry_no < num_retries) && (num_ilabels > length));
869 
870  if (num_ilabels > length) {
871  std::stringstream ilabel_vec;
872  std::copy(nof_ilabels.begin(), nof_ilabels.end(),
873  std::ostream_iterator<int>(ilabel_vec, ","));
874  std::string s = ilabel_vec.str();
875  s.erase(s.end() - 1);
876  KALDI_WARN << "EqualAlign: the randomly constructed paths lengths: " << s;
877  KALDI_WARN << "EqualAlign: utterance has too few frames " << length
878  << " to align.";
879  return false; // can't make it shorter by adding self-loops!.
880  }
881 
882  StateId num_self_loops = 0;
883  std::vector<ssize_t> self_loop_offsets(path.size());
884  for (size_t i = 0; i < path.size(); i++)
885  if ( (self_loop_offsets[i] = FindSelfLoopWithILabel(ifst, path[i]))
886  != static_cast<ssize_t>(-1) )
887  num_self_loops++;
888 
889  if (num_self_loops == 0
890  && num_ilabels < length) {
891  KALDI_WARN << "No self-loops on chosen path; cannot match length.";
892  return false; // no self-loops to make it longer.
893  }
894 
895  StateId num_extra = length - num_ilabels; // Number of self-loops we need.
896 
897  StateId min_num_loops = 0;
898  if (num_extra != 0) min_num_loops = num_extra / num_self_loops; // prevent div by zero.
899  StateId num_with_one_more_loop = num_extra - (min_num_loops*num_self_loops);
900  KALDI_ASSERT(num_with_one_more_loop < num_self_loops || num_self_loops == 0);
901 
902  ofst->AddState();
903  ofst->SetStart(0);
904  StateId cur_state = 0;
905  StateId counter = 0; // tell us when we should stop adding one more loop.
906  for (size_t i = 0; i < path.size(); i++) {
907  // First, add any self-loops that are necessary.
908  StateId num_loops = 0;
909  if (self_loop_offsets[i] != static_cast<ssize_t>(-1)) {
910  num_loops = min_num_loops + (counter < num_with_one_more_loop ? 1 : 0);
911  counter++;
912  }
913  for (StateId j = 0; j < num_loops; j++) {
914  ArcIterator<Fst<Arc> > aiter(ifst, path[i]);
915  aiter.Seek(self_loop_offsets[i]);
916  Arc arc = aiter.Value();
917  KALDI_ASSERT(arc.nextstate == path[i]
918  && arc.ilabel != 0); // make sure self-loop with ilabel.
919  StateId next_state = ofst->AddState();
920  ofst->AddArc(cur_state, Arc(arc.ilabel, arc.olabel, arc.weight, next_state));
921  cur_state = next_state;
922  }
923  if (i+1 < path.size()) { // add forward transition.
924  ArcIterator<Fst<Arc> > aiter(ifst, path[i]);
925  aiter.Seek(arc_offsets[i]);
926  Arc arc = aiter.Value();
927  KALDI_ASSERT(arc.nextstate == path[i+1]);
928  StateId next_state = ofst->AddState();
929  ofst->AddArc(cur_state, Arc(arc.ilabel, arc.olabel, arc.weight, next_state));
930  cur_state = next_state;
931  } else { // add final-prob.
932  Weight weight = ifst.Final(path[i]);
933  KALDI_ASSERT(weight != Weight::Zero());
934  ofst->SetFinal(cur_state, weight);
935  }
936  }
937  return true;
938 }
fst::StdArc::StateId StateId
ssize_t FindSelfLoopWithILabel(const Fst< Arc > &fst, typename Arc::StateId s)
#define KALDI_WARN
Definition: kaldi-error.h:150
fst::StdArc::Weight Weight
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
Definition: kaldi-math.cc:95

◆ ExpandInputSequences()

void ExpandInputSequences ( const std::vector< std::vector< I > > &  sequences,
MutableFst< Arc > *  fst 
)

ExpandInputSequences expands out the input symbols into sequences of input symbols.

It creates linear chains of states for each arc that had >1 augmented symbol on it. It also sets the input symbol table to NULL, since in case you did have a symbol table there it would no longer be valid. It leaves any weight and output symbols on the first arc of the chain.

Definition at line 163 of file factor-inl.h.

References KALDI_ASSERT_IS_INTEGER_TYPE, and rnnlm::n.

Referenced by TestFactor().

164  {
166  typedef typename Arc::StateId StateId;
167  typedef typename Arc::Label Label;
168  typedef typename Arc::Weight Weight;
169  fst->SetInputSymbols(NULL);
170  size_t size = sequences.size();
171  if (sequences.size() > 0) assert(sequences[0].size() == 0); // should be eps.
172  StateId num_states_at_start = fst->NumStates();
173  for (StateId s = 0; s < num_states_at_start; s++) {
174  StateId num_arcs = fst->NumArcs(s);
175  for (StateId aidx = 0; aidx < num_arcs; aidx++) {
176  ArcIterator<MutableFst<Arc> > aiter(*fst, s);
177  aiter.Seek(aidx);
178  Arc arc = aiter.Value();
179 
180  Label ilabel = arc.ilabel;
181  Label dest_state = arc.nextstate;
182  if (ilabel != 0) { // non-eps [nothing to do if eps]...
183  assert(ilabel < static_cast<Label>(size));
184  size_t len = sequences[ilabel].size();
185  if (len <= 1) {
186  if (len == 0) arc.ilabel = 0;
187  else arc.ilabel = sequences[ilabel][0];
188  MutableArcIterator<MutableFst<Arc> > mut_aiter(fst, s);
189  mut_aiter.Seek(aidx);
190  mut_aiter.SetValue(arc);
191  } else { // len>=2. Must create new states...
192  StateId curstate = -1; // keep compiler happy: this value never used.
193  for (size_t n = 0; n < len; n++) { // adding/modifying "len" arcs.
194  StateId nextstate;
195  if (n < len-1) {
196  nextstate = fst->AddState();
197  assert(nextstate >= num_states_at_start);
198  } else nextstate = dest_state; // going back to original arc's
199  // destination.
200  if (n == 0) {
201  arc.ilabel = sequences[ilabel][0];
202  arc.nextstate = nextstate;
203  MutableArcIterator<MutableFst<Arc> > mut_aiter(fst, s);
204  mut_aiter.Seek(aidx);
205  mut_aiter.SetValue(arc);
206  } else {
207  arc.ilabel = sequences[ilabel][n];
208  arc.olabel = 0;
209  arc.weight = Weight::One();
210  arc.nextstate = nextstate;
211  fst->AddArc(curstate, arc);
212  }
213  curstate = nextstate;
214  }
215  }
216  }
217  }
218  }
219 }
fst::StdArc::StateId StateId
#define KALDI_ASSERT_IS_INTEGER_TYPE(I)
Definition: kaldi-utils.h:133
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
struct rnnlm::@11::@12 n
fst::StdArc::Label Label
fst::StdArc::Weight Weight

◆ Factor() [1/2]

void Factor ( const Fst< Arc > &  fst,
MutableFst< Arc > *  ofst,
std::vector< std::vector< I > > *  symbols 
)

Factor identifies linear chains of states with an olabel (if any) only on the first arc of the chain, and possibly a sequence of ilabels; it outputs an FST with different symbols on the input that represent sequences of the original input symbols; it outputs the mapping from the new symbol to sequences of original symbols, as "symbols" [zero is reserved for epsilon].

As a side effect it also sorts the FST in depth-first order. Factor will usually do the best job when the olabels have been pushed to the left, i.e. if you make a call like

Push<Arc, REWEIGHT_TO_INITIAL>(fsta, &fstb, kPushLabels);

This is because it only creates a chain with olabels on the first arc of the chain (or a chain with no olabels). [it's possible to construct cases where pushing makes things worse, though]. After Factor, the composition of *ofst with the result of calling CreateFactorFst(*symbols) should be equivalent to fst. Alternatively, calling ExpandInputSequences with ofst and *symbols would produce something equivalent to fst.

Definition at line 69 of file factor-inl.h.

References GetStateProperties(), rnnlm::i, KALDI_ASSERT_IS_INTEGER_TYPE, kStateArcsIn, kStateArcsOut, kStateIlabelsOut, and Times().

Referenced by ConvertLattice(), Factor(), and TestFactor().

70  {
72  typedef typename Arc::StateId StateId;
73  typedef typename Arc::Label Label;
74  typedef typename Arc::Weight Weight;
75  assert(symbols_out != NULL);
76  ofst->DeleteStates();
77  if (fst.Start() < 0) return; // empty FST.
78  std::vector<StateId> order;
79  DfsOrderVisitor<Arc> dfs_order_visitor(&order);
80  DfsVisit(fst, &dfs_order_visitor);
81  assert(order.size() > 0);
82  StateId max_state = *(std::max_element(order.begin(), order.end()));
83  std::vector<StatePropertiesType> state_properties;
84  GetStateProperties(fst, max_state, &state_properties);
85 
86  std::vector<bool> remove(max_state+1); // if true, will remove this state.
87 
88  // Now identify states that will be removed (made the middle of a chain).
89  // The basic rule is that if the FstStateProperties equals
90  // (kStateArcsIn|kStateArcsOut) or (kStateArcsIn|kStateArcsOut|kStateIlabelsOut),
91  // then it is in the middle of a chain. This eliminates state with
92  // multiple input or output arcs, final states, and states with arcs out
93  // that have olabels [we assume these are pushed to the left, so occur on the
94  // 1st arc of a chain.
95 
96  for (StateId i = 0; i <= max_state; i++)
97  remove[i] = (state_properties[i] == (kStateArcsIn|kStateArcsOut)
98  || state_properties[i] == (kStateArcsIn|kStateArcsOut|kStateIlabelsOut));
99  std::vector<StateId> state_mapping(max_state+1, kNoStateId);
100 
101  typedef unordered_map<std::vector<I>, Label, kaldi::VectorHasher<I> > SymbolMapType;
102  SymbolMapType symbol_mapping;
103  Label symbol_counter = 0;
104  {
105  std::vector<I> eps;
106  symbol_mapping[eps] = symbol_counter++;
107  }
108  std::vector<I> this_sym; // a temporary used inside the loop.
109  for (size_t i = 0; i < order.size(); i++) {
110  StateId state = order[i];
111  if (!remove[state]) { // Process this state...
112  StateId &new_state = state_mapping[state];
113  if (new_state == kNoStateId) new_state = ofst->AddState();
114  for (ArcIterator<Fst<Arc> > aiter(fst, state); !aiter.Done(); aiter.Next()) {
115  Arc arc = aiter.Value();
116  if (arc.ilabel == 0) this_sym.clear();
117  else {
118  this_sym.resize(1);
119  this_sym[0] = arc.ilabel;
120  }
121  while (remove[arc.nextstate]) {
122  ArcIterator<Fst<Arc> > aiter2(fst, arc.nextstate);
123  assert(!aiter2.Done());
124  const Arc &nextarc = aiter2.Value();
125  arc.weight = Times(arc.weight, nextarc.weight);
126  assert(nextarc.olabel == 0);
127  if (nextarc.ilabel != 0) this_sym.push_back(nextarc.ilabel);
128  assert(static_cast<Label>(static_cast<I>(nextarc.ilabel))
129  == nextarc.ilabel); // check within integer range.
130  arc.nextstate = nextarc.nextstate;
131  }
132  StateId &new_nextstate = state_mapping[arc.nextstate];
133  if (new_nextstate == kNoStateId) new_nextstate = ofst->AddState();
134  arc.nextstate = new_nextstate;
135  if (symbol_mapping.count(this_sym) != 0) arc.ilabel = symbol_mapping[this_sym];
136  else arc.ilabel = symbol_mapping[this_sym] = symbol_counter++;
137  ofst->AddArc(new_state, arc);
138  }
139  if (fst.Final(state) != Weight::Zero())
140  ofst->SetFinal(new_state, fst.Final(state));
141  }
142  }
143  ofst->SetStart(state_mapping[fst.Start()]);
144 
145  // Now output the symbol sequences.
146  symbols_out->resize(symbol_counter);
147  for (typename SymbolMapType::const_iterator iter = symbol_mapping.begin();
148  iter != symbol_mapping.end(); ++iter) {
149  (*symbols_out)[iter->second] = iter->first;
150  }
151 }
fst::StdArc::StateId StateId
A hashing function-object for vectors.
Definition: stl-utils.h:216
#define KALDI_ASSERT_IS_INTEGER_TYPE(I)
Definition: kaldi-utils.h:133
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
void GetStateProperties(const Fst< Arc > &fst, typename Arc::StateId max_state, std::vector< StatePropertiesType > *props)
This function works out various properties of the states in the FST, using the bit properties defined...
Definition: factor-inl.h:37
LatticeWeightTpl< FloatType > Times(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2)
fst::StdArc::Label Label
fst::StdArc::Weight Weight

◆ Factor() [2/2]

void Factor ( const Fst< Arc > &  fst,
MutableFst< Arc > *  ofst1,
MutableFst< Arc > *  ofst2 
)

This is a more conventional interface of Factor that outputs the result as two FSTs.

Definition at line 154 of file factor-inl.h.

References CreateFactorFst(), and Factor().

155  {
156  typedef typename Arc::Label Label;
157  std::vector<std::vector<Label> > symbols;
158  Factor(fst, ofst2, &symbols);
159  CreateFactorFst(symbols, ofst1);
160 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
void CreateFactorFst(const std::vector< std::vector< I > > &sequences, MutableFst< Arc > *fst)
The function CreateFactorFst will create an FST that expands out the "factors" that are the indices o...
Definition: factor-inl.h:250
fst::StdArc::Label Label
void Factor(const Fst< Arc > &fst, MutableFst< Arc > *ofst1, MutableFst< Arc > *ofst2)
This is a more conventional interface of Factor that outputs the result as two FSTs.
Definition: factor-inl.h:154

◆ FileExists()

bool fst::FileExists ( std::string  strFilename)

Definition at line 31 of file deterministic-fst-test.cc.

31  {
32  struct stat stFileInfo;
33  bool blnReturn;
34  int intStat;
35 
36  // Attempt to get the file attributes
37  intStat = stat(strFilename.c_str(), &stFileInfo);
38  if (intStat == 0) {
39  // We were able to get the file attributes
40  // so the file obviously exists.
41  blnReturn = true;
42  } else {
43  // We were not able to get the file attributes.
44  // This may mean that we don't have permission to
45  // access the folder which contains this file. If you
46  // need to do that level of checking, lookup the
47  // return values of stat which will give you
48  // more details on why stat failed.
49  blnReturn = false;
50  }
51 
52  return blnReturn;
53 }

◆ FindSelfLoopWithILabel()

ssize_t fst::FindSelfLoopWithILabel ( const Fst< Arc > &  fst,
typename Arc::StateId  s 
)

Definition at line 794 of file fstext-utils-inl.h.

Referenced by EqualAlign().

794  {
795  for (ArcIterator<Fst<Arc> > aiter(fst, s); !aiter.Done(); aiter.Next())
796  if (aiter.Value().nextstate == s
797  && aiter.Value().ilabel != 0) return static_cast<ssize_t>(aiter.Position());
798  return static_cast<ssize_t>(-1);
799 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21

◆ FollowingInputSymbolsAreSame()

bool FollowingInputSymbolsAreSame ( bool  end_is_epsilon,
const Fst< Arc > &  fst 
)

Returns true if and only if the FST is such that the input symbols on arcs exiting any given state all have the same value.

If end_is_epsilon, treat end-state as an epsilon output arc [i.e. ensure end-states cannot have non-epsilon output transitions.]

Definition at line 497 of file fstext-utils-inl.h.

References FollowingInputSymbolsAreSameClass().

Referenced by MinimizeEncoded(), and TestMakeSymbolsSame().

497  {
498  IdentityFunction<typename Arc::Label> f;
499  return FollowingInputSymbolsAreSameClass(end_is_epsilon, fst, f);
500 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
bool FollowingInputSymbolsAreSameClass(bool end_is_epsilon, const Fst< Arc > &fst, const F &f)

◆ FollowingInputSymbolsAreSameClass()

bool FollowingInputSymbolsAreSameClass ( bool  end_is_epsilon,
const Fst< Arc > &  fst,
const F &  f 
)

Definition at line 504 of file fstext-utils-inl.h.

Referenced by FollowingInputSymbolsAreSame(), MinimizeEncoded(), and TestMakeSymbolsSameClass().

504  {
505  typedef typename Arc::StateId StateId;
506  typedef typename Arc::Weight Weight;
507  typedef typename F::Result ClassType;
508  const ClassType noClass = f(kNoLabel), epsClass = f(0);
509  for (StateIterator<Fst<Arc> > siter(fst); !siter.Done(); siter.Next()) {
510  StateId s = siter.Value();
511  ClassType c = noClass;
512  for (ArcIterator<Fst<Arc> > aiter(fst, s); !aiter.Done(); aiter.Next()) {
513  const Arc &arc = aiter.Value();
514  if (c == noClass)
515  c = f(arc.ilabel);
516  else
517  if (c != f(arc.ilabel))
518  return false;
519  }
520  if (end_is_epsilon && c != noClass &&
521  c != epsClass && fst.Final(s) != Weight::Zero())
522  return false;
523  }
524  return true;
525 }
fst::StdArc::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
fst::StdArc::Weight Weight

◆ GenAcceptorFromSequence()

static VectorFst<Arc>* fst::GenAcceptorFromSequence ( const vector< typename Arc::Label > &  symbols,
float  cost 
)
static

Definition at line 34 of file context-fst-test.cc.

References rnnlm::i, and kaldi::Rand().

34  {
35  typedef typename Arc::Weight Weight;
36  typedef typename Arc::StateId StateId;
37 
38  vector<float> split_cost(symbols.size()+1, 0.0); // for #-arcs + end-state.
39  { // compute split_cost. it must sum to "cost".
40  std::set<int32> indices;
41  size_t num_indices = 1 + (kaldi::Rand() % split_cost.size());
42  while (indices.size() < num_indices) indices.insert(kaldi::Rand() % split_cost.size());
43  for (std::set<int32>::iterator iter = indices.begin(); iter != indices.end(); ++iter) {
44  split_cost[*iter] = cost / num_indices;
45  }
46  }
47 
48  VectorFst<Arc> *fst = new VectorFst<Arc>();
49  StateId cur_state = fst->AddState();
50  fst->SetStart(cur_state);
51  for (size_t i = 0; i < symbols.size(); i++) {
52  StateId next_state = fst->AddState();
53  Arc arc;
54  arc.ilabel = symbols[i];
55  arc.olabel = symbols[i];
56  arc.nextstate = next_state;
57  arc.weight = (Weight) split_cost[i];
58  fst->AddArc(cur_state, arc);
59  cur_state = next_state;
60 
61  }
62  fst->SetFinal(cur_state, (Weight)split_cost[symbols.size()]);
63  return fst;
64 }
fst::StdArc::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
fst::StdArc::Weight Weight

◆ GenRandPhoneSeq()

static VectorFst<Arc>* fst::GenRandPhoneSeq ( vector< typename Arc::Label > &  phone_syms,
vector< typename Arc::Label > &  disambig_syms,
typename Arc::Label  subsequential_symbol,
int  num_subseq_syms,
float  seq_prob,
vector< typename Arc::Label > *  phoneseq_out 
)
static

Definition at line 124 of file context-fst-test.cc.

References rnnlm::i, KALDI_ASSERT, kaldi::Rand(), and kaldi::RandUniform().

129  {
130  KALDI_ASSERT(phoneseq_out != NULL);
131  typedef typename Arc::Label Label;
132  // Generate an FST that is a random phone sequence, ending
133  // with "num_subseq_syms" subsequential symbols. It will
134  // have disambiguation symbols randomly interspersed throughout.
135  // The number of phones is random (possibly zero).
136  size_t len = (kaldi::Rand() % 4) * (kaldi::Rand() % 3); // up to 3*2=6 phones.
137  float disambig_prob = 0.33;
138  phoneseq_out->clear();
139  vector<Label> syms; // the phones
140  for (size_t i = 0; i < len; i++) {
141  while (kaldi::RandUniform() < disambig_prob) syms.push_back(disambig_syms[kaldi::Rand() % disambig_syms.size()]);
142  Label phone_id = phone_syms[kaldi::Rand() % phone_syms.size()];
143  phoneseq_out->push_back(phone_id); // record in output the underlying phone sequence.
144  syms.push_back(phone_id);
145  }
146  for (size_t i = 0; static_cast<int32>(i) < num_subseq_syms; i++) {
147  while (kaldi::RandUniform() < disambig_prob) syms.push_back(disambig_syms[kaldi::Rand() % disambig_syms.size()]);
148  syms.push_back(subsequential_symbol);
149  }
150  while (kaldi::RandUniform() < disambig_prob) syms.push_back(disambig_syms[kaldi::Rand() % disambig_syms.size()]);
151 
152  // OK, now have the symbols of the FST as a vector.
153  return GenAcceptorFromSequence<Arc>(syms, seq_prob);
154 }
float RandUniform(struct RandomState *state=NULL)
Returns a random number strictly between 0 and 1.
Definition: kaldi-math.h:151
kaldi::int32 int32
fst::StdArc::Label Label
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ GetEncodingMultiple()

int32 fst::GetEncodingMultiple ( int32  nonterm_phones_offset)
inline

◆ GetInputSymbols()

void GetInputSymbols ( const Fst< Arc > &  fst,
bool  include_eps,
std::vector< I > *  symbols 
)

GetInputSymbols gets the list of symbols on the input of fst (including epsilon, if include_eps == true), as a sorted, unique list.

Definition at line 97 of file fstext-utils-inl.h.

References kaldi::CopySetToVector(), KALDI_ASSERT, and KALDI_ASSERT_IS_INTEGER_TYPE.

Referenced by ComposeContext(), ComposeContextLeftBiphone(), kaldi::CreateEditDistance(), kaldi::GetRandomAlignmentForPhone(), and TestMakeLinearAcceptor().

99  {
101  unordered_set<I> all_syms;
102  for (StateIterator<Fst<Arc> > siter(fst); !siter.Done(); siter.Next()) {
103  typename Arc::StateId s = siter.Value();
104  for (ArcIterator<Fst<Arc> > aiter(fst, s); !aiter.Done(); aiter.Next()) {
105  const Arc &arc = aiter.Value();
106  all_syms.insert(arc.ilabel);
107  }
108  }
109  // Remove epsilon, if instructed.
110  if (!include_eps && all_syms.count(0) != 0)
111  all_syms.erase(0);
112  KALDI_ASSERT(symbols != NULL);
113  kaldi::CopySetToVector(all_syms, symbols);
114  std::sort(symbols->begin(), symbols->end());
115 }
fst::StdArc::StateId StateId
void CopySetToVector(const std::set< T > &s, std::vector< T > *v)
Copies the elements of a set to a vector.
Definition: stl-utils.h:86
#define KALDI_ASSERT_IS_INTEGER_TYPE(I)
Definition: kaldi-utils.h:133
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ GetLinearSymbolSequence()

bool GetLinearSymbolSequence ( const Fst< Arc > &  fst,
std::vector< I > *  isymbols_out,
std::vector< I > *  osymbols_out,
typename Arc::Weight *  tot_weight_out 
)

GetLinearSymbolSequence gets the symbol sequence from a linear FST.

If the FST is not just a linear sequence, it returns false. If it is a linear sequence (including the empty FST), it returns true. In this case it outputs the symbol sequences as "isymbols_out" and "osymbols_out" (removing epsilons), and the total weight as "tot_weight". The total weight will be Weight::Zero() if the FST is empty. If any of the output pointers are NULL, it does not create that output.

Definition at line 178 of file fstext-utils-inl.h.

References Times().

Referenced by kaldi::AlignUtteranceWrapper(), CheckPhones(), kaldi::DecodeUtterance(), DecodeUtterance(), kaldi::DecodeUtteranceLatticeFaster(), kaldi::DecodeUtteranceLatticeIncremental(), kaldi::DecodeUtteranceLatticeSimple(), OnlineFasterDecoder::EndOfUtterance(), kaldi::GetDiagnosticsAndPrintOutput(), kaldi::LatticeToString(), main(), kaldi::MaybeDoSanityCheck(), MinimizeEncoded(), MinimumBayesRisk::MinimumBayesRisk(), NnetBatchDecoder::ProcessOutputUtterance(), TestEqualAlign(), TestMakeLinearAcceptor(), and DecodeUtteranceLatticeFasterClass::~DecodeUtteranceLatticeFasterClass().

181  {
182  typedef typename Arc::StateId StateId;
183  typedef typename Arc::Weight Weight;
184 
185  Weight tot_weight = Weight::One();
186  std::vector<I> ilabel_seq;
187  std::vector<I> olabel_seq;
188 
189  StateId cur_state = fst.Start();
190  if (cur_state == kNoStateId) { // empty sequence.
191  if (isymbols_out != NULL) isymbols_out->clear();
192  if (osymbols_out != NULL) osymbols_out->clear();
193  if (tot_weight_out != NULL) *tot_weight_out = Weight::Zero();
194  return true;
195  }
196  while (1) {
197  Weight w = fst.Final(cur_state);
198  if (w != Weight::Zero()) { // is final..
199  tot_weight = Times(w, tot_weight);
200  if (fst.NumArcs(cur_state) != 0) return false;
201  if (isymbols_out != NULL) *isymbols_out = ilabel_seq;
202  if (osymbols_out != NULL) *osymbols_out = olabel_seq;
203  if (tot_weight_out != NULL) *tot_weight_out = tot_weight;
204  return true;
205  } else {
206  if (fst.NumArcs(cur_state) != 1) return false;
207 
208  ArcIterator<Fst<Arc> > iter(fst, cur_state); // get the only arc.
209  const Arc &arc = iter.Value();
210  tot_weight = Times(arc.weight, tot_weight);
211  if (arc.ilabel != 0) ilabel_seq.push_back(arc.ilabel);
212  if (arc.olabel != 0) olabel_seq.push_back(arc.olabel);
213  cur_state = arc.nextstate;
214  }
215  }
216 }
fst::StdArc::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
LatticeWeightTpl< FloatType > Times(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2)
fst::StdArc::Weight Weight

◆ GetOutputSymbols()

void GetOutputSymbols ( const Fst< Arc > &  fst,
bool  include_eps,
std::vector< I > *  symbols 
)

GetOutputSymbols gets the list of symbols on the output of fst (including epsilon, if include_eps == true)

Definition at line 76 of file fstext-utils-inl.h.

References kaldi::CopySetToVector(), KALDI_ASSERT, and KALDI_ASSERT_IS_INTEGER_TYPE.

Referenced by kaldi::CreateEditDistance().

78  {
80  std::set<I> all_syms;
81  for (StateIterator<Fst<Arc> > siter(fst); !siter.Done(); siter.Next()) {
82  typename Arc::StateId s = siter.Value();
83  for (ArcIterator<Fst<Arc> > aiter(fst, s); !aiter.Done(); aiter.Next()) {
84  const Arc &arc = aiter.Value();
85  all_syms.insert(arc.olabel);
86  }
87  }
88 
89  // Remove epsilon, if instructed.
90  if (!include_eps && !all_syms.empty() && *all_syms.begin() == 0)
91  all_syms.erase(0);
92  KALDI_ASSERT(symbols != NULL);
93  kaldi::CopySetToVector(all_syms, symbols);
94 }
fst::StdArc::StateId StateId
void CopySetToVector(const std::set< T > &s, std::vector< T > *v)
Copies the elements of a set to a vector.
Definition: stl-utils.h:86
#define KALDI_ASSERT_IS_INTEGER_TYPE(I)
Definition: kaldi-utils.h:133
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ GetStateProperties()

void GetStateProperties ( const Fst< Arc > &  fst,
typename Arc::StateId  max_state,
std::vector< StatePropertiesType > *  props 
)

This function works out various properties of the states in the FST, using the bit properties defined in StatePropertiesEnum.

Definition at line 37 of file factor-inl.h.

References kStateArcsIn, kStateArcsOut, kStateFinal, kStateIlabelsOut, kStateInitial, kStateMultipleArcsIn, kStateMultipleArcsOut, and kStateOlabelsOut.

Referenced by Factor().

39  {
40  typedef typename Arc::StateId StateId;
41  typedef typename Arc::Weight Weight;
42  assert(props != NULL);
43  props->clear();
44  if (fst.Start() < 0) return; // Empty fst.
45  props->resize(max_state+1, 0);
46  assert(fst.Start() <= max_state);
47  (*props)[fst.Start()] |= kStateInitial;
48  for (StateId s = 0; s <= max_state; s++) {
49  StatePropertiesType &s_info = (*props)[s];
50  for (ArcIterator<Fst<Arc> > aiter(fst, s); !aiter.Done(); aiter.Next()) {
51  const Arc &arc = aiter.Value();
52  if (arc.ilabel != 0) s_info |= kStateIlabelsOut;
53  if (arc.olabel != 0) s_info |= kStateOlabelsOut;
54  StateId nexts = arc.nextstate;
55  assert(nexts <= max_state); // or input was invalid.
56  StatePropertiesType &nexts_info = (*props)[nexts];
57  if (s_info&kStateArcsOut) s_info |= kStateMultipleArcsOut;
58  s_info |= kStateArcsOut;
59  if (nexts_info&kStateArcsIn) nexts_info |= kStateMultipleArcsIn;
60  nexts_info |= kStateArcsIn;
61  }
62  if (fst.Final(s) != Weight::Zero()) s_info |= kStateFinal;
63  }
64 }
fst::StdArc::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
unsigned char StatePropertiesType
Definition: factor.h:122
fst::StdArc::Weight Weight

◆ GetSymbols()

void GetSymbols ( const SymbolTable &  symtab,
bool  include_eps,
std::vector< I > *  syms_out 
)

Definition at line 329 of file fstext-utils-inl.h.

References KALDI_ASSERT.

Referenced by main().

331  {
332  KALDI_ASSERT(syms_out != NULL);
333  syms_out->clear();
334  for (SymbolTableIterator iter(symtab);
335  !iter.Done();
336  iter.Next()) {
337  if (include_eps || iter.Value() != 0) {
338  syms_out->push_back(iter.Value());
339  KALDI_ASSERT(syms_out->back() == iter.Value()); // an integer-range thing.
340  }
341  }
342 }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ GraphLatticeScale()

std::vector<std::vector<double> > fst::GraphLatticeScale ( double  lmwt)
inline

Definition at line 147 of file lattice-utils.h.

Referenced by main().

147  {
148  std::vector<std::vector<double> > ans(2);
149  ans[0].resize(2, 0.0);
150  ans[1].resize(2, 0.0);
151  ans[0][0] = lmwt;
152  ans[1][1] = 1.0;
153  return ans;
154 }

◆ HighestNumberedInputSymbol()

Arc::Label HighestNumberedInputSymbol ( const Fst< Arc > &  fst)

Returns the highest numbered input symbol id of the FST (or zero for an empty FST.

Definition at line 54 of file fstext-utils-inl.h.

Referenced by DeterminizeLatticeInsertPhones(), main(), SafeDeterminizeMinimizeWrapper(), SafeDeterminizeWrapper(), TestDeterminizeStarInLog(), and TestPreDeterminize().

54  {
55  typename Arc::Label ans = 0;
56  for (StateIterator<Fst<Arc> > siter(fst); !siter.Done(); siter.Next()) {
57  typename Arc::StateId s = siter.Value();
58  for (ArcIterator<Fst<Arc> > aiter(fst, s); !aiter.Done(); aiter.Next()) {
59  const Arc &arc = aiter.Value();
60  ans = std::max(ans, arc.ilabel);
61  }
62  }
63  return ans;
64 }
fst::StdArc::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
fst::StdArc::Label Label

◆ HighestNumberedOutputSymbol()

Arc::Label HighestNumberedOutputSymbol ( const Fst< Arc > &  fst)

Returns the highest numbered output symbol id of the FST (or zero for an empty FST.

Definition at line 41 of file fstext-utils-inl.h.

Referenced by LatticeWordAligner::LatticeWordAligner().

41  {
42  typename Arc::Label ans = 0;
43  for (StateIterator<Fst<Arc> > siter(fst); !siter.Done(); siter.Next()) {
44  typename Arc::StateId s = siter.Value();
45  for (ArcIterator<Fst<Arc> > aiter(fst, s); !aiter.Done(); aiter.Next()) {
46  const Arc &arc = aiter.Value();
47  ans = std::max(ans, arc.olabel);
48  }
49  }
50  return ans;
51 }
fst::StdArc::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
fst::StdArc::Label Label

◆ InputDeterminizeSingleState() [1/2]

static void fst::InputDeterminizeSingleState ( StdArc::StateId  s,
VectorFst< StdArc > *  fst 
)
static

Definition at line 31 of file fstdeterminizestart.cc.

References rnnlm::i, and kaldi::LogAdd().

32  {
33  bool was_input_deterministic = true;
34  typedef StdArc Arc;
35  typedef Arc::StateId StateId;
36  typedef Arc::Label Label;
37  typedef Arc::Weight Weight;
38 
39  struct InfoForIlabel {
40  std::vector<size_t> arc_indexes; // indexes of all arcs with this ilabel
41  float tot_cost; // total cost of all arcs leaving state s for this
42  // ilabel, summed as if they were negative log-probs.
43  StateId new_state; // state-id of new state, if any, that we have created
44  // to remove duplicate symbols with this ilabel.
45  InfoForIlabel(): new_state(-1) { }
46  };
47 
48  std::unordered_map<Label, InfoForIlabel> label_map;
49 
50  size_t arc_index = 0;
51  for (ArcIterator<VectorFst<Arc> > aiter(*fst, s);
52  !aiter.Done(); aiter.Next(), ++arc_index) {
53  const Arc &arc = aiter.Value();
54  InfoForIlabel &info = label_map[arc.ilabel];
55  if (info.arc_indexes.empty()) {
56  info.tot_cost = arc.weight.Value();
57  } else {
58  info.tot_cost = -kaldi::LogAdd(-info.tot_cost, -arc.weight.Value());
59  was_input_deterministic = false;
60  }
61  info.arc_indexes.push_back(arc_index);
62  }
63 
64  if (was_input_deterministic)
65  return; // Nothing to do.
66 
67  // 'new_arcs' will contain the modified list of arcs
68  // leaving state s
69  std::vector<Arc> new_arcs;
70  new_arcs.reserve(arc_index);
71  arc_index = 0;
72  for (ArcIterator<VectorFst<Arc> > aiter(*fst, s);
73  !aiter.Done(); aiter.Next(), ++arc_index) {
74  const Arc &arc = aiter.Value();
75  Label ilabel = arc.ilabel;
76  InfoForIlabel &info = label_map[ilabel];
77  if (info.arc_indexes.size() == 1) {
78  new_arcs.push_back(arc); // no changes needed
79  } else {
80  if (info.new_state < 0) {
81  info.new_state = fst->AddState();
82  // add arc from state 's' to newly created state.
83  new_arcs.push_back(Arc(ilabel, 0, Weight(info.tot_cost),
84  info.new_state));
85  }
86  // add arc from new state to original destination of this arc.
87  fst->AddArc(info.new_state, Arc(0, arc.olabel,
88  Weight(arc.weight.Value() - info.tot_cost),
89  arc.nextstate));
90  }
91  }
92  fst->DeleteArcs(s);
93  for (size_t i = 0; i < new_arcs.size(); i++)
94  fst->AddArc(s, new_arcs[i]);
95 }
fst::StdArc::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
fst::StdArc StdArc
fst::StdArc::Label Label
fst::StdArc::Weight Weight
double LogAdd(double x, double y)
Definition: kaldi-math.h:184

◆ InputDeterminizeSingleState() [2/2]

static void fst::InputDeterminizeSingleState ( StdArc::StateId  s,
VectorFst< StdArc > *  fst 
)
static

This utility function input-determinizes a specified state s of the FST 'fst'.

(This input-determinizes while treating epsilon as a real symbol, although for the application we expect to use it, there won't be epsilons).

What this function does is: for any symbol i that appears as the ilabel of more than one arc leaving state s of FST 'fst', it creates an additional state, it creates a new state t with epsilon-input transitions leaving it for each of those multiple arcs leaving state s; it deletes the original arcs leaving state s; and it creates a single arc leaving state s to the newly created state with the ilabel i on it. It sets the weights as necessary to preserve equivalence and also to ensure that if, prior to this modification, the FST was stochastic when cast to the log semiring (see IsStochasticInLog()), it still will be. I.e. when interpreted as negative logprobs, the weight from state s to t would be the sum of the weights on the original arcs leaving state s.

This is used as a very cheap solution when preparing FSTs for the grammar decoder, to ensure that there is only one entry-state to the sub-FST for each phonetic left-context; this keeps the grammar-FST code (i.e. the code that stitches them together) simple. Of course it will tend to introduce unnecessary epsilons, and if we were careful we might be able to remove some of those, but this wouldn't have a substantial impact on overall decoder performance so we don't bother.

Definition at line 472 of file grammar-fst.cc.

References rnnlm::i, and kaldi::LogAdd().

Referenced by main(), and GrammarFstPreparer::Prepare().

473  {
474  bool was_input_deterministic = true;
475  typedef StdArc Arc;
476  typedef Arc::StateId StateId;
477  typedef Arc::Label Label;
478  typedef Arc::Weight Weight;
479 
480  struct InfoForIlabel {
481  std::vector<size_t> arc_indexes; // indexes of all arcs with this ilabel
482  float tot_cost; // total cost of all arcs leaving state s for this
483  // ilabel, summed as if they were negative log-probs.
484  StateId new_state; // state-id of new state, if any, that we have created
485  // to remove duplicate symbols with this ilabel.
486  InfoForIlabel(): new_state(-1) { }
487  };
488 
489  std::unordered_map<Label, InfoForIlabel> label_map;
490 
491  size_t arc_index = 0;
492  for (ArcIterator<VectorFst<Arc> > aiter(*fst, s);
493  !aiter.Done(); aiter.Next(), ++arc_index) {
494  const Arc &arc = aiter.Value();
495  InfoForIlabel &info = label_map[arc.ilabel];
496  if (info.arc_indexes.empty()) {
497  info.tot_cost = arc.weight.Value();
498  } else {
499  info.tot_cost = -kaldi::LogAdd(-info.tot_cost, -arc.weight.Value());
500  was_input_deterministic = false;
501  }
502  info.arc_indexes.push_back(arc_index);
503  }
504 
505  if (was_input_deterministic)
506  return; // Nothing to do.
507 
508  // 'new_arcs' will contain the modified list of arcs
509  // leaving state s
510  std::vector<Arc> new_arcs;
511  new_arcs.reserve(arc_index);
512  arc_index = 0;
513  for (ArcIterator<VectorFst<Arc> > aiter(*fst, s);
514  !aiter.Done(); aiter.Next(), ++arc_index) {
515  const Arc &arc = aiter.Value();
516  Label ilabel = arc.ilabel;
517  InfoForIlabel &info = label_map[ilabel];
518  if (info.arc_indexes.size() == 1) {
519  new_arcs.push_back(arc); // no changes needed
520  } else {
521  if (info.new_state < 0) {
522  info.new_state = fst->AddState();
523  // add arc from state 's' to newly created state.
524  new_arcs.push_back(Arc(ilabel, 0, Weight(info.tot_cost),
525  info.new_state));
526  }
527  // add arc from new state to original destination of this arc.
528  fst->AddArc(info.new_state, Arc(0, arc.olabel,
529  Weight(arc.weight.Value() - info.tot_cost),
530  arc.nextstate));
531  }
532  }
533  fst->DeleteArcs(s);
534  for (size_t i = 0; i < new_arcs.size(); i++)
535  fst->AddArc(s, new_arcs[i]);
536 }
fst::StdArc::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
fst::StdArc StdArc
fst::StdArc::Label Label
fst::StdArc::Weight Weight
double LogAdd(double x, double y)
Definition: kaldi-math.h:184

◆ IsStochasticFst() [1/2]

bool IsStochasticFst ( const Fst< LogArc > &  fst,
float  delta,
LogArc::Weight *  min_sum,
LogArc::Weight *  max_sum 
)
inline

Definition at line 1173 of file fstext-utils-inl.h.

References ApproxEqual(), and Plus().

Referenced by IsStochasticFstInLog(), main(), MinimizeEncoded(), RhoCompose(), and TestRemoveEpsLocalSpecial().

1176  {
1177  typedef LogArc Arc;
1178  typedef Arc::StateId StateId;
1179  typedef Arc::Weight Weight;
1180  bool first_time = true;
1181  bool ans = true;
1182  if (min_sum) *min_sum = LogArc::Weight::One();
1183  if (max_sum) *max_sum = LogArc::Weight::One();
1184  for (StateIterator<Fst<Arc> > siter(fst); !siter.Done(); siter.Next()) {
1185  StateId s = siter.Value();
1186  Weight sum = fst.Final(s);
1187  for (ArcIterator<Fst<Arc> > aiter(fst, s); !aiter.Done(); aiter.Next()) {
1188  const Arc &arc = aiter.Value();
1189  sum = Plus(sum, arc.weight);
1190  }
1191  if (!ApproxEqual(Weight::One(), sum, delta)) ans = false;
1192  if (first_time) {
1193  first_time = false;
1194  if (max_sum) *max_sum = sum;
1195  if (min_sum) *min_sum = sum;
1196  } else {
1197  // note that max and min are reversed from their normal
1198  // meanings here (max and min w.r.t. the underlying probabilities).
1199  if (max_sum && sum.Value() < max_sum->Value()) *max_sum = sum;
1200  if (min_sum && sum.Value() > min_sum->Value()) *min_sum = sum;
1201  }
1202  }
1203  if (first_time) { // just avoid NaNs if FST was empty.
1204  if (max_sum) *max_sum = Weight::One();
1205  if (min_sum) *min_sum = Weight::One();
1206  }
1207  return ans;
1208 }
fst::StdArc::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
LatticeWeightTpl< FloatType > Plus(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2)
fst::StdArc::Weight Weight
static bool ApproxEqual(float a, float b, float relative_tolerance=0.001)
return abs(a - b) <= relative_tolerance * (abs(a)+abs(b)).
Definition: kaldi-math.h:265

◆ IsStochasticFst() [2/2]

bool IsStochasticFst ( const Fst< Arc > &  fst,
float  delta = kDelta,
typename Arc::Weight *  min_sum = NULL,
typename Arc::Weight *  max_sum = NULL 
)
inline

This function returns true if, in the semiring of the FST, the sum (within the semiring) of all the arcs out of each state in the FST is one, to within delta.

After MakeStochasticFst, this should be true (for a connected FST).

Parameters
fst[in] the FST that we are testing.
delta[in] the tolerance to within which we test equality to 1.
min_sum[out] if non, NULL, contents will be set to the minimum sum of weights.
max_sum[out] if non, NULL, contents will be set to the maximum sum of weights.
Returns
Returns true if the FST is stochastic, and false otherwise.

Definition at line 1135 of file fstext-utils-inl.h.

References ApproxEqual(), and Plus().

1138  {
1139  typedef typename Arc::StateId StateId;
1140  typedef typename Arc::Weight Weight;
1141  NaturalLess<Weight> nl;
1142  bool first_time = true;
1143  bool ans = true;
1144  if (min_sum) *min_sum = Arc::Weight::One();
1145  if (max_sum) *max_sum = Arc::Weight::One();
1146  for (StateIterator<Fst<Arc> > siter(fst); !siter.Done(); siter.Next()) {
1147  StateId s = siter.Value();
1148  Weight sum = fst.Final(s);
1149  for (ArcIterator<Fst<Arc> > aiter(fst, s); !aiter.Done(); aiter.Next()) {
1150  const Arc &arc = aiter.Value();
1151  sum = Plus(sum, arc.weight);
1152  }
1153  if (!ApproxEqual(Weight::One(), sum, delta)) ans = false;
1154  if (first_time) {
1155  first_time = false;
1156  if (max_sum) *max_sum = sum;
1157  if (min_sum) *min_sum = sum;
1158  } else {
1159  if (max_sum && nl(*max_sum, sum)) *max_sum = sum;
1160  if (min_sum && nl(sum, *min_sum)) *min_sum = sum;
1161  }
1162  }
1163  if (first_time) { // just avoid NaNs if FST was empty.
1164  if (max_sum) *max_sum = Weight::One();
1165  if (min_sum) *min_sum = Weight::One();
1166  }
1167  return ans;
1168 }
fst::StdArc::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
LatticeWeightTpl< FloatType > Plus(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2)
fst::StdArc::Weight Weight
static bool ApproxEqual(float a, float b, float relative_tolerance=0.001)
return abs(a - b) <= relative_tolerance * (abs(a)+abs(b)).
Definition: kaldi-math.h:265

◆ IsStochasticFstInLog()

bool IsStochasticFstInLog ( const Fst< StdArc > &  fst,
float  delta,
StdArc::Weight *  min_sum,
StdArc::Weight *  max_sum 
)
inline

Definition at line 1215 of file fstext-utils-inl.h.

References IsStochasticFst(), and KALDI_ERR.

Referenced by main(), MinimizeEncoded(), and TestPushSpecial().

1218  {
1219  bool ans = false;
1220  LogArc::Weight log_min = LogArc::Weight::One(),
1221  log_max = LogArc::Weight::Zero();
1222  if (fst.Type() == "const") {
1223  ConstFst<LogArc> logfst;
1224  Cast(dynamic_cast<const ConstFst<StdArc>&>(fst), &logfst);
1225  ans = IsStochasticFst(logfst, delta, &log_min, &log_max);
1226  } else if (fst.Type() == "vector") {
1227  VectorFst<LogArc> logfst;
1228  Cast(dynamic_cast<const VectorFst<StdArc>&>(fst), &logfst);
1229  ans = IsStochasticFst(logfst, delta, &log_min, &log_max);
1230  } else {
1231  KALDI_ERR << "This version currently supports ConstFst<StdArc> "
1232  << "or VectorFst<StdArc>";
1233  }
1234  if (min_sum) *min_sum = StdArc::Weight(log_min.Value());
1235  if (max_sum) *max_sum = StdArc::Weight(log_max.Value());
1236  return ans;
1237 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
bool IsStochasticFst(const Fst< Arc > &fst, float delta, typename Arc::Weight *min_sum, typename Arc::Weight *max_sum)
This function returns true if, in the semiring of the FST, the sum (within the semiring) of all the a...
#define KALDI_ERR
Definition: kaldi-error.h:147
fst::StdArc::Weight Weight

◆ LatticeScale()

std::vector<std::vector<double> > fst::LatticeScale ( double  lmwt,
double  acwt 
)
inline

Definition at line 156 of file lattice-utils.h.

References CompactLatticeHasAlignment(), RemoveAlignmentsFromCompactLattice(), and ScaleLattice().

Referenced by main().

156  {
157  std::vector<std::vector<double> > ans(2);
158  ans[0].resize(2, 0.0);
159  ans[1].resize(2, 0.0);
160  ans[0][0] = lmwt;
161  ans[1][1] = acwt;
162  return ans;
163 }

◆ LatticeWeightTest()

void fst::LatticeWeightTest ( )

Definition at line 64 of file lattice-weight-test.cc.

References ApproxEqual(), kaldi::AssertEqual(), Compare(), Divide(), rnnlm::i, KALDI_ASSERT, LatticeWeightTpl< FloatType >::Member(), LatticeWeightTpl< BaseFloat >::One(), Plus(), LatticeWeightTpl< FloatType >::Quantize(), RandomLatticeWeight(), LatticeWeightTpl< FloatType >::Read(), LatticeWeightTpl< FloatType >::Reverse(), Times(), LatticeWeightTpl< FloatType >::Value1(), LatticeWeightTpl< FloatType >::Value2(), and LatticeWeightTpl< BaseFloat >::Zero().

Referenced by main().

64  {
65  for(int32 i = 0; i < 100; i++) {
67  LatticeWeight l3 = Plus(l1, l2);
68  LatticeWeight l4 = Times(l1, l2);
69  BaseFloat f1 = l1.Value1() + l1.Value2(), f2 = l2.Value1() + l2.Value2(), f3 = l3.Value1() + l3.Value2(),
70  f4 = l4.Value1() + l4.Value2();
71  kaldi::AssertEqual(std::min(f1, f2), f3);
72  kaldi::AssertEqual(f1 + f2, f4);
73 
74  KALDI_ASSERT(Plus(l3, l3) == l3);
75  KALDI_ASSERT(Plus(l1, l2) == Plus(l2, l1)); // commutativity of plus
76  KALDI_ASSERT(Times(l1, l2) == Times(l2, l1)); // commutativity of Times (true for this semiring, not always)
77  KALDI_ASSERT(Plus(l3, LatticeWeight::Zero()) == l3); // x + 0 = x
78  KALDI_ASSERT(Times(l3, LatticeWeight::One()) == l3); // x * 1 = x
79  KALDI_ASSERT(Times(l3, LatticeWeight::Zero()) == LatticeWeight::Zero()); // x * 0 = 0
80 
81  KALDI_ASSERT(l3.Reverse().Reverse() == l3);
82 
83  NaturalLess<LatticeWeight> nl;
84  bool a = nl(l1, l2);
85  bool b = (Plus(l1, l2) == l1 && l1 != l2);
86  KALDI_ASSERT(a == b);
87 
88  KALDI_ASSERT(Compare(l1, Plus(l1, l2)) != 1); // so do not have l1 > l1 + l2
90  {
91  LatticeWeight wa = Times(Plus(l1, l2), Plus(l5, l6)),
92  wb = Plus(Times(l1, l5), Plus(Times(l1, l6),
93  Plus(Times(l2, l5), Times(l2, l6))));
94  if (!ApproxEqual(wa, wb)) {
95  std::cout << "l1 = " << l1 << ", l2 = " << l2
96  << ", l5 = " << l5 << ", l6 = " << l6 << "\n";
97  std::cout << "ERROR: " << wa << " != " << wb << "\n";
98  }
99  // KALDI_ASSERT(Times(Plus(l1, l2), Plus(l5, l6))
100  // == Plus(Times(l1, l5), Plus(Times(l1,l6),
101  // Plus(Times(l2, l5), Times(l2, l6))))); // * distributes over +
102  }
103  KALDI_ASSERT(l1.Member() && l2.Member() && l3.Member() && l4.Member()
104  && l5.Member() && l6.Member());
105  if (l2 != LatticeWeight::Zero())
106  KALDI_ASSERT(ApproxEqual(Divide(Times(l1, l2), l2), l1)); // (a*b) / b = a if b != 0
107  KALDI_ASSERT(ApproxEqual(l1, l1.Quantize()));
108 
109  std::ostringstream s1;
110  s1 << l1;
111  std::istringstream s2(s1.str());
112  s2 >> l2;
113  KALDI_ASSERT(ApproxEqual(l1, l2, 0.001));
114  std::cout << s1.str() << '\n';
115  {
116  std::ostringstream s1b;
117  l1.Write(s1b);
118  std::istringstream s2b(s1b.str());
119  l3.Read(s2b);
120  KALDI_ASSERT(l1 == l3);
121  }
122  }
123 }
LatticeWeightTpl< FloatType > Divide(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2, DivideType typ=DIVIDE_ANY)
LatticeWeightTpl< FloatType > Plus(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2)
kaldi::int32 int32
LatticeWeightTpl< FloatType > Times(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2)
float BaseFloat
int Compare(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2)
Compare returns -1 if w1 < w2, +1 if w1 > w2, and 0 if w1 == w2.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
LatticeWeight RandomLatticeWeight()
static void AssertEqual(float a, float b, float relative_tolerance=0.001)
assert abs(a - b) <= relative_tolerance * (abs(a)+abs(b))
Definition: kaldi-math.h:276
LatticeWeightTpl< BaseFloat > LatticeWeight
static bool ApproxEqual(float a, float b, float relative_tolerance=0.001)
return abs(a - b) <= relative_tolerance * (abs(a)+abs(b)).
Definition: kaldi-math.h:265

◆ MakeFollowingInputSymbolsSame()

void MakeFollowingInputSymbolsSame ( bool  end_is_epsilon,
MutableFst< Arc > *  fst 
)

MakeFollowingInputSymbolsSame ensures that all arcs exiting any given fst state have the same input symbol.

It does this by detecting states that have differing input symbols on arcs that exit it, and inserting, for each of the following arcs with non-epsilon input symbol, a new dummy state that has an input-epsilon link from the fst state. The output symbol and weight stay on the link to the dummy state (in order to keep the FST output-deterministic and stochastic, if it already was). If end_is_epsilon, treat "being a final-state" like having an epsilon output link.

Definition at line 610 of file fstext-utils-inl.h.

References MakeFollowingInputSymbolsSameClass().

Referenced by MinimizeEncoded(), and TestMakeSymbolsSame().

610  {
611  IdentityFunction<typename Arc::Label> f;
612  MakeFollowingInputSymbolsSameClass(end_is_epsilon, fst, f);
613 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
void MakeFollowingInputSymbolsSameClass(bool end_is_epsilon, MutableFst< Arc > *fst, const F &f)
As MakeFollowingInputSymbolsSame, but takes a functor object that maps labels to classes.

◆ MakeFollowingInputSymbolsSameClass()

void MakeFollowingInputSymbolsSameClass ( bool  end_is_epsilon,
MutableFst< Arc > *  fst,
const F &  f 
)

As MakeFollowingInputSymbolsSame, but takes a functor object that maps labels to classes.

Definition at line 616 of file fstext-utils-inl.h.

References rnnlm::i, and rnnlm::j.

Referenced by kaldi::AddSelfLoopsNoReorder(), MakeFollowingInputSymbolsSame(), MinimizeEncoded(), and TestMakeSymbolsSameClass().

616  {
617  typedef typename Arc::StateId StateId;
618  typedef typename Arc::Weight Weight;
619  typedef typename F::Result ClassType;
620  std::vector<StateId> bad_states;
621  ClassType noClass = f(kNoLabel);
622  ClassType epsClass = f(0);
623  for (StateIterator<Fst<Arc> > siter(*fst); !siter.Done(); siter.Next()) {
624  StateId s = siter.Value();
625  ClassType c = noClass;
626  bool bad = false;
627  for (ArcIterator<Fst<Arc> > aiter(*fst, s); !aiter.Done(); aiter.Next()) {
628  const Arc &arc = aiter.Value();
629  if (c == noClass)
630  c = f(arc.ilabel);
631  else
632  if (c != f(arc.ilabel)) {
633  bad = true;
634  break;
635  }
636  }
637  if (end_is_epsilon && c != noClass &&
638  c != epsClass && fst->Final(s) != Weight::Zero())
639  bad = true;
640  if (bad)
641  bad_states.push_back(s);
642  }
643  std::vector<Arc> my_arcs;
644  for (size_t i = 0; i < bad_states.size(); i++) {
645  StateId s = bad_states[i];
646  my_arcs.clear();
647  for (ArcIterator<MutableFst<Arc> > aiter(*fst, s); !aiter.Done(); aiter.Next())
648  my_arcs.push_back(aiter.Value());
649 
650  for (size_t j = 0; j < my_arcs.size(); j++) {
651  Arc &arc = my_arcs[j];
652  if (arc.ilabel != 0) {
653  StateId newstate = fst->AddState();
654  // Create a new state for each non-eps arc in original FST, out of each bad state.
655  // Not as optimal as it could be, but does avoid some complicated weight-pushing
656  // issues in which, to maintain stochasticity, we would have to know which semiring
657  // we want to maintain stochasticity in.
658  fst->AddArc(newstate, Arc(arc.ilabel, 0, Weight::One(), arc.nextstate));
659  MutableArcIterator<MutableFst<Arc> > maiter(fst, s);
660  maiter.Seek(j);
661  maiter.SetValue(Arc(0, arc.olabel, arc.weight, newstate));
662  }
663  }
664  }
665 }
fst::StdArc::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
fst::StdArc::Weight Weight

◆ MakeLinearAcceptor()

void MakeLinearAcceptor ( const std::vector< I > &  labels,
MutableFst< Arc > *  ofst 
)

Creates unweighted linear acceptor from symbol sequence.

Definition at line 311 of file fstext-utils-inl.h.

References rnnlm::i.

Referenced by TrainingGraphCompiler::CompileGraphFromText(), TrainingGraphCompiler::CompileGraphsFromText(), main(), MinimizeEncoded(), and TestMakeLinearAcceptor().

311  {
312  typedef typename Arc::StateId StateId;
313  typedef typename Arc::Weight Weight;
314 
315  ofst->DeleteStates();
316  StateId cur_state = ofst->AddState();
317  ofst->SetStart(cur_state);
318  for (size_t i = 0; i < labels.size(); i++) {
319  StateId next_state = ofst->AddState();
320  Arc arc(labels[i], labels[i], Weight::One(), next_state);
321  ofst->AddArc(cur_state, arc);
322  cur_state = next_state;
323  }
324  ofst->SetFinal(cur_state, Weight::One());
325 }
fst::StdArc::StateId StateId
fst::StdArc::Weight Weight

◆ MakeLinearAcceptorWithAlternatives()

void MakeLinearAcceptorWithAlternatives ( const std::vector< std::vector< I > > &  labels,
MutableFst< Arc > *  ofst 
)

Creates an unweighted acceptor with a linear structure, with alternatives at each position.

Epsilon is treated like a normal symbol here. Each position in "labels" must have at least one alternative.

Definition at line 290 of file fstext-utils-inl.h.

References rnnlm::i, rnnlm::j, and KALDI_ASSERT.

Referenced by MinimizeEncoded().

291  {
292  typedef typename Arc::StateId StateId;
293  typedef typename Arc::Weight Weight;
294 
295  ofst->DeleteStates();
296  StateId cur_state = ofst->AddState();
297  ofst->SetStart(cur_state);
298  for (size_t i = 0; i < labels.size(); i++) {
299  KALDI_ASSERT(labels[i].size() != 0);
300  StateId next_state = ofst->AddState();
301  for (size_t j = 0; j < labels[i].size(); j++) {
302  Arc arc(labels[i][j], labels[i][j], Weight::One(), next_state);
303  ofst->AddArc(cur_state, arc);
304  }
305  cur_state = next_state;
306  }
307  ofst->SetFinal(cur_state, Weight::One());
308 }
fst::StdArc::StateId StateId
fst::StdArc::Weight Weight
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ MakeLoopFst()

VectorFst< Arc > * MakeLoopFst ( const std::vector< const ExpandedFst< Arc > * > &  fsts)

MakeLoopFst creates an FST that has a state that is both initial and final (weight == Weight::One()), and for each non-NULL pointer fsts[i], it has an arc out whose output-symbol is i and which goes to a sub-graph whose input language is equivalent to fsts[i], where the final-state becomes a transition to the loop-state.

Each fst in "fsts" should be an acceptor. The fst MakeLoopFst returns is output-deterministic, but not output-epsilon free necessarily, and arcs are sorted on output label. Note: if some of the pointers in the input vector "fsts" have the same value, "MakeLoopFst" uses this to speed up the computation. Formally: suppose I is the set of indexes i such that fsts[i] != NULL. Let L[i] be the language that the acceptor fsts[i] accepts. Let the language K be the set of input-output pairs i:l such that i in I and l in L[i]. Then the FST returned by MakeLoopFst accepts the language K*, where * is the Kleene closure (CLOSURE_STAR) of K. We could have implemented this via a combination of "project", "concat", "union" and "closure". But that FST would have been less well optimized and would have a lot of final-states.

Definition at line 669 of file fstext-utils-inl.h.

References rnnlm::i, and KALDI_ASSERT.

Referenced by kaldi::GetHTransducer(), MinimizeEncoded(), and TestMakeLoopFst().

669  {
670  typedef typename Arc::Weight Weight;
671  typedef typename Arc::StateId StateId;
672  typedef typename Arc::Label Label;
673 
674  VectorFst<Arc> *ans = new VectorFst<Arc>;
675  StateId loop_state = ans->AddState(); // = 0.
676  ans->SetStart(loop_state);
677  ans->SetFinal(loop_state, Weight::One());
678 
679  // "cache" is used as an optimization when some of the pointers in "fsts"
680  // may have the same value.
681  unordered_map<const ExpandedFst<Arc> *, Arc> cache;
682 
683  for (Label i = 0; i < static_cast<Label>(fsts.size()); i++) {
684  const ExpandedFst<Arc> *fst = fsts[i];
685  if (fst == NULL) continue;
686  { // optimization with cache: helpful if some members of "fsts" may
687  // contain the same pointer value (e.g. in GetHTransducer).
688  typename unordered_map<const ExpandedFst<Arc> *, Arc>::iterator
689  iter = cache.find(fst);
690  if (iter != cache.end()) {
691  Arc arc = iter->second;
692  arc.olabel = i;
693  ans->AddArc(0, arc);
694  continue;
695  }
696  }
697 
698  KALDI_ASSERT(fst->Properties(kAcceptor, true) == kAcceptor); // expect acceptor.
699 
700  StateId fst_num_states = fst->NumStates();
701  StateId fst_start_state = fst->Start();
702 
703  if (fst_start_state == kNoStateId)
704  continue; // empty fst.
705 
706  bool share_start_state =
707  fst->Properties(kInitialAcyclic, true) == kInitialAcyclic
708  && fst->NumArcs(fst_start_state) == 1
709  && fst->Final(fst_start_state) == Weight::Zero();
710 
711  std::vector<StateId> state_map(fst_num_states); // fst state -> ans state
712  for (StateId s = 0; s < fst_num_states; s++) {
713  if (s == fst_start_state && share_start_state) state_map[s] = loop_state;
714  else state_map[s] = ans->AddState();
715  }
716  if (!share_start_state) {
717  Arc arc(0, i, Weight::One(), state_map[fst_start_state]);
718  cache[fst] = arc;
719  ans->AddArc(0, arc);
720  }
721  for (StateId s = 0; s < fst_num_states; s++) {
722  // Add arcs out of state s.
723  for (ArcIterator<ExpandedFst<Arc> > aiter(*fst, s); !aiter.Done(); aiter.Next()) {
724  const Arc &arc = aiter.Value();
725  Label olabel = (s == fst_start_state && share_start_state ? i : 0);
726  Arc newarc(arc.ilabel, olabel, arc.weight, state_map[arc.nextstate]);
727  ans->AddArc(state_map[s], newarc);
728  if (s == fst_start_state && share_start_state)
729  cache[fst] = newarc;
730  }
731  if (fst->Final(s) != Weight::Zero()) {
732  KALDI_ASSERT(!(s == fst_start_state && share_start_state));
733  ans->AddArc(state_map[s], Arc(0, 0, fst->Final(s), loop_state));
734  }
735  }
736  }
737  return ans;
738 }
fst::StdArc::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
fst::StdArc::Label Label
fst::StdArc::Weight Weight
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ MakeLoopFstCompare()

VectorFst<Arc>* fst::MakeLoopFstCompare ( const vector< const ExpandedFst< Arc > *> &  fsts)

Definition at line 281 of file fstext-utils-test.cc.

References ClearSymbols(), fst::pre_determinize_helpers::Closure(), and rnnlm::i.

Referenced by TestMakeLoopFst().

281  {
282  VectorFst<Arc> *ans = new VectorFst<Arc>;
283  typedef typename Arc::Label Label;
284  typedef typename Arc::StateId StateId;
285  typedef typename Arc::Weight Weight;
286 
287  for (Label i = 0; i < fsts.size(); i++) {
288  if (fsts[i] != NULL) {
289  VectorFst<Arc> i_fst; // accepts symbol i on output.
290  i_fst.AddState(); i_fst.AddState();
291  i_fst.SetStart(0); i_fst.SetFinal(1, Weight::One());
292  i_fst.AddArc(0, Arc(0, i, Weight::One(), 1));
293  VectorFst<Arc> other_fst(*(fsts[i])); // copy it.
294  ClearSymbols(false, true, &other_fst); // Clear output symbols so symbols
295  // are on input side.
296  Concat(&i_fst, other_fst); // now i_fst is "i_fst [concat] other_fst".
297  Union(ans, i_fst);
298  }
299  }
300  Closure(ans, CLOSURE_STAR);
301  return ans;
302 }
fst::StdArc::StateId StateId
void ClearSymbols(bool clear_input, bool clear_output, MutableFst< Arc > *fst)
ClearSymbols sets all the symbols on the input and/or output side of the FST to zero, as specified.
void Closure(MutableFst< Arc > *fst, std::set< typename Arc::StateId > *S, const std::vector< bool > &pVec)
fst::StdArc::Label Label
fst::StdArc::Weight Weight

◆ MakePrecedingInputSymbolsSame()

void MakePrecedingInputSymbolsSame ( bool  start_is_epsilon,
MutableFst< Arc > *  fst 
)

MakePrecedingInputSymbolsSame ensures that all arcs entering any given fst state have the same input symbol.

It does this by detecting states that have differing input symbols going in, and inserting, for each of the preceding arcs with non-epsilon input symbol, a new dummy state that has an epsilon link to the fst state. If "start_is_epsilon", ensure that start-state can have only epsilon-links into it.

Definition at line 528 of file fstext-utils-inl.h.

References MakePrecedingInputSymbolsSameClass().

Referenced by MinimizeEncoded(), and TestMakeSymbolsSame().

528  {
529  IdentityFunction<typename Arc::Label> f;
530  MakePrecedingInputSymbolsSameClass(start_is_epsilon, fst, f);
531 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
void MakePrecedingInputSymbolsSameClass(bool start_is_epsilon, MutableFst< Arc > *fst, const F &f)
As MakePrecedingInputSymbolsSame, but takes a functor object that maps labels to classes.

◆ MakePrecedingInputSymbolsSameClass()

void MakePrecedingInputSymbolsSameClass ( bool  start_is_epsilon,
MutableFst< Arc > *  fst,
const F &  f 
)

As MakePrecedingInputSymbolsSame, but takes a functor object that maps labels to classes.

Definition at line 534 of file fstext-utils-inl.h.

References ConstIntegerSet< I >::count(), rnnlm::i, and KALDI_ASSERT.

Referenced by kaldi::AddSelfLoopsReorder(), MakePrecedingInputSymbolsSame(), MinimizeEncoded(), and TestMakeSymbolsSameClass().

534  {
535  typedef typename F::Result ClassType;
536  typedef typename Arc::StateId StateId;
537  typedef typename Arc::Weight Weight;
538  std::vector<ClassType> classes;
539  ClassType noClass = f(kNoLabel);
540  ClassType epsClass = f(0);
541  if (start_is_epsilon) { // treat having-start-state as epsilon in-transition.
542  StateId start_state = fst->Start();
543  if (start_state < 0 || start_state == kNoStateId) // empty FST.
544  return;
545  classes.resize(start_state+1, noClass);
546  classes[start_state] = epsClass;
547  }
548 
549  // Find bad states (states with multiple input-symbols into them).
550  std::set<StateId> bad_states; // states that we need to change.
551  for (StateIterator<Fst<Arc> > siter(*fst); !siter.Done(); siter.Next()) {
552  StateId s = siter.Value();
553  for (ArcIterator<Fst<Arc> > aiter(*fst, s); !aiter.Done(); aiter.Next()) {
554  const Arc &arc = aiter.Value();
555  if (classes.size() <= static_cast<size_t>(arc.nextstate))
556  classes.resize(arc.nextstate+1, noClass);
557  if (classes[arc.nextstate] == noClass)
558  classes[arc.nextstate] = f(arc.ilabel);
559  else
560  if (classes[arc.nextstate] != f(arc.ilabel))
561  bad_states.insert(arc.nextstate);
562  }
563  }
564  if (bad_states.empty()) return; // Nothing to do.
565  kaldi::ConstIntegerSet<StateId> bad_states_ciset(bad_states); // faster lookup.
566 
567  // Work out list of arcs we have to change as (state, arc-offset).
568  // Can't do the actual changes in this pass, since we have to add new
569  // states which invalidates the iterators.
570  std::vector<std::pair<StateId, size_t> > arcs_to_change;
571  for (StateIterator<Fst<Arc> > siter(*fst); !siter.Done(); siter.Next()) {
572  StateId s = siter.Value();
573  for (ArcIterator<Fst<Arc> > aiter(*fst, s); !aiter.Done(); aiter.Next()) {
574  const Arc &arc = aiter.Value();
575  if (arc.ilabel != 0 &&
576  bad_states_ciset.count(arc.nextstate) != 0)
577  arcs_to_change.push_back(std::make_pair(s, aiter.Position()));
578  }
579  }
580  KALDI_ASSERT(!arcs_to_change.empty()); // since !bad_states.empty().
581 
582  std::map<std::pair<StateId, ClassType>, StateId> state_map;
583  // state_map is a map from (bad-state, input-symbol-class) to dummy-state.
584 
585  for (size_t i = 0; i < arcs_to_change.size(); i++) {
586  StateId s = arcs_to_change[i].first;
587  ArcIterator<MutableFst<Arc> > aiter(*fst, s);
588  aiter.Seek(arcs_to_change[i].second);
589  Arc arc = aiter.Value();
590 
591  // Transition is non-eps transition to "bad" state. Introduce new state (or find
592  // existing one).
593  std::pair<StateId, ClassType> p(arc.nextstate, f(arc.ilabel));
594  if (state_map.count(p) == 0) {
595  StateId newstate = state_map[p] = fst->AddState();
596  fst->AddArc(newstate, Arc(0, 0, Weight::One(), arc.nextstate));
597  }
598  StateId dst_state = state_map[p];
599  arc.nextstate = dst_state;
600 
601  // Initialize the MutableArcIterator only now, as the call to NewState()
602  // may have invalidated the first arc iterator.
603  MutableArcIterator<MutableFst<Arc> > maiter(fst, s);
604  maiter.Seek(arcs_to_change[i].second);
605  maiter.SetValue(arc);
606  }
607 }
fst::StdArc::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
fst::StdArc::Weight Weight
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ MapInputSymbols()

void MapInputSymbols ( const std::vector< I > &  symbol_mapping,
MutableFst< Arc > *  fst 
)

Definition at line 168 of file fstext-utils-inl.h.

References KALDI_ASSERT_IS_INTEGER_TYPE.

Referenced by MinimizeEncoded(), and TestFactor().

169  {
171  // false == don't copy the "symbol_mapping", retain pointer--
172  // safe since short-lived object.
173  MapInputSymbolsMapper<Arc, I> mapper(symbol_mapping, false);
174  Map(fst, mapper);
175 }
#define KALDI_ASSERT_IS_INTEGER_TYPE(I)
Definition: kaldi-utils.h:133
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21

◆ MinimizeCompactLattice()

bool MinimizeCompactLattice ( MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, IntType > > > *  clat,
float  delta = fst::kDelta 
)

This function minimizes the compact lattice.

It is to be called after determinization (see ./determinize-lattice-pruned.h) and pushing (see ./push-lattice.h). If the lattice is not determinized and pushed this function will not combine as many states as it could, but it won't crash. Returns true on success, and false if it failed due to topological sorting failing. The output will be topologically sorted.

Definition at line 274 of file minimize-lattice.cc.

References CompactLatticeMinimizer< Weight, IntType >::Minimize().

Referenced by main(), DeterminizeLatticeTask::operator()(), and kaldi::TestMinimizeCompactLattice().

276  {
277  CompactLatticeMinimizer<Weight, IntType> minimizer(clat, delta);
278  return minimizer.Minimize();
279 }

◆ MinimizeCompactLattice< kaldi::LatticeWeight, kaldi::int32 >()

◆ MinimizeEncoded()

◆ NbestAsFsts()

void NbestAsFsts ( const Fst< Arc > &  fst,
size_t  n,
std::vector< VectorFst< Arc > > *  fsts_out 
)

Takes the n-shortest-paths (using ShortestPath), but outputs the result as a vector of up to n fsts.

This function will size the "fsts_out" vector to however many paths it got (which will not exceed n). n must be >= 1.

Definition at line 279 of file fstext-utils-inl.h.

References ConvertNbestToVector(), and KALDI_ASSERT.

Referenced by MinimizeEncoded(), kaldi::SentenceLevelConfidence(), and TestMakeLinearAcceptor().

281  {
282  KALDI_ASSERT(n > 0);
283  KALDI_ASSERT(fsts_out != NULL);
284  VectorFst<Arc> nbest_fst;
285  ShortestPath(fst, &nbest_fst, n);
286  ConvertNbestToVector(nbest_fst, fsts_out);
287 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
void ConvertNbestToVector(const Fst< Arc > &fst, std::vector< VectorFst< Arc > > *fsts_out)
This function converts an FST with a special structure, which is output by the OpenFst functions Shor...
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ NumArcs()

Arc::StateId NumArcs ( const ExpandedFst< Arc > &  fst)

Returns the total number of arcs in an FST.

Definition at line 67 of file fstext-utils-inl.h.

Referenced by kaldi::DeterminizeLatticeWrapper(), main(), and TrivialFactorWeightFstImpl< A, F >::NumArcs().

67  {
68  typedef typename Arc::StateId StateId;
69  StateId num_arcs = 0;
70  for (StateId s = 0; s < fst.NumStates(); s++)
71  num_arcs += fst.NumArcs(s);
72  return num_arcs;
73 }
fst::StdArc::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21

◆ operator!=() [1/2]

bool fst::operator!= ( const LatticeWeightTpl< FloatType > &  wa,
const LatticeWeightTpl< FloatType > &  wb 
)
inline

Definition at line 277 of file lattice-weight.h.

References LatticeWeightTpl< FloatType >::Value1(), and LatticeWeightTpl< FloatType >::Value2().

278  {
279  // Volatile qualifier thwarts over-aggressive compiler optimizations
280  // that lead to problems esp. with NaturalLess().
281  volatile FloatType va1 = wa.Value1(), va2 = wa.Value2(),
282  vb1 = wb.Value1(), vb2 = wb.Value2();
283  return (va1 != vb1 || va2 != vb2);
284 }

◆ operator!=() [2/2]

bool fst::operator!= ( const CompactLatticeWeightTpl< WeightType, IntType > &  w1,
const CompactLatticeWeightTpl< WeightType, IntType > &  w2 
)
inline

Definition at line 564 of file lattice-weight.h.

References CompactLatticeWeightTpl< WeightType, IntType >::String(), and CompactLatticeWeightTpl< WeightType, IntType >::Weight().

565  {
566  return (w1.Weight() != w2.Weight() || w1.String() != w2.String());
567 }

◆ operator<<() [1/2]

std::ostream & operator<< ( std::ostream &  strm,
const LatticeWeightTpl< FloatType > &  w 
)
inline

Definition at line 397 of file lattice-weight.h.

References LatticeWeightTpl< FloatType >::WriteFloatType().

397  {
398  LatticeWeightTpl<FloatType>::WriteFloatType(strm, w.Value1());
399  CHECK(FLAGS_fst_weight_separator.size() == 1);
400  strm << FLAGS_fst_weight_separator[0]; // comma by default;
401  // may or may not be settable from Kaldi programs.
402  LatticeWeightTpl<FloatType>::WriteFloatType(strm, w.Value2());
403  return strm;
404 }

◆ operator<<() [2/2]

std::ostream& fst::operator<< ( std::ostream &  strm,
const CompactLatticeWeightTpl< WeightType, IntType > &  w 
)
inline

Definition at line 729 of file lattice-weight.h.

References rnnlm::i.

729  {
730  strm << w.Weight();
731  CHECK(FLAGS_fst_weight_separator.size() == 1);
732  strm << FLAGS_fst_weight_separator[0]; // comma by default.
733  for(size_t i = 0; i < w.String().size(); i++) {
734  strm << w.String()[i];
735  if (i+1 < w.String().size())
736  strm << kStringSeparator; // '_'; defined in string-weight.h in OpenFst code.
737  }
738  return strm;
739 }

◆ operator==() [1/2]

bool fst::operator== ( const LatticeWeightTpl< FloatType > &  wa,
const LatticeWeightTpl< FloatType > &  wb 
)
inline

Definition at line 267 of file lattice-weight.h.

References LatticeWeightTpl< FloatType >::Value1(), and LatticeWeightTpl< FloatType >::Value2().

Referenced by DiscriminativeSupervision::DiscriminativeSupervision(), and ArpaLmCompilerImplInterface::~ArpaLmCompilerImplInterface().

268  {
269  // Volatile qualifier thwarts over-aggressive compiler optimizations
270  // that lead to problems esp. with NaturalLess().
271  volatile FloatType va1 = wa.Value1(), va2 = wa.Value2(),
272  vb1 = wb.Value1(), vb2 = wb.Value2();
273  return (va1 == vb1 && va2 == vb2);
274 }

◆ operator==() [2/2]

bool fst::operator== ( const CompactLatticeWeightTpl< WeightType, IntType > &  w1,
const CompactLatticeWeightTpl< WeightType, IntType > &  w2 
)
inline

Definition at line 558 of file lattice-weight.h.

References CompactLatticeWeightTpl< WeightType, IntType >::String(), and CompactLatticeWeightTpl< WeightType, IntType >::Weight().

559  {
560  return (w1.Weight() == w2.Weight() && w1.String() == w2.String());
561 }

◆ operator>>() [1/2]

std::istream & operator>> ( std::istream &  strm,
LatticeWeightTpl< FloatType > &  w 
)
inline

Definition at line 407 of file lattice-weight.h.

References LatticeWeightTpl< FloatType >::ReadNoParen().

407  {
408  CHECK(FLAGS_fst_weight_separator.size() == 1);
409  // separator defaults to ','
410  return w1.ReadNoParen(strm, FLAGS_fst_weight_separator[0]);
411 }

◆ operator>>() [2/2]

std::istream& fst::operator>> ( std::istream &  strm,
CompactLatticeWeightTpl< WeightType, IntType > &  w 
)
inline

Definition at line 742 of file lattice-weight.h.

References rnnlm::i, CompactLatticeWeightTpl< WeightType, IntType >::SetString(), and CompactLatticeWeightTpl< WeightType, IntType >::SetWeight().

742  {
743  std::string s;
744  strm >> s;
745  if (strm.fail()) {
746  return strm;
747  }
748  CHECK(FLAGS_fst_weight_separator.size() == 1);
749  size_t pos = s.find_last_of(FLAGS_fst_weight_separator); // normally ","
750  if (pos == std::string::npos) {
751  strm.clear(std::ios::badbit);
752  return strm;
753  }
754  // get parts of str before and after the separator (default: ',');
755  std::string s1(s, 0, pos), s2(s, pos+1);
756  std::istringstream strm1(s1);
757  WeightType weight;
758  strm1 >> weight;
759  w.SetWeight(weight);
760  if (strm1.fail() || !strm1.eof()) {
761  strm.clear(std::ios::badbit);
762  return strm;
763  }
764  // read string part.
765  std::vector<IntType> string;
766  const char *c = s2.c_str();
767  while(*c != '\0') {
768  if (*c == kStringSeparator) // '_'
769  c++;
770  char *c2;
771  long int i = strtol(c, &c2, 10);
772  if (c2 == c || static_cast<long int>(static_cast<IntType>(i)) != i) {
773  strm.clear(std::ios::badbit);
774  return strm;
775  }
776  c = c2;
777  string.push_back(static_cast<IntType>(i));
778  }
779  w.SetString(string);
780  return strm;
781 }

◆ PenalizeArcsWithSomeInputSymbols()

void fst::PenalizeArcsWithSomeInputSymbols ( const std::vector< I > &  symbols_in,
float  penalty,
VectorFst< Arc > *  fst 
)

Definition at line 58 of file fstrmsymbols.cc.

References ConstIntegerSet< I >::count(), and Times().

Referenced by main().

60  {
61  typedef typename Arc::StateId StateId;
62  typedef typename Arc::Label Label;
63  typedef typename Arc::Weight Weight;
64 
65  Weight penalty_weight(penalty);
66 
67  kaldi::ConstIntegerSet<I> symbol_set(symbols_in);
68 
69  StateId num_states = fst->NumStates();
70  for (StateId s = 0; s < num_states; s++) {
71  for (MutableArcIterator<VectorFst<Arc> > iter(fst, s);
72  !iter.Done(); iter.Next()) {
73  if (symbol_set.count(iter.Value().ilabel) != 0) {
74  Arc arc = iter.Value();
75  arc.weight = Times(arc.weight, penalty_weight);
76  iter.SetValue(arc);
77  }
78  }
79  }
80 }
fst::StdArc::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
LatticeWeightTpl< FloatType > Times(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2)
fst::StdArc::Label Label
fst::StdArc::Weight Weight

◆ PhiCompose()

void PhiCompose ( const Fst< Arc > &  fst1,
const Fst< Arc > &  fst2,
typename Arc::Label  phi_label,
MutableFst< Arc > *  ofst 
)

Definition at line 1021 of file fstext-utils-inl.h.

References KALDI_ASSERT.

Referenced by main(), and MinimizeEncoded().

1024  {
1025  KALDI_ASSERT(phi_label != kNoLabel); // just use regular compose in this case.
1026  typedef Fst<Arc> F;
1027  typedef PhiMatcher<SortedMatcher<F> > PM;
1028  CacheOptions base_opts;
1029  base_opts.gc_limit = 0; // Cache only the last state for fastest copy.
1030  // ComposeFstImplOptions templated on matcher for fst1, matcher for fst2.
1031  // The matcher for fst1 doesn't matter; we'll use fst2's matcher.
1032  ComposeFstImplOptions<SortedMatcher<F>, PM> impl_opts(base_opts);
1033 
1034  // the false below is something called phi_loop which is something I don't
1035  // fully understand, but I don't think we want it.
1036 
1037  // These pointers are taken ownership of, by ComposeFst.
1038  PM *phi_matcher =
1039  new PM(fst2, MATCH_INPUT, phi_label, false);
1040  SortedMatcher<F> *sorted_matcher =
1041  new SortedMatcher<F>(fst1, MATCH_NONE); // tell it
1042  // not to use this matcher, as this would mean we would
1043  // not follow phi transitions.
1044  impl_opts.matcher1 = sorted_matcher;
1045  impl_opts.matcher2 = phi_matcher;
1046  *ofst = ComposeFst<Arc>(fst1, fst2, impl_opts);
1047  Connect(ofst);
1048 }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Plus() [1/5]

ArcticWeightTpl<T> fst::Plus ( const ArcticWeightTpl< T > &  w1,
const ArcticWeightTpl< T > &  w2 
)
inline

Definition at line 87 of file arctic-weight.h.

88  {
89  return w1.Value() > w2.Value() ? w1 : w2;
90 }

◆ Plus() [2/5]

ArcticWeightTpl<float> fst::Plus ( const ArcticWeightTpl< float > &  w1,
const ArcticWeightTpl< float > &  w2 
)
inline

Definition at line 92 of file arctic-weight.h.

93  {
94  return Plus<float>(w1, w2);
95 }

◆ Plus() [3/5]

ArcticWeightTpl<double> fst::Plus ( const ArcticWeightTpl< double > &  w1,
const ArcticWeightTpl< double > &  w2 
)
inline

Definition at line 97 of file arctic-weight.h.

98  {
99  return Plus<double>(w1, w2);
100 }

◆ Plus() [4/5]

◆ Plus() [5/5]

CompactLatticeWeightTpl<WeightType, IntType> fst::Plus ( const CompactLatticeWeightTpl< WeightType, IntType > &  w1,
const CompactLatticeWeightTpl< WeightType, IntType > &  w2 
)
inline

Definition at line 664 of file lattice-weight.h.

References Compare().

666  {
667  return (Compare(w1, w2) >= 0 ? w1 : w2);
668 }
int Compare(const TropicalWeight &w1, const TropicalWeight &w2)

◆ PrecedingInputSymbolsAreSame()

bool PrecedingInputSymbolsAreSame ( bool  start_is_epsilon,
const Fst< Arc > &  fst 
)

Returns true if and only if the FST is such that the input symbols on arcs entering any given state all have the same value.

if "start_is_epsilon", treat start-state as an epsilon input arc [i.e. ensure only epsilon can enter start-state].

Definition at line 460 of file fstext-utils-inl.h.

References PrecedingInputSymbolsAreSameClass().

Referenced by MinimizeEncoded(), and TestMakeSymbolsSame().

460  {
461  IdentityFunction<typename Arc::Label> f;
462  return PrecedingInputSymbolsAreSameClass(start_is_epsilon, fst, f);
463 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
bool PrecedingInputSymbolsAreSameClass(bool start_is_epsilon, const Fst< Arc > &fst, const F &f)
This is as PrecedingInputSymbolsAreSame, but with a functor f that maps labels to classes...

◆ PrecedingInputSymbolsAreSameClass()

bool PrecedingInputSymbolsAreSameClass ( bool  start_is_epsilon,
const Fst< Arc > &  fst,
const F &  f 
)

This is as PrecedingInputSymbolsAreSame, but with a functor f that maps labels to classes.

The function tests whether the symbols preceding any given state are in the same class. Formally, f is of a type F that has an operator of type F::Result F::operator() (F::Arg a) const; where F::Result is an integer type and F::Arc can be constructed from Arc::Label. this must apply to valid labels and also to kNoLabel (so we can have a marker for the invalid labels.

Definition at line 466 of file fstext-utils-inl.h.

Referenced by MinimizeEncoded(), PrecedingInputSymbolsAreSame(), and TestMakeSymbolsSameClass().

466  {
467  typedef typename F::Result ClassType;
468  typedef typename Arc::StateId StateId;
469  std::vector<ClassType> classes;
470  ClassType noClass = f(kNoLabel);
471 
472  if (start_is_epsilon) {
473  StateId start_state = fst.Start();
474  if (start_state < 0 || start_state == kNoStateId)
475  return true; // empty fst-- doesn't matter.
476  classes.resize(start_state+1, noClass);
477  classes[start_state] = 0;
478  }
479 
480  for (StateIterator<Fst<Arc> > siter(fst); !siter.Done(); siter.Next()) {
481  StateId s = siter.Value();
482  for (ArcIterator<Fst<Arc> > aiter(fst, s); !aiter.Done(); aiter.Next()) {
483  const Arc &arc = aiter.Value();
484  if (classes.size() <= arc.nextstate)
485  classes.resize(arc.nextstate+1, noClass);
486  if (classes[arc.nextstate] == noClass)
487  classes[arc.nextstate] = f(arc.ilabel);
488  else
489  if (classes[arc.nextstate] != f(arc.ilabel))
490  return false;
491  }
492  }
493  return true;
494 }
fst::StdArc::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21

◆ PreDeterminize()

void PreDeterminize ( MutableFst< Arc > *  fst,
typename Arc::Label  first_new_sym,
std::vector< Int > *  symsOut 
)

Definition at line 317 of file pre-determinize-inl.h.

References fst::pre_determinize_helpers::Closure(), fst::pre_determinize_helpers::CopySetToVector(), CreateSuperFinal(), rnnlm::i, fst::pre_determinize_helpers::InsertMember(), KALDI_ASSERT, KALDI_ERR, KALDI_VLOG, and rnnlm::n.

Referenced by SafeDeterminizeMinimizeWrapper(), SafeDeterminizeWrapper(), TestDeterminize(), TestDeterminizeStarInLog(), TestFactor(), TestMinimize(), TestPreDeterminize(), and TestPush().

319  {
320  typedef typename Arc::Label Label;
321  typedef typename Arc::StateId StateId;
322  typedef size_t ArcId; // Our own typedef, not standard OpenFst. Use size_t
323  // for compatibility with argument of ArcIterator::Seek().
324  typedef typename Arc::Weight Weight;
325  assert(first_new_sym > 0);
326  assert(fst != NULL);
327  if (fst->Start() == kNoStateId) return; // for empty FST, nothing to do.
328  assert(symsOut != NULL && symsOut->size() == 0); // we will output the symbols we add into this.
329 
330  { // (D)(i)(a): check is trim (i.e. connected, in OpenFST parlance).
331  KALDI_VLOG(2) << "PreDeterminize: Checking FST properties";
332  uint64 props = fst->Properties(kAccessible|kCoAccessible, true); // true-> computes properties if unknown at time when called.
333  if (props != (kAccessible|kCoAccessible)) { // All states are not both accessible and co-accessible...
334  KALDI_ERR << "PreDeterminize: FST is not trim";
335  }
336  }
337 
338  { // (D)(i)(b): make single final state.
339  KALDI_VLOG(2) << "PreDeterminize: creating single final state";
341  }
342 
343  { // (D)(i)(c): sort arcs on input.
344  KALDI_VLOG(2) << "PreDeterminize: sorting arcs on input";
345  ILabelCompare<Arc> icomp;
346  ArcSort(fst, icomp);
347  }
348 
349  StateId n_states = 0, max_state = 0; // Compute n_states, max_state = highest-numbered state.
350  { // compute nStates, maxStates.
351  for (StateIterator<MutableFst<Arc> > iter(*fst); ! iter.Done(); iter.Next()) {
352  StateId state = iter.Value();
353  assert(state>=0);
354  n_states++;
355  if (state > max_state) max_state = state;
356  }
357  KALDI_VLOG(2) << "PreDeterminize: n_states = "<<(n_states)<<", max_state ="<<(max_state);
358  }
359 
360  std::vector<bool> p_vec(max_state+1, false); // compute this next.
361  { // D(ii): computing the array p. ["problematic states, i.e. states with >1 input transition,
362  // counting being the initial state as an input transition"].
363  std::vector<bool> seen_vec(max_state+1, false); // rather than counting incoming transitions we just have a bool that says we saw at least one.
364 
365  seen_vec[fst->Start()] = true;
366  for (StateIterator<MutableFst<Arc> > siter(*fst); ! siter.Done(); siter.Next()) {
367  for (ArcIterator<MutableFst<Arc> > aiter(*fst, siter.Value()); ! aiter.Done(); aiter.Next()) {
368  const Arc &arc = aiter.Value();
369  assert(arc.nextstate>=0&&arc.nextstate<max_state+1);
370  if (seen_vec[arc.nextstate])
371  p_vec[arc.nextstate] = true; // now have >1 transition in, so problematic.
372  else
373  seen_vec[arc.nextstate] = true;
374  }
375  }
376  }
377  // D(iii): set up m(a)
378  std::map<std::pair<StateId, ArcId>, size_t> m_map;
379  // This is the array m, indexed by arcs. It maps to the index of the symbol we add.
380 
381 
382  // WARNING: we should be sure to clean up this memory before exiting. Do not return
383  // or throw an exception from this function, later than this point, without cleaning up!
384  // Note that the vectors are shared between Q and S (they "belong to" S.
385  std::vector<std::vector<StateId>* > S(max_state+1, (std::vector<StateId>*)(void*)0);
386  std::vector<std::pair<std::vector<StateId>*, size_t> > Q;
387 
388  // D(iv): initialize S and Q.
389  {
390  std::vector<StateId> all_seed_states; // all "problematic" states, plus initial state (if not problematic).
391  if (!p_vec[fst->Start()])
392  all_seed_states.push_back(fst->Start());
393  for (StateId s = 0;s<=max_state; s++)
394  if (p_vec[s]) all_seed_states.push_back(s);
395 
396  for (size_t idx = 0;idx < all_seed_states.size(); idx++) {
397  StateId s = all_seed_states[idx];
398  std::set<StateId> closure_s;
399  closure_s.insert(s); // insert "seed" state.
400  pre_determinize_helpers::Closure(fst, &closure_s, p_vec); // follow epsilons to non-problematic states.
401  // Closure in this case whis will usually not add anything, for typical topologies in speech
402  std::vector<StateId> closure_s_vec;
403  pre_determinize_helpers::CopySetToVector(closure_s, &closure_s_vec);
404  KALDI_ASSERT(closure_s_vec.size() != 0);
405  std::vector<StateId> *ptr = pre_determinize_helpers::InsertMember(closure_s_vec, &S);
406  KALDI_ASSERT(ptr != NULL); // Or conceptual bug or programming error.
407  Q.push_back(std::pair<std::vector<StateId>*, size_t>(ptr, 0));
408  }
409  }
410 
411  std::vector<bool> d_vec(max_state+1, false); // "done vector". Purely for debugging.
412 
413 
414  size_t num_extra_det_states = 0;
415 
416  // (D)(v)
417  while (Q.size() != 0) {
418 
419  // (D)(v)(a)
420  std::pair<std::vector<StateId>*, size_t> cur_pair(Q.back());
421  Q.pop_back();
422  const std::vector<StateId> &A(*cur_pair.first);
423  size_t n =cur_pair.second; // next special symbol to add.
424 
425  // (D)(v)(b)
426  for (size_t idx = 0;idx < A.size(); idx++) {
427  assert(d_vec[A[idx]] == false && "This state has been seen before. Algorithm error.");
428  d_vec[A[idx]] = true;
429  }
430 
431  // From here is (D)(v)(c). We work out S_\eps and S_t (for t\neq eps)
432  // simultaneously at first.
433  std::map<Label, std::set<std::pair<std::pair<StateId, ArcId>, StateId> > > arc_hash;
434  // arc_hash is a hash with info of all arcs from states in the set A to
435  // non-problematic states.
436  // It is a map from ilabel to pair(pair(start-state, arc-offset), end-state).
437  // Here, arc-offset reflects the order in which we accessed the arc using the
438  // ArcIterator (zero for the first arc).
439 
440 
441  { // This block sets up arc_hash
442  for (size_t idx = 0;idx < A.size(); idx++) {
443  StateId s = A[idx];
444  assert(s>=0 && s<=max_state);
445  ArcId arc_id = 0;
446  for (ArcIterator<MutableFst<Arc> > aiter(*fst, s); ! aiter.Done(); aiter.Next(), ++arc_id) {
447  const Arc &arc = aiter.Value();
448 
449  std::pair<std::pair<StateId, ArcId>, StateId>
450  this_pair(std::pair<StateId, ArcId>(s, arc_id), arc.nextstate);
451  bool inserted = (arc_hash[arc.ilabel].insert(this_pair)).second;
452  assert(inserted); // Otherwise we had a duplicate.
453  }
454  }
455  }
456 
457  // (D)(v)(d)
458  if (arc_hash.count(0) == 1) { // We have epsilon transitions out.
459  std::set<std::pair<std::pair<StateId, ArcId>, StateId> > &eps_set = arc_hash[0];
460  typedef typename std::set<std::pair<std::pair<StateId, ArcId>, StateId> >::iterator set_iter_t;
461  for (set_iter_t siter = eps_set.begin(); siter != eps_set.end(); ++siter) {
462  const std::pair<std::pair<StateId, ArcId>, StateId> &this_pr = *siter;
463  if (p_vec[this_pr.second]) { // Eps-transition to problematic state.
464  assert(m_map.count(this_pr.first) == 0);
465  m_map[this_pr.first] = n;
466  n++;
467  }
468  }
469  }
470 
471  // (D)(v)(e)
472  {
473  typedef typename std::map<Label, std::set<std::pair<std::pair<StateId, ArcId>, StateId> > >::iterator map_iter_t;
474  typedef typename std::set<std::pair<std::pair<StateId, ArcId>, StateId> >::iterator set_iter_t2;
475  for (map_iter_t miter = arc_hash.begin(); miter != arc_hash.end(); ++miter) {
476  Label t = miter->first;
477  std::set<std::pair<std::pair<StateId, ArcId>, StateId> > &S_t = miter->second;
478  if (t != 0) { // For t != epsilon,
479  std::set<StateId> V_t; // set of destination non-problem states. Will create this set now.
480 
481  // exists_noproblem is true iff |U_t| > 0.
482  size_t k = 0;
483 
484  // First loop "for each transition a in T_t" (i.e. transitions to problematic states)
485  // The if-statement if (|S_t|>1) is pushed inside the loop, as the loop also computes
486  // the set V_t.
487  for (set_iter_t2 siter = S_t.begin(); siter != S_t.end(); ++siter) {
488  const std::pair<std::pair<StateId, ArcId>, StateId> &this_pr = *siter;
489  if (p_vec[this_pr.second]) { // only consider problematic states (just set T_t)
490  if (S_t.size() > 1) { // This is where we pushed the if-statement in.
491  assert(m_map.count(this_pr.first) == 0);
492  m_map[this_pr.first] = k;
493  k++;
494  num_extra_det_states++;
495  }
496  } else { // Create the set V_t.
497  V_t.insert(this_pr.second);
498  }
499  }
500  if (V_t.size() != 0) {
501  pre_determinize_helpers::Closure(fst, &V_t, p_vec); // follow epsilons to non-problematic states.
502  std::vector<StateId> closure_V_t_vec;
503  pre_determinize_helpers::CopySetToVector(V_t, &closure_V_t_vec);
504  std::vector<StateId> *ptr = pre_determinize_helpers::InsertMember(closure_V_t_vec, &S);
505  if (ptr != NULL) { // was inserted.
506  Q.push_back(std::pair<std::vector<StateId>*, size_t>(ptr, k));
507  }
508  }
509  }
510  }
511  }
512  } // end while (Q.size() != 0)
513 
514 
515  { // (D)(vi): Check that for each state in the FST, d(s) = true.
516  for (StateIterator<MutableFst<Arc> > siter(*fst); ! siter.Done(); siter.Next()) {
517  StateId val = siter.Value();
518  assert(d_vec[val] == true);
519  }
520  }
521 
522  { // (D)(vii): compute symbol-table ID's.
523  // sets up symsOut array.
524  int64 n = -1;
525  for (typename std::map<std::pair<StateId, ArcId>, size_t>::iterator m_iter = m_map.begin();
526  m_iter != m_map.end();
527  ++m_iter) {
528  n = std::max(n, (int64) m_iter->second); // m_iter->second is of type size_t.
529  }
530  // At this point n is the highest symbol-id (type size_t) of symbols we must add.
531  n++; // This is now the number of symbols we must add.
532  for (size_t i = 0;static_cast<int64>(i)<n;i++) symsOut->push_back(first_new_sym + i);
533  }
534 
535  // (D)(viii): set up hash.
536  std::map<std::pair<StateId, size_t>, StateId> h_map;
537 
538  { // D(ix): add extra symbols! This is where the work gets done.
539 
540  // Core part of this is below, search for (*)
541  size_t n_states_added = 0;
542 
543  for (typename std::map<std::pair<StateId, ArcId>, size_t>::iterator m_iter = m_map.begin();
544  m_iter != m_map.end();
545  ++m_iter) {
546  StateId state = m_iter->first.first;
547  ArcId arcpos = m_iter->first.second;
548  size_t m_a = m_iter->second;
549 
550  MutableArcIterator<MutableFst<Arc> > aiter(fst, state);
551  aiter.Seek(arcpos);
552  Arc arc = aiter.Value();
553 
554  // (*) core part here.
555  if (arc.ilabel == 0)
556  arc.ilabel = (*symsOut)[m_a];
557  else {
558  std::pair<StateId, size_t> pr(arc.nextstate, m_a);
559  if (!h_map.count(pr)) {
560  n_states_added++;
561  StateId newstate = fst->AddState();
562  assert(newstate>=0);
563  Arc new_arc( (*symsOut)[m_a], (Label)0, Weight::One(), arc.nextstate);
564  fst->AddArc(newstate, new_arc);
565  h_map[pr] = newstate;
566  }
567  arc.nextstate = h_map[pr];
568  }
569  aiter.SetValue(arc);
570  }
571 
572  KALDI_VLOG(2) << "Added " <<(n_states_added)<<" new states and added/changed "<<(m_map.size())<<" arcs";
573 
574  }
575  // Now free up memory.
576  for (size_t i = 0;i < S.size();i++)
577  delete S[i];
578 } // end function PreDeterminize
fst::StdArc::StateId StateId
void CopySetToVector(const std::set< T > s, std::vector< T > *v)
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
struct rnnlm::@11::@12 n
Arc::StateId CreateSuperFinal(MutableFst< Arc > *fst)
#define KALDI_ERR
Definition: kaldi-error.h:147
void Closure(MutableFst< Arc > *fst, std::set< typename Arc::StateId > *S, const std::vector< bool > &pVec)
fst::StdArc::Label Label
fst::StdArc::Weight Weight
std::vector< T > * InsertMember(const std::vector< T > m, std::vector< std::vector< T > *> *S)
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
#define KALDI_VLOG(v)
Definition: kaldi-error.h:156

◆ PrepareForGrammarFst()

void PrepareForGrammarFst ( int32  nonterm_phones_offset,
VectorFst< StdArc > *  fst 
)

This function prepares 'ifst' for use in GrammarFst: it ensures that it has the expected properties, changing it slightly as needed.

'ifst' is expected to be a fully compiled HCLG graph that is intended to be used in GrammarFst. The user will most likely want to copy it to the ConstFst type after calling this function.

The following describes what this function does, and the reasons why it has to do these things:

  • To keep the ArcIterator code simple (to avoid branches in loops), even for expanded states we store the destination fst-instance index separately per state, not per arc. This requires that any transitions across FST boundaries from a single FST must be to a single destination FST (for a given source state). We fix this problem by introducing epsilon arcs and new states whenever we find a state that would cause a problem for the above.
  • In order to signal to the GrammarFst code that a particular state has cross-FST-boundary transitions, we set the final-prob to a nonzero value on that state. Specifically, we use a weight with Value() == 4096.0. When the GrammarFst code sees that value it knows that it was not a 'real' final-prob. Prior to doing this we ensure, by adding epsilon transitions as needed, that the state did not previously have a final-prob.
  • For arcs that are final arcs in an FST that represents a nonterminal (these arcs would have #nonterm_exit on them), we ensure that the states that they transition to have unit final-prob (i.e. final-prob equal to One()), by incorporating any final-prob into the arc itself. This avoids the GrammarFst code having to inspect those final-probs when expanding states.
Parameters
[in]nonterm_phones_offsetThe integer id of the symbols #nonterm_bos in the phones.txt file.
[in,out]fstThe FST to be (slightly) modified.

Definition at line 982 of file grammar-fst.cc.

References GrammarFstPreparer::Prepare().

Referenced by main().

983  {
984  GrammarFstPreparer p(nonterm_phones_offset, fst);
985  p.Prepare();
986 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21

◆ Print()

void fst::Print ( const Fst< Arc > &  fst,
std::string  message 
)

Definition at line 362 of file fstext-utils-test.cc.

Referenced by TransitionModel::GetPhones(), MaxChangeStats::MaxChangeStats(), NnetDiscriminativeStats::NnetDiscriminativeStats(), DiscriminativeObjectiveInfo::PrintAll(), SplitExampleStats::SplitExampleStats(), and GeneralDescriptor::~GeneralDescriptor().

362  {
363  std::cout << message << "\n";
364  FstPrinter<Arc> fstprinter(fst, NULL, NULL, NULL, false, true, "\t");
365  fstprinter.Print(&std::cout, "standard output");
366 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21

◆ PrintProxyFstPath()

bool PrintProxyFstPath ( const VectorFst< StdArc > &  proxy,
vector< vector< StdArc::Label > > *  path,
vector< StdArc::Weight > *  weight,
StdArc::StateId  cur_state,
vector< StdArc::Label >  cur_path,
StdArc::Weight  cur_weight 
)

Definition at line 30 of file generate-proxy-keywords.cc.

References Times().

Referenced by main().

35  {
36  if (proxy.Final(cur_state) != StdArc::Weight::Zero()) {
37  // Assumes only final state has non-zero weight.
38  cur_weight = Times(proxy.Final(cur_state), cur_weight);
39  path->push_back(cur_path);
40  weight->push_back(cur_weight);
41  return true;
42  }
43 
44  for (ArcIterator<StdFst> aiter(proxy, cur_state);
45  !aiter.Done(); aiter.Next()) {
46  const StdArc &arc = aiter.Value();
47  StdArc::Weight temp_weight = Times(arc.weight, cur_weight);
48  cur_path.push_back(arc.ilabel);
49  PrintProxyFstPath(proxy, path, weight,
50  arc.nextstate, cur_path, temp_weight);
51  cur_path.pop_back();
52  }
53 
54  return true;
55 }
fst::StdArc StdArc
LatticeWeightTpl< FloatType > Times(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2)
bool PrintProxyFstPath(const VectorFst< StdArc > &proxy, vector< vector< StdArc::Label > > *path, vector< StdArc::Weight > *weight, StdArc::StateId cur_state, vector< StdArc::Label > cur_path, StdArc::Weight cur_weight)
fst::StdArc::Weight Weight

◆ PropagateFinal()

void PropagateFinal ( typename Arc::Label  phi_label,
MutableFst< Arc > *  fst 
)

Definition at line 1084 of file fstext-utils-inl.h.

References KALDI_WARN, and PropagateFinalInternal().

Referenced by main(), and MinimizeEncoded().

1085  {
1086  typedef typename Arc::StateId StateId;
1087  if (fst->Properties(kIEpsilons, true)) // just warn.
1088  KALDI_WARN << "PropagateFinal: this may not work as desired "
1089  "since your FST has input epsilons.";
1090  StateId num_states = fst->NumStates();
1091  for (StateId s = 0; s < num_states; s++)
1092  PropagateFinalInternal(phi_label, s, fst);
1093 }
fst::StdArc::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
#define KALDI_WARN
Definition: kaldi-error.h:150
void PropagateFinalInternal(typename Arc::Label phi_label, typename Arc::StateId s, MutableFst< Arc > *fst)

◆ PropagateFinalInternal()

void fst::PropagateFinalInternal ( typename Arc::Label  phi_label,
typename Arc::StateId  s,
MutableFst< Arc > *  fst 
)

Definition at line 1051 of file fstext-utils-inl.h.

References KALDI_ASSERT, and Times().

Referenced by PropagateFinal().

1054  {
1055  typedef typename Arc::Weight Weight;
1056  if (fst->Final(s) == Weight::Zero()) {
1057  // search for phi transition. We assume there
1058  // is just one-- phi nondeterminism is not allowed
1059  // anyway.
1060  int num_phis = 0;
1061  for (ArcIterator<Fst<Arc> > aiter(*fst, s);
1062  !aiter.Done(); aiter.Next()) {
1063  const Arc &arc = aiter.Value();
1064  if (arc.ilabel == phi_label) {
1065  num_phis++;
1066  if (arc.nextstate == s) continue; // don't expect
1067  // phi loops but ignore them anyway.
1068 
1069  // If this recurses infinitely, it means there
1070  // are loops of phi transitions, which there should
1071  // not be in a normal backoff LM. We could make this
1072  // routine work for this case, but currently there is
1073  // no need.
1074  PropagateFinalInternal(phi_label, arc.nextstate, fst);
1075  if (fst->Final(arc.nextstate) != Weight::Zero())
1076  fst->SetFinal(s, Times(fst->Final(arc.nextstate), arc.weight));
1077  }
1078  KALDI_ASSERT(num_phis <= 1 && "Phi nondeterminism found");
1079  }
1080  }
1081 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
LatticeWeightTpl< FloatType > Times(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2)
fst::StdArc::Weight Weight
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void PropagateFinalInternal(typename Arc::Label phi_label, typename Arc::StateId s, MutableFst< Arc > *fst)

◆ PruneCompactLattice()

void fst::PruneCompactLattice ( Weight  beam,
MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, Int > > > *  fst 
)

◆ PruneSpecial()

void PruneSpecial ( const Fst< Arc > &  ifst,
VectorFst< Arc > *  ofst,
typename Arc::Weight  beam,
size_t  max_states = 0 
)

The function PruneSpecial is like the standard OpenFst function "prune", except it does not expand the entire "ifst"- this is useful for cases where ifst is an on-demand FST such as a ComposeFst and we don't want to visit it all.

It supports pruning either to a specified beam (if beam is not One()), or to a specified max_states (if max_states is > 0). One of the two must be specified.

Requirements:

  • Costs must be non-negative (equivalently, weights must not be greater than One()).
  • There must be a Compare(a, b) function that compares two weights and returns (-1,0,1) if (a<b, a=b, a>b). We define this in Kaldi, for TropicalWeight, LogWeight (I think), and LatticeWeight... also CompactLatticeWeight, but we doubt that will be used here; better to use PruneCompactLattice().

Definition at line 161 of file prune-special-inl.h.

Referenced by main().

164  {
165  PruneSpecialClass<Arc> c(ifst, ofst, beam, max_states);
166 }

◆ PushCompactLatticeStrings()

bool PushCompactLatticeStrings ( MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, IntType > > > *  clat)

This function pushes the transition-ids as far towards the start as they will go.

It can be useful prior to lattice-align-words (for non-linear lattices). We can't use the generic OpenFst "push" function because it uses the sum as the divisor, which is not appropriate in this case (a+b generally won't divide a or b in this semiring). It returns true on success, false if it failed due to TopSort failing, which should never happen, but we handle it gracefully by just leaving the lattice the same. This function used to be called just PushCompactLattice.

Definition at line 210 of file push-lattice.cc.

References CompactLatticePusher< Weight, IntType >::Push().

Referenced by main(), DeterminizeLatticeTask::operator()(), kaldi::TestMinimizeCompactLattice(), and kaldi::TestPushCompactLatticeStrings().

211  {
212  CompactLatticePusher<Weight, IntType> pusher(clat);
213  return pusher.Push();
214 }

◆ PushCompactLatticeStrings< kaldi::LatticeWeight, kaldi::int32 >()

◆ PushCompactLatticeWeights()

bool PushCompactLatticeWeights ( MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, IntType > > > *  clat)

This function pushes the weights in the CompactLattice so that all states except possibly the start state, have Weight components (of type LatticeWeight) that "sum to one" in the LatticeWeight (i.e.

interpreting the weights as negated log-probs). It returns true on success, false if it failed due to TopSort failing, which should never happen, but we handle it gracefully by just leaving the lattice the same.

Definition at line 217 of file push-lattice.cc.

References Divide(), KALDI_ASSERT, KALDI_WARN, Plus(), Times(), and CompactLatticeWeightTpl< WeightType, IntType >::Zero().

Referenced by main(), DeterminizeLatticeTask::operator()(), kaldi::TestMinimizeCompactLattice(), and kaldi::TestPushCompactLatticeWeights().

218  {
219  if (clat->Properties(kTopSorted, true) == 0) {
220  if (!TopSort(clat)) {
221  KALDI_WARN << "Topological sorting of state-level lattice failed "
222  "(probably your lexicon has empty words or your LM has epsilon cycles; this "
223  " is a bad idea.)";
224  return false;
225  }
226  }
227  typedef CompactLatticeWeightTpl<Weight, IntType> CompactWeight;
228  typedef ArcTpl<CompactWeight> CompactArc;
229  typedef typename CompactArc::StateId StateId;
230 
231  StateId num_states = clat->NumStates();
232  if (num_states == 0) {
233  KALDI_WARN << "Pushing weights of empty compact lattice";
234  return true; // this is technically success because an empty
235  // lattice is already pushed.
236  }
237  std::vector<Weight> weight_to_end(num_states); // Note: LatticeWeight
238  // contains two floats.
239  for (StateId s = num_states - 1; s >= 0; s--) {
240  Weight this_weight_to_end = clat->Final(s).Weight();
241  for (ArcIterator<MutableFst<CompactArc> > aiter(*clat, s);
242  !aiter.Done(); aiter.Next()) {
243  const CompactArc &arc = aiter.Value();
244  KALDI_ASSERT(arc.nextstate > s && "Cyclic lattices not allowed.");
245  this_weight_to_end = Plus(this_weight_to_end,
246  Times(aiter.Value().weight.Weight(),
247  weight_to_end[arc.nextstate]));
248  }
249  if (this_weight_to_end == Weight::Zero()) {
250  KALDI_WARN << "Lattice has non-coaccessible states.";
251  }
252  weight_to_end[s] = this_weight_to_end;
253  }
254  weight_to_end[0] = Weight::One(); // We leave the "leftover weight" on
255  // the start state, which won't
256  // necessarily end up summing to one.
257  for (StateId s = 0; s < num_states; s++) {
258  Weight this_weight_to_end = weight_to_end[s];
259  if (this_weight_to_end == Weight::Zero())
260  continue;
261  for (MutableArcIterator<MutableFst<CompactArc> > aiter(clat, s);
262  !aiter.Done(); aiter.Next()) {
263  CompactArc arc = aiter.Value();
264  Weight next_weight_to_end = weight_to_end[arc.nextstate];
265  if (next_weight_to_end != Weight::Zero()) {
266  arc.weight.SetWeight(Times(arc.weight.Weight(),
267  Divide(next_weight_to_end,
268  this_weight_to_end)));
269  aiter.SetValue(arc);
270  }
271  }
272  CompactWeight final_weight = clat->Final(s);
273  if (final_weight != CompactWeight::Zero()) {
274  final_weight.SetWeight(Divide(final_weight.Weight(), this_weight_to_end));
275  clat->SetFinal(s, final_weight);
276  }
277  }
278 
279  return true;
280 }
fst::StdArc::StateId StateId
LatticeWeightTpl< FloatType > Divide(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2, DivideType typ=DIVIDE_ANY)
LatticeWeightTpl< FloatType > Plus(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2)
LatticeWeightTpl< FloatType > Times(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2)
#define KALDI_WARN
Definition: kaldi-error.h:150
fst::StdArc::Weight Weight
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ PushCompactLatticeWeights< kaldi::LatticeWeight, kaldi::int32 >()

◆ PushInLog()

void fst::PushInLog ( VectorFst< StdArc > *  fst,
uint32  ptype,
float  delta = kDelta 
)

Definition at line 91 of file fstext-utils.h.

91  {
92 
93  // PushInLog pushes the FST
94  // and returns a new pushed FST (labels and weights pushed to the left).
95  VectorFst<LogArc> *fst_log = new VectorFst<LogArc>; // Want to determinize in log semiring.
96  Cast(*fst, fst_log);
97  VectorFst<StdArc> tmp;
98  *fst = tmp; // free up memory.
99  VectorFst<LogArc> *fst_pushed_log = new VectorFst<LogArc>;
100  Push<LogArc, rtype>(*fst_log, fst_pushed_log, ptype, delta);
101  Cast(*fst_pushed_log, fst);
102  delete fst_log;
103  delete fst_pushed_log;
104 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21

◆ PushSpecial()

void PushSpecial ( VectorFst< StdArc > *  fst,
float  delta 
)

Definition at line 226 of file push-special.cc.

Referenced by main(), and TestPushSpecial().

226  {
227  if (fst->NumStates() > 0)
228  PushSpecialClass c(fst, delta); // all the work
229  // gets done in the initializer.
230 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21

◆ RandFst()

VectorFst<Arc>* fst::RandFst ( RandFstOptions  opts = RandFstOptions())

Returns a random FST.

Useful for randomized algorithm testing. Only works if weight can be constructed from float.

Definition at line 56 of file rand-fst.h.

References rnnlm::i, rnnlm::j, and kaldi::Rand().

56  {
57  typedef typename Arc::StateId StateId;
58  typedef typename Arc::Weight Weight;
59 
60  VectorFst<Arc> *fst = new VectorFst<Arc>();
61 
62  start:
63 
64  // Create states.
65  std::vector<StateId> all_states;
66  for (size_t i = 0;i < (size_t)opts.n_states;i++) {
67  StateId this_state = fst->AddState();
68  if (i == 0) fst->SetStart(i);
69  all_states.push_back(this_state);
70  }
71  // Set final states.
72  for (size_t j = 0;j < (size_t)opts.n_final;j++) {
73  StateId id = all_states[kaldi::Rand() % opts.n_states];
74  Weight weight = (Weight)(opts.weight_multiplier*(kaldi::Rand() % 5));
75  fst->SetFinal(id, weight);
76  }
77  // Create arcs.
78  for (size_t i = 0;i < (size_t)opts.n_arcs;i++) {
79  Arc a;
80  StateId start_state;
81  if(!opts.acyclic) { // no restriction on arcs.
82  start_state = all_states[kaldi::Rand() % opts.n_states];
83  a.nextstate = all_states[kaldi::Rand() % opts.n_states];
84  } else {
85  start_state = all_states[kaldi::Rand() % (opts.n_states-1)];
86  a.nextstate = start_state + 1 + (kaldi::Rand() % (opts.n_states-start_state-1));
87  }
88  a.ilabel = kaldi::Rand() % opts.n_syms;
89  a.olabel = kaldi::Rand() % opts.n_syms; // same input+output vocab.
90  a.weight = (Weight) (opts.weight_multiplier*(kaldi::Rand() % 4));
91 
92  fst->AddArc(start_state, a);
93  }
94 
95  // Trim resulting FST.
96  Connect(fst);
97  if (opts.acyclic)
98  assert(fst->Properties(kAcyclic, true) & kAcyclic);
99  if (fst->Start() == kNoStateId && !opts.allow_empty) {
100  goto start;
101  }
102  return fst;
103 }
fst::StdArc::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
fst::StdArc::Weight Weight

◆ RandomCompactLatticeWeight()

CompactLatticeWeight fst::RandomCompactLatticeWeight ( )

Definition at line 51 of file lattice-weight-test.cc.

References rnnlm::i, kaldi::Rand(), RandomLatticeWeight(), and LatticeWeightTpl< BaseFloat >::Zero().

Referenced by CompactLatticeWeightTest().

51  {
53  if (w == LatticeWeight::Zero()) {
54  return CompactLatticeWeight(w, vector<int32>());
55  } else {
56  int32 len = kaldi::Rand() % 4;
57  vector<int32> str;
58  for(int32 i = 0; i < len; i++)
59  str.push_back(kaldi::Rand() % 10 + 1);
60  return CompactLatticeWeight(w, str);
61  }
62 }
kaldi::int32 int32
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
LatticeWeight RandomLatticeWeight()
LatticeWeightTpl< BaseFloat > LatticeWeight
CompactLatticeWeightTpl< LatticeWeight, int32 > CompactLatticeWeight

◆ RandomLatticeWeight()

LatticeWeight fst::RandomLatticeWeight ( )

Definition at line 38 of file lattice-weight-test.cc.

References kaldi::Rand(), kaldi::RandGauss(), and LatticeWeightTpl< BaseFloat >::Zero().

Referenced by LatticeWeightTest(), and RandomCompactLatticeWeight().

38  {
39  int tmp = kaldi::Rand() % 4;
40  if (tmp == 0) {
41  return LatticeWeight::Zero();
42  } else if (tmp == 1) {
43  return LatticeWeight( 1, 2); // sometimes return special values..
44  } else if (tmp == 2) {
45  return LatticeWeight( 2, 1); // this tests more thoroughly certain properties...
46  } else {
47  return LatticeWeight( 100 * kaldi::RandGauss(), 100 * kaldi::RandGauss());
48  }
49 }
float RandGauss(struct RandomState *state=NULL)
Definition: kaldi-math.h:155
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
LatticeWeightTpl< BaseFloat > LatticeWeight

◆ RandPairFst()

VectorFst<Arc>* fst::RandPairFst ( RandFstOptions  opts = RandFstOptions())

Returns a random FST.

Useful for randomized algorithm testing. Only works if weight can be constructed from a pair of floats

Definition at line 108 of file rand-fst.h.

References rnnlm::i, rnnlm::j, and kaldi::Rand().

108  {
109  typedef typename Arc::StateId StateId;
110  typedef typename Arc::Weight Weight;
111 
112  VectorFst<Arc> *fst = new VectorFst<Arc>();
113 
114  start:
115 
116  // Create states.
117  std::vector<StateId> all_states;
118  for (size_t i = 0;i < (size_t)opts.n_states;i++) {
119  StateId this_state = fst->AddState();
120  if (i == 0) fst->SetStart(i);
121  all_states.push_back(this_state);
122  }
123  // Set final states.
124  for (size_t j = 0; j < (size_t)opts.n_final;j++) {
125  StateId id = all_states[kaldi::Rand() % opts.n_states];
126  Weight weight (opts.weight_multiplier*(kaldi::Rand() % 5), opts.weight_multiplier*(kaldi::Rand() % 5));
127  fst->SetFinal(id, weight);
128  }
129  // Create arcs.
130  for (size_t i = 0;i < (size_t)opts.n_arcs;i++) {
131  Arc a;
132  StateId start_state;
133  if(!opts.acyclic) { // no restriction on arcs.
134  start_state = all_states[kaldi::Rand() % opts.n_states];
135  a.nextstate = all_states[kaldi::Rand() % opts.n_states];
136  } else {
137  start_state = all_states[kaldi::Rand() % (opts.n_states-1)];
138  a.nextstate = start_state + 1 + (kaldi::Rand() % (opts.n_states-start_state-1));
139  }
140  a.ilabel = kaldi::Rand() % opts.n_syms;
141  a.olabel = kaldi::Rand() % opts.n_syms; // same input+output vocab.
142  a.weight = Weight (opts.weight_multiplier*(kaldi::Rand() % 4), opts.weight_multiplier*(kaldi::Rand() % 4));
143 
144  fst->AddArc(start_state, a);
145  }
146 
147  // Trim resulting FST.
148  Connect(fst);
149  if (opts.acyclic)
150  assert(fst->Properties(kAcyclic, true) & kAcyclic);
151  if (fst->Start() == kNoStateId && !opts.allow_empty) {
152  goto start;
153  }
154  return fst;
155 }
fst::StdArc::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
fst::StdArc::Weight Weight

◆ ReadAndPrepareLmFst()

fst::VectorFst< fst::StdArc > * ReadAndPrepareLmFst ( std::string  rxfilename)

Definition at line 125 of file kaldi-fst-io.cc.

References ReadFstKaldi().

Referenced by main().

125  {
126  // ReadFstKaldi() will die with exception on failure.
127  fst::VectorFst<fst::StdArc> *ans = fst::ReadFstKaldi(rxfilename);
128  if (ans->Properties(fst::kAcceptor, true) == 0) {
129  // If it's not already an acceptor, project on the output, i.e. copy olabels
130  // to ilabels. Generally the G.fst's on disk will have the disambiguation
131  // symbol #0 on the input symbols of the backoff arc, and projection will
132  // replace them with epsilons which is what is on the output symbols of
133  // those arcs.
134  fst::Project(ans, fst::PROJECT_OUTPUT);
135  }
136  if (ans->Properties(fst::kILabelSorted, true) == 0) {
137  // Make sure LM is sorted on ilabel.
138  fst::ILabelCompare<fst::StdArc> ilabel_comp;
139  fst::ArcSort(ans, ilabel_comp);
140  }
141  return ans;
142 }
void ReadFstKaldi(std::istream &is, bool binary, VectorFst< Arc > *fst)

◆ ReadAsConstFst()

ConstFst<StdArc>* fst::ReadAsConstFst ( std::string  rxfilename)

Definition at line 32 of file make-grammar-fst.cc.

References ReadFstKaldiGeneric().

Referenced by main().

32  {
33  // the following call will throw if there is an error.
34  Fst<StdArc> *fst = ReadFstKaldiGeneric(rxfilename);
35  ConstFst<StdArc> *const_fst = dynamic_cast<ConstFst<StdArc>* >(fst);
36  if (!const_fst) {
37  const_fst = new ConstFst<StdArc>(*fst);
38  delete fst;
39  }
40  return const_fst;
41 }
Fst< StdArc > * ReadFstKaldiGeneric(std::string rxfilename, bool throw_on_err)
Definition: kaldi-fst-io.cc:45
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21

◆ ReadConstFstFromStream()

static ConstFst<StdArc>* fst::ReadConstFstFromStream ( std::istream &  is)
static

Definition at line 406 of file grammar-fst.cc.

References KALDI_ERR.

Referenced by GrammarFst::Read().

406  {
407  fst::FstHeader hdr;
408  std::string stream_name("unknown");
409  if (!hdr.Read(is, stream_name))
410  KALDI_ERR << "Reading FST: error reading FST header";
411  FstReadOptions ropts("<unspecified>", &hdr);
412  ConstFst<StdArc> *ans = ConstFst<StdArc>::Read(is, ropts);
413  if (!ans)
414  KALDI_ERR << "Could not read ConstFst from stream.";
415  return ans;
416 }
#define KALDI_ERR
Definition: kaldi-error.h:147

◆ ReadFstKaldi() [1/4]

VectorFst< StdArc > * ReadFstKaldi ( std::string  rxfilename)

Definition at line 29 of file kaldi-fst-io.cc.

References KALDI_ERR, kaldi::PrintableRxfilename(), and Input::Stream().

29  {
30  if (rxfilename == "") rxfilename = "-"; // interpret "" as stdin,
31  // for compatibility with OpenFst conventions.
32  kaldi::Input ki(rxfilename);
33  fst::FstHeader hdr;
34  if (!hdr.Read(ki.Stream(), rxfilename))
35  KALDI_ERR << "Reading FST: error reading FST header from "
36  << kaldi::PrintableRxfilename(rxfilename);
37  FstReadOptions ropts("<unspecified>", &hdr);
38  VectorFst<StdArc> *fst = VectorFst<StdArc>::Read(ki.Stream(), ropts);
39  if (!fst)
40  KALDI_ERR << "Could not read fst from "
41  << kaldi::PrintableRxfilename(rxfilename);
42  return fst;
43 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
#define KALDI_ERR
Definition: kaldi-error.h:147
std::string PrintableRxfilename(const std::string &rxfilename)
PrintableRxfilename turns the rxfilename into a more human-readable form for error reporting...
Definition: kaldi-io.cc:61

◆ ReadFstKaldi() [2/4]

void fst::ReadFstKaldi ( std::string  rxfilename,
VectorFst< StdArc > *  ofst 
)

◆ ReadFstKaldi() [3/4]

void ReadFstKaldi ( std::istream &  is,
bool  binary,
VectorFst< Arc > *  fst 
)

Definition at line 73 of file kaldi-fst-io-inl.h.

References kaldi::ConvertStringToInteger(), rnnlm::d, KALDI_ERR, kaldi::SplitStringToIntegers(), kaldi::SplitStringToVector(), and StrToWeight().

Referenced by main(), VectorFstTplHolder< Arc >::Read(), ReadAndPrepareLmFst(), and ReadFstKaldi().

74  {
75  typedef typename Arc::Weight Weight;
76  typedef typename Arc::StateId StateId;
77  if (binary) {
78  // We don't have access to the filename here, so write [unknown].
79  VectorFst<Arc> *ans =
80  VectorFst<Arc>::Read(is, fst::FstReadOptions(std::string("[unknown]")));
81  if (ans == NULL) {
82  KALDI_ERR << "Error reading FST from stream.";
83  }
84  *fst = *ans; // shallow copy.
85  delete ans;
86  } else {
87  // Consume the \r on Windows, the \n that the text-form FST format starts
88  // with, and any extra spaces that might have got in there somehow.
89  while (std::isspace(is.peek()) && is.peek() != '\n') is.get();
90  if (is.peek() == '\n') is.get(); // consume the newline.
91  else { // saw spaces but no newline.. this is not expected.
92  KALDI_ERR << "Reading FST: unexpected sequence of spaces "
93  << " at file position " << is.tellg();
94  }
95  using std::string;
96  using std::vector;
99  fst->DeleteStates();
100  string line;
101  size_t nline = 0;
102  string separator = FLAGS_fst_field_separator + "\r\n";
103  while (std::getline(is, line)) {
104  nline++;
105  vector<string> col;
106  // on Windows we'll write in text and read in binary mode.
107  kaldi::SplitStringToVector(line, separator.c_str(), true, &col);
108  if (col.size() == 0) break; // Empty line is a signal to stop, in our
109  // archive format.
110  if (col.size() > 5) {
111  KALDI_ERR << "Bad line in FST: " << line;
112  }
113  StateId s;
114  if (!ConvertStringToInteger(col[0], &s)) {
115  KALDI_ERR << "Bad line in FST: " << line;
116  }
117  while (s >= fst->NumStates())
118  fst->AddState();
119  if (nline == 1) fst->SetStart(s);
120 
121  bool ok = true;
122  Arc arc;
123  Weight w;
124  StateId d = s;
125  switch (col.size()) {
126  case 1:
127  fst->SetFinal(s, Weight::One());
128  break;
129  case 2:
130  if (!StrToWeight(col[1], true, &w)) ok = false;
131  else fst->SetFinal(s, w);
132  break;
133  case 3: // 3 columns not ok for Lattice format; it's not an acceptor.
134  ok = false;
135  break;
136  case 4:
137  ok = ConvertStringToInteger(col[1], &arc.nextstate) &&
138  ConvertStringToInteger(col[2], &arc.ilabel) &&
139  ConvertStringToInteger(col[3], &arc.olabel);
140  if (ok) {
141  d = arc.nextstate;
142  arc.weight = Weight::One();
143  fst->AddArc(s, arc);
144  }
145  break;
146  case 5:
147  ok = ConvertStringToInteger(col[1], &arc.nextstate) &&
148  ConvertStringToInteger(col[2], &arc.ilabel) &&
149  ConvertStringToInteger(col[3], &arc.olabel) &&
150  StrToWeight(col[4], false, &arc.weight);
151  if (ok) {
152  d = arc.nextstate;
153  fst->AddArc(s, arc);
154  }
155  break;
156  default:
157  ok = false;
158  }
159  while (d >= fst->NumStates()) fst->AddState();
160  if (!ok)
161  KALDI_ERR << "Bad line in FST: " << line;
162  }
163  }
164 }
fst::StdArc::StateId StateId
bool ConvertStringToInteger(const std::string &str, Int *out)
Converts a string into an integer via strtoll and returns false if there was any kind of problem (i...
Definition: text-utils.h:118
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
bool SplitStringToIntegers(const std::string &full, const char *delim, bool omit_empty_strings, std::vector< I > *out)
Split a string (e.g.
Definition: text-utils.h:68
void SplitStringToVector(const std::string &full, const char *delim, bool omit_empty_strings, std::vector< std::string > *out)
Split a string using any of the single character delimiters.
Definition: text-utils.cc:63
bool StrToWeight(const std::string &s, bool allow_zero, W *w)
#define KALDI_ERR
Definition: kaldi-error.h:147
fst::StdArc::Weight Weight

◆ ReadFstKaldi() [4/4]

void fst::ReadFstKaldi ( std::string  rxfilename,
fst::StdVectorFst ofst 
)

Definition at line 109 of file kaldi-fst-io.cc.

References ReadFstKaldi().

109  {
110  fst::StdVectorFst *fst = ReadFstKaldi(rxfilename);
111  *ofst = *fst;
112  delete fst;
113 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
fst::StdVectorFst StdVectorFst
void ReadFstKaldi(std::string rxfilename, fst::StdVectorFst *ofst)

◆ ReadFstKaldiGeneric()

Fst< StdArc > * ReadFstKaldiGeneric ( std::string  rxfilename,
bool  throw_on_err 
)

Definition at line 45 of file kaldi-fst-io.cc.

References KALDI_ERR, KALDI_WARN, kaldi::PrintableRxfilename(), and Input::Stream().

Referenced by main(), and ReadAsConstFst().

45  {
46  if (rxfilename == "") rxfilename = "-"; // interpret "" as stdin,
47  // for compatibility with OpenFst conventions.
48  kaldi::Input ki(rxfilename);
49  fst::FstHeader hdr;
50  // Read FstHeader which contains the type of FST
51  if (!hdr.Read(ki.Stream(), rxfilename)) {
52  if(throw_on_err) {
53  KALDI_ERR << "Reading FST: error reading FST header from "
54  << kaldi::PrintableRxfilename(rxfilename);
55  } else {
56  KALDI_WARN << "We fail to read FST header from "
57  << kaldi::PrintableRxfilename(rxfilename)
58  << ". A NULL pointer is returned.";
59  return NULL;
60  }
61  }
62  // Check the type of Arc
63  if (hdr.ArcType() != fst::StdArc::Type()) {
64  if(throw_on_err) {
65  KALDI_ERR << "FST with arc type " << hdr.ArcType() << " is not supported.";
66  } else {
67  KALDI_WARN << "Fst with arc type" << hdr.ArcType()
68  << " is not supported. A NULL pointer is returned.";
69  return NULL;
70  }
71  }
72  // Read the FST
73  FstReadOptions ropts("<unspecified>", &hdr);
74  Fst<StdArc> *fst = NULL;
75  if (hdr.FstType() == "const") {
76  fst = ConstFst<StdArc>::Read(ki.Stream(), ropts);
77  } else if (hdr.FstType() == "vector") {
78  fst = VectorFst<StdArc>::Read(ki.Stream(), ropts);
79  }
80  if (!fst) {
81  if(throw_on_err) {
82  KALDI_ERR << "Could not read fst from "
83  << kaldi::PrintableRxfilename(rxfilename);
84  } else {
85  KALDI_WARN << "Could not read fst from "
86  << kaldi::PrintableRxfilename(rxfilename)
87  << ". A NULL pointer is returned.";
88  return NULL;
89  }
90  }
91  return fst;
92 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
std::string PrintableRxfilename(const std::string &rxfilename)
PrintableRxfilename turns the rxfilename into a more human-readable form for error reporting...
Definition: kaldi-io.cc:61

◆ ReadILabelInfo() [1/2]

void fst::ReadILabelInfo ( std::istream &  is,
bool  binary,
std::vector< std::vector< int32 > > *  ilabel_info 
)

Utility function for reading ilabel-info vectors from disk.

Definition at line 335 of file context-fst.cc.

References rnnlm::i, kaldi::ReadBasicType(), and kaldi::ReadIntegerVector().

Referenced by main(), and TestContextFst().

336  {
337  int32 size = info->size();
338  kaldi::ReadBasicType(is, binary, &size);
339  info->resize(size);
340  for (int32 i = 0; i < size; i++) {
341  kaldi::ReadIntegerVector(is, binary, &((*info)[i]));
342  }
343 }
void ReadBasicType(std::istream &is, bool binary, T *t)
ReadBasicType is the name of the read function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:55
kaldi::int32 int32
void ReadIntegerVector(std::istream &is, bool binary, std::vector< T > *v)
Function for reading STL vector of integer types.
Definition: io-funcs-inl.h:232

◆ ReadILabelInfo() [2/2]

void fst::ReadILabelInfo ( std::istream &  is,
bool  binary,
vector< vector< int32 > > *  info 
)

Utility function for reading ilabel-info vectors from disk.

Definition at line 335 of file context-fst.cc.

References rnnlm::i, kaldi::ReadBasicType(), and kaldi::ReadIntegerVector().

Referenced by main(), and TestContextFst().

336  {
337  int32 size = info->size();
338  kaldi::ReadBasicType(is, binary, &size);
339  info->resize(size);
340  for (int32 i = 0; i < size; i++) {
341  kaldi::ReadIntegerVector(is, binary, &((*info)[i]));
342  }
343 }
void ReadBasicType(std::istream &is, bool binary, T *t)
ReadBasicType is the name of the read function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:55
kaldi::int32 int32
void ReadIntegerVector(std::istream &is, bool binary, std::vector< T > *v)
Function for reading STL vector of integer types.
Definition: io-funcs-inl.h:232

◆ RemoveAlignmentsFromCompactLattice()

void RemoveAlignmentsFromCompactLattice ( MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, Int > > > *  fst)

Removes state-level alignments (the strings that are part of the weights).

Definition at line 222 of file lattice-utils-inl.h.

Referenced by LatticeScale(), main(), and MinimumBayesRisk::MinimumBayesRisk().

223  {
224  typedef CompactLatticeWeightTpl<Weight, Int> W;
225  typedef ArcTpl<W> Arc;
226  typedef MutableFst<Arc> Fst;
227  typedef typename Arc::StateId StateId;
228  StateId num_states = fst->NumStates();
229  for (StateId s = 0; s < num_states; s++) {
230  for (MutableArcIterator<Fst> aiter(fst, s);
231  !aiter.Done();
232  aiter.Next()) {
233  Arc arc = aiter.Value();
234  arc.weight = W(arc.weight.Weight(), std::vector<Int>());
235  aiter.SetValue(arc);
236  }
237  W final_weight = fst->Final(s);
238  if (final_weight != W::Zero())
239  fst->SetFinal(s, W(final_weight.Weight(), std::vector<Int>()));
240  }
241 }
fst::StdArc::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21

◆ RemoveArcsWithSomeInputSymbols()

void fst::RemoveArcsWithSomeInputSymbols ( const std::vector< I > &  symbols_in,
VectorFst< Arc > *  fst 
)

Definition at line 33 of file fstrmsymbols.cc.

References ConstIntegerSet< I >::count(), and KALDI_WARN.

Referenced by main().

34  {
35  typedef typename Arc::StateId StateId;
36 
37  kaldi::ConstIntegerSet<I> symbol_set(symbols_in);
38 
39  StateId num_states = fst->NumStates();
40  StateId dead_state = fst->AddState();
41  for (StateId s = 0; s < num_states; s++) {
42  for (MutableArcIterator<VectorFst<Arc> > iter(fst, s);
43  !iter.Done(); iter.Next()) {
44  if (symbol_set.count(iter.Value().ilabel) != 0) {
45  Arc arc = iter.Value();
46  arc.nextstate = dead_state;
47  iter.SetValue(arc);
48  }
49  }
50  }
51  // Connect() will actually remove the arcs, and the dead state.
52  Connect(fst);
53  if (fst->NumStates() == 0)
54  KALDI_WARN << "After Connect(), fst was empty.";
55 }
fst::StdArc::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
#define KALDI_WARN
Definition: kaldi-error.h:150

◆ RemoveEpsLocal()

void RemoveEpsLocal ( MutableFst< Arc > *  fst)

RemoveEpsLocal remove some (but not necessarily all) epsilons in an FST, using an algorithm that is guaranteed to never increase the number of arcs in the FST (and will also never increase the number of states).

The algorithm is not optimal but is reasonably clever. It does not just remove epsilon arcs;it also combines pairs of input-epsilon and output-epsilon arcs into one. The algorithm preserves equivalence and stochasticity in the given semiring. If you want to preserve stochasticity in a different semiring (e.g. log), then use RemoveEpsLocalSpecial, which only works for StdArc but which preserves stochasticity, where possible (*) in the LogArc sense. The reason that we can't just cast to a different semiring is that in that case we would no longer be able to guarantee equivalence in the original semiring (this arises from what happens when we combine identical arcs). (*) by "where possible".. there are situations where we wouldn't be able to preserve stochasticity in the LogArc sense while maintaining equivalence in the StdArc sense, so in these situations we maintain equivalence.

Definition at line 309 of file remove-eps-local-inl.h.

Referenced by TrainingGraphCompiler::CompileGraph(), TrainingGraphCompiler::CompileGraphs(), SimpleDecoder::GetBestPath(), FasterDecoder::GetBestPath(), BiglmFasterDecoder::GetBestPath(), kaldi::GetHmmAsFsa(), main(), OnlineFasterDecoder::MakeLattice(), ArpaLmCompiler::RemoveRedundantStates(), SafeDeterminizeMinimizeWrapper(), SafeDeterminizeMinimizeWrapperInLog(), TestRemoveEpsLocal(), and OnlineFasterDecoder::TracebackNFrames().

309  {
310  RemoveEpsLocalClass<Arc> c(fst); // work gets done in initializer.
311 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21

◆ RemoveEpsLocalSpecial()

void RemoveEpsLocalSpecial ( MutableFst< StdArc > *  fst)
inline

As RemoveEpsLocal but takes care to preserve stochasticity when cast to LogArc.

Definition at line 314 of file remove-eps-local-inl.h.

Referenced by main(), and TestRemoveEpsLocalSpecial().

314  {
315  // work gets done in initializer.
316  RemoveEpsLocalClass<StdArc, ReweightPlusLogArc> c(fst);
317 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21

◆ RemoveSomeInputSymbols()

void RemoveSomeInputSymbols ( const std::vector< I > &  to_remove,
MutableFst< Arc > *  fst 
)

RemoveSomeInputSymbols removes any symbol that appears in "to_remove", from the input side of the FST, replacing them with epsilon.

Definition at line 119 of file fstext-utils-inl.h.

References KALDI_ASSERT_IS_INTEGER_TYPE.

Referenced by TrainingGraphCompiler::CompileGraph(), TrainingGraphCompiler::CompileGraphs(), main(), MinimizeEncoded(), LatticeWordAligner::RemoveEpsilonsFromLattice(), LatticeLexiconWordAligner::RemoveEpsilonsFromLattice(), SafeDeterminizeMinimizeWrapper(), SafeDeterminizeWrapper(), and WordAlignedLatticeTester::TestEquivalent().

120  {
122  RemoveSomeInputSymbolsMapper<Arc, I> mapper(to_remove);
123  Map(fst, mapper);
124 }
#define KALDI_ASSERT_IS_INTEGER_TYPE(I)
Definition: kaldi-utils.h:133
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21

◆ RemoveUselessArcs()

void RemoveUselessArcs ( MutableFst< Arc > *  fst)

Definition at line 950 of file fstext-utils-inl.h.

References rnnlm::j, KALDI_ASSERT, and KALDI_VLOG.

Referenced by MinimizeEncoded(), and TestRemoveUselessArcs().

950  {
951  typedef typename Arc::Label Label;
952  typedef typename Arc::StateId StateId;
953  typedef typename Arc::Weight Weight;
954  NaturalLess<Weight> nl;
955  StateId non_coacc_state = kNoStateId;
956  size_t num_arcs_removed = 0, tot_arcs = 0;
957  for (StateIterator<MutableFst<Arc> > siter(*fst);
958  !siter.Done();
959  siter.Next()) {
960  std::vector<size_t> arcs_to_delete;
961  std::vector<Arc> arcs;
962  // pair2arclist lets us look up the arcs
963  std::map<std::pair<Label, StateId>, std::vector<size_t> > pair2arclist;
964  StateId state = siter.Value();
965  for (ArcIterator<MutableFst<Arc> > aiter(*fst, state);
966  !aiter.Done();
967  aiter.Next()) {
968  size_t pos = arcs.size();
969  const Arc &arc = aiter.Value();
970  arcs.push_back(arc);
971  pair2arclist[std::make_pair(arc.ilabel, arc.nextstate)].push_back(pos);
972  }
973  typename std::map<std::pair<Label, StateId>, std::vector<size_t> >::iterator
974  iter = pair2arclist.begin(), end = pair2arclist.end();
975  for (; iter!= end; ++iter) {
976  const std::vector<size_t> &poslist = iter->second;
977  if (poslist.size() > 1) { // >1 arc with same ilabel, dest-state
978  size_t best_pos = poslist[0];
979  Weight best_weight = arcs[best_pos].weight;
980  for (size_t j = 1; j < poslist.size(); j++) {
981  size_t pos = poslist[j];
982  Weight this_weight = arcs[pos].weight;
983  if (nl(this_weight, best_weight)) { // NaturalLess seems to be somehow
984  // "backwards".
985  best_weight = this_weight; // found a better one.
986  best_pos = pos;
987  }
988  }
989  for (size_t j = 0; j < poslist.size(); j++)
990  if (poslist[j] != best_pos)
991  arcs_to_delete.push_back(poslist[j]);
992  } else {
993  KALDI_ASSERT(poslist.size() == 1);
994  size_t pos = poslist[0];
995  Arc &arc = arcs[pos];
996  if (arc.ilabel == 0 && arc.nextstate == state)
997  arcs_to_delete.push_back(pos);
998  }
999  }
1000  tot_arcs += arcs.size();
1001  if (arcs_to_delete.size() != 0) {
1002  num_arcs_removed += arcs_to_delete.size();
1003  if (non_coacc_state == kNoStateId)
1004  non_coacc_state = fst->AddState();
1005  MutableArcIterator<MutableFst<Arc> > maiter(fst, state);
1006  for (size_t j = 0; j < arcs_to_delete.size(); j++) {
1007  size_t pos = arcs_to_delete[j];
1008  maiter.Seek(pos);
1009  arcs[pos].nextstate = non_coacc_state;
1010  maiter.SetValue(arcs[pos]);
1011  }
1012  }
1013  }
1014  if (non_coacc_state != kNoStateId)
1015  Connect(fst);
1016  KALDI_VLOG(1) << "removed " << num_arcs_removed << " of " << tot_arcs
1017  << "arcs.";
1018 }
fst::StdArc::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
fst::StdArc::Label Label
fst::StdArc::Weight Weight
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
#define KALDI_VLOG(v)
Definition: kaldi-error.h:156

◆ RemoveWeights()

void RemoveWeights ( MutableFst< Arc > *  ifst)

Definition at line 434 of file fstext-utils-inl.h.

Referenced by kaldi::DifferenceWrapper(), main(), MinimizeEncoded(), and TestAcceptorMinimize().

434  {
435  typedef typename Arc::StateId StateId;
436  typedef typename Arc::Weight Weight;
437 
438  for (StateIterator<MutableFst<Arc> > siter(*ifst); !siter.Done(); siter.Next()) {
439  StateId s = siter.Value();
440  for (MutableArcIterator<MutableFst<Arc> > aiter(ifst, s); !aiter.Done(); aiter.Next()) {
441  Arc arc(aiter.Value());
442  arc.weight = Weight::One();
443  aiter.SetValue(arc);
444  }
445  if (ifst->Final(s) != Weight::Zero())
446  ifst->SetFinal(s, Weight::One());
447  }
448  ifst->SetProperties(kUnweighted, kUnweighted);
449 }
fst::StdArc::StateId StateId
fst::StdArc::Weight Weight

◆ RhoCompose()

void RhoCompose ( const Fst< Arc > &  fst1,
const Fst< Arc > &  fst2,
typename Arc::Label  rho_label,
MutableFst< Arc > *  ofst 
)

Definition at line 1096 of file fstext-utils-inl.h.

References IsStochasticFst(), and KALDI_ASSERT.

Referenced by MinimizeEncoded().

1099  {
1100  KALDI_ASSERT(rho_label != kNoLabel); // just use regular compose in this case.
1101  typedef Fst<Arc> F;
1102  typedef RhoMatcher<SortedMatcher<F> > RM;
1103  CacheOptions base_opts;
1104  base_opts.gc_limit = 0; // Cache only the last state for fastest copy.
1105  // ComposeFstImplOptions templated on matcher for fst1, matcher for fst2.
1106  // The matcher for fst1 doesn't matter; we'll use fst2's matcher.
1107  ComposeFstImplOptions<SortedMatcher<F>, RM> impl_opts(base_opts);
1108 
1109  // the false below is something called rho_loop which is something I don't
1110  // fully understand, but I don't think we want it.
1111 
1112  // These pointers are taken ownership of, by ComposeFst.
1113  RM *rho_matcher =
1114  new RM(fst2, MATCH_INPUT, rho_label);
1115  SortedMatcher<F> *sorted_matcher =
1116  new SortedMatcher<F>(fst1, MATCH_NONE); // tell it
1117  // not to use this matcher, as this would mean we would
1118  // not follow rho transitions.
1119  impl_opts.matcher1 = sorted_matcher;
1120  impl_opts.matcher2 = rho_matcher;
1121  *ofst = ComposeFst<Arc>(fst1, fst2, impl_opts);
1122  Connect(ofst);
1123 }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ SafeDeterminizeMinimizeWrapper()

void SafeDeterminizeMinimizeWrapper ( MutableFst< Arc > *  ifst,
VectorFst< Arc > *  ofst,
float  delta = kDelta 
)

SafeDeterminizeMinimizeWapper is as SafeDeterminizeWrapper except that it also minimizes (encoded minimization, which is safe).

This algorithm will destroy "ifst".

Definition at line 357 of file fstext-utils-inl.h.

References DeterminizeStar(), HighestNumberedInputSymbol(), MinimizeEncoded(), PreDeterminize(), RemoveEpsLocal(), and RemoveSomeInputSymbols().

Referenced by MinimizeEncoded(), and TestSafeDeterminizeWrapper().

357  {
358  typename Arc::Label highest_sym = HighestNumberedInputSymbol(*ifst);
359  std::vector<typename Arc::Label> extra_syms;
360  PreDeterminize(ifst,
361  (typename Arc::Label)(highest_sym+1),
362  &extra_syms);
363  DeterminizeStar(*ifst, ofst, delta);
364  RemoveSomeInputSymbols(extra_syms, ofst); // remove the extra symbols.
365  RemoveEpsLocal(ofst); // this is "safe" and will never hurt.
366  MinimizeEncoded(ofst, delta);
367 }
void RemoveEpsLocal(MutableFst< Arc > *fst)
RemoveEpsLocal remove some (but not necessarily all) epsilons in an FST, using an algorithm that is g...
void PreDeterminize(MutableFst< Arc > *fst, typename Arc::Label first_new_sym, std::vector< Int > *symsOut)
void MinimizeEncoded(VectorFst< Arc > *fst, float delta=kDelta)
Definition: fstext-utils.h:114
fst::StdArc::Label Label
Arc::Label HighestNumberedInputSymbol(const Fst< Arc > &fst)
Returns the highest numbered input symbol id of the FST (or zero for an empty FST.
bool DeterminizeStar(F &ifst, MutableFst< typename F::Arc > *ofst, float delta, bool *debug_ptr, int max_states, bool allow_partial)
This function implements the normal version of DeterminizeStar, in which the output strings are repre...
void RemoveSomeInputSymbols(const std::vector< I > &to_remove, MutableFst< Arc > *fst)
RemoveSomeInputSymbols removes any symbol that appears in "to_remove", from the input side of the FST...

◆ SafeDeterminizeMinimizeWrapperInLog()

void SafeDeterminizeMinimizeWrapperInLog ( VectorFst< StdArc > *  ifst,
VectorFst< StdArc > *  ofst,
float  delta = kDelta 
)
inline

SafeDeterminizeMinimizeWapperInLog is as SafeDeterminizeMinimizeWrapper except it first casts tothe log semiring.

Definition at line 408 of file fstext-utils-inl.h.

References MinimizeEncoded(), RemoveEpsLocal(), and SafeDeterminizeWrapper().

Referenced by MinimizeEncoded(), and TestSafeDeterminizeWrapper().

408  {
409  VectorFst<LogArc> *ifst_log = new VectorFst<LogArc>; // Want to determinize in log semiring.
410  Cast(*ifst, ifst_log);
411  VectorFst<LogArc> *ofst_log = new VectorFst<LogArc>;
412  SafeDeterminizeWrapper(ifst_log, ofst_log, delta);
413  Cast(*ofst_log, ofst);
414  delete ifst_log;
415  delete ofst_log;
416  RemoveEpsLocal(ofst); // this is "safe" and will never hurt. Do this in tropical, which is important.
417  MinimizeEncoded(ofst, delta); // Non-deterministic minimization will fail in log semiring so do it with StdARc.
418 }
void RemoveEpsLocal(MutableFst< Arc > *fst)
RemoveEpsLocal remove some (but not necessarily all) epsilons in an FST, using an algorithm that is g...
void MinimizeEncoded(VectorFst< Arc > *fst, float delta=kDelta)
Definition: fstext-utils.h:114
void SafeDeterminizeWrapper(MutableFst< Arc > *ifst, MutableFst< Arc > *ofst, float delta)
Does PreDeterminize and DeterminizeStar and then removes the disambiguation symbols.

◆ SafeDeterminizeWrapper()

void SafeDeterminizeWrapper ( MutableFst< Arc > *  ifst,
MutableFst< Arc > *  ofst,
float  delta = kDelta 
)

Does PreDeterminize and DeterminizeStar and then removes the disambiguation symbols.

This is a form of determinization that will never blow up. Note that ifst is non-const and can be considered to be destroyed by this operation. Does not do epsilon removal (RemoveEpsLocal)– this is so it's safe to cast to log and do this, and maintain equivalence in tropical.

Definition at line 345 of file fstext-utils-inl.h.

References DeterminizeStar(), HighestNumberedInputSymbol(), PreDeterminize(), and RemoveSomeInputSymbols().

Referenced by MinimizeEncoded(), SafeDeterminizeMinimizeWrapperInLog(), SafeDeterminizeWrapperInLog(), and TestSafeDeterminizeWrapper().

345  {
346  typename Arc::Label highest_sym = HighestNumberedInputSymbol(*ifst);
347  std::vector<typename Arc::Label> extra_syms;
348  PreDeterminize(ifst,
349  (typename Arc::Label)(highest_sym+1),
350  &extra_syms);
351  DeterminizeStar(*ifst, ofst, delta);
352  RemoveSomeInputSymbols(extra_syms, ofst); // remove the extra symbols.
353 }
void PreDeterminize(MutableFst< Arc > *fst, typename Arc::Label first_new_sym, std::vector< Int > *symsOut)
fst::StdArc::Label Label
Arc::Label HighestNumberedInputSymbol(const Fst< Arc > &fst)
Returns the highest numbered input symbol id of the FST (or zero for an empty FST.
bool DeterminizeStar(F &ifst, MutableFst< typename F::Arc > *ofst, float delta, bool *debug_ptr, int max_states, bool allow_partial)
This function implements the normal version of DeterminizeStar, in which the output strings are repre...
void RemoveSomeInputSymbols(const std::vector< I > &to_remove, MutableFst< Arc > *fst)
RemoveSomeInputSymbols removes any symbol that appears in "to_remove", from the input side of the FST...

◆ SafeDeterminizeWrapperInLog()

void fst::SafeDeterminizeWrapperInLog ( VectorFst< StdArc > *  ifst,
VectorFst< StdArc > *  ofst,
float  delta 
)
inline

Definition at line 421 of file fstext-utils-inl.h.

References SafeDeterminizeWrapper().

421  {
422  VectorFst<LogArc> *ifst_log = new VectorFst<LogArc>; // Want to determinize in log semiring.
423  Cast(*ifst, ifst_log);
424  VectorFst<LogArc> *ofst_log = new VectorFst<LogArc>;
425  SafeDeterminizeWrapper(ifst_log, ofst_log, delta);
426  Cast(*ofst_log, ofst);
427  delete ifst_log;
428  delete ofst_log;
429 }
void SafeDeterminizeWrapper(MutableFst< Arc > *ifst, MutableFst< Arc > *ofst, float delta)
Does PreDeterminize and DeterminizeStar and then removes the disambiguation symbols.

◆ ScaleLattice()

void ScaleLattice ( const std::vector< std::vector< ScaleFloat > > &  scale,
MutableFst< ArcTpl< Weight > > *  fst 
)

Scales the pairs of weights in LatticeWeight or CompactLatticeWeight by viewing the pair (a, b) as a 2-vector and pre-multiplying by the 2x2 matrix in "scale".

E.g. typically scale would equal [ 1 0; 0 acwt ] if we want to scale the acoustics by "acwt".

Definition at line 197 of file lattice-utils-inl.h.

References DefaultLatticeScale(), and ScaleTupleWeight().

Referenced by DiscriminativeSupervisionSplitter::CreateRangeLattice(), kaldi::DecodeUtterance(), kaldi::DecodeUtteranceLatticeFaster(), kaldi::DecodeUtteranceLatticeIncremental(), kaldi::DecodeUtteranceLatticeSimple(), LatticeScale(), main(), DeterminizeLatticeTask::operator()(), DecodeUtteranceLatticeFasterClass::operator()(), DiscriminativeSupervisionSplitter::PrepareLattice(), NnetBatchDecoder::ProcessOutputUtterance(), and TestScalePair().

199  {
200  assert(scale.size() == 2 && scale[0].size() == 2 && scale[1].size() == 2);
201  if (scale == DefaultLatticeScale()) // nothing to do.
202  return;
203  typedef ArcTpl<Weight> Arc;
204  typedef MutableFst<Arc> Fst;
205  typedef typename Arc::StateId StateId;
206  StateId num_states = fst->NumStates();
207  for (StateId s = 0; s < num_states; s++) {
208  for (MutableArcIterator<Fst> aiter(fst, s);
209  !aiter.Done();
210  aiter.Next()) {
211  Arc arc = aiter.Value();
212  arc.weight = Weight(ScaleTupleWeight(arc.weight, scale));
213  aiter.SetValue(arc);
214  }
215  Weight final_weight = fst->Final(s);
216  if (final_weight != Weight::Zero())
217  fst->SetFinal(s, Weight(ScaleTupleWeight(final_weight, scale)));
218  }
219 }
fst::StdArc::StateId StateId
LatticeWeightTpl< FloatType > ScaleTupleWeight(const LatticeWeightTpl< FloatType > &w, const std::vector< std::vector< ScaleFloatType > > &scale)
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
fst::StdArc::Weight Weight
std::vector< std::vector< double > > DefaultLatticeScale()
Returns a default 2x2 matrix scaling factor for LatticeWeight.

◆ ScaleTupleWeight() [1/3]

LatticeWeightTpl<FloatType> fst::ScaleTupleWeight ( const LatticeWeightTpl< FloatType > &  w,
const std::vector< std::vector< ScaleFloatType > > &  scale 
)
inline

Definition at line 232 of file lattice-weight.h.

References LatticeWeightTpl< FloatType >::Value1(), and LatticeWeightTpl< FloatType >::Value2().

Referenced by ScaleLattice(), and ScaleTupleWeight().

234  {
235  // Without the next special case we'd get NaNs from infinity * 0
236  if (w.Value1() == std::numeric_limits<FloatType>::infinity())
237  return LatticeWeightTpl<FloatType>::Zero();
238  return LatticeWeightTpl<FloatType>(scale[0][0] * w.Value1() + scale[0][1] * w.Value2(),
239  scale[1][0] * w.Value1() + scale[1][1] * w.Value2());
240 }

◆ ScaleTupleWeight() [2/3]

PairWeight<TropicalWeightTpl<FloatType>, TropicalWeightTpl<FloatType> > fst::ScaleTupleWeight ( const PairWeight< TropicalWeightTpl< FloatType >, TropicalWeightTpl< FloatType > > &  w,
const std::vector< std::vector< ScaleFloatType > > &  scale 
)
inline

Definition at line 249 of file lattice-weight.h.

252  {
253  typedef TropicalWeightTpl<FloatType> BaseType;
254  typedef PairWeight<BaseType, BaseType> PairType;
255  const BaseType zero = BaseType::Zero();
256  // Without the next special case we'd get NaNs from infinity * 0
257  if (w.Value1() == zero || w.Value2() == zero)
258  return PairType(zero, zero);
259  FloatType f1 = w.Value1().Value(), f2 = w.Value2().Value();
260  return PairType(BaseType(scale[0][0] * f1 + scale[0][1] * f2),
261  BaseType(scale[1][0] * f1 + scale[1][1] * f2));
262 }

◆ ScaleTupleWeight() [3/3]

CompactLatticeWeightTpl<Weight, IntType> fst::ScaleTupleWeight ( const CompactLatticeWeightTpl< Weight, IntType > &  w,
const std::vector< std::vector< ScaleFloatType > > &  scale 
)
inline

Scales the pair (a, b) of floating-point weights inside a CompactLatticeWeight by premultiplying it (viewed as a vector) by a 2x2 matrix "scale".

Assumes there is a ScaleTupleWeight function that applies to "Weight"; this currently only works if Weight equals LatticeWeightTpl<FloatType> for some FloatType.

Definition at line 808 of file lattice-weight.h.

References ScaleTupleWeight(), CompactLatticeWeightTpl< WeightType, IntType >::String(), and CompactLatticeWeightTpl< WeightType, IntType >::Weight().

810  {
811  return CompactLatticeWeightTpl<Weight, IntType>(
812  Weight(ScaleTupleWeight(w.Weight(), scale)), w.String());
813 }
CompactLatticeWeightTpl< Weight, IntType > ScaleTupleWeight(const CompactLatticeWeightTpl< Weight, IntType > &w, const std::vector< std::vector< ScaleFloatType > > &scale)
Scales the pair (a, b) of floating-point weights inside a CompactLatticeWeight by premultiplying it (...
fst::StdArc::Weight Weight

◆ StrToWeight()

bool fst::StrToWeight ( const std::string &  s,
bool  allow_zero,
W *  w 
)
inline

Definition at line 63 of file kaldi-fst-io-inl.h.

Referenced by ReadFstKaldi().

63  {
64  std::istringstream strm(s);
65  strm >> *w;
66  if (strm.fail() || (!allow_zero && *w == W::Zero())) {
67  return false;
68  }
69  return true;
70 }

◆ TableCompose() [1/2]

void fst::TableCompose ( const Fst< Arc > &  ifst1,
const Fst< Arc > &  ifst2,
MutableFst< Arc > *  ofst,
const TableComposeOptions opts = TableComposeOptions() 
)

Definition at line 315 of file table-matcher.h.

Referenced by TrainingGraphCompiler::CompileGraph(), TrainingGraphCompiler::CompileGraphs(), main(), TestTableMatcher(), TestTableMatcherCacheLeft(), and TestTableMatcherCacheRight().

317  {
318  typedef Fst<Arc> F;
319  CacheOptions nopts;
320  nopts.gc_limit = 0; // Cache only the last state for fastest copy.
321  if (opts.table_match_type == MATCH_OUTPUT) {
322  // ComposeFstImplOptions templated on matcher for fst1, matcher for fst2.
323  ComposeFstImplOptions<TableMatcher<F>, SortedMatcher<F> > impl_opts(nopts);
324  impl_opts.matcher1 = new TableMatcher<F>(ifst1, MATCH_OUTPUT, opts);
325  *ofst = ComposeFst<Arc>(ifst1, ifst2, impl_opts);
326  } else {
327  assert(opts.table_match_type == MATCH_INPUT) ;
328  // ComposeFstImplOptions templated on matcher for fst1, matcher for fst2.
329  ComposeFstImplOptions<SortedMatcher<F>, TableMatcher<F> > impl_opts(nopts);
330  impl_opts.matcher2 = new TableMatcher<F>(ifst2, MATCH_INPUT, opts);
331  *ofst = ComposeFst<Arc>(ifst1, ifst2, impl_opts);
332  }
333  if (opts.connect) Connect(ofst);
334 }

◆ TableCompose() [2/2]

void fst::TableCompose ( const Fst< Arc > &  ifst1,
const Fst< Arc > &  ifst2,
MutableFst< Arc > *  ofst,
TableComposeCache< Fst< Arc > > *  cache 
)

Definition at line 348 of file table-matcher.h.

References TableMatcher< F, BackoffMatcher >::Copy().

350  {
351  typedef Fst<Arc> F;
352  assert(cache != NULL);
353  CacheOptions nopts;
354  nopts.gc_limit = 0; // Cache only the last state for fastest copy.
355  if (cache->opts.table_match_type == MATCH_OUTPUT) {
356  ComposeFstImplOptions<TableMatcher<F>, SortedMatcher<F> > impl_opts(nopts);
357  if (cache->matcher == NULL)
358  cache->matcher = new TableMatcher<F>(ifst1, MATCH_OUTPUT, cache->opts);
359  impl_opts.matcher1 = cache->matcher->Copy(); // not passing "safe": may not
360  // be thread-safe-- anway I don't understand this part.
361  *ofst = ComposeFst<Arc>(ifst1, ifst2, impl_opts);
362  } else {
363  assert(cache->opts.table_match_type == MATCH_INPUT) ;
364  ComposeFstImplOptions<SortedMatcher<F>, TableMatcher<F> > impl_opts(nopts);
365  if (cache->matcher == NULL)
366  cache->matcher = new TableMatcher<F>(ifst2, MATCH_INPUT, cache->opts);
367  impl_opts.matcher2 = cache->matcher->Copy();
368  *ofst = ComposeFst<Arc>(ifst1, ifst2, impl_opts);
369  }
370  if (cache->opts.connect) Connect(ofst);
371 }

◆ TestAcceptorMinimize()

void fst::TestAcceptorMinimize ( )

Definition at line 198 of file fstext-utils-test.cc.

References kaldi::Rand(), and RemoveWeights().

198  {
199  typedef typename Arc::Label Label;
200  typedef typename Arc::StateId StateId;
201  typedef typename Arc::Weight Weight;
202 
203  VectorFst<Arc> *fst = RandFst<Arc>();
204 
205  Project(fst, PROJECT_INPUT);
206  RemoveWeights(fst);
207 
208  VectorFst<Arc> fst2(*fst);
209  internal::AcceptorMinimize(&fst2);
210 
211  assert(RandEquivalent(*fst, fst2, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
212 
213  delete fst;
214 }
fst::StdArc::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
fst::StdArc::Label Label
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
fst::StdArc::Weight Weight
void RemoveWeights(MutableFst< Arc > *ifst)

◆ TestAddSelfLoops()

void fst::TestAddSelfLoops ( )

Definition at line 143 of file pre-determinize-test.cc.

References AddSelfLoops(), CreateNewSymbols(), and kaldi::Rand().

143  {
144  typedef typename Arc::Label Label;
145  typedef typename Arc::StateId StateId;
146  typedef typename Arc::Weight Weight;
147 
148  VectorFst<Arc> *fst = new VectorFst<Arc>();
149  SymbolTable *ilabels = new SymbolTable("my-symbol-table");
150  SymbolTable *olabels = new SymbolTable("my-symbol-table-2");
151  Label i0 = ilabels->AddSymbol("<eps>");
152  Label i1 = ilabels->AddSymbol("1");
153  Label i2 = ilabels->AddSymbol("2");
154 
155  Label o0 = olabels->AddSymbol("<eps>");
156  Label o1 = olabels->AddSymbol("1");
157 
158  assert(i0 == 0 && o0 == 0);
159  StateId s0 = fst->AddState(), s1 = fst->AddState(), s2 = fst->AddState();
160  fst->SetStart(s0);
161  assert(s0 == 0);
162 
163  fst->SetFinal(s2, (Weight)2); // state 2 is final.
164  {
165  Arc arc;
166  arc.ilabel = i1;
167  arc.olabel = o0;
168  arc.nextstate = 1;
169  arc.weight = (Weight)1;
170  fst->AddArc(s0, arc); // arc from 0 to 1 with epsilon out.
171  }
172  {
173  Arc arc;
174  arc.ilabel = i2;
175  arc.olabel = o1;
176  arc.nextstate = 2;
177  arc.weight = (Weight)2;
178  fst->AddArc(s1, arc); // arc from 1 to 2 with "1" out.
179  }
180  std::cout <<" printing before adding self-loops\n";
181  {
182  FstPrinter<Arc> fstprinter(*fst, ilabels, olabels, NULL, false, true, "\t");
183  fstprinter.Print(&std::cout, "standard output");
184  }
185 
186 
187  // So states 1 and 2 should have self-loops on.
188  size_t num_extra = kaldi::Rand() % 5;
189  vector<Label> extra_ilabels, extra_olabels;
190  CreateNewSymbols(ilabels, num_extra, "in#", &extra_ilabels);
191  CreateNewSymbols(olabels, num_extra, "out#", &extra_olabels);
192 
193  AddSelfLoops(fst, extra_ilabels, extra_olabels);
194 
195  assert(fst->NumArcs(0) == 1);
196  assert(fst->NumArcs(1) == 1 + num_extra);
197  assert(fst->NumArcs(2) == num_extra);
198 
199  std::cout <<" printing after adding self-loops\n";
200  {
201  FstPrinter<Arc> fstprinter(*fst, ilabels, olabels, NULL, false, true, "\t");
202  fstprinter.Print(&std::cout, "standard output");
203  }
204 
205  delete fst;
206  delete ilabels;
207  delete olabels;
208 }
fst::StdArc::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
void CreateNewSymbols(SymbolTable *input_sym_table, int nSym, std::string prefix, std::vector< Label > *symsOut)
void AddSelfLoops(MutableFst< Arc > *fst, std::vector< typename Arc::Label > &isyms, std::vector< typename Arc::Label > &osyms)
AddSelfLoops is a function you will probably want to use alongside PreDeterminize, to add self-loops to any FSTs that you compose on the left hand side of the one modified by PreDeterminize.
fst::StdArc::Label Label
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
fst::StdArc::Weight Weight

◆ TestBackoffAndCache()

void fst::TestBackoffAndCache ( )

Definition at line 148 of file deterministic-fst-test.cc.

References ApproxEqual(), CreateBackoffFst(), CreateResultFst(), CacheDeterministicOnDemandFst< Arc >::Final(), and CacheDeterministicOnDemandFst< Arc >::GetArc().

Referenced by main().

148  {
149  // Build from existing fst
150  cout << "Test with single generated backoff FST" << endl;
151  StdVectorFst *nfst = CreateBackoffFst();
152  StdVectorFst *rfst = CreateResultFst();
153 
154  // before using, make sure that it is input sorted
155  ArcSort(nfst, StdILabelCompare());
158 
159  // Compare all arcs in dfst1 with expected result
160  for (StateIterator<StdVectorFst> riter(*rfst); !riter.Done(); riter.Next()) {
161  StateId rsrc = riter.Value();
162  // verify that states have same weight (or final status)
163  assert(ApproxEqual(rfst->Final(rsrc), dfst1.Final(rsrc)));
164  for (ArcIterator<StdVectorFst> aiter(*rfst, rsrc); !aiter.Done(); aiter.Next()) {
165  StdArc rarc = aiter.Value();
166  StdArc darc;
167  if (dfst1.GetArc(rsrc, rarc.ilabel, &darc)) {
168  assert(ApproxEqual(rarc.weight, darc.weight, 0.001));
169  assert(rarc.ilabel==darc.ilabel);
170  assert(rarc.olabel==darc.olabel);
171  assert(rarc.nextstate == darc.nextstate);
172  cerr << " Got same arc at state "<<rsrc<<": "<<rarc.ilabel<<" "<<darc.ilabel<<endl;
173  } else {
174  cerr << "Couldn't find arc "<<rarc.ilabel<<" for state "<<rsrc<<endl;
175  exit(1);
176  }
177  }
178  }
179  delete nfst;
180  delete rfst;
181 }
This class wraps an Fst, representing a language model, using the interface for "BackoffDeterministic...
fst::StdArc::StateId StateId
fst::StdArc StdArc
fst::StdVectorFst StdVectorFst
StdVectorFst * CreateBackoffFst()
StdVectorFst * CreateResultFst()
static bool ApproxEqual(float a, float b, float relative_tolerance=0.001)
return abs(a - b) <= relative_tolerance * (abs(a)+abs(b)).
Definition: kaldi-math.h:265

◆ TestCompose()

void fst::TestCompose ( )

Definition at line 183 of file deterministic-fst-test.cc.

References ApproxEqual(), CreateBackoffFst(), CreateResultFst(), LmExampleDeterministicOnDemandFst< Arc >::Final(), LmExampleDeterministicOnDemandFst< Arc >::GetArc(), KALDI_ASSERT, CacheDeterministicOnDemandFst< Arc >::Start(), LmExampleDeterministicOnDemandFst< Arc >::Start(), and WalkSinglePath().

Referenced by main().

183  {
184  cout << "Test with single generated backoff FST" << endl;
185  StdVectorFst *nfst = CreateBackoffFst();
186  StdVectorFst *rfst = CreateResultFst();
187 
188  StdVectorFst composed_fst;
189  Compose(*rfst, *rfst, &composed_fst);
190 
191  // before using, make sure that it is input sorted
192  ArcSort(nfst, StdILabelCompare());
194  ComposeDeterministicOnDemandFst<StdArc> dfst1b(&dfst1a, &dfst1a);
196 
197  typedef StdArc::StateId StateId;
198  std::map<StateId, StateId> state_map;
199  state_map[composed_fst.Start()] = dfst1.Start();
200 
201  VectorFst<StdArc> path_fst;
202  ShortestPath(composed_fst, &path_fst);
203 
204  BackoffDeterministicOnDemandFst<StdArc> dfst2(composed_fst);
205 
206  Weight w1 = WalkSinglePath(&path_fst, &dfst1),
207  w2 = WalkSinglePath(&path_fst, &dfst2);
208  KALDI_ASSERT(ApproxEqual(w1, w2));
209 
210  delete rfst;
211  delete nfst;
212 
213  { // Mostly checking for compilation errors here.
215  KALDI_ASSERT(lm_eg.Start() == 0);
216  KALDI_ASSERT(lm_eg.Final(0).Value() == 0.5); // I made it this value.
217  StdArc arc;
218  bool b = lm_eg.GetArc(0, 100, &arc);
219  KALDI_ASSERT(b && arc.nextstate == 1 && arc.ilabel == 100 && arc.olabel == 100
220  && arc.weight.Value() == 0.25);
221  }
222 }
This class wraps an Fst, representing a language model, using the interface for "BackoffDeterministic...
fst::StdArc::StateId StateId
This class is for didactic purposes, it does not really do anything.
fst::StdArc StdArc
fst::StdVectorFst StdVectorFst
Weight WalkSinglePath(StdVectorFst *ifst, DeterministicOnDemandFst< StdArc > *dfst)
fst::StdArc::Weight Weight
StdVectorFst * CreateBackoffFst()
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
StdVectorFst * CreateResultFst()
static bool ApproxEqual(float a, float b, float relative_tolerance=0.001)
return abs(a - b) <= relative_tolerance * (abs(a)+abs(b)).
Definition: kaldi-math.h:265

◆ TestContextFst()

static void fst::TestContextFst ( bool  verbose,
bool  use_matcher 
)
static

Definition at line 158 of file context-fst-test.cc.

References kaldi::AssertEqual(), ComposeDeterministicOnDemandInverse(), kaldi::CopySetToVector(), rnnlm::i, InverseContextFst::IlabelInfo(), kaldi::Rand(), kaldi::RandUniform(), ReadILabelInfo(), Input::Stream(), and WriteILabelInfo().

Referenced by main().

158  {
159  typedef StdArc Arc;
160  typedef Arc::Label Label;
161  typedef Arc::StateId StateId;
162  typedef Arc::Weight Weight;
163 
164  // Generate a random set of phones.
165  size_t num_phones = 1 + kaldi::Rand() % 10;
166  std::set<int32> phones_set;
167  while (phones_set.size() < num_phones) phones_set.insert(1 + kaldi::Rand() % (num_phones + 5)); // don't use 0 [== epsilon]
168  vector<int32> phones;
169  kaldi::CopySetToVector(phones_set, &phones);
170 
171  int N = 1 + kaldi::Rand() % 4; // Context size, in range 1..4.
172  int P = kaldi::Rand() % N; // 1.. N-1.
173  if (verbose) std::cout << "N = "<< N << ", P = "<<P<<'\n';
174 
175  Label subsequential_symbol = 1000;
176  vector<int32> disambig_syms;
177  for (size_t i =0; i < 5; i++) disambig_syms.push_back(500 + i);
178  vector<int32> phone_syms;
179  for (size_t i = 0; i < phones.size();i++) phone_syms.push_back(phones[i]);
180 
181 
182  InverseContextFst inv_cfst(subsequential_symbol,
183  phones, disambig_syms,
184  N, P);
185 
186 
187  /* Now create random phone-sequences and compose them with the context FST.
188  */
189 
190  for (size_t p = 0; p < 10; p++) {
191  vector<int32> phone_seq;
192  int num_subseq = N - P - 1; // zero if P == N-1, i.e. P is last element, i.e. left-context only.
193  float tot_cost = 20.0 * kaldi::RandUniform();
194  VectorFst<Arc> *f = GenRandPhoneSeq<Arc>(phone_syms, disambig_syms, subsequential_symbol, num_subseq, tot_cost, &phone_seq);
195  if (verbose) {
196  std::cout << "Sequence FST is:\n";
197  { // Try to print the fst.
198  FstPrinter<Arc> fstprinter(*f, NULL, NULL, NULL, false, true, "\t");
199  fstprinter.Print(&std::cout, "standard output");
200  }
201  }
202 
203  VectorFst<Arc> fst_composed;
204 
205  ComposeDeterministicOnDemandInverse(*f, &inv_cfst, &fst_composed);
206 
207 
208  // Testing WriteILabelInfo and ReadILabelInfo.
209  {
210  bool binary = (kaldi::Rand() % 2 == 0);
211  WriteILabelInfo(kaldi::Output("tmpf", binary).Stream(),
212  binary, inv_cfst.IlabelInfo());
213 
214  bool binary_in;
215  vector<vector<int32> > ilabel_info;
216  kaldi::Input ki("tmpf", &binary_in);
217  ReadILabelInfo(ki.Stream(),
218  binary_in, &ilabel_info);
219  assert(ilabel_info == inv_cfst.IlabelInfo());
220  }
221 
222 
223  if (verbose) {
224  std::cout << "Composed FST is:\n";
225  { // Try to print the fst.
226  FstPrinter<Arc> fstprinter(fst_composed, NULL, NULL, NULL, false, true, "\t");
227  fstprinter.Print(&std::cout, "standard output");
228  }
229  }
230 
231  // now check the composed FST.
232  float tot_cost_check = CheckPhones<Arc>(fst_composed,
233  phone_syms,
234  disambig_syms,
235  phone_seq,
236  inv_cfst.IlabelInfo(),
237  N, P);
238  kaldi::AssertEqual(tot_cost, tot_cost_check);
239 
240  delete f;
241  }
242 
243  unlink("tmpf");
244 }
fst::StdArc::StateId StateId
void WriteILabelInfo(std::ostream &os, bool binary, const vector< vector< int32 > > &info)
Utility function for writing ilabel-info vectors to disk.
Definition: context-fst.cc:325
void CopySetToVector(const std::set< T > &s, std::vector< T > *v)
Copies the elements of a set to a vector.
Definition: stl-utils.h:86
float RandUniform(struct RandomState *state=NULL)
Returns a random number strictly between 0 and 1.
Definition: kaldi-math.h:151
fst::StdArc StdArc
void ComposeDeterministicOnDemandInverse(const Fst< Arc > &right, DeterministicOnDemandFst< Arc > *left, MutableFst< Arc > *fst_composed)
This function does &#39;*fst_composed = Compose(Inverse(*fst2), fst1)&#39; Note that the arguments are revers...
void ReadILabelInfo(std::istream &is, bool binary, vector< vector< int32 > > *info)
Utility function for reading ilabel-info vectors from disk.
Definition: context-fst.cc:335
fst::StdArc::Label Label
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
fst::StdArc::Weight Weight
static void AssertEqual(float a, float b, float relative_tolerance=0.001)
assert abs(a - b) <= relative_tolerance * (abs(a)+abs(b))
Definition: kaldi-math.h:276

◆ TestConvert()

void fst::TestConvert ( bool  invert)

Definition at line 26 of file lattice-utils-test.cc.

References rnnlm::i, and kaldi::Rand().

26  {
27  typedef ArcTpl<Weight> Arc;
28  typedef ArcTpl<CompactLatticeWeightTpl<Weight, Int> > CompactArc;
29  for(int i = 0; i < 5; i++) {
30  VectorFst<Arc> *fst = RandFst<Arc>();
31  std::cout << "FST before converting to compact-arc is:\n";
32  {
33  FstPrinter<Arc> fstprinter(*fst, NULL, NULL, NULL, false, true, "\t");
34  fstprinter.Print(&std::cout, "standard output");
35  }
36  VectorFst<CompactArc> ofst;
37  ConvertLattice<Weight, Int>(*fst, &ofst, invert);
38 
39  std::cout << "FST after converting is:\n";
40  {
41  FstPrinter<CompactArc> fstprinter(ofst, NULL, NULL, NULL, false, true, "\t");
42  fstprinter.Print(&std::cout, "standard output");
43  }
44  VectorFst<Arc> origfst;
45  ConvertLattice<Weight, Int>(ofst, &origfst, invert);
46  std::cout << "FST after back conversion is:\n";
47  {
48  FstPrinter<Arc> fstprinter(origfst, NULL, NULL, NULL, false, true, "\t");
49  fstprinter.Print(&std::cout, "standard output");
50  }
51 
52  assert(RandEquivalent(*fst, origfst, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
53  delete fst;
54  }
55 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45

◆ TestConvert2()

void fst::TestConvert2 ( )

Definition at line 114 of file lattice-utils-test.cc.

References ConvertLattice(), rnnlm::i, and kaldi::Rand().

114  {
115  typedef ArcTpl<LatticeWeightTpl<float> > ArcF;
116  typedef ArcTpl<LatticeWeightTpl<double> > ArcD;
117  typedef ArcTpl<CompactLatticeWeightTpl<LatticeWeightTpl<float>, Int> > CArcF;
118  typedef ArcTpl<CompactLatticeWeightTpl<LatticeWeightTpl<double>, Int> > CArcD;
119 
120  for(int i = 0; i < 2; i++) {
121  {
122  VectorFst<ArcF> *fst1 = RandPairFst<ArcF>();
123  VectorFst<ArcD> fst2;
124  VectorFst<ArcF> fst3;
125  ConvertLattice(*fst1, &fst2);
126  ConvertLattice(fst2, &fst3);
127 
128  assert(RandEquivalent(*fst1, fst3, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
129  delete fst1;
130  }
131 
132  {
133  VectorFst<ArcF> *fst1 = RandPairFst<ArcF>();
134  VectorFst<CArcF> cfst1, cfst3;
135  ConvertLattice(*fst1, &cfst1);
136  VectorFst<CArcD> cfst2;
137  ConvertLattice(cfst1, &cfst2);
138  ConvertLattice(cfst2, &cfst3);
139  assert(RandEquivalent(cfst1, cfst3, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
140  delete fst1;
141  }
142 
143  {
144  VectorFst<ArcF> *fst1 = RandPairFst<ArcF>();
145  VectorFst<CArcD> cfst1, cfst3;
146  ConvertLattice(*fst1, &cfst1);
147  VectorFst<CArcF> cfst2;
148  ConvertLattice(cfst1, &cfst2);
149  ConvertLattice(cfst2, &cfst3);
150  assert(RandEquivalent(cfst1, cfst3, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
151  delete fst1;
152  }
153 
154  {
155  VectorFst<ArcD> *fst1 = RandPairFst<ArcD>();
156  VectorFst<CArcD> cfst1, cfst3;
157  ConvertLattice(*fst1, &cfst1);
158  VectorFst<CArcF> cfst2;
159  ConvertLattice(cfst1, &cfst2);
160  ConvertLattice(cfst2, &cfst3);
161  assert(RandEquivalent(cfst1, cfst3, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
162  delete fst1;
163  }
164 
165  {
166  VectorFst<ArcD> *fst1 = RandPairFst<ArcD>();
167  VectorFst<CArcF> cfst1;
168  ConvertLattice(*fst1, &cfst1);
169  VectorFst<ArcD> fst2;
170  ConvertLattice(cfst1, &fst2);
171  assert(RandEquivalent(*fst1, fst2, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
172  delete fst1;
173  }
174 
175  {
176  VectorFst<ArcF> *fst1 = RandPairFst<ArcF>();
177  VectorFst<CArcD> cfst1;
178  ConvertLattice(*fst1, &cfst1);
179  VectorFst<ArcF> fst2;
180  ConvertLattice(cfst1, &fst2);
181  assert(RandEquivalent(*fst1, fst2, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
182  delete fst1;
183  }
184 
185  {
186  VectorFst<ArcD> *fst1 = RandPairFst<ArcD>();
187  VectorFst<CArcF> cfst1;
188  ConvertLattice(*fst1, &cfst1);
189  VectorFst<ArcD> fst2;
190  ConvertLattice(cfst1, &fst2);
191  assert(RandEquivalent(*fst1, fst2, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
192  delete fst1;
193  }
194  }
195 }
void ConvertLattice(const ExpandedFst< ArcTpl< Weight > > &ifst, MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, Int > > > *ofst, bool invert)
Convert lattice from a normal FST to a CompactLattice FST.
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45

◆ TestConvertPair()

void fst::TestConvertPair ( bool  invert)

Definition at line 200 of file lattice-utils-test.cc.

References rnnlm::i, and kaldi::Rand().

200  {
201  typedef ArcTpl<Weight> Arc;
202  typedef ArcTpl<CompactLatticeWeightTpl<Weight, Int> > CompactArc;
203  for(int i = 0; i < 2; i++) {
204  VectorFst<Arc> *fst = RandPairFst<Arc>();
205  /*std::cout << "FST before converting to compact-arc is:\n";
206  {
207  FstPrinter<Arc> fstprinter(*fst, NULL, NULL, NULL, false, true);
208  fstprinter.Print(&std::cout, "standard output");
209  }*/
210  VectorFst<CompactArc> ofst;
211  ConvertLattice<Weight, Int>(*fst, &ofst, invert);
212 
213  /*std::cout << "FST after converting is:\n";
214  {
215  FstPrinter<CompactArc> fstprinter(ofst, NULL, NULL, NULL, false, true);
216  fstprinter.Print(&std::cout, "standard output");
217  }*/
218  VectorFst<Arc> origfst;
219  ConvertLattice<Weight, Int>(ofst, &origfst, invert);
220  /*std::cout << "FST after back conversion is:\n";
221  {
222  FstPrinter<Arc> fstprinter(origfst, NULL, NULL, NULL, false, true);
223  fstprinter.Print(&std::cout, "standard output");
224  }*/
225 
226  assert(RandEquivalent(*fst, origfst, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
227  delete fst;
228  }
229 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45

◆ TestDeterminize()

void fst::TestDeterminize ( )

Definition at line 61 of file determinize-star-test.cc.

References DeleteISymbols(), DeterminizeStar(), rnnlm::i, rnnlm::j, PreDeterminize(), and kaldi::Rand().

61  {
62  typedef typename Arc::Label Label;
63  typedef typename Arc::StateId StateId;
64  typedef typename Arc::Weight Weight;
65 
66  VectorFst<Arc> *fst = new VectorFst<Arc>();
67  int n_syms = 2 + kaldi::Rand() % 5, n_states = 3 + kaldi::Rand() % 10, n_arcs = 5 + kaldi::Rand() % 30, n_final = 1 + kaldi::Rand()%3; // Up to 2 unique symbols.
68  std::cout << "Testing pre-determinize with "<<n_syms<<" symbols, "<<n_states<<" states and "<<n_arcs<<" arcs and "<<n_final<<" final states.\n";
69  SymbolTable *sptr = NULL;
70 
71  std::vector<Label> all_syms; // including epsilon.
72  // Put symbols in the symbol table from 1..n_syms-1.
73  for (size_t i = 0;i < (size_t)n_syms;i++)
74  all_syms.push_back(i);
75 
76  // Create states.
77  std::vector<StateId> all_states;
78  for (size_t i = 0;i < (size_t)n_states;i++) {
79  StateId this_state = fst->AddState();
80  if (i == 0) fst->SetStart(i);
81  all_states.push_back(this_state);
82  }
83  // Set final states.
84  for (size_t j = 0;j < (size_t)n_final;j++) {
85  StateId id = all_states[kaldi::Rand() % n_states];
86  Weight weight = (Weight)(0.33*(kaldi::Rand() % 5) );
87  printf("calling SetFinal with %d and %f\n", id, weight.Value());
88  fst->SetFinal(id, weight);
89  }
90  // Create arcs.
91  for (size_t i = 0;i < (size_t)n_arcs;i++) {
92  Arc a;
93  a.nextstate = all_states[kaldi::Rand() % n_states];
94  a.ilabel = all_syms[kaldi::Rand() % n_syms];
95  a.olabel = all_syms[kaldi::Rand() % n_syms]; // same input+output vocab.
96  a.weight = (Weight) (0.33*(kaldi::Rand() % 2));
97  StateId start_state = all_states[kaldi::Rand() % n_states];
98  fst->AddArc(start_state, a);
99  }
100 
101  std::cout <<" printing before trimming\n";
102  {
103  FstPrinter<Arc> fstprinter(*fst, sptr, sptr, NULL, false, true, "\t");
104  fstprinter.Print(&std::cout, "standard output");
105  }
106  // Trim resulting FST.
107  Connect(fst);
108 
109  std::cout <<" printing after trimming\n";
110  {
111  FstPrinter<Arc> fstprinter(*fst, sptr, sptr, NULL, false, true, "\t");
112  fstprinter.Print(&std::cout, "standard output");
113  }
114 
115  VectorFst<Arc> *fst_copy_orig = new VectorFst<Arc>(*fst);
116 
117  std::vector<Label> extra_syms;
118  if (fst->Start() != kNoStateId) { // "Connect" did not make it empty....
119  PreDeterminize(fst, 1000, &extra_syms);
120  }
121 
122  std::cout <<" printing after predeterminization\n";
123  {
124  FstPrinter<Arc> fstprinter(*fst, sptr, sptr, NULL, false, true, "\t");
125  fstprinter.Print(&std::cout, "standard output");
126  }
127 
128 
129  { // Remove epsilon. All default args.
130  bool connect = true;
131  Weight weight_threshold = Weight::Zero();
132  int64 nstate = -1; // Relates to pruning.
133  double delta = kDelta; // I think a small weight value. Relates to some kind of pruning,
134  // I guess. But with no epsilon cycles, probably doensn't matter.
135  RmEpsilon(fst, connect, weight_threshold, nstate, delta);
136  }
137 
138  std::cout <<" printing after epsilon removal\n";
139  {
140  FstPrinter<Arc> fstprinter(*fst, sptr, sptr, NULL, false, true, "\t");
141  fstprinter.Print(&std::cout, "standard output");
142  }
143  VectorFst<Arc> ofst_orig;
144  VectorFst<Arc> ofst_star;
145 
146  {
147  printf("Determinizing with baseline\n");
148  DeterminizeOptions<Arc> opts; // Default options.
149  Determinize(*fst, &ofst_orig, opts);
150  }
151 
152  {
153  printf("Determinizing with DeterminizeStar\n");
154  DeterminizeStar(*fst, &ofst_star);
155  }
156 
157  {
158  std::cout <<" printing after determinization [baseline]\n";
159  FstPrinter<Arc> fstprinter(ofst_orig, sptr, sptr, NULL, false, true, "\t");
160  fstprinter.Print(&std::cout, "standard output");
161  assert(ofst_orig.Properties(kIDeterministic, true) == kIDeterministic);
162  }
163 
164  {
165  std::cout <<" printing after determinization [star]\n";
166  FstPrinter<Arc> fstprinter(ofst_star, sptr, sptr, NULL, false, true, "\t");
167  fstprinter.Print(&std::cout, "standard output");
168  assert(ofst_star.Properties(kIDeterministic, true) == kIDeterministic);
169  }
170 
171  assert(RandEquivalent(ofst_orig, ofst_star, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
172 
173  int64 num_removed = DeleteISymbols(&ofst_star, extra_syms);
174  std::cout <<" printing after removing "<<num_removed<<" instances of extra symbols\n";
175  {
176  FstPrinter<Arc> fstprinter(ofst_star, sptr, sptr, NULL, false, true, "\t");
177  fstprinter.Print(&std::cout, "standard output");
178  }
179 
180  std::cout <<" Checking equivalent to original FST.\n";
181  // giving Rand() as a seed stops the random number generator from always being reset to
182  // the same point each time, while maintaining determinism of the test.
183  assert(RandEquivalent(ofst_star, *fst_copy_orig, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
184 
185  delete fst;
186  delete fst_copy_orig;
187 }
fst::StdArc::StateId StateId
void PreDeterminize(MutableFst< Arc > *fst, typename Arc::Label first_new_sym, std::vector< Int > *symsOut)
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
fst::StdArc::Label Label
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
fst::StdArc::Weight Weight
int64 DeleteISymbols(MutableFst< Arc > *fst, std::vector< typename Arc::Label > isyms)
bool DeterminizeStar(F &ifst, MutableFst< typename F::Arc > *ofst, float delta, bool *debug_ptr, int max_states, bool allow_partial)
This function implements the normal version of DeterminizeStar, in which the output strings are repre...

◆ TestDeterminize2()

void fst::TestDeterminize2 ( )

Definition at line 190 of file determinize-star-test.cc.

References RandFstOptions::acyclic, rnnlm::i, and kaldi::Rand().

190  {
191  for(int i = 0; i < 10; i++) {
192  RandFstOptions opts;
193  opts.acyclic = true;
194  VectorFst<Arc> *ifst = RandFst<Arc>(opts);
195  VectorFst<Arc> ofst;
196  Determinize(*ifst, &ofst);
197  assert(RandEquivalent(*ifst, ofst, 5, 0.01, kaldi::Rand(), 100));
198  delete ifst;
199  }
200 }
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45

◆ TestDeterminizeGeneral()

void fst::TestDeterminizeGeneral ( )

Definition at line 34 of file determinize-star-test.cc.

References rnnlm::i, and kaldi::Rand().

34  {
35  int max_states = 100; // don't allow more det-states than this.
36  for(int i = 0; i < 100; i++) {
37  VectorFst<Arc> *fst = RandFst<Arc>();
38  std::cout << "FST before determinizing is:\n";
39  {
40  FstPrinter<Arc> fstprinter(*fst, NULL, NULL, NULL, false, true, "\t");
41  fstprinter.Print(&std::cout, "standard output");
42  }
43  VectorFst<Arc> ofst;
44  try {
45  DeterminizeStar<Fst<Arc> >(*fst, &ofst, kDelta, NULL, max_states);
46  std::cout << "FST after determinizing is:\n";
47  {
48  FstPrinter<Arc> fstprinter(ofst, NULL, NULL, NULL, false, true, "\t");
49  fstprinter.Print(&std::cout, "standard output");
50  }
51  assert(RandEquivalent(*fst, ofst, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length, max*/));
52  } catch (...) {
53  std::cout << "Failed to determinize *this FST (probably not determinizable)\n";
54  }
55  delete fst;
56  }
57 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45

◆ TestDeterminizeLattice()

void fst::TestDeterminizeLattice ( )

Definition at line 76 of file determinize-lattice-test.cc.

References RandFstOptions::allow_empty, rnnlm::i, DeterminizeLatticeOptions::max_mem, RandFstOptions::n_arcs, RandFstOptions::n_final, RandFstOptions::n_states, kaldi::Rand(), and RandFstOptions::weight_multiplier.

76  {
77  typedef typename Arc::Weight Weight;
78  typedef int32 Int;
79  typedef ArcTpl<CompactLatticeWeightTpl<Weight, Int> > CompactArc;
80 
81  for(int i = 0; i < 100; i++) {
82  RandFstOptions opts;
83  opts.n_states = 4;
84  opts.n_arcs = 10;
85  opts.n_final = 2;
86  opts.allow_empty = false;
87  opts.weight_multiplier = 0.5; // impt for the randomly generated weights
88  // to be exactly representable in float,
89  // or this test fails because numerical differences can cause symmetry in
90  // weights to be broken, which causes the wrong path to be chosen as far
91  // as the string part is concerned.
92 
93  VectorFst<Arc> *fst = RandFst<Arc>();
94  std::cout << "FST before lattice-determinizing is:\n";
95  {
96  FstPrinter<Arc> fstprinter(*fst, NULL, NULL, NULL, false, true, "\t");
97  fstprinter.Print(&std::cout, "standard output");
98  }
99  VectorFst<Arc> det_fst;
100  try {
101  DeterminizeLatticeOptions lat_opts;
102  lat_opts.max_mem = 100;
103 
104  if (!DeterminizeLattice<TropicalWeight, int32>(*fst, &det_fst, lat_opts, NULL))
105  throw std::runtime_error("could not determinize");
106  std::cout << "FST after lattice-determinizing is:\n";
107  {
108  FstPrinter<Arc> fstprinter(det_fst, NULL, NULL, NULL, false, true, "\t");
109  fstprinter.Print(&std::cout, "standard output");
110  }
111  assert(det_fst.Properties(kIDeterministic, true) & kIDeterministic);
112  // OK, now determinize it a different way and check equivalence.
113  // [note: it's not normal determinization, it's taking the best path
114  // for any input-symbol sequence....
115  VectorFst<CompactArc> compact_fst, compact_det_fst;
116  ConvertLattice<Weight, Int>(*fst, &compact_fst, false);
117  std::cout << "Compact FST is:\n";
118  {
119  FstPrinter<CompactArc> fstprinter(compact_fst, NULL, NULL, NULL, false, true, "\t");
120  fstprinter.Print(&std::cout, "standard output");
121  }
122  if (kaldi::Rand() % 2 == 1)
123  ConvertLattice<Weight, Int>(det_fst, &compact_det_fst, false);
124  else
125  if (!DeterminizeLattice<TropicalWeight, int32>(*fst, &compact_det_fst, lat_opts, NULL))
126  throw std::runtime_error("could not determinize");
127 
128  std::cout << "Compact version of determinized FST is:\n";
129  {
130  FstPrinter<CompactArc> fstprinter(compact_det_fst, NULL, NULL, NULL, false, true, "\t");
131  fstprinter.Print(&std::cout, "standard output");
132  }
133 
134  assert(RandEquivalent(compact_det_fst, compact_fst, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length, max*/));
135  } catch (...) {
136  std::cout << "Failed to lattice-determinize this FST (probably not determinizable)\n";
137  }
138  delete fst;
139  }
140 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
kaldi::int32 int32
float weight_multiplier
Definition: rand-fst.h:41
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
fst::StdArc::Weight Weight

◆ TestDeterminizeLattice2()

void fst::TestDeterminizeLattice2 ( )

Definition at line 144 of file determinize-lattice-test.cc.

References RandFstOptions::acyclic, and rnnlm::i.

144  {
145  RandFstOptions opts;
146  opts.acyclic = true;
147  for(int i = 0; i < 100; i++) {
148  VectorFst<Arc> *fst = RandFst<Arc>(opts);
149  std::cout << "FST before lattice-determinizing is:\n";
150  {
151  FstPrinter<Arc> fstprinter(*fst, NULL, NULL, NULL, false, true, "\t");
152  fstprinter.Print(&std::cout, "standard output");
153  }
154  VectorFst<Arc> ofst;
155  DeterminizeLattice<TropicalWeight, int32>(*fst, &ofst);
156  std::cout << "FST after lattice-determinizing is:\n";
157  {
158  FstPrinter<Arc> fstprinter(ofst, NULL, NULL, NULL, false, true, "\t");
159  fstprinter.Print(&std::cout, "standard output");
160  }
161  delete fst;
162  }
163 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21

◆ TestDeterminizeLatticePruned()

void fst::TestDeterminizeLatticePruned ( )

Definition at line 36 of file determinize-lattice-pruned-test.cc.

References RandFstOptions::acyclic, RandFstOptions::allow_empty, rnnlm::i, KALDI_ASSERT, DeterminizeLatticePrunedOptions::max_arcs, DeterminizeLatticePrunedOptions::max_mem, DeterminizeLatticePrunedOptions::max_states, RandFstOptions::n_arcs, RandFstOptions::n_final, RandFstOptions::n_states, kaldi::PruneLattice(), kaldi::Rand(), and RandFstOptions::weight_multiplier.

36  {
37  typedef kaldi::int32 Int;
38  typedef typename Arc::Weight Weight;
39  typedef ArcTpl<CompactLatticeWeightTpl<Weight, Int> > CompactArc;
40 
41  for(int i = 0; i < 100; i++) {
42  RandFstOptions opts;
43  opts.n_states = 4;
44  opts.n_arcs = 10;
45  opts.n_final = 2;
46  opts.allow_empty = false;
47  opts.weight_multiplier = 0.5; // impt for the randomly generated weights
48  opts.acyclic = true;
49  // to be exactly representable in float,
50  // or this test fails because numerical differences can cause symmetry in
51  // weights to be broken, which causes the wrong path to be chosen as far
52  // as the string part is concerned.
53 
54  VectorFst<Arc> *fst = RandPairFst<Arc>(opts);
55 
56  bool sorted = TopSort(fst);
57  KALDI_ASSERT(sorted);
58 
59  ILabelCompare<Arc> ilabel_comp;
60  if (kaldi::Rand() % 2 == 0)
61  ArcSort(fst, ilabel_comp);
62 
63  std::cout << "FST before lattice-determinizing is:\n";
64  {
65  FstPrinter<Arc> fstprinter(*fst, NULL, NULL, NULL, false, true, "\t");
66  fstprinter.Print(&std::cout, "standard output");
67  }
68  VectorFst<Arc> det_fst;
69  try {
71  lat_opts.max_mem = ((kaldi::Rand() % 2 == 0) ? 100 : 1000);
72  lat_opts.max_states = ((kaldi::Rand() % 2 == 0) ? -1 : 20);
73  lat_opts.max_arcs = ((kaldi::Rand() % 2 == 0) ? -1 : 30);
74  bool ans = DeterminizeLatticePruned<Weight>(*fst, 10.0, &det_fst, lat_opts);
75 
76  std::cout << "FST after lattice-determinizing is:\n";
77  {
78  FstPrinter<Arc> fstprinter(det_fst, NULL, NULL, NULL, false, true, "\t");
79  fstprinter.Print(&std::cout, "standard output");
80  }
81  KALDI_ASSERT(det_fst.Properties(kIDeterministic, true) & kIDeterministic);
82  // OK, now determinize it a different way and check equivalence.
83  // [note: it's not normal determinization, it's taking the best path
84  // for any input-symbol sequence....
85 
86 
87  VectorFst<Arc> pruned_fst(*fst);
88  if (pruned_fst.NumStates() != 0)
89  kaldi::PruneLattice(10.0, &pruned_fst);
90 
91  VectorFst<CompactArc> compact_pruned_fst, compact_pruned_det_fst;
92  ConvertLattice<Weight, Int>(pruned_fst, &compact_pruned_fst, false);
93  std::cout << "Compact pruned FST is:\n";
94  {
95  FstPrinter<CompactArc> fstprinter(compact_pruned_fst, NULL, NULL, NULL, false, true, "\t");
96  fstprinter.Print(&std::cout, "standard output");
97  }
98  ConvertLattice<Weight, Int>(det_fst, &compact_pruned_det_fst, false);
99 
100  std::cout << "Compact version of determinized FST is:\n";
101  {
102  FstPrinter<CompactArc> fstprinter(compact_pruned_det_fst, NULL, NULL, NULL, false, true, "\t");
103  fstprinter.Print(&std::cout, "standard output");
104  }
105 
106  if (ans)
107  KALDI_ASSERT(RandEquivalent(compact_pruned_det_fst, compact_pruned_fst, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length, max*/));
108  } catch (...) {
109  std::cout << "Failed to lattice-determinize this FST (probably not determinizable)\n";
110  }
111  delete fst;
112  }
113 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
kaldi::int32 int32
float weight_multiplier
Definition: rand-fst.h:41
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
fst::StdArc::Weight Weight
bool PruneLattice(BaseFloat beam, LatType *lat)
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ TestDeterminizeLatticePruned2()

void fst::TestDeterminizeLatticePruned2 ( )

Definition at line 117 of file determinize-lattice-pruned-test.cc.

References RandFstOptions::acyclic, and rnnlm::i.

117  {
118  typedef typename Arc::Weight Weight;
119  RandFstOptions opts;
120  opts.acyclic = true;
121  for(int i = 0; i < 100; i++) {
122  VectorFst<Arc> *fst = RandPairFst<Arc>(opts);
123  std::cout << "FST before lattice-determinizing is:\n";
124  {
125  FstPrinter<Arc> fstprinter(*fst, NULL, NULL, NULL, false, true, "\t");
126  fstprinter.Print(&std::cout, "standard output");
127  }
128  VectorFst<Arc> ofst;
129  DeterminizeLatticePruned<Weight>(*fst, 10.0, &ofst);
130  std::cout << "FST after lattice-determinizing is:\n";
131  {
132  FstPrinter<Arc> fstprinter(ofst, NULL, NULL, NULL, false, true, "\t");
133  fstprinter.Print(&std::cout, "standard output");
134  }
135  delete fst;
136  }
137 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
fst::StdArc::Weight Weight

◆ TestDeterminizeStarInLog()

void fst::TestDeterminizeStarInLog ( )

Definition at line 79 of file fstext-utils-test.cc.

References HighestNumberedInputSymbol(), and PreDeterminize().

79  {
80  VectorFst<Arc> *fst = RandFst<Arc>();
81  VectorFst<Arc> fst_copy(fst);
82  typename Arc::Label next_sym = 1 + HighestNumberedInputSymbol(*fst);
83  vector<typename Arc::Label> syms;
84  PreDeterminize(fst, NULL, "#", next_sym, &syms);
85 
86 
87 }
void PreDeterminize(MutableFst< Arc > *fst, typename Arc::Label first_new_sym, std::vector< Int > *symsOut)
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
fst::StdArc::Label Label
Arc::Label HighestNumberedInputSymbol(const Fst< Arc > &fst)
Returns the highest numbered input symbol id of the FST (or zero for an empty FST.

◆ TestEnsureEpsilonProperty()

void fst::TestEnsureEpsilonProperty ( )

Definition at line 27 of file epsilon-property-test.cc.

References RandFstOptions::acyclic, ComputeStateInfo(), EnsureEpsilonProperty(), rnnlm::i, kStateHasEpsilonArcsEntering, kStateHasEpsilonArcsLeaving, kStateHasNonEpsilonArcsEntering, kStateHasNonEpsilonArcsLeaving, and kaldi::Rand().

Referenced by main().

27  {
28 
29  for (int32 i = 0; i < 10; i++) {
30  RandFstOptions opts;
31  opts.acyclic = true;
32  VectorFst<LogArc> *fst = RandFst<LogArc>(opts);
33  VectorFst<LogArc> fst2(*fst); // copy it...
34  EnsureEpsilonProperty(&fst2);
35 
36  std::vector<char> info;
37  ComputeStateInfo(fst2, &info);
38  for (size_t i = 0; i < info.size(); i++) {
39  char c = info[i];
40  assert(!((c & kStateHasEpsilonArcsEntering) != 0 &&
42  assert(!((c & kStateHasEpsilonArcsLeaving) != 0 &&
43  (c & kStateHasNonEpsilonArcsLeaving) != 0));
44  }
45  assert(RandEquivalent(fst2, *fst, 5, 0.01, kaldi::Rand(), 10));
46  delete fst;
47  }
48 }
void EnsureEpsilonProperty(VectorFst< Arc > *fst)
This function modifies the fst (while maintaining equivalence) in such a way that, after the modification, all states of the FST which have epsilon-arcs entering them, have no non-epsilon arcs entering them, and all states which have epsilon-arcs leaving them, have no non-epsilon arcs leaving them.
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
kaldi::int32 int32
void ComputeStateInfo(const VectorFst< Arc > &fst, std::vector< char > *epsilon_info)
This function will set epsilon_info to have size equal to the NumStates() of the FST, containing a logical-or of the enum values kStateHasEpsilonArcsEntering, kStateHasNonEpsilonArcsEntering, kStateHasEpsilonArcsLeaving, and kStateHasNonEpsilonArcsLeaving.
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45

◆ TestEqualAlign()

void fst::TestEqualAlign ( )

Definition at line 336 of file fstext-utils-test.cc.

References RandFstOptions::allow_empty, EqualAlign(), GetLinearSymbolSequence(), rnnlm::i, and kaldi::Rand().

336  {
337  for (size_t i = 0; i < 4; i++) {
338  RandFstOptions opts;
339  opts.allow_empty = false;
340  VectorFst<Arc> *fst = RandFst<Arc>();
341  int length = 10 + kaldi::Rand() % 20;
342 
343  VectorFst<Arc> fst_path;
344  if (EqualAlign(*fst, length, kaldi::Rand(), &fst_path)) {
345  std::cout << "EqualAlign succeeded\n";
346  vector<int32> isymbol_seq, osymbol_seq;
347  typename Arc::Weight weight;
348  GetLinearSymbolSequence(fst_path, &isymbol_seq, &osymbol_seq, &weight);
349  assert(isymbol_seq.size() == length);
350  Invert(&fst_path);
351  VectorFst<Arc> fst_composed;
352  Compose(fst_path, *fst, &fst_composed);
353  assert(fst_composed.Start() != kNoStateId); // make sure nonempty.
354  } else {
355  std::cout << "EqualAlign did not generate alignment\n";
356  }
357  delete fst;
358  }
359 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
bool GetLinearSymbolSequence(const Fst< Arc > &fst, std::vector< I > *isymbols_out, std::vector< I > *osymbols_out, typename Arc::Weight *tot_weight_out)
GetLinearSymbolSequence gets the symbol sequence from a linear FST.
bool EqualAlign(const Fst< Arc > &ifst, typename Arc::StateId length, int rand_seed, MutableFst< Arc > *ofst, int num_retries)
EqualAlign is similar to RandGen, but it generates a sequence with exactly "length" input symbols...
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
fst::StdArc::Weight Weight

◆ TestFactor() [1/2]

static void fst::TestFactor ( )
static

Definition at line 32 of file factor-test.cc.

References CreateFactorFst(), ExpandInputSequences(), Factor(), rnnlm::i, MapInputSymbols(), rnnlm::n, kaldi::Rand(), and kaldi::RandUniform().

32  {
33  typedef typename Arc::Label Label;
34  typedef typename Arc::StateId StateId;
35  typedef typename Arc::Weight Weight;
36 
37  VectorFst<Arc> fst;
38  int n_syms = 2 + kaldi::Rand() % 5, n_arcs = 5 + kaldi::Rand() % 30, n_final = 1 + kaldi::Rand()%10;
39 
40  SymbolTable symtab("my-symbol-table"), *sptr = &symtab;
41 
42  vector<Label> all_syms; // including epsilon.
43  // Put symbols in the symbol table from 1..n_syms-1.
44  for (size_t i = 0;i < (size_t)n_syms;i++) {
45  std::stringstream ss;
46  if (i == 0) ss << "<eps>";
47  else ss<<i;
48  Label cur_lab = sptr->AddSymbol(ss.str());
49  assert(cur_lab == (Label)i);
50  all_syms.push_back(cur_lab);
51  }
52  assert(all_syms[0] == 0);
53 
54  fst.AddState();
55  int cur_num_states = 1;
56  for (int i = 0; i < n_arcs; i++) {
57  StateId src_state = kaldi::Rand() % cur_num_states;
58  StateId dst_state;
59  if (kaldi::RandUniform() < 0.1) dst_state = kaldi::Rand() % cur_num_states;
60  else {
61  dst_state = cur_num_states++; fst.AddState();
62  }
63  Arc arc;
64  if (kaldi::RandUniform() < 0.5) arc.ilabel = all_syms[kaldi::Rand()%all_syms.size()];
65  else arc.ilabel = 0;
66  if (kaldi::RandUniform() < 0.5) arc.olabel = all_syms[kaldi::Rand()%all_syms.size()];
67  else arc.olabel = 0;
68  arc.weight = (Weight) (0 + 0.1*(kaldi::Rand() % 5));
69  arc.nextstate = dst_state;
70  fst.AddArc(src_state, arc);
71  }
72  for (int i = 0; i < n_final; i++) {
73  fst.SetFinal(kaldi::Rand() % cur_num_states, (Weight) (0 + 0.1*(kaldi::Rand() % 5)));
74  }
75 
76  if (kaldi::RandUniform() < 0.8) fst.SetStart(0); // usually leads to nicer examples.
77  else fst.SetStart(kaldi::Rand() % cur_num_states);
78 
79  std::cout <<" printing before trimming\n";
80  {
81  FstPrinter<Arc> fstprinter(fst, sptr, sptr, NULL, false, true, "\t");
82  fstprinter.Print(&std::cout, "standard output");
83  }
84  // Trim resulting FST.
85  Connect(&fst);
86 
87  std::cout <<" printing after trimming\n";
88  {
89  FstPrinter<Arc> fstprinter(fst, sptr, sptr, NULL, false, true, "\t");
90  fstprinter.Print(&std::cout, "standard output");
91  }
92 
93  if (fst.Start() == kNoStateId) return; // "Connect" made it empty.
94 
95  VectorFst<Arc> fst_pushed;
96  Push<Arc, REWEIGHT_TO_INITIAL>(fst, &fst_pushed, kPushLabels);
97 
98  VectorFst<Arc> fst_factored;
99  vector<vector<typename Arc::Label> > symbols;
100 
101  Factor(fst, &fst_factored, &symbols);
102 
103  // Check no epsilons in "symbols".
104  for (size_t i = 0; i < symbols.size(); i++)
105  assert(symbols[i].size() == 0 || *(std::min(symbols[i].begin(), symbols[i].end())) > 0);
106 
107  VectorFst<Arc> fst_factored_pushed;
108  vector<vector<typename Arc::Label> > symbols_pushed;
109  Factor(fst_pushed, &fst_factored_pushed, &symbols_pushed);
110 
111  std::cout << "Unfactored has "<<fst.NumStates()<<" states, factored has "<<fst_factored.NumStates()<<", and pushed+factored has "<<fst_factored_pushed.NumStates()<<'\n';
112 
113  assert(fst_factored.NumStates() <= fst.NumStates());
114  // assert(fst_factored_pushed.NumStates() <= fst_factored.NumStates()); // pushing should only help. [ no, it doesn't]
115  assert(fst_factored_pushed.NumStates() <= fst_pushed.NumStates());
116 
117  VectorFst<Arc> fst_factored_copy(fst_factored);
118 
119  VectorFst<Arc> fst_factored_unfactored(fst_factored);
120  ExpandInputSequences(symbols, &fst_factored_unfactored);
121 
122  VectorFst<Arc> factor_fst;
123  CreateFactorFst(symbols, &factor_fst);
124  VectorFst<Arc> fst_factored_unfactored2;
125  Compose(factor_fst, fst_factored, &fst_factored_unfactored2);
126 
127  ExpandInputSequences(symbols_pushed, &fst_factored_pushed);
128 
129  assert(RandEquivalent(fst, fst_factored_unfactored, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
130 
131  assert(RandEquivalent(fst, fst_factored_unfactored2, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
132 
133  assert(RandEquivalent(fst, fst_factored_pushed, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
134 
135  { // Have tested for equivalence; now do another test: that FactorFst actually finds all
136  // the factors. Do this by inserting factors using ExpandInputSequences and making sure it gets
137  // rid of them all.
138  Label max_label = *(std::max_element(all_syms.begin(), all_syms.end()));
139  vector<vector<Label> > new_labels(max_label+1);
140  for (Label l = 1; l < static_cast<Label>(new_labels.size()); l++) {
141  int n = kaldi::Rand() % 5;
142  for (int i = 0; i < n; i++) new_labels[l].push_back(kaldi::Rand() % 100);
143  }
144  VectorFst<Arc> fst_expanded(fst);
145  ExpandInputSequences(new_labels, &fst_expanded);
146 
147  vector<vector<Label> > factors;
148  VectorFst<Arc> fst_reduced;
149  Factor(fst_expanded, &fst_reduced, &factors);
150  assert(fst_reduced.NumStates() <= fst.NumStates()); // Checking that it found all the factors.
151  }
152 
153  { // This block test MapInputSymbols [but relies on the correctness of Factor
154  // and ExpandInputSequences to do so].
155 
156  std::map<Label, Label> symbols_reverse_map; // from new->old.
157  symbols_reverse_map[0] = 0; // map eps to eps.
158  for (Label i = 1; i < static_cast<Label>(symbols.size()); i++) {
159  Label new_i;
160  do {
161  new_i = kaldi::Rand() % (symbols.size() + 20);
162  } while (symbols_reverse_map.count(new_i) == 1);
163  symbols_reverse_map[new_i] = i;
164  }
165  vector<vector<Label> > symbols_new;
166  vector<Label> symbol_map(symbols.size()); // from old->new.
167  typename std::map<Label, Label>::iterator iter = symbols_reverse_map.begin();
168  for (; iter != symbols_reverse_map.end(); iter++) {
169  Label new_label = iter->first, old_label = iter->second;
170  if (new_label >= static_cast<Label>(symbols_new.size())) symbols_new.resize(new_label+1);
171  symbols_new[new_label] = symbols[old_label];
172  symbol_map[old_label] = new_label;
173  }
174  MapInputSymbols(symbol_map, &fst_factored_copy);
175  ExpandInputSequences(symbols_new, &fst_factored_copy);
176  assert(RandEquivalent(fst, fst_factored_copy,
177  5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/,
178  100/*path length-- max?*/));
179  }
180 
181 }
fst::StdArc::StateId StateId
float RandUniform(struct RandomState *state=NULL)
Returns a random number strictly between 0 and 1.
Definition: kaldi-math.h:151
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
struct rnnlm::@11::@12 n
void CreateFactorFst(const std::vector< std::vector< I > > &sequences, MutableFst< Arc > *fst)
The function CreateFactorFst will create an FST that expands out the "factors" that are the indices o...
Definition: factor-inl.h:250
void Factor(const Fst< Arc > &fst, MutableFst< Arc > *ofst, std::vector< std::vector< I > > *symbols_out)
Factor identifies linear chains of states with an olabel (if any) only on the first arc of the chain...
Definition: factor-inl.h:69
fst::StdArc::Label Label
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
fst::StdArc::Weight Weight
void MapInputSymbols(const std::vector< I > &symbol_mapping, MutableFst< Arc > *fst)
void ExpandInputSequences(const std::vector< std::vector< I > > &sequences, MutableFst< Arc > *fst)
ExpandInputSequences expands out the input symbols into sequences of input symbols.
Definition: factor-inl.h:163

◆ TestFactor() [2/2]

void fst::TestFactor ( )

Definition at line 33 of file trivial-factor-weight-test.cc.

References DeterminizeStar(), rnnlm::i, StringRepository< Label, StringId >::IdOfEmpty(), StringRepository< Label, StringId >::IdOfLabel(), StringRepository< Label, StringId >::IdOfSeq(), rnnlm::j, PreDeterminize(), kaldi::Rand(), StringRepository< Label, StringId >::RemovePrefix(), StringRepository< Label, StringId >::SeqOfId(), and TestStringRepository().

33  {
34  typedef typename Arc::Label Label;
35  typedef typename Arc::StateId StateId;
36  typedef typename Arc::Weight Weight;
37 
38  VectorFst<Arc> *fst = new VectorFst<Arc>();
39  int n_syms = 2 + kaldi::Rand() % 5, n_states = 3 + kaldi::Rand() % 10, n_arcs = 5 + kaldi::Rand() % 30, n_final = 1 + kaldi::Rand()%3; // Up to 2 unique symbols.
40  cout << "Testing pre-determinize with "<<n_syms<<" symbols, "<<n_states<<" states and "<<n_arcs<<" arcs and "<<n_final<<" final states.\n";
41  SymbolTable *sptr = NULL;
42 
43  vector<Label> all_syms; // including epsilon.
44  // Put symbols in the symbol table from 1..n_syms-1.
45  for (size_t i = 0;i < (size_t)n_syms;i++)
46  all_syms.push_back(i);
47 
48  // Create states.
49  vector<StateId> all_states;
50  for (size_t i = 0;i < (size_t)n_states;i++) {
51  StateId this_state = fst->AddState();
52  if (i == 0) fst->SetStart(i);
53  all_states.push_back(this_state);
54  }
55  // Set final states.
56  for (size_t j = 0;j < (size_t)n_final;j++) {
57  StateId id = all_states[kaldi::Rand() % n_states];
58  Weight weight = (Weight)(0.33*(kaldi::Rand() % 5) );
59  printf("calling SetFinal with %d and %f\n", id, weight.Value());
60  fst->SetFinal(id, weight);
61  }
62  // Create arcs.
63  for (size_t i = 0;i < (size_t)n_arcs;i++) {
64  Arc a;
65  a.nextstate = all_states[kaldi::Rand() % n_states];
66  a.ilabel = all_syms[kaldi::Rand() % n_syms];
67  a.olabel = all_syms[kaldi::Rand() % n_syms]; // same input+output vocab.
68  a.weight = (Weight) (0.33*(kaldi::Rand() % 2));
69  StateId start_state = all_states[kaldi::Rand() % n_states];
70  fst->AddArc(start_state, a);
71  }
72 
73  std::cout <<" printing before trimming\n";
74  {
75  FstPrinter<Arc> fstprinter(*fst, sptr, sptr, NULL, false, true, "\t");
76  fstprinter.Print(&std::cout, "standard output");
77  }
78  // Trim resulting FST.
79  Connect(fst);
80 
81  std::cout <<" printing after trimming\n";
82  {
83  FstPrinter<Arc> fstprinter(*fst, sptr, sptr, NULL, false, true, "\t");
84  fstprinter.Print(&std::cout, "standard output");
85  }
86 
87  vector<Label> extra_syms;
88  if (fst->Start() != kNoStateId) { // "Connect" did not make it empty....
89  PreDeterminize(fst, 1000, &extra_syms);
90  }
91 
92  std::cout <<" printing after predeterminization\n";
93  {
94  FstPrinter<Arc> fstprinter(*fst, sptr, sptr, NULL, false, true, "\t");
95  fstprinter.Print(&std::cout, "standard output");
96  }
97 
98 
99  { // Remove epsilon. All default args.
100  bool connect = true;
101  Weight weight_threshold = Weight::Zero();
102  int64 nstate = -1; // Relates to pruning.
103  double delta = kDelta; // I think a small weight value. Relates to some kind of pruning,
104  // I guess. But with no epsilon cycles, probably doensn't matter.
105  RmEpsilon(fst, connect, weight_threshold, nstate, delta);
106  }
107 
108  std::cout <<" printing after double-epsilon removal\n";
109  {
110  FstPrinter<Arc> fstprinter(*fst, sptr, sptr, NULL, false, true, "\t");
111  fstprinter.Print(&std::cout, "standard output");
112  }
113  VectorFst<Arc> ofst_star;
114 
115  {
116  printf("Converting to Gallic semiring");
117  VectorFst<GallicArc<Arc> > gallic_fst;
118  VectorFst<GallicArc<Arc> > gallic_fst_noeps;
119  VectorFst<GallicArc<Arc> > gallic_fst_det;
120 
121 
122  {
123  printf("Determinizing with DeterminizeStar, converting to Gallic\n");
124  DeterminizeStar(*fst, &gallic_fst);
125  }
126 
127  {
128  std::cout <<" printing gallic FST\n";
129  FstPrinter<GallicArc<Arc> > fstprinter(gallic_fst, sptr, sptr, NULL, false, true, "\t");
130  fstprinter.Print(&std::cout, "standard output");
131  }
132 
133 
134  // Map(ofst_star, &gallic_fst, ToGallicMapper<Arc, STRING_LEFT>());
135 
136  printf("Converting gallic back to regular\n");
137  TrivialFactorWeightFst< GallicArc<Arc, GALLIC_LEFT>, GallicFactor<typename Arc::Label,
138  typename Arc::Weight, GALLIC_LEFT> > fwfst(gallic_fst);
139  {
140  std::cout <<" printing factor-weight FST\n";
141  FstPrinter<GallicArc<Arc> > fstprinter(fwfst, sptr, sptr, NULL, false, true, "\t");
142  fstprinter.Print(&std::cout, "standard output");
143  }
144 
145  Map(fwfst, &ofst_star, FromGallicMapper<Arc, GALLIC_LEFT>());
146 
147  {
148  std::cout <<" printing after converting back to regular FST\n";
149  FstPrinter<Arc> fstprinter(ofst_star, sptr, sptr, NULL, false, true, "\t");
150  fstprinter.Print(&std::cout, "standard output");
151  }
152 
153 
154  VectorFst<GallicArc<Arc> > new_gallic_fst;
155  Map(ofst_star, &new_gallic_fst, ToGallicMapper<Arc, GALLIC_LEFT>());
156 
157  assert(RandEquivalent(gallic_fst, new_gallic_fst, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
158 
159  }
160 
161  delete fst;
162 }
fst::StdArc::StateId StateId
void PreDeterminize(MutableFst< Arc > *fst, typename Arc::Label first_new_sym, std::vector< Int > *symsOut)
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
fst::StdArc::Label Label
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
fst::StdArc::Weight Weight
bool DeterminizeStar(F &ifst, MutableFst< typename F::Arc > *ofst, float delta, bool *debug_ptr, int max_states, bool allow_partial)
This function implements the normal version of DeterminizeStar, in which the output strings are repre...

◆ TestLatticeStringRepository()

void fst::TestLatticeStringRepository ( )

Definition at line 29 of file determinize-lattice-test.cc.

References LatticeStringRepository< IntType >::CommonPrefix(), LatticeStringRepository< IntType >::ConvertToVector(), LatticeStringRepository< IntType >::EmptyString(), rnnlm::i, LatticeStringRepository< IntType >::IsPrefixOf(), kaldi::Rand(), LatticeStringRepository< IntType >::ReduceToCommonPrefix(), and LatticeStringRepository< IntType >::Successor().

Referenced by main().

29  {
30  typedef int32 IntType;
31 
34 
35  for(int i = 0; i < 100; i++) {
36  int len = kaldi::Rand() % 5;
37  vector<IntType> str(len), str2(kaldi::Rand() % 4);
38  const Entry *e = NULL;
39  for(int i = 0; i < len; i++) {
40  str[i] = kaldi::Rand() % 5;
41  e = sr.Successor(e, str[i]);
42  }
43  sr.ConvertToVector(e, &str2);
44  assert(str == str2);
45 
46  int len2 = kaldi::Rand() % 5;
47  str2.resize(len2);
48  const Entry *f = sr.EmptyString(); // NULL
49  for(int i = 0; i < len2; i++) {
50  str2[i] = kaldi::Rand() % 5;
51  f = sr.Successor(f, str2[i]);
52  }
53  vector<IntType> prefix, prefix2(kaldi::Rand() % 10),
54  prefix3;
55  for(int i = 0; i < len && i < len2; i++) {
56  if (str[i] == str2[i]) prefix.push_back(str[i]);
57  else break;
58  }
59  const Entry *g = sr.CommonPrefix(e, f);
60  sr.ConvertToVector(g, &prefix2);
61  sr.ConvertToVector(e, &prefix3);
62  sr.ReduceToCommonPrefix(f, &prefix3);
63  assert(prefix == prefix2);
64  assert(prefix == prefix3);
65  assert(sr.IsPrefixOf(g, e));
66  assert(sr.IsPrefixOf(g, f));
67  if (str.size() > prefix.size())
68  assert(!sr.IsPrefixOf(e, g));
69  }
70 }
const Entry * CommonPrefix(const Entry *a, const Entry *b)
void ConvertToVector(const Entry *entry, std::vector< IntType > *out) const
kaldi::int32 int32
const Entry * Successor(const Entry *parent, IntType i)
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
bool IsPrefixOf(const Entry *a, const Entry *b) const
void ReduceToCommonPrefix(const Entry *a, std::vector< IntType > *b)

◆ TestMakeLinearAcceptor()

void fst::TestMakeLinearAcceptor ( )

Definition at line 32 of file fstext-utils-test.cc.

References GetInputSymbols(), GetLinearSymbolSequence(), rnnlm::i, rnnlm::j, KALDI_ASSERT, MakeLinearAcceptor(), NbestAsFsts(), kaldi::Rand(), and kaldi::SortAndUniq().

32  {
33  typedef typename Arc::Label Label;
34  typedef typename Arc::StateId StateId;
35  typedef typename Arc::Weight Weight;
36 
37  int len = kaldi::Rand() % 10;
38  vector<I> vec;
39  vector<I> vec_nozeros;
40  for (int i = 0; i < len; i++) {
41  int j = kaldi::Rand() % len;
42  vec.push_back(j);
43  if (j != 0) vec_nozeros.push_back(j);
44  }
45 
46 
47  VectorFst<Arc> vfst;
48  MakeLinearAcceptor(vec, &vfst);
49  vector<I> vec2;
50  vector<I> vec3;
51  Weight w;
52  GetLinearSymbolSequence(vfst, &vec2, &vec3, &w);
53  assert(w == Weight::One());
54  assert(vec_nozeros == vec2);
55  assert(vec_nozeros == vec3);
56 
57  if (vec2.size() != 0 || vec3.size() != 0) { // This test might not work
58  // for empty sequences...
59  {
60  vector<VectorFst<Arc> > fstvec;
61  NbestAsFsts(vfst, 1, &fstvec);
62  KALDI_ASSERT(fstvec.size() == 1);
63  assert(RandEquivalent(vfst, fstvec[0], 2/*paths*/, 0.01/*delta*/,
64  kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
65  }
66  }
67  bool include_eps = (kaldi::Rand() % 2 == 0);
68  if (!include_eps) vec = vec_nozeros;
69  kaldi::SortAndUniq(&vec);
70 
71  vector<I> vec4;
72  GetInputSymbols(vfst, include_eps, &vec4);
73  assert(vec4 == vec);
74  vector<I> vec5;
75  GetInputSymbols(vfst, include_eps, &vec5);
76 }
fst::StdArc::StateId StateId
void SortAndUniq(std::vector< T > *vec)
Sorts and uniq&#39;s (removes duplicates) from a vector.
Definition: stl-utils.h:39
bool GetLinearSymbolSequence(const Fst< Arc > &fst, std::vector< I > *isymbols_out, std::vector< I > *osymbols_out, typename Arc::Weight *tot_weight_out)
GetLinearSymbolSequence gets the symbol sequence from a linear FST.
void GetInputSymbols(const Fst< Arc > &fst, bool include_eps, std::vector< I > *symbols)
GetInputSymbols gets the list of symbols on the input of fst (including epsilon, if include_eps == tr...
void MakeLinearAcceptor(const std::vector< I > &labels, MutableFst< Arc > *ofst)
Creates unweighted linear acceptor from symbol sequence.
void NbestAsFsts(const Fst< Arc > &fst, size_t n, std::vector< VectorFst< Arc > > *fsts_out)
Takes the n-shortest-paths (using ShortestPath), but outputs the result as a vector of up to n fsts...
fst::StdArc::Label Label
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
fst::StdArc::Weight Weight
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ TestMakeLoopFst()

void fst::TestMakeLoopFst ( )

Definition at line 305 of file fstext-utils-test.cc.

References rnnlm::i, MakeLoopFst(), MakeLoopFstCompare(), and kaldi::Rand().

305  {
306 
307  int num_fsts = kaldi::Rand() % 10;
308  vector<const ExpandedFst<Arc>* > fsts(num_fsts, (const ExpandedFst<Arc>*)NULL);
309  for (int i = 0; i < num_fsts; i++) {
310  if (kaldi::Rand() % 2 == 0) { // put an fst there.
311  VectorFst<Arc> *fst = RandFst<Arc>();
312  Project(fst, PROJECT_INPUT); // make input & output labels the same.
313  fsts[i] = fst;
314  } else { // this is to test that it works with the caching.
315  fsts[i] = fsts[i/2];
316  }
317  }
318 
319  VectorFst<Arc> *fst1 = MakeLoopFst(fsts),
320  *fst2 = MakeLoopFstCompare(fsts);
321 
322  assert(fst1->Properties(kOLabelSorted, kOLabelSorted) != 0);
323 
324  assert(RandEquivalent(*fst1, *fst2, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
325  delete fst1;
326  delete fst2;
327  std::sort(fsts.begin(), fsts.end());
328  fsts.erase(std::unique(fsts.begin(), fsts.end()), fsts.end());
329  for (int i = 0; i < (int)fsts.size(); i++)
330  delete fsts[i];
331 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
VectorFst< Arc > * MakeLoopFstCompare(const vector< const ExpandedFst< Arc > *> &fsts)
VectorFst< Arc > * MakeLoopFst(const std::vector< const ExpandedFst< Arc > *> &fsts)
MakeLoopFst creates an FST that has a state that is both initial and final (weight == Weight::One())...
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45

◆ TestMakeSymbolsSame()

void fst::TestMakeSymbolsSame ( )

Definition at line 217 of file fstext-utils-test.cc.

References FollowingInputSymbolsAreSame(), MakeFollowingInputSymbolsSame(), MakePrecedingInputSymbolsSame(), PrecedingInputSymbolsAreSame(), and kaldi::Rand().

217  {
218 
219  VectorFst<Arc> *fst = RandFst<Arc>();
220  bool foll = (kaldi::Rand() % 2 == 0);
221  bool is_symbol = (kaldi::Rand() % 2 == 0);
222 
223 
224  VectorFst<Arc> fst2(*fst);
225 
226  if (foll) {
227  MakeFollowingInputSymbolsSame(is_symbol, &fst2);
228  assert(FollowingInputSymbolsAreSame(is_symbol, fst2));
229  } else {
230  MakePrecedingInputSymbolsSame(is_symbol, &fst2);
231  assert(PrecedingInputSymbolsAreSame(is_symbol, fst2));
232  }
233 
234 
235  assert(RandEquivalent(*fst, fst2, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
236 
237  delete fst;
238 }
void MakePrecedingInputSymbolsSame(bool start_is_epsilon, MutableFst< Arc > *fst)
MakePrecedingInputSymbolsSame ensures that all arcs entering any given fst state have the same input ...
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
bool PrecedingInputSymbolsAreSame(bool start_is_epsilon, const Fst< Arc > &fst)
Returns true if and only if the FST is such that the input symbols on arcs entering any given state a...
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
bool FollowingInputSymbolsAreSame(bool end_is_epsilon, const Fst< Arc > &fst)
Returns true if and only if the FST is such that the input symbols on arcs exiting any given state al...
void MakeFollowingInputSymbolsSame(bool end_is_epsilon, MutableFst< Arc > *fst)
MakeFollowingInputSymbolsSame ensures that all arcs exiting any given fst state have the same input s...

◆ TestMakeSymbolsSameClass()

void fst::TestMakeSymbolsSameClass ( )

Definition at line 254 of file fstext-utils-test.cc.

References FollowingInputSymbolsAreSameClass(), MakeFollowingInputSymbolsSameClass(), MakePrecedingInputSymbolsSameClass(), PrecedingInputSymbolsAreSameClass(), and kaldi::Rand().

254  {
255 
256  VectorFst<Arc> *fst = RandFst<Arc>();
257  bool foll = (kaldi::Rand() % 2 == 0);
258  bool is_symbol = (kaldi::Rand() % 2 == 0);
259 
260 
261  VectorFst<Arc> fst2(*fst);
262 
263  TestFunctor<Arc> f;
264  if (foll) {
265  MakeFollowingInputSymbolsSameClass(is_symbol, &fst2, f);
266  assert(FollowingInputSymbolsAreSameClass(is_symbol, fst2, f));
267  } else {
268  MakePrecedingInputSymbolsSameClass(is_symbol, &fst2, f);
269  assert(PrecedingInputSymbolsAreSameClass(is_symbol, fst2, f));
270  }
271 
272  assert(RandEquivalent(*fst, fst2, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
273 
274  delete fst;
275 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
bool PrecedingInputSymbolsAreSameClass(bool start_is_epsilon, const Fst< Arc > &fst, const F &f)
This is as PrecedingInputSymbolsAreSame, but with a functor f that maps labels to classes...
void MakePrecedingInputSymbolsSameClass(bool start_is_epsilon, MutableFst< Arc > *fst, const F &f)
As MakePrecedingInputSymbolsSame, but takes a functor object that maps labels to classes.
bool FollowingInputSymbolsAreSameClass(bool end_is_epsilon, const Fst< Arc > &fst, const F &f)
void MakeFollowingInputSymbolsSameClass(bool end_is_epsilon, MutableFst< Arc > *fst, const F &f)
As MakeFollowingInputSymbolsSame, but takes a functor object that maps labels to classes.

◆ TestMinimize()

void fst::TestMinimize ( )

Definition at line 280 of file determinize-star-test.cc.

References DeleteISymbols(), DeterminizeStar(), rnnlm::i, rnnlm::j, PreDeterminize(), and kaldi::Rand().

280  {
281  typedef typename Arc::Label Label;
282  typedef typename Arc::StateId StateId;
283  typedef typename Arc::Weight Weight;
284 
285  VectorFst<Arc> *fst = new VectorFst<Arc>();
286  int n_syms = 2 + kaldi::Rand() % 5, n_states = 3 + kaldi::Rand() % 10, n_arcs = 5 + kaldi::Rand() % 30, n_final = 1 + kaldi::Rand()%3; // Up to 2 unique symbols.
287  std::cout << "Testing pre-determinize with "<<n_syms<<" symbols, "<<n_states<<" states and "<<n_arcs<<" arcs and "<<n_final<<" final states.\n";
288  SymbolTable *sptr =NULL;
289 
290  std::vector<Label> all_syms; // including epsilon.
291  // Put symbols in the symbol table from 1..n_syms-1.
292  for (size_t i = 0;i < (size_t)n_syms;i++)
293  all_syms.push_back(i);
294 
295  // Create states.
296  std::vector<StateId> all_states;
297  for (size_t i = 0;i < (size_t)n_states;i++) {
298  StateId this_state = fst->AddState();
299  if (i == 0) fst->SetStart(i);
300  all_states.push_back(this_state);
301  }
302  // Set final states.
303  for (size_t j = 0;j < (size_t)n_final;j++) {
304  StateId id = all_states[kaldi::Rand() % n_states];
305  Weight weight = (Weight)(0.33*(kaldi::Rand() % 5) );
306  printf("calling SetFinal with %d and %f\n", id, weight.Value());
307  fst->SetFinal(id, weight);
308  }
309  // Create arcs.
310  for (size_t i = 0;i < (size_t)n_arcs;i++) {
311  Arc a;
312  a.nextstate = all_states[kaldi::Rand() % n_states];
313  a.ilabel = all_syms[kaldi::Rand() % n_syms];
314  a.olabel = all_syms[kaldi::Rand() % n_syms]; // same input+output vocab.
315  a.weight = (Weight) (0.33*(kaldi::Rand() % 2));
316  StateId start_state = all_states[kaldi::Rand() % n_states];
317  fst->AddArc(start_state, a);
318  }
319 
320  std::cout <<" printing before trimming\n";
321  {
322  FstPrinter<Arc> fstprinter(*fst, sptr, sptr, NULL, false, true, "\t");
323  fstprinter.Print(&std::cout, "standard output");
324  }
325  // Trim resulting FST.
326  Connect(fst);
327 
328  std::cout <<" printing after trimming\n";
329  {
330  FstPrinter<Arc> fstprinter(*fst, sptr, sptr, NULL, false, true, "\t");
331  fstprinter.Print(&std::cout, "standard output");
332  }
333 
334  VectorFst<Arc> *fst_copy_orig = new VectorFst<Arc>(*fst);
335 
336  std::vector<Label> extra_syms;
337  if (fst->Start() != kNoStateId) { // "Connect" did not make it empty....
338  PreDeterminize(fst, 1000, &extra_syms);
339  }
340 
341  std::cout <<" printing after predeterminization\n";
342  {
343  FstPrinter<Arc> fstprinter(*fst, sptr, sptr, NULL, false, true, "\t");
344  fstprinter.Print(&std::cout, "standard output");
345  }
346 
347 
348  { // Remove epsilon. All default args.
349  bool connect = true;
350  Weight weight_threshold = Weight::Zero();
351  int64 nstate = -1; // Relates to pruning.
352  double delta = kDelta; // I think a small weight value. Relates to some kind of pruning,
353  // I guess. But with no epsilon cycles, probably doensn't matter.
354  RmEpsilon(fst, connect, weight_threshold, nstate, delta);
355  }
356 
357  std::cout <<" printing after epsilon removal\n";
358  {
359  FstPrinter<Arc> fstprinter(*fst, sptr, sptr, NULL, false, true, "\t");
360  fstprinter.Print(&std::cout, "standard output");
361  }
362  VectorFst<Arc> ofst_orig;
363  VectorFst<Arc> ofst_star;
364 
365  {
366  printf("Determinizing with baseline\n");
367  DeterminizeOptions<Arc> opts; // Default options.
368  Determinize(*fst, &ofst_orig, opts);
369  }
370  {
371  std::cout <<" printing after determinization [baseline]\n";
372  FstPrinter<Arc> fstprinter(ofst_orig, sptr, sptr, NULL, false, true, "\t");
373  fstprinter.Print(&std::cout, "standard output");
374  }
375 
376 
377  {
378  printf("Determinizing with DeterminizeStar to Gallic semiring\n");
379  VectorFst<GallicArc<Arc> > gallic_fst;
380 
381  DeterminizeStar(*fst, &gallic_fst);
382  {
383  std::cout <<" printing after determinization by DeterminizeStar [in gallic]\n";
384  FstPrinter<GallicArc< Arc> > fstprinter(gallic_fst, sptr, sptr, NULL, false, true, "\t");
385  fstprinter.Print(&std::cout, "standard output");
386  }
387 
388 
389  printf("Pushing weights\n");
390  Push(&gallic_fst, REWEIGHT_TO_INITIAL, kDelta);
391 
392  {
393  std::cout <<" printing after pushing weights [in gallic]\n";
394  FstPrinter<GallicArc< Arc> > fstprinter(gallic_fst, sptr, sptr, NULL, false, true, "\t");
395  fstprinter.Print(&std::cout, "standard output");
396  }
397 
398 
399  printf("Minimizing [in Gallic]\n");
400  Minimize(&gallic_fst);
401  {
402  std::cout <<" printing after minimization [in gallic]\n";
403  FstPrinter<GallicArc< Arc> > fstprinter(gallic_fst, sptr, sptr, NULL, false, true, "\t");
404  fstprinter.Print(&std::cout, "standard output");
405  }
406 
407  printf("Converting gallic back to regular [my approach]\n");
408  TrivialFactorWeightFst< GallicArc<Arc, GALLIC_LEFT>, GallicFactor<typename Arc::Label,
409  typename Arc::Weight, GALLIC_LEFT> > fwfst(gallic_fst);
410  {
411  std::cout <<" printing factor-weight FST\n";
412  FstPrinter<GallicArc< Arc> > fstprinter(fwfst, sptr, sptr, NULL, false, true, "\t");
413  fstprinter.Print(&std::cout, "standard output");
414  }
415 
416  Map(fwfst, &ofst_star, FromGallicMapper<Arc, GALLIC_LEFT>());
417 
418  {
419  std::cout <<" printing after converting back to regular FST\n";
420  FstPrinter<Arc> fstprinter(ofst_star, sptr, sptr, NULL, false, true, "\t");
421  fstprinter.Print(&std::cout, "standard output");
422  }
423 
424  }
425 
426 
427  assert(RandEquivalent(ofst_orig, ofst_star, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
428 
429 
430  int64 num_removed = DeleteISymbols(&ofst_star, extra_syms);
431  std::cout <<" printing after removing "<<num_removed<<" instances of extra symbols\n";
432  {
433  FstPrinter<Arc> fstprinter(ofst_star, sptr, sptr, NULL, false, true, "\t");
434  fstprinter.Print(&std::cout, "standard output");
435  }
436 
437  std::cout <<" Checking equivalent to original FST.\n";
438  // giving Rand() as a seed stops the random number generator from always being reset to
439  // the same point each time, while maintaining determinism of the test.
440  assert(RandEquivalent(ofst_star, *fst_copy_orig, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
441 
442  delete fst;
443  delete fst_copy_orig;
444 }
fst::StdArc::StateId StateId
void PreDeterminize(MutableFst< Arc > *fst, typename Arc::Label first_new_sym, std::vector< Int > *symsOut)
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
fst::StdArc::Label Label
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
fst::StdArc::Weight Weight
int64 DeleteISymbols(MutableFst< Arc > *fst, std::vector< typename Arc::Label > isyms)
bool DeterminizeStar(F &ifst, MutableFst< typename F::Arc > *ofst, float delta, bool *debug_ptr, int max_states, bool allow_partial)
This function implements the normal version of DeterminizeStar, in which the output strings are repre...

◆ TestPreDeterminize()

void fst::TestPreDeterminize ( )

Definition at line 33 of file pre-determinize-test.cc.

References DeleteISymbols(), HighestNumberedInputSymbol(), rnnlm::i, rnnlm::j, PreDeterminize(), and kaldi::Rand().

33  {
34  typedef typename Arc::Label Label;
35  typedef typename Arc::StateId StateId;
36  typedef typename Arc::Weight Weight;
37 
38  VectorFst<Arc> *fst = new VectorFst<Arc>();
39  int n_syms = 2 + kaldi::Rand() % 5, n_states = 3 + kaldi::Rand() % 10, n_arcs = 5 + kaldi::Rand() % 30, n_final = 1 + kaldi::Rand()%3; // Up to 2 unique symbols.
40  cout << "Testing pre-determinize with "<<n_syms<<" symbols, "<<n_states<<" states and "<<n_arcs<<" arcs and "<<n_final<<" final states.\n";
41  SymbolTable *sptr = NULL;
42 
43  vector<Label> all_syms; // including epsilon.
44  // Put symbols in the symbol table from 1..n_syms-1.
45  for (size_t i = 0;i < (size_t)n_syms;i++)
46  all_syms.push_back(i);
47 
48  // Create states.
49  vector<StateId> all_states;
50  for (size_t i = 0;i < (size_t)n_states;i++) {
51  StateId this_state = fst->AddState();
52  if (i == 0) fst->SetStart(i);
53  all_states.push_back(this_state);
54  }
55  // Set final states.
56  for (size_t j = 0;j < (size_t)n_final;j++) {
57  StateId id = all_states[kaldi::Rand() % n_states];
58  Weight weight = (Weight)(0.33*(kaldi::Rand() % 5) );
59  printf("calling SetFinal with %d and %f\n", id, weight.Value());
60  fst->SetFinal(id, weight);
61  }
62  // Create arcs.
63  for (size_t i = 0;i < (size_t)n_arcs;i++) {
64  Arc a;
65  a.nextstate = all_states[kaldi::Rand() % n_states];
66  a.ilabel = all_syms[kaldi::Rand() % n_syms];
67  a.olabel = all_syms[kaldi::Rand() % n_syms]; // same input+output vocab.
68  a.weight = (Weight) (0.33*(kaldi::Rand() % 2));
69  StateId start_state = all_states[kaldi::Rand() % n_states];
70  fst->AddArc(start_state, a);
71  }
72 
73  std::cout <<" printing before trimming\n";
74  {
75  FstPrinter<Arc> fstprinter(*fst, sptr, sptr, NULL, false, true, "\t");
76  fstprinter.Print(&std::cout, "standard output");
77  }
78  // Trim resulting FST.
79  Connect(fst);
80 
81  std::cout <<" printing after trimming\n";
82  {
83  FstPrinter<Arc> fstprinter(*fst, sptr, sptr, NULL, false, true, "\t");
84  fstprinter.Print(&std::cout, "standard output");
85  }
86 
87  VectorFst<Arc> *fst_copy_orig = new VectorFst<Arc>(*fst);
88 
89  vector<Label> extra_syms;
90  if (fst->Start() != kNoStateId) { // "Connect" did not make it empty....
91  typename Arc::Label highest_sym = HighestNumberedInputSymbol(*fst);
92  PreDeterminize(fst, highest_sym+1, &extra_syms);
93  }
94 
95  std::cout <<" printing after predeterminization\n";
96  {
97  FstPrinter<Arc> fstprinter(*fst, sptr, sptr, NULL, false, true, "\t");
98  fstprinter.Print(&std::cout, "standard output");
99  }
100 
101 
102  { // Remove epsilon. All default args.
103  bool connect = true;
104  Weight weight_threshold = Weight::Zero();
105  int64 nstate = -1; // Relates to pruning.
106  double delta = kDelta; // I think a small weight value. Relates to some kind of pruning,
107  // I guess. But with no epsilon cycles, probably doensn't matter.
108  RmEpsilon(fst, connect, weight_threshold, nstate, delta);
109  }
110 
111  std::cout <<" printing after epsilon removal\n";
112  {
113  FstPrinter<Arc> fstprinter(*fst, sptr, sptr, NULL, false, true, "\t");
114  fstprinter.Print(&std::cout, "standard output");
115  }
116 
117 
118  VectorFst<Arc> ofst;
119  DeterminizeOptions<Arc> opts; // Default options.
120  Determinize(*fst, &ofst, opts);
121  std::cout <<" printing after determinization\n";
122  {
123  FstPrinter<Arc> fstprinter(ofst, sptr, sptr, NULL, false, true, "\t");
124  fstprinter.Print(&std::cout, "standard output");
125  }
126 
127  int64 num_removed = DeleteISymbols(&ofst, extra_syms);
128  std::cout <<" printing after removing "<<num_removed<<" instances of extra symbols\n";
129  {
130  FstPrinter<Arc> fstprinter(ofst, sptr, sptr, NULL, false, true, "\t");
131  fstprinter.Print(&std::cout, "standard output");
132  }
133 
134  std::cout <<" Checking equivalent to original FST.\n";
135  // giving Rand() as a seed stops the random number generator from always being reset to
136  // the same point each time, while maintaining determinism of the test.
137  assert(RandEquivalent(ofst, *fst_copy_orig, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
138 
139  delete fst;
140  delete fst_copy_orig;
141 }
fst::StdArc::StateId StateId
void PreDeterminize(MutableFst< Arc > *fst, typename Arc::Label first_new_sym, std::vector< Int > *symsOut)
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
fst::StdArc::Label Label
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
fst::StdArc::Weight Weight
Arc::Label HighestNumberedInputSymbol(const Fst< Arc > &fst)
Returns the highest numbered input symbol id of the FST (or zero for an empty FST.
int64 DeleteISymbols(MutableFst< Arc > *fst, std::vector< typename Arc::Label > isyms)

◆ TestPruneSpecial()

static void fst::TestPruneSpecial ( )
static

Definition at line 27 of file prune-special-test.cc.

References RandFstOptions::acyclic, KALDI_ASSERT, and kaldi::Rand().

Referenced by main().

27  {
28  typedef StdArc Arc;
29  typedef Arc::Label Label;
30  typedef Arc::StateId StateId;
31  typedef Arc::Weight Weight;
32 
33  RandFstOptions opts;
34  opts.acyclic = false;
35  VectorFst<Arc> *ifst = RandFst<StdArc>(opts);
36 
37  float beam = 0.55;
38 
39  {
40  FstPrinter<Arc> fstprinter(*ifst, NULL, NULL, NULL, false, true, "\t");
41  fstprinter.Print(&std::cout, "standard output");
42  std::cout << std::endl;
43  }
44 
45  // Do the special pruning.
46  VectorFst<Arc> ofst1;
47  PruneSpecial<StdArc>(*ifst, &ofst1, beam);
48  {
49  FstPrinter<Arc> fstprinter(ofst1, NULL, NULL, NULL, false, true, "\t");
50  fstprinter.Print(&std::cout, "standard output");
51  std::cout << std::endl;
52  }
53 
54  // Do the normal pruning.
55  VectorFst<Arc> ofst2;
56  Prune(*ifst, &ofst2, beam);
57  {
58  FstPrinter<Arc> fstprinter(ofst2, NULL, NULL, NULL, false, true, "\t");
59  fstprinter.Print(&std::cout, "standard output");
60  std::cout << std::endl;
61  }
62 
63  KALDI_ASSERT(RandEquivalent(ofst1, ofst2,
64  5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/,
65  100/*path length-- max?*/));
66 
67  delete ifst;
68 }
fst::StdArc::StateId StateId
fst::StdArc StdArc
fst::StdArc::Label Label
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
fst::StdArc::Weight Weight
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ TestPush()

void fst::TestPush ( )

Definition at line 202 of file determinize-star-test.cc.

References rnnlm::i, rnnlm::j, PreDeterminize(), and kaldi::Rand().

202  {
203  typedef typename Arc::Label Label;
204  typedef typename Arc::StateId StateId;
205  typedef typename Arc::Weight Weight;
206 
207  VectorFst<Arc> *fst = new VectorFst<Arc>();
208  int n_syms = 2 + kaldi::Rand() % 5, n_states = 3 + kaldi::Rand() % 10, n_arcs = 5 + kaldi::Rand() % 30, n_final = 1 + kaldi::Rand()%3; // Up to 2 unique symbols.
209  std::cout << "Testing pre-determinize with "<<n_syms<<" symbols, "<<n_states<<" states and "<<n_arcs<<" arcs and "<<n_final<<" final states.\n";
210  SymbolTable *sptr = NULL;
211 
212  std::vector<Label> all_syms; // including epsilon.
213  // Put symbols in the symbol table from 1..n_syms-1.
214  for (size_t i = 0;i < (size_t)n_syms;i++)
215  all_syms.push_back(i);
216 
217  // Create states.
218  std::vector<StateId> all_states;
219  for (size_t i = 0;i < (size_t)n_states;i++) {
220  StateId this_state = fst->AddState();
221  if (i == 0) fst->SetStart(i);
222  all_states.push_back(this_state);
223  }
224  // Set final states.
225  for (size_t j = 0;j < (size_t)n_final;j++) {
226  StateId id = all_states[kaldi::Rand() % n_states];
227  Weight weight = (Weight)(0.33*(kaldi::Rand() % 5) );
228  printf("calling SetFinal with %d and %f\n", id, weight.Value());
229  fst->SetFinal(id, weight);
230  }
231  // Create arcs.
232  for (size_t i = 0;i < (size_t)n_arcs;i++) {
233  Arc a;
234  a.nextstate = all_states[kaldi::Rand() % n_states];
235  a.ilabel = all_syms[kaldi::Rand() % n_syms];
236  a.olabel = all_syms[kaldi::Rand() % n_syms]; // same input+output vocab.
237  a.weight = (Weight) (0.33*(kaldi::Rand() % 2));
238  StateId start_state = all_states[kaldi::Rand() % n_states];
239  fst->AddArc(start_state, a);
240  }
241 
242  std::cout <<" printing before trimming\n";
243  {
244  FstPrinter<Arc> fstprinter(*fst, sptr, sptr, NULL, false, true, "\t");
245  fstprinter.Print(&std::cout, "standard output");
246  }
247  // Trim resulting FST.
248  Connect(fst);
249 
250  std::cout <<" printing after trimming\n";
251  {
252  FstPrinter<Arc> fstprinter(*fst, sptr, sptr, NULL, false, true, "\t");
253  fstprinter.Print(&std::cout, "standard output");
254  }
255 
256  VectorFst<Arc> *fst_copy_orig = new VectorFst<Arc>(*fst);
257 
258  std::vector<Label> extra_syms;
259  if (fst->Start() != kNoStateId) { // "Connect" did not make it empty....
260  PreDeterminize(fst, 1000, &extra_syms);
261  }
262 
263  VectorFst<Arc> fst_pushed;
264  std::cout << "Pushing FST\n";
265  Push<Arc, REWEIGHT_TO_INITIAL>(*fst, &fst_pushed, kPushWeights|kPushLabels, kDelta);
266 
267  std::cout <<" printing after pushing\n";
268  {
269  FstPrinter<Arc> fstprinter(fst_pushed, sptr, sptr, NULL, false, true, "\t");
270  fstprinter.Print(&std::cout, "standard output");
271  }
272 
273  assert(RandEquivalent(*fst, fst_pushed, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
274 
275  delete fst;
276  delete fst_copy_orig;
277 }
fst::StdArc::StateId StateId
void PreDeterminize(MutableFst< Arc > *fst, typename Arc::Label first_new_sym, std::vector< Int > *symsOut)
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
fst::StdArc::Label Label
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
fst::StdArc::Weight Weight

◆ TestPushInLog()

void fst::TestPushInLog ( )

Definition at line 182 of file fstext-utils-test.cc.

References kaldi::Rand().

182  { // also tests SafeDeterminizeMinimizeWrapper().
183  typedef StdArc Arc;
184  typedef Arc::Label Label;
185  typedef Arc::StateId StateId;
186  typedef Arc::Weight Weight;
187 
188  VectorFst<Arc> *fst = RandFst<Arc>();
189  VectorFst<Arc> fst2(*fst);
190  PushInLog<REWEIGHT_TO_INITIAL>(&fst2, kPushLabels|kPushWeights, 0.01); // speed it up using large delta.
191  assert(RandEquivalent(*fst, fst2, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
192 
193  delete fst;
194 }
fst::StdArc::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
fst::StdArc StdArc
fst::StdArc::Label Label
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
fst::StdArc::Weight Weight

◆ TestPushSpecial()

static void fst::TestPushSpecial ( )
static

Definition at line 31 of file push-special-test.cc.

References IsStochasticFstInLog(), KALDI_ASSERT, KALDI_LOG, PushSpecial(), and kaldi::Rand().

Referenced by main().

31  {
32  typedef StdArc Arc;
33  typedef Arc::Label Label;
34  typedef Arc::StateId StateId;
35  typedef Arc::Weight Weight;
36 
37  VectorFst<Arc> *fst = RandFst<StdArc>();
38 
39  {
40  FstPrinter<Arc> fstprinter(*fst, NULL, NULL, NULL, false, true, "\t");
41  fstprinter.Print(&std::cout, "standard output");
42  }
43 
44  VectorFst<Arc> fst_copy(*fst);
45 
46  float delta = kDelta;
47  PushSpecial(&fst_copy, delta);
48 
49  Weight min, max;
50  float delta_dontcare = 0.1;
51  IsStochasticFstInLog(fst_copy, delta_dontcare, &min, &max);
52  // the per-state normalizers are allowed to deviate from the average by delta
53  // up and down, so the difference from the min to max weight should be 2*delta
54  // or less. We give it a bit of wiggle room (->2.5) due to numerical roundoff.
55 
56 
57  {
58  FstPrinter<Arc> fstprinter(fst_copy, NULL, NULL, NULL, false, true, "\t");
59  fstprinter.Print(&std::cout, "standard output");
60  }
61  KALDI_LOG << "Min value is " << min.Value() << ", max value is " << max.Value();
62 
63  // below, should be <= delta but different pieces of code compute this in this
64  // part vs. push-special, so the roundoff may be different.
65  KALDI_ASSERT(std::abs(min.Value() - max.Value()) <= 1.2 * delta);
66 
67  KALDI_ASSERT(RandEquivalent(*fst, fst_copy,
68  5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
69  delete fst;
70 }
fst::StdArc::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
fst::StdArc StdArc
bool IsStochasticFstInLog(const Fst< StdArc > &fst, float delta, StdArc::Weight *min_sum, StdArc::Weight *max_sum)
fst::StdArc::Label Label
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
fst::StdArc::Weight Weight
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void PushSpecial(VectorFst< StdArc > *fst, float delta)
#define KALDI_LOG
Definition: kaldi-error.h:153

◆ TestRemoveEpsLocal()

static void fst::TestRemoveEpsLocal ( )
static

Definition at line 33 of file remove-eps-local-test.cc.

References rnnlm::i, kaldi::Rand(), kaldi::RandUniform(), and RemoveEpsLocal().

33  {
34  typedef typename Arc::Label Label;
35  typedef typename Arc::StateId StateId;
36  typedef typename Arc::Weight Weight;
37 
38  VectorFst<Arc> fst;
39  int n_syms = 2 + kaldi::Rand() % 5, n_arcs = 5 + kaldi::Rand() % 30, n_final = 1 + kaldi::Rand()%10;
40 
41  SymbolTable symtab("my-symbol-table"), *sptr = &symtab;
42 
43  vector<Label> all_syms; // including epsilon.
44  // Put symbols in the symbol table from 1..n_syms-1.
45  for (size_t i = 0;i < (size_t)n_syms;i++) {
46  std::stringstream ss;
47  if (i == 0) ss << "<eps>";
48  else ss<<i;
49  Label cur_lab = sptr->AddSymbol(ss.str());
50  assert(cur_lab == (Label)i);
51  all_syms.push_back(cur_lab);
52  }
53  assert(all_syms[0] == 0);
54 
55  fst.AddState();
56  int cur_num_states = 1;
57  for (int i = 0; i < n_arcs; i++) {
58  StateId src_state = kaldi::Rand() % cur_num_states;
59  StateId dst_state;
60  if (kaldi::RandUniform() < 0.1) dst_state = kaldi::Rand() % cur_num_states;
61  else {
62  dst_state = cur_num_states++; fst.AddState();
63  }
64  Arc arc;
65  if (kaldi::RandUniform() < 0.3) arc.ilabel = all_syms[kaldi::Rand()%all_syms.size()];
66  else arc.ilabel = 0;
67  if (kaldi::RandUniform() < 0.3) arc.olabel = all_syms[kaldi::Rand()%all_syms.size()];
68  else arc.olabel = 0;
69  arc.weight = (Weight) (0 + 0.1*(kaldi::Rand() % 5));
70  arc.nextstate = dst_state;
71  fst.AddArc(src_state, arc);
72  }
73  for (int i = 0; i < n_final; i++) {
74  fst.SetFinal(kaldi::Rand() % cur_num_states, (Weight) (0 + 0.1*(kaldi::Rand() % 5)));
75  }
76 
77  if (kaldi::RandUniform() < 0.8) fst.SetStart(0); // usually leads to nicer examples.
78  else fst.SetStart(kaldi::Rand() % cur_num_states);
79 
80  Connect(&fst);
81  if (fst.Start() == kNoStateId) return; // "Connect" made it empty.
82 
83  std::cout <<" printing after trimming\n";
84  {
85  FstPrinter<Arc> fstprinter(fst, sptr, sptr, NULL, false, true, "\t");
86  fstprinter.Print(&std::cout, "standard output");
87  }
88 
89  VectorFst<Arc> fst_copy1(fst);
90 
91 
92  RemoveEpsLocal(&fst_copy1);
93 
94 
95 
96  {
97  std::cout << "copy1 = \n";
98  FstPrinter<Arc> fstprinter(fst_copy1, sptr, sptr, NULL, false, true, "\t");
99  fstprinter.Print(&std::cout, "standard output");
100  }
101 
102 
103  int num_states_0 = fst.NumStates();
104  int num_states_1 = fst_copy1.NumStates();
105 
106 
107  std::cout << "Number of states 0 = "<<num_states_0<<", 1 = "<<num_states_1<<'\n';
108 
109  assert(RandEquivalent(fst, fst_copy1, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
110 }
fst::StdArc::StateId StateId
float RandUniform(struct RandomState *state=NULL)
Returns a random number strictly between 0 and 1.
Definition: kaldi-math.h:151
void RemoveEpsLocal(MutableFst< Arc > *fst)
RemoveEpsLocal remove some (but not necessarily all) epsilons in an FST, using an algorithm that is g...
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
fst::StdArc::Label Label
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
fst::StdArc::Weight Weight

◆ TestRemoveEpsLocalSpecial()

static void fst::TestRemoveEpsLocalSpecial ( )
static

Definition at line 113 of file remove-eps-local-test.cc.

References ApproxEqual(), Divide(), IsStochasticFst(), Plus(), kaldi::Rand(), and RemoveEpsLocalSpecial().

Referenced by main().

113  {
114  // test that RemoveEpsLocalSpecial preserves equivalence in tropical while
115  // maintaining stochasticity in log.
116  typedef VectorFst<LogArc> Fst;
117  typedef LogArc::Weight Weight;
118  typedef LogArc::StateId StateId;
119  typedef LogArc Arc;
120  VectorFst<LogArc> *logfst = RandFst<LogArc>();
121 
122  { // Make the FST stochastic.
123  for (StateId s = 0; s < logfst->NumStates(); s++) {
124  Weight w = logfst->Final(s);
125  for (ArcIterator<Fst> aiter(*logfst, s); !aiter.Done(); aiter.Next()) {
126  w = Plus(w, aiter.Value().weight);
127  }
128  if (w != Weight::Zero()) {
129  logfst->SetFinal(s, Divide(logfst->Final(s), w, DIVIDE_ANY));
130  for (MutableArcIterator<Fst> aiter(logfst, s); !aiter.Done(); aiter.Next()) {
131  Arc a = aiter.Value();
132  a.weight = Divide(a.weight, w, DIVIDE_ANY);
133  aiter.SetValue(a);
134  }
135  }
136  }
137  }
138 #ifndef _MSC_VER
139  assert(IsStochasticFst(*logfst, kDelta*10));
140 #endif
141  {
142  std::cout << "logfst = \n";
143  FstPrinter<LogArc> fstprinter(*logfst, NULL, NULL, NULL, false, true, "\t");
144  fstprinter.Print(&std::cout, "standard output");
145  }
146 
147  VectorFst<StdArc> fst;
148  Cast(*logfst, &fst);
149  VectorFst<StdArc> fst_copy(fst);
150  RemoveEpsLocalSpecial(&fst); // removes eps in std-arc but keep stochastic in log-arc
151  // make sure equivalent.
152  assert(RandEquivalent(fst, fst_copy, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
153  VectorFst<LogArc> logfst2;
154  Cast(fst, &logfst2);
155 
156  {
157  std::cout << "logfst2 = \n";
158  FstPrinter<LogArc> fstprinter(logfst2, NULL, NULL, NULL, false, true, "\t");
159  fstprinter.Print(&std::cout, "standard output");
160  }
161  if (ApproxEqual(ShortestDistance(*logfst), ShortestDistance(logfst2))) {
162  // make sure we preserved stochasticity in cases where doing so was
163  // possible... if the log-semiring total weight changed, then it is
164  // not possible so don't assert this.
165  assert(IsStochasticFst(logfst2, kDelta*10));
166  }
167  delete logfst;
168 }
fst::StdArc::StateId StateId
LatticeWeightTpl< FloatType > Divide(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2, DivideType typ=DIVIDE_ANY)
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
LatticeWeightTpl< FloatType > Plus(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2)
void RemoveEpsLocalSpecial(MutableFst< StdArc > *fst)
As RemoveEpsLocal but takes care to preserve stochasticity when cast to LogArc.
bool IsStochasticFst(const Fst< LogArc > &fst, float delta, LogArc::Weight *min_sum, LogArc::Weight *max_sum)
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
fst::StdArc::Weight Weight
static bool ApproxEqual(float a, float b, float relative_tolerance=0.001)
return abs(a - b) <= relative_tolerance * (abs(a)+abs(b)).
Definition: kaldi-math.h:265

◆ TestRemoveUselessArcs()

void fst::TestRemoveUselessArcs ( )

Definition at line 370 of file fstext-utils-test.cc.

References RandFstOptions::allow_empty, ApproxEqual(), rnnlm::i, and RemoveUselessArcs().

370  {
371  for (size_t i = 0; i < 4; i++) {
372  RandFstOptions opts;
373  opts.allow_empty = false;
374  VectorFst<Arc> *fst = RandFst<Arc>();
375  // Print(*fst, "[testremoveuselessarcs]:fst:");
376  UniformArcSelector<Arc> selector;
377  RandGenOptions<UniformArcSelector<Arc> > randgen_opts(selector);
378  VectorFst<Arc> fst_path;
379  RandGen(*fst, &fst_path, randgen_opts);
380  Project(&fst_path, PROJECT_INPUT);
381  // Print(fst_path, "[testremoveuselessarcs]:fstpath:");
382 
383  VectorFst<Arc> fst_nouseless(*fst);
384  RemoveUselessArcs(&fst_nouseless);
385  // Print(fst_nouseless, "[testremoveuselessarcs]:fst_nouseless:");
386 
387  VectorFst<Arc> orig_composed,
388  nouseless_composed;
389  Compose(fst_path, *fst, &orig_composed);
390  Compose(fst_path, fst_nouseless, &nouseless_composed);
391 
392  // Print(orig_composed, "[testremoveuselessarcs]:orig_composed");
393  // Print(nouseless_composed, "[testremoveuselessarcs]:nouseless_composed");
394 
395  VectorFst<Arc> orig_bestpath,
396  nouseless_bestpath;
397  ShortestPath(orig_composed, &orig_bestpath);
398  ShortestPath(nouseless_composed, &nouseless_bestpath);
399  // Print(orig_bestpath, "[testremoveuselessarcs]:orig_bestpath");
400  // Print(nouseless_bestpath, "[testremoveuselessarcs]:nouseless_bestpath");
401 
402  typename Arc::Weight worig, wnouseless;
403  GetLinearSymbolSequence<Arc, int>(orig_bestpath, NULL, NULL, &worig);
404  GetLinearSymbolSequence<Arc, int>(nouseless_bestpath, NULL, NULL, &wnouseless);
405  assert(ApproxEqual(worig, wnouseless, kDelta));
406 
407  // assert(RandEquivalent(orig_bestpath, nouseless_bestpath, 5/*paths*/, 0.01/*delta*/, Rand()/*seed*/, 100/*path length-- max?*/));
408  delete fst;
409  }
410 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
void RemoveUselessArcs(MutableFst< Arc > *fst)
fst::StdArc::Weight Weight
static bool ApproxEqual(float a, float b, float relative_tolerance=0.001)
return abs(a - b) <= relative_tolerance * (abs(a)+abs(b)).
Definition: kaldi-math.h:265

◆ TestSafeDeterminizeWrapper()

void fst::TestSafeDeterminizeWrapper ( )

Definition at line 90 of file fstext-utils-test.cc.

References rnnlm::i, rnnlm::j, KALDI_LOG, kaldi::Rand(), SafeDeterminizeMinimizeWrapper(), SafeDeterminizeMinimizeWrapperInLog(), and SafeDeterminizeWrapper().

90  { // also tests SafeDeterminizeMinimizeWrapper().
91  typedef typename Arc::Label Label;
92  typedef typename Arc::StateId StateId;
93  typedef typename Arc::Weight Weight;
94 
95  VectorFst<Arc> *fst = new VectorFst<Arc>();
96  int n_syms = 2 + kaldi::Rand() % 5, n_states = 3 + kaldi::Rand() % 10, n_arcs = 5 + kaldi::Rand() % 30, n_final = 1 + kaldi::Rand()%3; // Up to 2 unique symbols.
97  cout << "Testing pre-determinize with "<<n_syms<<" symbols, "<<n_states<<" states and "<<n_arcs<<" arcs and "<<n_final<<" final states.\n";
98  SymbolTable *sptr = new SymbolTable("my-symbol-table");
99  sptr->AddSymbol("<eps>");
100  delete sptr;
101  sptr = new SymbolTable("my-symbol-table");
102 
103  vector<Label> all_syms; // including epsilon.
104  // Put symbols in the symbol table from 1..n_syms-1.
105  for (size_t i = 0;i < (size_t)n_syms;i++) {
106  std::stringstream ss;
107  if (i == 0) ss << "<eps>";
108  else ss<<i;
109  Label cur_lab = sptr->AddSymbol(ss.str());
110  assert(cur_lab == (Label)i);
111  all_syms.push_back(cur_lab);
112  }
113  assert(all_syms[0] == 0);
114 
115  // Create states.
116  vector<StateId> all_states;
117  for (size_t i = 0;i < (size_t)n_states;i++) {
118  StateId this_state = fst->AddState();
119  if (i == 0) fst->SetStart(i);
120  all_states.push_back(this_state);
121  }
122  // Set final states.
123  for (size_t j = 0;j < (size_t)n_final;j++) {
124  StateId id = all_states[kaldi::Rand() % n_states];
125  Weight weight = (Weight)(0.33*(kaldi::Rand() % 5) );
126  printf("calling SetFinal with %d and %f\n", id, weight.Value());
127  fst->SetFinal(id, weight);
128  }
129  // Create arcs.
130  for (size_t i = 0;i < (size_t)n_arcs;i++) {
131  Arc a;
132  a.nextstate = all_states[kaldi::Rand() % n_states];
133  a.ilabel = all_syms[kaldi::Rand() % n_syms];
134  a.olabel = all_syms[kaldi::Rand() % n_syms]; // same input+output vocab.
135  a.weight = (Weight) (0.33*(kaldi::Rand() % 2));
136  StateId start_state = all_states[kaldi::Rand() % n_states];
137  fst->AddArc(start_state, a);
138  }
139 
140  std::cout <<" printing before trimming\n";
141  {
142  FstPrinter<Arc> fstprinter(*fst, sptr, sptr, NULL, false, true, "\t");
143  fstprinter.Print(&std::cout, "standard output");
144  }
145  // Trim resulting FST.
146  Connect(fst);
147 
148  std::cout <<" printing after trimming\n";
149  {
150  FstPrinter<Arc> fstprinter(*fst, sptr, sptr, NULL, false, true, "\t");
151  fstprinter.Print(&std::cout, "standard output");
152  }
153 
154  VectorFst<Arc> *fst_copy_orig = new VectorFst<Arc>(*fst);
155 
156  VectorFst<Arc> *fst_det = new VectorFst<Arc>;
157 
158  vector<Label> extra_syms;
159  if (fst->Start() != kNoStateId) { // "Connect" did not make it empty....
160  if (kaldi::Rand() % 2 == 0)
161  SafeDeterminizeWrapper(fst_copy_orig, fst_det);
162  else {
163  if (kaldi::Rand() % 2 == 0)
164  SafeDeterminizeMinimizeWrapper(fst_copy_orig, fst_det);
165  else
166  SafeDeterminizeMinimizeWrapperInLog(fst_copy_orig, fst_det);
167  }
168 
169  // no because does shortest-dist on weights even if not pushing on them.
170  // PushInLog<REWEIGHT_TO_INITIAL>(fst_det, kPushLabels); // will always succeed.
171  KALDI_LOG << "Num states [orig]: " << fst->NumStates() << "[det]" << fst_det->NumStates();
172  assert(RandEquivalent(*fst, *fst_det, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/));
173  }
174  delete fst;
175  delete fst_copy_orig;
176  delete fst_det;
177  delete sptr;
178 }
fst::StdArc::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
fst::StdArc::Label Label
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
fst::StdArc::Weight Weight
void SafeDeterminizeWrapper(MutableFst< Arc > *ifst, MutableFst< Arc > *ofst, float delta)
Does PreDeterminize and DeterminizeStar and then removes the disambiguation symbols.
void SafeDeterminizeMinimizeWrapperInLog(VectorFst< StdArc > *ifst, VectorFst< StdArc > *ofst, float delta)
SafeDeterminizeMinimizeWapperInLog is as SafeDeterminizeMinimizeWrapper except it first casts tothe l...
void SafeDeterminizeMinimizeWrapper(MutableFst< Arc > *ifst, VectorFst< Arc > *ofst, float delta)
SafeDeterminizeMinimizeWapper is as SafeDeterminizeWrapper except that it also minimizes (encoded min...
#define KALDI_LOG
Definition: kaldi-error.h:153

◆ TestScalePair()

void fst::TestScalePair ( bool  invert)

Definition at line 234 of file lattice-utils-test.cc.

References DefaultLatticeScale(), rnnlm::i, kaldi::Rand(), and ScaleLattice().

234  {
235  std::vector<std::vector<double> > scale1 = DefaultLatticeScale(),
236  scale2 = DefaultLatticeScale();
237  // important that all these numbers exactly representable as floats..
238  // exact floating-point comparisons are used in LatticeWeight, and
239  // this exactness is being tested here.. this test will fail for
240  // other types of number.
241  if (kaldi::Rand() % 4 == 0) {
242  scale1[0][0] = 2.0;
243  scale2[0][0] = 0.5;
244  scale1[1][1] = 4.0;
245  scale2[1][1] = 0.25;
246  } else if (kaldi::Rand() % 3 == 0) {
247  // use that [1 0.25; 0 1] [ 1 -0.25; 0 1] is the unit matrix.
248  scale1[0][1] = 0.25;
249  scale2[0][1] = -0.25;
250  } else if (kaldi::Rand() % 2 == 0) {
251  scale1[1][0] = 0.25;
252  scale2[1][0] = -0.25;
253  }
254 
255 
256  typedef ArcTpl<Weight> Arc;
257  typedef ArcTpl<CompactLatticeWeightTpl<Weight, Int> > CompactArc;
258  for(int i = 0; i < 2; i++) {
259  VectorFst<Arc> *fst = RandPairFst<Arc>();
260  /*std::cout << "FST before converting to compact-arc is:\n";
261  {
262  FstPrinter<Arc> fstprinter(*fst, NULL, NULL, NULL, false, true);
263  fstprinter.Print(&std::cout, "standard output");
264  }*/
265  VectorFst<CompactArc> ofst;
266  ConvertLattice<Weight, Int>(*fst, &ofst, invert);
267  ScaleLattice(scale1, &ofst);
268  /*std::cout << "FST after converting and scaling is:\n";
269  {
270  FstPrinter<CompactArc> fstprinter(ofst, NULL, NULL, NULL, false, true);
271  fstprinter.Print(&std::cout, "standard output");
272  }*/
273  VectorFst<Arc> origfst;
274  ConvertLattice<Weight, Int>(ofst, &origfst, invert);
275  ScaleLattice(scale2, &origfst);
276  /*std::cout << "FST after back conversion and scaling is:\n";
277  {
278  FstPrinter<Arc> fstprinter(origfst, NULL, NULL, NULL, false, true);
279  fstprinter.Print(&std::cout, "standard output");
280  }*/
281  // If RandEquivalent doesn't work, it could be due to a nasty issue related to the use
282  // of exact floating-point comparisons in the Plus function of LatticeWeight.
283  if (!RandEquivalent(*fst, origfst, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/)) {
284  std::cerr << "Warn, randequivalent returned false. Checking equivalence another way.\n";
285  assert(Equal(*fst, origfst));
286  }
287  delete fst;
288  }
289 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
void ScaleLattice(const std::vector< std::vector< ScaleFloat > > &scale, MutableFst< ArcTpl< Weight > > *fst)
Scales the pairs of weights in LatticeWeight or CompactLatticeWeight by viewing the pair (a...
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
std::vector< std::vector< double > > DefaultLatticeScale()
Returns a default 2x2 matrix scaling factor for LatticeWeight.

◆ TestShortestPath()

void fst::TestShortestPath ( )

Definition at line 60 of file lattice-utils-test.cc.

References ApproxEqual(), and rnnlm::i.

60  {
61  for (int p = 0; p < 10; p++) {
62  typedef ArcTpl<Weight> Arc;
63  typedef ArcTpl<CompactLatticeWeightTpl<Weight, Int> > CompactArc;
64  for(int i = 0; i < 5; i++) {
65  VectorFst<Arc> *fst = RandPairFst<Arc>();
66  std::cout << "Testing shortest path\n";
67  std::cout << "FST before converting to compact-arc is:\n";
68  {
69  FstPrinter<Arc> fstprinter(*fst, NULL, NULL, NULL, false, true, "\t");
70  fstprinter.Print(&std::cout, "standard output");
71  }
72  VectorFst<CompactArc> cfst;
73  ConvertLattice<Weight, Int>(*fst, &cfst, false); // invert == false
74 
75 
76  {
77  VectorFst<Arc> nbest_fst_1;
78  ShortestPath(*fst, &nbest_fst_1, 1);
79  VectorFst<Arc> nbest_fst_2;
80  ShortestPath(*fst, &nbest_fst_2, 3);
81  VectorFst<Arc> nbest_fst_1b;
82  ShortestPath(nbest_fst_2, &nbest_fst_1b, 1);
83 
84 
85  assert(ApproxEqual(ShortestDistance(nbest_fst_1),
86  ShortestDistance(nbest_fst_1b)));
87 
88  // since semiring is idempotent, this should succeed too.
89  assert(ApproxEqual(ShortestDistance(*fst),
90  ShortestDistance(nbest_fst_1b)));
91  }
92  {
93  VectorFst<CompactArc> nbest_fst_1;
94  ShortestPath(cfst, &nbest_fst_1, 1);
95  VectorFst<CompactArc> nbest_fst_2;
96  ShortestPath(cfst, &nbest_fst_2, 3);
97  VectorFst<CompactArc> nbest_fst_1b;
98  ShortestPath(nbest_fst_2, &nbest_fst_1b, 1);
99 
100  assert(ApproxEqual(ShortestDistance(nbest_fst_1),
101  ShortestDistance(nbest_fst_1b)));
102  // since semiring is idempotent, this should succeed too.
103  assert(ApproxEqual(ShortestDistance(cfst),
104  ShortestDistance(nbest_fst_1b)));
105  }
106 
107  delete fst;
108  }
109  }
110 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
static bool ApproxEqual(float a, float b, float relative_tolerance=0.001)
return abs(a - b) <= relative_tolerance * (abs(a)+abs(b)).
Definition: kaldi-math.h:265

◆ TestStringRepository()

void TestStringRepository ( )

Definition at line 447 of file determinize-star-test.cc.

References rnnlm::i, StringRepository< Label, StringId >::IdOfEmpty(), StringRepository< Label, StringId >::IdOfLabel(), StringRepository< Label, StringId >::IdOfSeq(), rnnlm::j, kaldi::Rand(), StringRepository< Label, StringId >::RemovePrefix(), and StringRepository< Label, StringId >::SeqOfId().

Referenced by TestFactor().

447  {
448  typedef typename Arc::Label Label;
449 
450  StringRepository<Label, inttype> sr;
451 
452  int N = 100;
453  if (sizeof(inttype) == 1) N = 64;
454  std::vector<std::vector<Label> > strings(N);
455  std::vector<inttype> ids(N);
456 
457  for (int i = 0;i < N;i++) {
458  size_t len = kaldi::Rand() % 4;
459  std::vector<Label> vec;
460  for (size_t j = 0;j < len;j++) vec.push_back( (kaldi::Rand()%10) + 150*(kaldi::Rand()%2)); // make it have reasonable range.
461  if (i < 500 && vec.size() == 0) ids[i] = sr.IdOfEmpty();
462  else if (i < 500 && vec.size() == 1) ids[i] = sr.IdOfLabel(vec[0]);
463  else ids[i] = sr.IdOfSeq(vec);
464 
465  strings[i] = vec;
466  }
467 
468  for (int i = 0;i < N;i++) {
469  std::vector<Label> tmpv;
470  tmpv.push_back(10); // just put in garbage.
471  sr.SeqOfId(ids[i], &tmpv);
472  assert(tmpv == strings[i]);
473  assert(sr.IdOfSeq(strings[i]) == ids[i]);
474  if (strings[i].size() == 0) assert(ids[i] == sr.IdOfEmpty());
475  if (strings[i].size() == 1) assert(ids[i] == sr.IdOfLabel(strings[i][0]));
476 
477  if (sizeof(inttype) != 1) {
478  size_t prefix_len = kaldi::Rand() % (strings[i].size() + 1);
479  inttype s2 = sr.RemovePrefix(ids[i], prefix_len);
480  std::vector<Label> vec2;
481  sr.SeqOfId(s2, &vec2);
482  for (size_t j = 0;j < strings[i].size()-prefix_len;j++) {
483  assert(vec2[j] == strings[i][j+prefix_len]);
484  }
485  }
486 
487  }
488 }
fst::StdArc::Label Label
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45

◆ TestTableMatcher()

void fst::TestTableMatcher ( bool  connect,
bool  left 
)

Definition at line 28 of file table-matcher-test.cc.

References TableComposeOptions::connect, TableMatcherOptions::min_table_size, kaldi::Rand(), TableComposeOptions::table_match_type, TableMatcherOptions::table_ratio, and TableCompose().

28  {
29  typedef typename Arc::Label Label;
30  typedef typename Arc::StateId StateId;
31  typedef typename Arc::Weight Weight;
32 
33 
34  VectorFst<Arc> *fst1 = RandFst<Arc>();
35 
36  VectorFst<Arc> *fst2 = RandFst<Arc>();
37 
38  ILabelCompare<Arc> ilabel_comp;
39  OLabelCompare<Arc> olabel_comp;
40 
42  if (left) opts.table_match_type = MATCH_OUTPUT;
43  else opts.table_match_type = MATCH_INPUT;
44  opts.min_table_size = 1 + kaldi::Rand() % 5;
45  opts.table_ratio = 0.25 * (kaldi::Rand() % 5);
46  opts.connect = connect;
47 
48  ArcSort(fst1, olabel_comp);
49  ArcSort(fst2, ilabel_comp);
50 
51  VectorFst<Arc> composed;
52 
53  TableCompose(*fst1, *fst2, &composed, opts);
54 
55  if (!connect) Connect(&composed);
56 
57  VectorFst<Arc> composed_baseline;
58 
59  Compose(*fst1, *fst2, &composed_baseline);
60 
61 
62  std::cout << "Connect = "<< (connect?"True\n":"False\n");
63 
64  std::cout <<"Table-Composed FST\n";
65  {
66  FstPrinter<Arc> fstprinter(composed, NULL, NULL, NULL, false, true, "\t");
67  fstprinter.Print(&std::cout, "standard output");
68  }
69 
70  std::cout <<" Baseline-Composed FST\n";
71  {
72  FstPrinter<Arc> fstprinter(composed_baseline, NULL, NULL, NULL, false, true, "\t");
73  fstprinter.Print(&std::cout, "standard output");
74  }
75 
76  if ( !RandEquivalent(composed, composed_baseline, 3/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 20/*path length-- max?*/)) {
77  VectorFst<Arc> diff1;
78  Difference(composed, composed_baseline, &diff1);
79  std::cout <<" Diff1 (composed - baseline) \n";
80  {
81  FstPrinter<Arc> fstprinter(diff1, NULL, NULL, NULL, false, true, "\t");
82  fstprinter.Print(&std::cout, "standard output");
83  }
84 
85 
86  VectorFst<Arc> diff2;
87  Difference(composed_baseline, composed, &diff2);
88  std::cout <<" Diff2 (baseline - composed) \n";
89  {
90  FstPrinter<Arc> fstprinter(diff2, NULL, NULL, NULL, false, true, "\t");
91  fstprinter.Print(&std::cout, "standard output");
92  }
93 
94  assert(0);
95  }
96 
97  delete fst1;
98  delete fst2;
99 }
fst::StdArc::StateId StateId
void TableCompose(const Fst< Arc > &ifst1, const Fst< Arc > &ifst2, MutableFst< Arc > *ofst, const TableComposeOptions &opts=TableComposeOptions())
fst::StdArc::Label Label
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
fst::StdArc::Weight Weight

◆ TestTableMatcherCacheLeft()

void fst::TestTableMatcherCacheLeft ( bool  connect)

Definition at line 104 of file table-matcher-test.cc.

References rnnlm::i, kaldi::Rand(), TableComposeOptions::table_match_type, and TableCompose().

104  {
105  typedef typename Arc::Label Label;
106  typedef typename Arc::StateId StateId;
107  typedef typename Arc::Weight Weight;
108 
109 
110  VectorFst<Arc> *fst1 = RandFst<Arc>();
111 
112 
113  TableComposeOptions opts;
114  opts.table_match_type = MATCH_OUTPUT;
115  opts.min_table_size = 1 + kaldi::Rand() % 5;
116  opts.table_ratio = 0.25 * (kaldi::Rand() % 5);
117  opts.connect = connect;
118 
119  TableComposeCache<Fst<Arc> > cache(opts);
120 
121  for (size_t i = 0; i < 3; i++) {
122 
123  VectorFst<Arc> *fst2 = RandFst<Arc>();
124 
125  ILabelCompare<Arc> ilabel_comp;
126  OLabelCompare<Arc> olabel_comp;
127 
128 
129  ArcSort(fst1, olabel_comp);
130  ArcSort(fst2, ilabel_comp);
131 
132  VectorFst<Arc> composed;
133 
134  TableCompose(*fst1, *fst2, &composed, &cache);
135 
136  if (!connect) Connect(&composed);
137 
138  VectorFst<Arc> composed_baseline;
139 
140  Compose(*fst1, *fst2, &composed_baseline);
141 
142 
143  std::cout << "Connect = "<< (connect?"True\n":"False\n");
144 
145 
146  if ( !RandEquivalent(composed, composed_baseline, 3/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 100/*path length-- max?*/)) {
147  VectorFst<Arc> diff1;
148  Difference(composed, composed_baseline, &diff1);
149  std::cout <<" Diff1 (composed - baseline) \n";
150  {
151  FstPrinter<Arc> fstprinter(diff1, NULL, NULL, NULL, false, true, "\t");
152  fstprinter.Print(&std::cout, "standard output");
153  }
154 
155 
156  VectorFst<Arc> diff2;
157  Difference(composed_baseline, composed, &diff2);
158  std::cout <<" Diff2 (baseline - composed) \n";
159  {
160  FstPrinter<Arc> fstprinter(diff2, NULL, NULL, NULL, false, true, "\t");
161  fstprinter.Print(&std::cout, "standard output");
162  }
163 
164  assert(0);
165  }
166  delete fst2;
167  }
168 
169  delete fst1;
170 }
fst::StdArc::StateId StateId
void TableCompose(const Fst< Arc > &ifst1, const Fst< Arc > &ifst2, MutableFst< Arc > *ofst, const TableComposeOptions &opts=TableComposeOptions())
TableComposeCache lets us do multiple compositions while caching the same matcher.
fst::StdArc::Label Label
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
fst::StdArc::Weight Weight

◆ TestTableMatcherCacheRight()

void fst::TestTableMatcherCacheRight ( bool  connect)

Definition at line 173 of file table-matcher-test.cc.

References TableComposeOptions::connect, rnnlm::i, TableMatcherOptions::min_table_size, kaldi::Rand(), TableComposeOptions::table_match_type, TableMatcherOptions::table_ratio, and TableCompose().

173  {
174  typedef typename Arc::Label Label;
175  typedef typename Arc::StateId StateId;
176  typedef typename Arc::Weight Weight;
177 
178 
179  VectorFst<Arc> *fst2 = RandFst<Arc>();
180  ILabelCompare<Arc> ilabel_comp;
181  ArcSort(fst2, ilabel_comp);
182 
183 
184  TableComposeOptions opts;
185  opts.table_match_type = MATCH_INPUT;
186  opts.min_table_size = 1 + kaldi::Rand() % 5;
187  opts.table_ratio = 0.25 * (kaldi::Rand() % 5);
188  opts.connect = connect;
189 
190  TableComposeCache<Fst<Arc> > cache(opts);
191 
192  for (size_t i = 0; i < 2; i++) {
193 
194  VectorFst<Arc> *fst1 = RandFst<Arc>();
195 
196 
197  OLabelCompare<Arc> olabel_comp;
198 
199 
200  ArcSort(fst1, olabel_comp);
201 
202  VectorFst<Arc> composed;
203 
204  TableCompose(*fst1, *fst2, &composed, &cache);
205 
206  if (!connect) Connect(&composed);
207 
208  VectorFst<Arc> composed_baseline;
209 
210  Compose(*fst1, *fst2, &composed_baseline);
211 
212 
213  std::cout << "Connect = "<< (connect?"True\n":"False\n");
214 
215 
216  if ( !RandEquivalent(composed, composed_baseline, 5/*paths*/, 0.01/*delta*/, kaldi::Rand()/*seed*/, 20/*path length-- max?*/)) {
217  VectorFst<Arc> diff1;
218  Difference(composed, composed_baseline, &diff1);
219  std::cout <<" Diff1 (composed - baseline) \n";
220  {
221  FstPrinter<Arc> fstprinter(diff1, NULL, NULL, NULL, false, true, "\t");
222  fstprinter.Print(&std::cout, "standard output");
223  }
224 
225 
226  VectorFst<Arc> diff2;
227  Difference(composed_baseline, composed, &diff2);
228  std::cout <<" Diff2 (baseline - composed) \n";
229  {
230  FstPrinter<Arc> fstprinter(diff2, NULL, NULL, NULL, false, true, "\t");
231  fstprinter.Print(&std::cout, "standard output");
232  }
233 
234  assert(0);
235  }
236  delete fst1;
237  }
238 
239  delete fst2;
240 }
fst::StdArc::StateId StateId
void TableCompose(const Fst< Arc > &ifst1, const Fst< Arc > &ifst2, MutableFst< Arc > *ofst, const TableComposeOptions &opts=TableComposeOptions())
TableComposeCache lets us do multiple compositions while caching the same matcher.
fst::StdArc::Label Label
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
fst::StdArc::Weight Weight

◆ Times() [1/5]

ArcticWeightTpl<T> fst::Times ( const ArcticWeightTpl< T > &  w1,
const ArcticWeightTpl< T > &  w2 
)
inline

Definition at line 103 of file arctic-weight.h.

104  {
105  T f1 = w1.Value(), f2 = w2.Value();
106  if (f1 == -std::numeric_limits<T>::infinity())
107  return w1;
108  else if (f2 == -std::numeric_limits<T>::infinity())
109  return w2;
110  else
111  return ArcticWeightTpl<T>(f1 + f2);
112 }

◆ Times() [2/5]

ArcticWeightTpl<float> fst::Times ( const ArcticWeightTpl< float > &  w1,
const ArcticWeightTpl< float > &  w2 
)
inline

Definition at line 114 of file arctic-weight.h.

115  {
116  return Times<float>(w1, w2);
117 }

◆ Times() [3/5]

ArcticWeightTpl<double> fst::Times ( const ArcticWeightTpl< double > &  w1,
const ArcticWeightTpl< double > &  w2 
)
inline

Definition at line 119 of file arctic-weight.h.

120  {
121  return Times<double>(w1, w2);
122 }

◆ Times() [4/5]

LatticeWeightTpl<FloatType> fst::Times ( const LatticeWeightTpl< FloatType > &  w1,
const LatticeWeightTpl< FloatType > &  w2 
)
inline

Definition at line 362 of file lattice-weight.h.

References LatticeWeightTpl< FloatType >::Value1(), and LatticeWeightTpl< FloatType >::Value2().

Referenced by kaldi::AddSelfLoopsNoReorder(), kaldi::AddSelfLoopsReorder(), kaldi::AddTransitionProbs(), LatticeWordAligner::ComputationState::Advance(), LatticePhoneAligner::ComputationState::Advance(), LatticeLexiconWordAligner::ComputationState::Advance(), RemoveEpsLocalClass< Arc, ReweightPlus >::CanCombineArcs(), RemoveEpsLocalClass< Arc, ReweightPlus >::CanCombineFinal(), CompactLatticeWeightTest(), ComposeDeterministicOnDemand(), ComposeDeterministicOnDemandInverse(), ConvertLatticeWeight(), PruneSpecialClass< Arc >::Done(), LatticeDeterminizerPruned< Weight, IntType >::EpsilonClosure(), LatticeDeterminizer< Weight, IntType >::EpsilonClosure(), DeterminizerStar< F >::EpsilonClosure::ExpandOneElement(), Factor(), BackoffDeterministicOnDemandFst< Arc >::Final(), ComposeDeterministicOnDemandFst< Arc >::Final(), GrammarFstPreparer::FixArcsToFinalStates(), kaldi::GenerateActivePaths(), BackoffDeterministicOnDemandFst< Arc >::GetArc(), ComposeDeterministicOnDemandFst< Arc >::GetArc(), BiglmFasterDecoder::GetBestPath(), GetLinearSymbolSequence(), LatticeWeightTest(), main(), PushSpecialClass::ModifyFst(), PenalizeArcsWithSomeInputSymbols(), PrintProxyFstPath(), LatticeIncrementalDeterminizer::ProcessArcsFromChunkStartState(), LatticeBiglmFasterDecoder::ProcessEmitting(), DeterminizerStar< F >::ProcessFinal(), LatticeDeterminizerPruned< Weight, IntType >::ProcessFinal(), LatticeDeterminizer< Weight, IntType >::ProcessFinal(), PruneSpecialClass< Arc >::ProcessState(), PrunedCompactLatticeComposer::ProcessTransition(), LatticeDeterminizerPruned< Weight, IntType >::ProcessTransition(), LatticeDeterminizer< Weight, IntType >::ProcessTransition(), DeterminizerStar< F >::ProcessTransitions(), LatticeDeterminizerPruned< Weight, IntType >::ProcessTransitions(), LatticeDeterminizer< Weight, IntType >::ProcessTransitions(), PropagateFinalInternal(), BiglmFasterDecoder::PropagateLm(), LatticeBiglmFasterDecoder::PropagateLm(), PushCompactLatticeWeights(), BiglmFasterDecoder::ReachedFinal(), RemoveEpsLocalClass< Arc, ReweightPlus >::Reweight(), LatticeIncrementalDeterminizer::SetFinalCosts(), Times(), BiglmFasterDecoder::Token::Token(), LatticeIncrementalDeterminizer::TransferArcsToClat(), and WalkSinglePath().

363  {
364  return LatticeWeightTpl<FloatType>(w1.Value1()+w2.Value1(), w1.Value2()+w2.Value2());
365 }

◆ Times() [5/5]

CompactLatticeWeightTpl<WeightType, IntType> fst::Times ( const CompactLatticeWeightTpl< WeightType, IntType > &  w1,
const CompactLatticeWeightTpl< WeightType, IntType > &  w2 
)
inline

Definition at line 671 of file lattice-weight.h.

References CompactLatticeWeightTpl< WeightType, IntType >::String(), Times(), CompactLatticeWeightTpl< WeightType, IntType >::Weight(), and CompactLatticeWeightTpl< WeightType, IntType >::Zero().

673  {
674  WeightType w = Times(w1.Weight(), w2.Weight());
675  if (w == WeightType::Zero()) {
676  return CompactLatticeWeightTpl<WeightType, IntType>::Zero();
677  // special case to ensure zero is unique
678  } else {
679  std::vector<IntType> v;
680  v.resize(w1.String().size() + w2.String().size());
681  typename std::vector<IntType>::iterator iter = v.begin();
682  iter = std::copy(w1.String().begin(), w1.String().end(), iter); // returns end of first range.
683  std::copy(w2.String().begin(), w2.String().end(), iter);
684  return CompactLatticeWeightTpl<WeightType, IntType>(w, v);
685  }
686 }
CompactLatticeWeightTpl< WeightType, IntType > Times(const CompactLatticeWeightTpl< WeightType, IntType > &w1, const CompactLatticeWeightTpl< WeightType, IntType > &w2)

◆ WalkSinglePath()

Weight fst::WalkSinglePath ( StdVectorFst ifst,
DeterministicOnDemandFst< StdArc > *  dfst 
)

Definition at line 120 of file deterministic-fst-test.cc.

References DeterministicOnDemandFst< Arc >::Final(), DeterministicOnDemandFst< Arc >::GetArc(), KALDI_LOG, DeterministicOnDemandFst< Arc >::Start(), and Times().

Referenced by TestCompose().

120  {
121  StdArc oarc; // = new StdArc();
122  StateId isrc=ifst->Start();
123  StateId dsrc=dfst->Start();
124  Weight totalCost = Weight::One();
125 
126  while (ifst->Final(isrc) == Weight::Zero()) { // while not final
127  fst::ArcIterator<StdVectorFst> aiter(*ifst, isrc);
128  const StdArc &iarc = aiter.Value();
129  if (dfst->GetArc(dsrc, iarc.olabel, &oarc)) {
130  Weight cost = Times(iarc.weight, oarc.weight);
131  // cout << " Matched label "<<iarc.olabel<<" at summed cost "<<cost<<endl;
132  totalCost = Times(totalCost, cost);
133  } else {
134  cout << " Can't match arc ["<<iarc.ilabel<<","<<iarc.olabel<<","<<iarc.weight<<"] from "<<isrc<<endl;
135  exit(1);
136  }
137  isrc = iarc.nextstate;
138  KALDI_LOG << "Setting dsrc = " << oarc.nextstate;
139  dsrc = oarc.nextstate;
140  }
141  totalCost = Times(totalCost, dfst->Final(dsrc));
142 
143  cout << " Total cost: " << totalCost << endl;
144  return totalCost;
145 }
fst::StdArc::StateId StateId
virtual bool GetArc(StateId s, Label ilabel, Arc *oarc)=0
Note: ilabel must not be epsilon.
virtual Weight Final(StateId s)=0
fst::StdArc StdArc
virtual StateId Start()=0
LatticeWeightTpl< FloatType > Times(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2)
fst::StdArc::Weight Weight
#define KALDI_LOG
Definition: kaldi-error.h:153

◆ WriteFstKaldi() [1/2]

void WriteFstKaldi ( std::ostream &  os,
bool  binary,
const VectorFst< Arc > &  t 
)

Definition at line 31 of file kaldi-fst-io-inl.h.

References KALDI_ERR.

Referenced by main(), and VectorFstTplHolder< Arc >::Write().

32  {
33  bool ok;
34  if (binary) {
35  // Binary-mode writing.
36  ok = t.Write(os, FstWriteOptions());
37  } else {
38  // Text-mode output. Note: we expect that t.InputSymbols() and
39  // t.OutputSymbols() would always return NULL. The corresponding input
40  // routine would not work if the FST actually had symbols attached. Write a
41  // newline to start the FST; in a table, the first line of the FST will
42  // appear on its own line.
43  os << '\n';
44  bool acceptor = false, write_one = false;
45  FstPrinter<Arc> printer(t, t.InputSymbols(), t.OutputSymbols(),
46  NULL, acceptor, write_one, "\t");
47  printer.Print(&os, "<unknown>");
48  if (os.fail())
49  KALDI_ERR << "Stream failure detected writing FST to stream";
50  // Write another newline as a terminating character. The read routine will
51  // detect this [this is a Kaldi mechanism, not something in the original
52  // OpenFst code].
53  os << '\n';
54  ok = os.good();
55  }
56  if (!ok) {
57  KALDI_ERR << "Error writing FST to stream";
58  }
59 }
#define KALDI_ERR
Definition: kaldi-error.h:147

◆ WriteFstKaldi() [2/2]

void WriteFstKaldi ( const VectorFst< StdArc > &  fst,
std::string  wxfilename 
)

Definition at line 115 of file kaldi-fst-io.cc.

References kaldi::PrintableWxfilename(), and Output::Stream().

116  {
117  if (wxfilename == "") wxfilename = "-"; // interpret "" as stdout,
118  // for compatibility with OpenFst conventions.
119  bool write_binary = true, write_header = false;
120  kaldi::Output ko(wxfilename, write_binary, write_header);
121  FstWriteOptions wopts(kaldi::PrintableWxfilename(wxfilename));
122  fst.Write(ko.Stream(), wopts);
123 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
std::string PrintableWxfilename(const std::string &wxfilename)
PrintableWxfilename turns the wxfilename into a more human-readable form for error reporting...
Definition: kaldi-io.cc:73

◆ WriteILabelInfo() [1/2]

void fst::WriteILabelInfo ( std::ostream &  os,
bool  binary,
const std::vector< std::vector< int32 > > &  ilabel_info 
)

Utility function for writing ilabel-info vectors to disk.

Definition at line 325 of file context-fst.cc.

References rnnlm::i, kaldi::WriteBasicType(), and kaldi::WriteIntegerVector().

Referenced by main(), and TestContextFst().

326  {
327  int32 size = info.size();
328  kaldi::WriteBasicType(os, binary, size);
329  for (int32 i = 0; i < size; i++) {
330  kaldi::WriteIntegerVector(os, binary, info[i]);
331  }
332 }
kaldi::int32 int32
void WriteIntegerVector(std::ostream &os, bool binary, const std::vector< T > &v)
Function for writing STL vectors of integer types.
Definition: io-funcs-inl.h:198
void WriteBasicType(std::ostream &os, bool binary, T t)
WriteBasicType is the name of the write function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:34

◆ WriteILabelInfo() [2/2]

void fst::WriteILabelInfo ( std::ostream &  os,
bool  binary,
const vector< vector< int32 > > &  info 
)

Utility function for writing ilabel-info vectors to disk.

Definition at line 325 of file context-fst.cc.

References rnnlm::i, kaldi::WriteBasicType(), and kaldi::WriteIntegerVector().

Referenced by main(), and TestContextFst().

326  {
327  int32 size = info.size();
328  kaldi::WriteBasicType(os, binary, size);
329  for (int32 i = 0; i < size; i++) {
330  kaldi::WriteIntegerVector(os, binary, info[i]);
331  }
332 }
kaldi::int32 int32
void WriteIntegerVector(std::ostream &os, bool binary, const std::vector< T > &v)
Function for writing STL vectors of integer types.
Definition: io-funcs-inl.h:198
void WriteBasicType(std::ostream &os, bool binary, T t)
WriteBasicType is the name of the write function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:34