LatticeReader Class Reference

LatticeReader provides (static) functions for reading both Lattice and CompactLattice, in text form. More...

Static Public Member Functions

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 CompactLattice in the stream so it tries to read both formats until it becomes clear which is the correct one. More...
 
static bool StrToWeight (const std::string &s, bool allow_zero, Weight *w)
 
static bool StrToCWeight (const std::string &s, bool allow_zero, CWeight *w)
 

Private Types

typedef LatticeArc Arc
 
typedef LatticeWeight Weight
 
typedef CompactLatticeArc CArc
 
typedef CompactLatticeWeight CWeight
 
typedef Arc::Label Label
 
typedef Arc::StateId StateId
 

Detailed Description

LatticeReader provides (static) functions for reading both Lattice and CompactLattice, in text form.

Definition at line 94 of file kaldi-lattice.cc.

Member Typedef Documentation

◆ Arc

typedef LatticeArc Arc
private

Definition at line 95 of file kaldi-lattice.cc.

◆ CArc

typedef CompactLatticeArc CArc
private

Definition at line 97 of file kaldi-lattice.cc.

◆ CWeight

typedef CompactLatticeWeight CWeight
private

Definition at line 98 of file kaldi-lattice.cc.

◆ Label

typedef Arc::Label Label
private

Definition at line 99 of file kaldi-lattice.cc.

◆ StateId

typedef Arc::StateId StateId
private

Definition at line 100 of file kaldi-lattice.cc.

◆ Weight

typedef LatticeWeight Weight
private

Definition at line 96 of file kaldi-lattice.cc.

Member Function Documentation

◆ ReadText()

static std::pair<Lattice*, CompactLattice*> ReadText ( std::istream &  is)
inlinestatic

This function reads from the FST text format; it does not know in advance whether it's a Lattice or CompactLattice in the stream so it tries to read both formats until it becomes clear which is the correct one.

Definition at line 108 of file kaldi-lattice.cc.

References kaldi::ConvertStringToInteger(), rnnlm::d, KALDI_WARN, LatticeWeightTpl< BaseFloat >::One(), CompactLatticeWeightTpl< WeightType, IntType >::One(), kaldi::SplitStringToVector(), LatticeReader::StrToCWeight(), and LatticeReader::StrToWeight().

Referenced by kaldi::ReadCompactLatticeText(), and kaldi::ReadLatticeText().

109  {
110  typedef std::pair<Lattice*, CompactLattice*> PairT;
111  using std::string;
112  using std::vector;
113  Lattice *fst = new Lattice();
114  CompactLattice *cfst = new CompactLattice();
115  string line;
116  size_t nline = 0;
117  string separator = FLAGS_fst_field_separator + "\r\n";
118  while (std::getline(is, line)) {
119  nline++;
120  vector<string> col;
121  // on Windows we'll write in text and read in binary mode.
122  SplitStringToVector(line, separator.c_str(), true, &col);
123  if (col.size() == 0) break; // Empty line is a signal to stop, in our
124  // archive format.
125  if (col.size() > 5) {
126  KALDI_WARN << "Reading lattice: bad line in FST: " << line;
127  delete fst;
128  delete cfst;
129  return PairT(static_cast<Lattice*>(NULL),
130  static_cast<CompactLattice*>(NULL));
131  }
132  StateId s;
133  if (!ConvertStringToInteger(col[0], &s)) {
134  KALDI_WARN << "FstCompiler: bad line in FST: " << line;
135  delete fst;
136  delete cfst;
137  return PairT(static_cast<Lattice*>(NULL),
138  static_cast<CompactLattice*>(NULL));
139  }
140  if (fst)
141  while (s >= fst->NumStates())
142  fst->AddState();
143  if (cfst)
144  while (s >= cfst->NumStates())
145  cfst->AddState();
146  if (nline == 1) {
147  if (fst) fst->SetStart(s);
148  if (cfst) cfst->SetStart(s);
149  }
150 
151  if (fst) { // we still have fst; try to read that arc.
152  bool ok = true;
153  Arc arc;
154  Weight w;
155  StateId d = s;
156  switch (col.size()) {
157  case 1 :
158  fst->SetFinal(s, Weight::One());
159  break;
160  case 2:
161  if (!StrToWeight(col[1], true, &w)) ok = false;
162  else fst->SetFinal(s, w);
163  break;
164  case 3: // 3 columns not ok for Lattice format; it's not an acceptor.
165  ok = false;
166  break;
167  case 4:
168  ok = ConvertStringToInteger(col[1], &arc.nextstate) &&
169  ConvertStringToInteger(col[2], &arc.ilabel) &&
170  ConvertStringToInteger(col[3], &arc.olabel);
171  if (ok) {
172  d = arc.nextstate;
173  arc.weight = Weight::One();
174  fst->AddArc(s, arc);
175  }
176  break;
177  case 5:
178  ok = ConvertStringToInteger(col[1], &arc.nextstate) &&
179  ConvertStringToInteger(col[2], &arc.ilabel) &&
180  ConvertStringToInteger(col[3], &arc.olabel) &&
181  StrToWeight(col[4], false, &arc.weight);
182  if (ok) {
183  d = arc.nextstate;
184  fst->AddArc(s, arc);
185  }
186  break;
187  default:
188  ok = false;
189  }
190  while (d >= fst->NumStates())
191  fst->AddState();
192  if (!ok) {
193  delete fst;
194  fst = NULL;
195  }
196  }
197  if (cfst) {
198  bool ok = true;
199  CArc arc;
200  CWeight w;
201  StateId d = s;
202  switch (col.size()) {
203  case 1 :
204  cfst->SetFinal(s, CWeight::One());
205  break;
206  case 2:
207  if (!StrToCWeight(col[1], true, &w)) ok = false;
208  else cfst->SetFinal(s, w);
209  break;
210  case 3: // compact-lattice is acceptor format: state, next-state, label.
211  ok = ConvertStringToInteger(col[1], &arc.nextstate) &&
212  ConvertStringToInteger(col[2], &arc.ilabel);
213  if (ok) {
214  d = arc.nextstate;
215  arc.olabel = arc.ilabel;
216  arc.weight = CWeight::One();
217  cfst->AddArc(s, arc);
218  }
219  break;
220  case 4:
221  ok = ConvertStringToInteger(col[1], &arc.nextstate) &&
222  ConvertStringToInteger(col[2], &arc.ilabel) &&
223  StrToCWeight(col[3], false, &arc.weight);
224  if (ok) {
225  d = arc.nextstate;
226  arc.olabel = arc.ilabel;
227  cfst->AddArc(s, arc);
228  }
229  break;
230  case 5: default:
231  ok = false;
232  }
233  while (d >= cfst->NumStates())
234  cfst->AddState();
235  if (!ok) {
236  delete cfst;
237  cfst = NULL;
238  }
239  }
240  if (!fst && !cfst) {
241  KALDI_WARN << "Bad line in lattice text format: " << line;
242  // read until we get an empty line, so at least we
243  // have a chance to read the next one (although this might
244  // be a bit futile since the calling code will get unhappy
245  // about failing to read this one.
246  while (std::getline(is, line)) {
247  SplitStringToVector(line, separator.c_str(), true, &col);
248  if (col.empty()) break;
249  }
250  return PairT(static_cast<Lattice*>(NULL),
251  static_cast<CompactLattice*>(NULL));
252  }
253  }
254  return PairT(fst, cfst);
255  }
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...
Definition: text-utils.h:118
static const LatticeWeightTpl One()
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
LatticeWeight Weight
static bool StrToWeight(const std::string &s, bool allow_zero, Weight *w)
static const CompactLatticeWeightTpl< WeightType, IntType > One()
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.
Definition: text-utils.cc:63
fst::VectorFst< LatticeArc > Lattice
Definition: kaldi-lattice.h:44
#define KALDI_WARN
Definition: kaldi-error.h:150
static bool StrToCWeight(const std::string &s, bool allow_zero, CWeight *w)
fst::VectorFst< CompactLatticeArc > CompactLattice
Definition: kaldi-lattice.h:46
CompactLatticeArc CArc
CompactLatticeWeight CWeight

◆ StrToCWeight()

static bool StrToCWeight ( const std::string &  s,
bool  allow_zero,
CWeight w 
)
inlinestatic

Definition at line 266 of file kaldi-lattice.cc.

References CompactLatticeWeightTpl< WeightType, IntType >::Zero().

Referenced by LatticeReader::ReadText().

266  {
267  std::istringstream strm(s);
268  strm >> *w;
269  if (!strm || (!allow_zero && *w == CWeight::Zero())) {
270  return false;
271  }
272  return true;
273  }
static const CompactLatticeWeightTpl< WeightType, IntType > Zero()

◆ StrToWeight()

static bool StrToWeight ( const std::string &  s,
bool  allow_zero,
Weight w 
)
inlinestatic

Definition at line 257 of file kaldi-lattice.cc.

References LatticeWeightTpl< BaseFloat >::Zero().

Referenced by LatticeReader::ReadText().

257  {
258  std::istringstream strm(s);
259  strm >> *w;
260  if (!strm || (!allow_zero && *w == Weight::Zero())) {
261  return false;
262  }
263  return true;
264  }
static const LatticeWeightTpl Zero()

The documentation for this class was generated from the following file: