25 #include "fst/fstlib.h" 32 VectorFst<StdArc> *
fst) {
33 bool was_input_deterministic =
true;
39 struct InfoForIlabel {
40 std::vector<size_t> arc_indexes;
45 InfoForIlabel(): new_state(-1) { }
48 std::unordered_map<Label, InfoForIlabel> label_map;
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();
58 info.tot_cost = -
kaldi::LogAdd(-info.tot_cost, -arc.weight.Value());
59 was_input_deterministic =
false;
61 info.arc_indexes.push_back(arc_index);
64 if (was_input_deterministic)
69 std::vector<Arc> new_arcs;
70 new_arcs.reserve(arc_index);
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);
80 if (info.new_state < 0) {
81 info.new_state = fst->AddState();
83 new_arcs.push_back(
Arc(ilabel, 0,
Weight(info.tot_cost),
87 fst->AddArc(info.new_state, Arc(0, arc.olabel,
88 Weight(arc.weight.Value() - info.tot_cost),
93 for (
size_t i = 0;
i < new_arcs.size();
i++)
94 fst->AddArc(s, new_arcs[
i]);
116 int main(
int argc,
char *argv[]) {
118 using namespace kaldi;
123 "Removes some (but not all) epsilons in an algorithm that will always reduce the number of\n" 124 "arcs+states. Option to preserves equivalence in tropical or log semiring, and\n" 125 "if in tropical, stochasticit in either log or tropical.\n" 127 "Usage: fstrmepslocal [in.fst [out.fst] ]\n";
130 bool use_log =
false;
131 bool stochastic_in_log =
true;
133 "Preserve equivalence in log semiring [false->tropical]\n");
134 po.
Register(
"stochastic-in-log", &stochastic_in_log,
135 "Preserve stochasticity in log semiring [false->tropical]\n");
143 std::string fst_in_filename = po.
GetOptArg(1),
152 }
catch(
const std::exception &e) {
153 std::cerr << e.what();
fst::StdArc::StateId StateId
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
void Register(const std::string &name, bool *ptr, const std::string &doc)
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
static void InputDeterminizeSingleState(StdArc::StateId s, VectorFst< StdArc > *fst)
This utility function input-determinizes a specified state s of the FST 'fst'.
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
fst::StdArc::Weight Weight
double LogAdd(double x, double y)
int main(int argc, char *argv[])
int NumArgs() const
Number of positional parameters (c.f. argc-1).
void WriteFstKaldi(std::ostream &os, bool binary, const VectorFst< Arc > &t)
void ReadFstKaldi(std::istream &is, bool binary, VectorFst< Arc > *fst)
std::string GetOptArg(int param) const