22 #ifndef KALDI_FSTEXT_KALDI_FST_IO_INL_H_ 23 #define KALDI_FSTEXT_KALDI_FST_IO_INL_H_ 32 const VectorFst<Arc> &t) {
36 ok = t.Write(os, FstWriteOptions());
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>");
49 KALDI_ERR <<
"Stream failure detected writing FST to stream";
57 KALDI_ERR <<
"Error writing FST to stream";
63 inline bool StrToWeight(
const std::string &s,
bool allow_zero, W *w) {
64 std::istringstream strm(s);
66 if (strm.fail() || (!allow_zero && *w == W::Zero())) {
74 VectorFst<Arc> *
fst) {
80 VectorFst<Arc>::Read(is, fst::FstReadOptions(std::string(
"[unknown]")));
82 KALDI_ERR <<
"Error reading FST from stream.";
89 while (std::isspace(is.peek()) && is.peek() !=
'\n') is.get();
90 if (is.peek() ==
'\n') is.get();
92 KALDI_ERR <<
"Reading FST: unexpected sequence of spaces " 93 <<
" at file position " << is.tellg();
102 string separator = FLAGS_fst_field_separator +
"\r\n";
103 while (std::getline(is, line)) {
108 if (col.size() == 0)
break;
110 if (col.size() > 5) {
111 KALDI_ERR <<
"Bad line in FST: " << line;
115 KALDI_ERR <<
"Bad line in FST: " << line;
117 while (s >= fst->NumStates())
119 if (nline == 1) fst->SetStart(s);
125 switch (col.size()) {
127 fst->SetFinal(s, Weight::One());
131 else fst->SetFinal(s, w);
142 arc.weight = Weight::One();
159 while (d >= fst->NumStates()) fst->AddState();
161 KALDI_ERR <<
"Bad line in FST: " << line;
184 KALDI_WARN <<
"End of stream detected reading Fst";
186 }
else if (isspace(c)) {
191 t_ =
new VectorFst<Arc>();
199 t_ =
new VectorFst<Arc>();
211 #endif // KALDI_FSTEXT_KALDI_FST_IO_INL_H_ fst::StdArc::StateId StateId
bool Read(std::istream &is)
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...
static bool Write(std::ostream &os, bool binary, const T &t)
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
bool SplitStringToIntegers(const std::string &full, const char *delim, bool omit_empty_strings, std::vector< I > *out)
Split a string (e.g.
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.
bool StrToWeight(const std::string &s, bool allow_zero, W *w)
fst::StdArc::Weight Weight
void WriteFstKaldi(std::ostream &os, bool binary, const VectorFst< Arc > &t)
void ReadFstKaldi(std::istream &is, bool binary, VectorFst< Arc > *fst)