20 #ifndef KALDI_DECODER_GRAMMAR_FST_H_ 21 #define KALDI_DECODER_GRAMMAR_FST_H_ 35 #include "fst/fstlib.h" 60 GrammarFstArc(Label ilabel, Label olabel, Weight weight, StateId nextstate)
63 weight(std::move(weight)),
64 nextstate(nextstate) {}
67 #define KALDI_GRAMMAR_FST_SPECIAL_WEIGHT 4096.0 74 template<>
class ArcIterator<GrammarFst>;
144 int32 nonterm_phones_offset,
145 std::shared_ptr<
const ConstFst<StdArc> > top_fst,
146 const std::vector<std::pair<
int32, std::shared_ptr<
const ConstFst<StdArc> > > > &ifsts);
160 void Write(std::ostream &os,
bool binary)
const;
163 void Read(std::istream &os,
bool binary);
168 return static_cast<StateId
>(top_fst_->Start());
174 if (s != static_cast<StateId>(static_cast<int32>(s))) {
175 return Weight::Zero();
177 BaseStateId base_state =
static_cast<BaseStateId
>(s);
178 Weight ans = top_fst_->Final(base_state);
180 return Weight::Zero();
192 int32 instance_id = s >> 32;
193 BaseStateId base_state =
static_cast<int32>(s);
195 const ConstFst<StdArc> *base_fst = instance.
fst;
197 return base_fst->NumInputEpsilons(base_state);
203 inline std::string
Type()
const {
return "grammar"; }
213 void InitNonterminalMap();
223 bool InitEntryArcs(
int32 i);
226 void InitInstances();
258 void InitEntryOrReentryArcs(
259 const ConstFst<StdArc> &
fst,
261 int32 nonterminal_symbol,
262 std::unordered_map<int32, int32> *phone_to_arc);
266 return nonterm_phones_offset_ +
static_cast<int32>(
n);
281 void DecodeSymbol(Label label,
282 int32 *nonterminal_symbol,
283 int32 *left_context_phone);
341 static inline void CombineArcs(
const StdArc &leaving_arc,
342 const StdArc &arriving_arc,
343 float cost_correction,
353 BaseStateId state_id) {
354 std::unordered_map<BaseStateId, ExpandedState*> &expanded_states =
355 instances_[instance_id].expanded_states;
357 std::unordered_map<BaseStateId, ExpandedState*>::iterator iter =
358 expanded_states.find(state_id);
359 if (iter != expanded_states.end()) {
365 instances_[instance_id].expanded_states[state_id] = ans;
406 const ConstFst<StdArc> *
fst;
466 std::vector<std::pair<int32, std::shared_ptr<const ConstFst<StdArc> > > >
ifsts_;
509 int32 instance_id = s >> 32;
515 const ConstFst<StdArc> *base_fst = instance.
fst;
518 dest_instance_ = instance_id;
519 base_fst->InitArcIterator(s, &
data_);
529 data_.narcs = expanded_state->
arcs.size();
541 if (i_ <
data_.narcs) {
566 arc_.ilabel = src.ilabel;
567 arc_.olabel = src.olabel;
568 arc_.weight = src.weight;
569 arc_.nextstate = (
static_cast<int64
>(dest_instance_) << 32) |
601 VectorFst<StdArc> *vector_fst);
639 VectorFst<StdArc> *
fst);
fst::StdArc::StateId StateId
std::unordered_map< BaseStateId, ExpandedState * > expanded_states
const ConstFst< StdArc > * fst
std::vector< std::unordered_map< int32, int32 > > entry_arcs_
ArcIterator(const GrammarFst &fst_in, StateId s)
ExpandedState * GetExpandedState(int32 instance_id, BaseStateId state_id)
Called from the ArcIterator constructor when we encounter an FST state with nonzero final-prob...
std::unordered_map< int32, int32 > nonterminal_map_
std::vector< StdArc > arcs
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
std::vector< FstInstance > instances_
NonterminalValues
An anonymous enum to define some values for symbols used in our grammar-fst framework.
#define KALDI_GRAMMAR_FST_SPECIAL_WEIGHT
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...
std::unordered_map< int32, int32 > parent_reentry_arcs
GrammarFstArc(Label ilabel, Label olabel, Weight weight, StateId nextstate)
StdArc::StateId BaseStateId
GrammarFst()
This constructor should only be used prior to calling Read().
Represents an expanded state in an FstInstance.
std::vector< std::pair< int32, std::shared_ptr< const ConstFst< StdArc > > > > ifsts_
int32 GetPhoneSymbolFor(enum NonterminalValues n)
ArcIteratorData< StdArc > data_
GrammarFst is an FST that is 'stitched together' from multiple FSTs, that can recursively incorporate...
typename Arc::StateId StateId
const Arc & Value() const
typename StdArc::StateId BaseStateId
Weight Final(StateId s) const
fst::TropicalWeight Weight
std::shared_ptr< const ConstFst< StdArc > > top_fst_
int32 nonterm_phones_offset_
size_t NumInputEpsilons(StateId s) const
std::unordered_map< int64, int32 > child_instances
typename GrammarFst::Arc Arc
void CopyToVectorFst(GrammarFst *grammar_fst, VectorFst< StdArc > *vector_fst)
This function copies a GrammarFst to a VectorFst (intended mostly for testing and comparison purposes...