kaldi-lattice.h
Go to the documentation of this file.
1 // lat/kaldi-lattice.h
2 
3 // Copyright 2009-2011 Microsoft Corporation
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 
21 #ifndef KALDI_LAT_KALDI_LATTICE_H_
22 #define KALDI_LAT_KALDI_LATTICE_H_
23 
24 #include "fstext/fstext-lib.h"
25 #include "base/kaldi-common.h"
26 #include "util/common-utils.h"
27 
28 
29 namespace kaldi {
30 // will import some things above...
31 
33 
34 // careful: kaldi::int32 is not always the same C type as fst::int32
36 
39 
40 typedef fst::ArcTpl<LatticeWeight> LatticeArc;
41 
42 typedef fst::ArcTpl<CompactLatticeWeight> CompactLatticeArc;
43 
44 typedef fst::VectorFst<LatticeArc> Lattice;
45 
46 typedef fst::VectorFst<CompactLatticeArc> CompactLattice;
47 
48 // The following functions for writing and reading lattices in binary or text
49 // form are provided here in case you need to include lattices in larger,
50 // Kaldi-type objects with their own Read and Write functions. Caution: these
51 // functions return false on stream failure rather than throwing an exception as
52 // most similar Kaldi functions would do.
53 
54 bool WriteCompactLattice(std::ostream &os, bool binary,
55  const CompactLattice &clat);
56 bool WriteLattice(std::ostream &os, bool binary,
57  const Lattice &lat);
58 
59 // the following function requires that *clat be
60 // NULL when called.
61 bool ReadCompactLattice(std::istream &is, bool binary,
62  CompactLattice **clat);
63 // the following function requires that *lat be
64 // NULL when called.
65 bool ReadLattice(std::istream &is, bool binary,
66  Lattice **lat);
67 
68 
70  public:
71  typedef CompactLattice T;
72 
73  CompactLatticeHolder() { t_ = NULL; }
74 
75  static bool Write(std::ostream &os, bool binary, const T &t) {
76  // Note: we don't include the binary-mode header when writing
77  // this object to disk; this ensures that if we write to single
78  // files, the result can be read by OpenFst.
79  return WriteCompactLattice(os, binary, t);
80  }
81 
82  bool Read(std::istream &is);
83 
84  static bool IsReadInBinary() { return true; }
85 
86  T &Value() {
87  KALDI_ASSERT(t_ != NULL && "Called Value() on empty CompactLatticeHolder");
88  return *t_;
89  }
90 
91  void Clear() { delete t_; t_ = NULL; }
92 
93  void Swap(CompactLatticeHolder *other) {
94  std::swap(t_, other->t_);
95  }
96 
97  bool ExtractRange(const CompactLatticeHolder &other, const std::string &range) {
98  KALDI_ERR << "ExtractRange is not defined for this type of holder.";
99  return false;
100  }
101 
103  private:
104  T *t_;
105 };
106 
108  public:
109  typedef Lattice T;
110 
111  LatticeHolder() { t_ = NULL; }
112 
113  static bool Write(std::ostream &os, bool binary, const T &t) {
114  // Note: we don't include the binary-mode header when writing
115  // this object to disk; this ensures that if we write to single
116  // files, the result can be read by OpenFst.
117  return WriteLattice(os, binary, t);
118  }
119 
120  bool Read(std::istream &is);
121 
122  static bool IsReadInBinary() { return true; }
123 
124  T &Value() {
125  KALDI_ASSERT(t_ != NULL && "Called Value() on empty LatticeHolder");
126  return *t_;
127  }
128 
129  void Clear() { delete t_; t_ = NULL; }
130 
131  void Swap(LatticeHolder *other) {
132  std::swap(t_, other->t_);
133  }
134 
135  bool ExtractRange(const LatticeHolder &other, const std::string &range) {
136  KALDI_ERR << "ExtractRange is not defined for this type of holder.";
137  return false;
138  }
139 
141  private:
142  T *t_;
143 };
144 
148 
152 
153 
154 } // namespace kaldi
155 
156 #endif // KALDI_LAT_KALDI_LATTICE_H_
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
fst::ArcTpl< LatticeWeight > LatticeArc
Definition: kaldi-lattice.h:40
bool ReadLattice(std::istream &is, bool binary, Lattice **lat)
static bool Write(std::ostream &os, bool binary, const T &t)
Definition: kaldi-lattice.h:75
fst::CompactLatticeWeightTpl< LatticeWeight, int32 > CompactLatticeWeight
Definition: kaldi-lattice.h:35
RandomAccessTableReader< LatticeHolder > RandomAccessLatticeReader
void swap(basic_filebuf< CharT, Traits > &x, basic_filebuf< CharT, Traits > &y)
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
fst::CompactLatticeWeightCommonDivisorTpl< LatticeWeight, int32 > CompactLatticeWeightCommonDivisor
Definition: kaldi-lattice.h:38
TableWriter< CompactLatticeHolder > CompactLatticeWriter
bool ExtractRange(const CompactLatticeHolder &other, const std::string &range)
Definition: kaldi-lattice.h:97
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
fst::LatticeWeightTpl< BaseFloat > LatticeWeight
Definition: kaldi-lattice.h:32
static bool Write(std::ostream &os, bool binary, const T &t)
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
fst::VectorFst< LatticeArc > Lattice
Definition: kaldi-lattice.h:44
#define KALDI_ERR
Definition: kaldi-error.h:147
fst::VectorFst< CompactLatticeArc > CompactLattice
Definition: kaldi-lattice.h:46
RandomAccessTableReader< CompactLatticeHolder > RandomAccessCompactLatticeReader
static bool IsReadInBinary()
SequentialTableReader< LatticeHolder > SequentialLatticeReader
bool WriteLattice(std::ostream &os, bool binary, const Lattice &t)
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
bool WriteCompactLattice(std::ostream &os, bool binary, const CompactLattice &t)
SequentialTableReader< CompactLatticeHolder > SequentialCompactLatticeReader
bool ExtractRange(const LatticeHolder &other, const std::string &range)
fst::ArcTpl< CompactLatticeWeight > CompactLatticeArc
Definition: kaldi-lattice.h:42
bool Read(std::istream &is)
void Swap(CompactLatticeHolder *other)
Definition: kaldi-lattice.h:93
TableWriter< LatticeHolder > LatticeWriter
void Swap(LatticeHolder *other)
bool ReadCompactLattice(std::istream &is, bool binary, CompactLattice **clat)