kaldi-fst-io.h
Go to the documentation of this file.
1 // fstext/kaldi-fst-io.h
2 
3 // Copyright 2009-2011 Microsoft Corporation
4 // 2012-2015 Johns Hopkins University (Author: Daniel Povey)
5 // 2013 Guoguo Chen
6 
7 // See ../../COPYING for clarification regarding multiple authors
8 //
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 //
13 // http://www.apache.org/licenses/LICENSE-2.0
14 //
15 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
17 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
18 // MERCHANTABLITY OR NON-INFRINGEMENT.
19 // See the Apache 2 License for the specific language governing permissions and
20 // limitations under the License.
21 
22 #ifndef KALDI_FSTEXT_KALDI_FST_IO_H_
23 #define KALDI_FSTEXT_KALDI_FST_IO_H_
24 
25 #include <fst/fstlib.h>
26 #include <fst/fst-decl.h>
27 #include <fst/script/print-impl.h>
28 #include "base/kaldi-common.h"
29 
30 // Some functions for writing Fsts.
31 // I/O for FSTs is a bit of a mess, and not very well integrated with Kaldi's
32 // generic I/O mechanisms, because we want files containing just FSTs to
33 // be readable by OpenFST's native binaries, which is not compatible
34 // with the normal \0B header that identifies Kaldi files as containing
35 // binary data.
36 // So use the functions here with your eyes open, and with caution!
37 namespace fst {
38 
39 // Read a binary FST using Kaldi I/O mechanisms (pipes, etc.)
40 // On error, throws using KALDI_ERR. Note: this
41 // doesn't support the text-mode option that we generally like to support.
42 VectorFst<StdArc> *ReadFstKaldi(std::string rxfilename);
43 
44 // Read a binary FST using Kaldi I/O mechanisms (pipes, etc.)
45 // If it can't read the FST, if throw_on_err == true it throws using KALDI_ERR;
46 // otherwise it prints a warning and returns. Note:this
47 // doesn't support the text-mode option that we generally like to support.
48 // This version currently supports ConstFst<StdArc> or VectorFst<StdArc>
49 // (const-fst can give better performance for decoding).
50 Fst<StdArc> *ReadFstKaldiGeneric(std::string rxfilename,
51  bool throw_on_err = true);
52 
53 // This function attempts to dynamic_cast the pointer 'fst' (which will likely
54 // have been returned by ReadFstGeneric()), to the more derived
55 // type VectorFst<StdArc>. If this succeeds, it returns the same pointer;
56 // if it fails, it converts the FST type (by creating a new VectorFst<stdArc>
57 // initialized by 'fst'), prints a warning, and deletes 'fst'.
58 VectorFst<StdArc> *CastOrConvertToVectorFst(Fst<StdArc> *fst);
59 
60 // Version of ReadFstKaldi() that writes to a pointer. Assumes
61 // the FST is binary with no binary marker. Crashes on error.
62 void ReadFstKaldi(std::string rxfilename, VectorFst<StdArc> *ofst);
63 
64 // Write an FST using Kaldi I/O mechanisms (pipes, etc.)
65 // On error, throws using KALDI_ERR. For use only in code in fstbin/,
66 // as it doesn't support the text-mode option.
67 void WriteFstKaldi(const VectorFst<StdArc> &fst,
68  std::string wxfilename);
69 
70 // This is a more general Kaldi-type-IO mechanism of writing FSTs to
71 // streams, supporting binary or text-mode writing. (note: we just
72 // write the integers, symbol tables are not supported).
73 // On error, throws using KALDI_ERR.
74 template <class Arc>
75 void WriteFstKaldi(std::ostream &os, bool binary,
76  const VectorFst<Arc> &fst);
77 
78 // A generic Kaldi-type-IO mechanism of reading FSTs from streams,
79 // supporting binary or text-mode reading/writing.
80 template <class Arc>
81 void ReadFstKaldi(std::istream &is, bool binary,
82  VectorFst<Arc> *fst);
83 
84 // Read an FST file for LM (G.fst) and make it an acceptor,
85 // and make sure it is sorted on labels
86 fst::VectorFst<fst::StdArc> *ReadAndPrepareLmFst(std::string rxfilename);
87 
88 // This is a Holder class with T = VectorFst<Arc>, that meets the requirements
89 // of a Holder class as described in ../util/kaldi-holder.h. This enables us to
90 // read/write collections of FSTs indexed by strings, using the Table concept (
91 // see ../util/kaldi-table.h).
92 // Originally it was only templated on T = VectorFst<StdArc>, but as the keyword
93 // spotting stuff introduced more types of FSTs, we made it also templated on
94 // the arc.
95 template<class Arc>
97  public:
98  typedef VectorFst<Arc> T;
99 
100  VectorFstTplHolder(): t_(NULL) { }
101 
102  static bool Write(std::ostream &os, bool binary, const T &t);
103 
104  void Copy(const T &t) { // copies it into the holder.
105  Clear();
106  t_ = new T(t);
107  }
108 
109  // Reads into the holder.
110  bool Read(std::istream &is);
111 
112  // It's potentially a binary format, so must read in binary mode (linefeed
113  // translation will corrupt the file. We don't know till we open the file if
114  // it's really binary, so we need to read in binary mode to be on the safe
115  // side. Extra linefeeds won't matter, the text-mode reading code ignores
116  // them.
117  static bool IsReadInBinary() { return true; }
118 
119  T &Value() {
120  // code error if !t_.
121  if (!t_) KALDI_ERR << "VectorFstTplHolder::Value() called wrongly.";
122  return *t_;
123  }
124 
125  void Clear() {
126  if (t_) {
127  delete t_;
128  t_ = NULL;
129  }
130  }
131 
133  std::swap(t_, other->t_);
134  }
135 
137  const std::string &range) {
138  KALDI_ERR << "ExtractRange is not defined for this type of holder.";
139  return false;
140  }
141 
143  // No destructor. Assignment and
144  // copy constructor take their default implementations.
145  private:
147  T *t_;
148 };
149 
150 // Now make the original VectorFstHolder as the typedef of VectorFstHolder<StdArc>.
152 
153 
154 } // end namespace fst
155 
156 #include "fstext/kaldi-fst-io-inl.h"
157 #endif
bool Read(std::istream &is)
static bool Write(std::ostream &os, bool binary, const T &t)
Fst< StdArc > * ReadFstKaldiGeneric(std::string rxfilename, bool throw_on_err)
Definition: kaldi-fst-io.cc:45
void Swap(VectorFstTplHolder< Arc > *other)
Definition: kaldi-fst-io.h:132
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
VectorFstTplHolder< StdArc > VectorFstHolder
Definition: kaldi-fst-io.h:151
VectorFst< Arc > T
Definition: kaldi-fst-io.h:98
void swap(basic_filebuf< CharT, Traits > &x, basic_filebuf< CharT, Traits > &y)
bool ExtractRange(const VectorFstTplHolder< Arc > &other, const std::string &range)
Definition: kaldi-fst-io.h:136
KALDI_DISALLOW_COPY_AND_ASSIGN(VectorFstTplHolder)
fst::VectorFst< fst::StdArc > * ReadAndPrepareLmFst(std::string rxfilename)
#define KALDI_ERR
Definition: kaldi-error.h:147
void WriteFstKaldi(std::ostream &os, bool binary, const VectorFst< Arc > &t)
void ReadFstKaldi(std::istream &is, bool binary, VectorFst< Arc > *fst)
VectorFst< StdArc > * CastOrConvertToVectorFst(Fst< StdArc > *fst)
Definition: kaldi-fst-io.cc:94
static bool IsReadInBinary()
Definition: kaldi-fst-io.h:117
void Copy(const T &t)
Definition: kaldi-fst-io.h:104