23 #include "fst/script/print-impl.h" 28 template<
class OrigWeightType>
30 if (!ifst)
return NULL;
45 template<
class OrigWeightType>
47 if (!ifst)
return NULL;
65 fst::FstWriteOptions opts;
69 return t.Write(os, opts);
77 bool acceptor =
true, write_one =
false;
78 fst::FstPrinter<CompactLatticeArc> printer(t, t.InputSymbols(),
80 NULL, acceptor, write_one,
"\t");
81 printer.Print(&os,
"<unknown>");
108 static std::pair<Lattice*, CompactLattice*>
ReadText(
110 typedef std::pair<Lattice*, CompactLattice*> PairT;
117 string separator = FLAGS_fst_field_separator +
"\r\n";
118 while (std::getline(is, line)) {
123 if (col.size() == 0)
break;
125 if (col.size() > 5) {
126 KALDI_WARN <<
"Reading lattice: bad line in FST: " << line;
129 return PairT(static_cast<Lattice*>(NULL),
130 static_cast<CompactLattice*>(NULL));
134 KALDI_WARN <<
"FstCompiler: bad line in FST: " << line;
137 return PairT(static_cast<Lattice*>(NULL),
138 static_cast<CompactLattice*>(NULL));
141 while (s >= fst->NumStates())
144 while (s >= cfst->NumStates())
147 if (fst) fst->SetStart(s);
148 if (cfst) cfst->SetStart(s);
156 switch (col.size()) {
162 else fst->SetFinal(s, w);
190 while (d >= fst->NumStates())
202 switch (col.size()) {
208 else cfst->SetFinal(s, w);
215 arc.olabel = arc.ilabel;
217 cfst->AddArc(s, arc);
226 arc.olabel = arc.ilabel;
227 cfst->AddArc(s, arc);
233 while (d >= cfst->NumStates())
241 KALDI_WARN <<
"Bad line in lattice text format: " << line;
246 while (std::getline(is, line)) {
248 if (col.empty())
break;
250 return PairT(static_cast<Lattice*>(NULL),
251 static_cast<CompactLattice*>(NULL));
254 return PairT(fst, cfst);
257 static bool StrToWeight(
const std::string &s,
bool allow_zero, Weight *w) {
258 std::istringstream strm(s);
266 static bool StrToCWeight(
const std::string &s,
bool allow_zero, CWeight *w) {
267 std::istringstream strm(s);
279 if (lat_pair.second != NULL) {
280 delete lat_pair.first;
281 return lat_pair.second;
282 }
else if (lat_pair.first != NULL) {
293 if (lat_pair.first != NULL) {
294 delete lat_pair.second;
295 return lat_pair.first;
296 }
else if (lat_pair.second != NULL) {
309 if (!hdr.Read(is,
"<unknown>")) {
310 KALDI_WARN <<
"Reading compact lattice: error reading FST header.";
313 if (hdr.FstType() !=
"vector") {
314 KALDI_WARN <<
"Reading compact lattice: unsupported FST type: " 318 fst::FstReadOptions ropts(
"<unspecified>",
325 typedef fst::VectorFst<fst::ArcTpl<T1> > F1;
326 typedef fst::VectorFst<fst::ArcTpl<T2> > F2;
327 typedef fst::VectorFst<fst::ArcTpl<T3> > F3;
328 typedef fst::VectorFst<fst::ArcTpl<T4> > F4;
331 if (hdr.ArcType() == T1::Type()) {
333 }
else if (hdr.ArcType() == T2::Type()) {
335 }
else if (hdr.ArcType() == T3::Type()) {
337 }
else if (hdr.ArcType() == T4::Type()) {
340 KALDI_WARN <<
"FST with arc type " << hdr.ArcType()
341 <<
" cannot be converted to CompactLattice.\n";
345 KALDI_WARN <<
"Error reading compact lattice (after reading header).";
353 while (std::isspace(is.peek()) && is.peek() !=
'\n') is.get();
354 if (is.peek() ==
'\n') is.get();
356 KALDI_WARN <<
"Reading compact lattice: unexpected sequence of spaces " 357 <<
" at file position " << is.tellg();
361 return (*clat != NULL);
370 KALDI_WARN <<
"End of stream detected reading CompactLattice.";
372 }
else if (isspace(c)) {
377 }
else if (c != 214) {
379 KALDI_WARN <<
"Reading compact lattice: does not appear to be an FST " 380 <<
" [non-space but no magic number detected], file pos is " 390 fst::FstWriteOptions opts;
394 return t.Write(os, opts);
402 bool acceptor =
false, write_one =
false;
403 fst::FstPrinter<LatticeArc> printer(t, t.InputSymbols(),
405 NULL, acceptor, write_one,
"\t");
406 printer.Print(&os,
"<unknown>");
422 if (!hdr.Read(is,
"<unknown>")) {
423 KALDI_WARN <<
"Reading lattice: error reading FST header.";
426 if (hdr.FstType() !=
"vector") {
427 KALDI_WARN <<
"Reading lattice: unsupported FST type: " 431 fst::FstReadOptions ropts(
"<unspecified>",
438 typedef fst::VectorFst<fst::ArcTpl<T1> > F1;
439 typedef fst::VectorFst<fst::ArcTpl<T2> > F2;
440 typedef fst::VectorFst<fst::ArcTpl<T3> > F3;
441 typedef fst::VectorFst<fst::ArcTpl<T4> > F4;
444 if (hdr.ArcType() == T1::Type()) {
446 }
else if (hdr.ArcType() == T2::Type()) {
448 }
else if (hdr.ArcType() == T3::Type()) {
450 }
else if (hdr.ArcType() == T4::Type()) {
453 KALDI_WARN <<
"FST with arc type " << hdr.ArcType()
454 <<
" cannot be converted to Lattice.\n";
458 KALDI_WARN <<
"Error reading lattice (after reading header).";
466 while (std::isspace(is.peek()) && is.peek() !=
'\n') is.get();
467 if (is.peek() ==
'\n') is.get();
469 KALDI_WARN <<
"Reading compact lattice: unexpected sequence of spaces " 470 <<
" at file position " << is.tellg();
474 return (*lat != NULL);
486 KALDI_WARN <<
"End of stream detected reading Lattice.";
488 }
else if (isspace(c)) {
493 }
else if (c != 214) {
495 KALDI_WARN <<
"Reading compact lattice: does not appear to be an FST " 496 <<
" [non-space but no magic number detected], file pos is " fst::StdArc::StateId StateId
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...
fst::ArcTpl< LatticeWeight > LatticeArc
bool ReadLattice(std::istream &is, bool binary, Lattice **lat)
static const LatticeWeightTpl One()
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
static bool StrToWeight(const std::string &s, bool allow_zero, Weight *w)
bool Read(std::istream &is)
Lattice * ReadLatticeText(std::istream &is)
static const CompactLatticeWeightTpl< WeightType, IntType > One()
CompactLattice * ConvertToCompactLattice(fst::VectorFst< OrigWeightType > *ifst)
Converts lattice types if necessary, deleting its input.
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.
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.
static const LatticeWeightTpl Zero()
static std::pair< Lattice *, CompactLattice * > ReadText(std::istream &is)
This function reads from the FST text format; it does not know in advance whether it's a Lattice or C...
fst::VectorFst< LatticeArc > Lattice
static bool StrToCWeight(const std::string &s, bool allow_zero, CWeight *w)
LatticeReader provides (static) functions for reading both Lattice and CompactLattice, in text form.
fst::VectorFst< CompactLatticeArc > CompactLattice
CompactLattice * ReadCompactLatticeText(std::istream &is)
bool WriteLattice(std::ostream &os, bool binary, const Lattice &t)
#define KALDI_ASSERT(cond)
bool WriteCompactLattice(std::ostream &os, bool binary, const CompactLattice &t)
static const CompactLatticeWeightTpl< WeightType, IntType > Zero()
fst::ArcTpl< CompactLatticeWeight > CompactLatticeArc
bool Read(std::istream &is)
CompactLatticeWeight CWeight
Lattice * ConvertToLattice(fst::VectorFst< OrigWeightType > *ifst)
Converts lattice types if necessary, deleting its input.
bool ReadCompactLattice(std::istream &is, bool binary, CompactLattice **clat)