47 using namespace kaldi;
52 "Construct GrammarFst and write it to disk (or convert it to ConstFst\n" 53 "and write that to disk instead). Mostly intended for demonstration\n" 54 "and testing purposes (since it may be more convenient to construct\n" 55 "GrammarFst from code). See kaldi-asr.org/doc/grammar.html\n" 56 "Can also be used to prepares FSTs for this use, by calling\n" 57 "PrepareForGrammarFst(), which does things like adding final-probs and\n" 58 "making small structural tweaks to the FST\n" 60 "Usage (1): make-grammar-fst [options] <top-level-fst> <symbol1> <fst1> \\\n" 61 " [<symbol2> <fst2> ...]] <fst-out>\n" 63 "<symbol1>, <symbol2> are the integer ids of the corresponding\n" 64 " user-defined nonterminal symbols (e.g. #nonterm:contact_list) in the\n" 66 "e.g.: make-grammar-fst --nonterm-phones-offset=317 HCLG.fst \\\n" 67 " 320 HCLG1.fst HCLG_grammar.fst\n" 69 "Usage (2): make-grammar-fst <fst-in> <fst-out>\n" 70 " Prepare individual FST for compilation into GrammarFst.\n" 71 " E.g. make-grammar-fst HCLG.fst HCLGmod.fst. The outputs of this\n" 72 " will then become the arguments <top-level-fst>, <fst1>, ... for usage\n" 75 "The --nonterm-phones-offset option is required for both usage patterns.\n";
81 int32 nonterm_phones_offset = -1;
82 bool write_as_grammar =
true;
84 po.Register(
"nonterm-phones-offset", &nonterm_phones_offset,
85 "Integer id of #nonterm_bos in phones.txt");
86 po.Register(
"write-as-grammar", &write_as_grammar,
"If true, " 87 "write as GrammarFst object; if false, convert to " 88 "ConstFst<StdArc> (readable by standard decoders) " 94 if (po.NumArgs() < 2 || po.NumArgs() % 2 != 0) {
99 if (nonterm_phones_offset < 0)
100 KALDI_ERR <<
"The --nonterm-phones-offset option must be supplied " 103 if (po.NumArgs() == 2) {
114 std::string top_fst_str = po.GetArg(1),
115 fst_out_str = po.GetArg(po.NumArgs());
117 std::shared_ptr<const ConstFst<StdArc> > top_fst(
119 std::vector<std::pair<int32, std::shared_ptr<const ConstFst<StdArc> > > > pairs;
121 int32 num_pairs = (po.NumArgs() - 2) / 2;
122 for (
int32 i = 1;
i <= num_pairs;
i++) {
124 std::string nonterm_str = po.GetArg(2*
i);
127 KALDI_ERR <<
"Expected positive integer as nonterminal, got: " 129 std::string fst_str = po.GetArg(2*
i + 1);
130 std::shared_ptr<const ConstFst<StdArc> > this_fst(
ReadAsConstFst(fst_str));
131 pairs.push_back(std::pair<
int32, std::shared_ptr<
const ConstFst<StdArc> > >(
132 nonterminal, this_fst));
139 if (write_as_grammar) {
144 VectorFst<StdArc> vfst;
147 ConstFst<StdArc> cfst(vfst);
150 bool binary =
true, write_binary_header =
false;
151 Output ko(fst_out_str, binary, write_binary_header);
153 cfst.Write(ko.Stream(), wopts);
156 KALDI_LOG <<
"Created grammar FST and wrote it to " 158 }
catch(
const std::exception &e) {
159 std::cerr << e.what();
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
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...
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
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...
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
ConstFst< StdArc > * ReadAsConstFst(std::string rxfilename)
GrammarFst is an FST that is 'stitched together' from multiple FSTs, that can recursively incorporate...
void WriteFstKaldi(std::ostream &os, bool binary, const VectorFst< Arc > &t)
void ReadFstKaldi(std::istream &is, bool binary, VectorFst< Arc > *fst)
void WriteKaldiObject(const C &c, const std::string &filename, bool binary)
std::string PrintableWxfilename(const std::string &wxfilename)
PrintableWxfilename turns the wxfilename into a more human-readable form for error reporting...
void CopyToVectorFst(GrammarFst *grammar_fst, VectorFst< StdArc > *vector_fst)
This function copies a GrammarFst to a VectorFst (intended mostly for testing and comparison purposes...