deterministic-fst.h
Go to the documentation of this file.
1 // fstext/deterministic-fst.h
2 
3 // Copyright 2011-2012 Gilles Boulianne
4 // 2014 Telepoint Global Hosting Service, LLC. (Author: David Snyder)
5 // 2012-2015 Johns Hopkins University (author: Daniel Povey)
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 // This file includes material from the OpenFST Library v1.2.7 available at
23 // http://www.openfst.org and released under the Apache License Version 2.0.
24 //
25 // See ../../COPYING for clarification regarding multiple authors
26 //
27 // Licensed under the Apache License, Version 2.0 (the "License");
28 // you may not use this file except in compliance with the License.
29 // You may obtain a copy of the License at
30 //
31 // http://www.apache.org/licenses/LICENSE-2.0
32 //
33 // Unless required by applicable law or agreed to in writing, software
34 // distributed under the License is distributed on an "AS IS" BASIS,
35 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36 // See the License for the specific language governing permissions and
37 // limitations under the License.
38 //
39 // Copyright 2005-2010 Google, Inc.
40 // Author: riley@google.com (Michael Riley)
41 
42 #ifndef KALDI_FSTEXT_DETERMINISTIC_FST_H_
43 #define KALDI_FSTEXT_DETERMINISTIC_FST_H_
44 
45 /* This header defines the DeterministicOnDemand interface,
46  which is an FST with a special interface that allows
47  only a single arc with a non-epsilon input symbol
48  out of each state.
49 */
50 
51 #include <algorithm>
52 #include <string>
53 #include <utility>
54 #include <vector>
55 
56 #include <fst/fstlib.h>
57 #include <fst/fst-decl.h>
58 
59 #include "util/stl-utils.h"
60 
61 namespace fst {
62 
65 
66 
74 template<class Arc>
76  public:
77  typedef typename Arc::StateId StateId;
78  typedef typename Arc::Weight Weight;
79  typedef typename Arc::Label Label;
80 
81  virtual StateId Start() = 0;
82 
83  virtual Weight Final(StateId s) = 0;
84 
86  virtual bool GetArc(StateId s, Label ilabel, Arc *oarc) = 0;
87 
89 };
90 
99 template<class Arc>
101  public:
102  typedef typename Arc::Weight Weight;
103  typedef typename Arc::StateId StateId;
104  typedef typename Arc::Label Label;
105 
106  explicit BackoffDeterministicOnDemandFst(const Fst<Arc> &fst);
107 
108  StateId Start() { return fst_.Start(); }
109 
110  Weight Final(StateId s);
111 
112  bool GetArc(StateId s, Label ilabel, Arc *oarc);
113 
114  private:
115  inline StateId GetBackoffState(StateId s, Weight *w);
116 
117  const Fst<Arc> &fst_;
118 };
119 
130  public:
134 
135  // Constructor does not take ownership of 'det_fst'.
138  scale_(scale), det_fst_(*det_fst) { }
139 
140  StateId Start() { return det_fst_.Start(); }
141 
142  Weight Final(StateId s) {
143  // Note: Weight is indirectly a typedef to TropicalWeight.
144  Weight final = det_fst_.Final(s);
145  if (final == Weight::Zero()) return Weight::Zero();
146  else return TropicalWeight(final.Value() * scale_);
147  }
148 
149  inline bool GetArc(StateId s, Label ilabel, StdArc *oarc) {
150  if (det_fst_.GetArc(s, ilabel, oarc)) {
151  oarc->weight = TropicalWeight(oarc->weight.Value() * scale_);
152  return true;
153  } else {
154  return false;
155  }
156  }
157 
158  private:
159  float scale_;
161 };
162 
172 template<class Arc>
174  public:
175  typedef typename Arc::Weight Weight;
176  typedef typename Arc::StateId StateId;
177  typedef typename Arc::Label Label;
178 
179  UnweightedNgramFst(int n);
180 
181  StateId Start() { return start_state_; };
182 
183  Weight Final(StateId s);
184 
185  bool GetArc(StateId s, Label ilabel, Arc *oarc);
186 
187  private:
188  typedef unordered_map<std::vector<Label>,
190  // The order of the n-gram.
191  int n_;
192  MapType state_map_;
193  StateId start_state_;
194  // Map from history-state to pair.
195  std::vector<std::vector<Label> > state_vec_;
196 };
197 
198 template<class Arc>
200  public:
201  typedef typename Arc::StateId StateId;
202  typedef typename Arc::Weight Weight;
203  typedef typename Arc::Label Label;
204 
211 
212  virtual StateId Start() { return start_state_; }
213 
214  virtual Weight Final(StateId s);
215 
216  virtual bool GetArc(StateId s, Label ilabel, Arc *oarc);
217 
218  private:
221  typedef unordered_map<std::pair<StateId, StateId>, StateId, kaldi::PairHasher<StateId> > MapType;
222  MapType state_map_;
223  std::vector<std::pair<StateId, StateId> > state_vec_; // maps from
224  // StateId to pair.
225  StateId next_state_;
226  StateId start_state_;
227 };
228 
229 template<class Arc>
231  public:
232  typedef typename Arc::StateId StateId;
233  typedef typename Arc::Weight Weight;
234  typedef typename Arc::Label Label;
235 
238  StateId num_cached_arcs = 100000);
239 
240  virtual StateId Start() { return fst_->Start(); }
241 
243  virtual Weight Final(StateId s) { return fst_->Final(s); }
244 
245  virtual bool GetArc(StateId s, Label ilabel, Arc *oarc);
246 
247  private:
248  // Get index for cached arc.
249  inline size_t GetIndex(StateId src_state, Label ilabel);
250 
253  std::vector<std::pair<StateId, Arc> > cached_arcs_;
254 };
255 
256 
261 template<class Arc>
263  public:
264  typedef typename Arc::StateId StateId;
265  typedef typename Arc::Weight Weight;
266  typedef typename Arc::Label Label;
267 
269  Label bos_symbol,
270  Label eos_symbol);
271 
272 
273  virtual StateId Start() { return start_state_; }
274 
276  virtual Weight Final(StateId s);
277 
278  virtual bool GetArc(StateId s, Label ilabel, Arc *oarc);
279 
280  private:
281  // Get index for cached arc.
282  inline size_t GetIndex(StateId src_state, Label ilabel);
283 
284  typedef unordered_map<std::vector<Label>, StateId, kaldi::VectorHasher<Label> > MapType;
285  void *lm_;
286  Label bos_symbol_; // beginning of sentence symbol
287  Label eos_symbol_; // end of sentence symbol.
288  // This example code does not handle <UNK>; we assume the LM has the same vocab as
289  // the recognizer.
290  MapType state_map_;
291  StateId start_state_;
292  std::vector<std::vector<Label> > state_vec_; // maps from history-state to pair.
293 
294  void *lm; // wouldn't really be void.
295 };
296 
297 
298 // Compose an FST (which may be a lattice) with a DeterministicOnDemandFst and
299 // store the result in fst_composed. This is mainly used for expanding lattice
300 // n-gram histories, where fst1 is a lattice and fst2 is an UnweightedNgramFst.
301 // This does not call Connect.
302 template<class Arc>
303 void ComposeDeterministicOnDemand(const Fst<Arc> &fst1,
305  MutableFst<Arc> *fst_composed);
306 
321 template<class Arc>
322 void ComposeDeterministicOnDemandInverse(const Fst<Arc> &fst1,
324  MutableFst<Arc> *fst_composed);
325 
326 
327 
328 
330 
331 } // namespace fst
332 
333 #include "deterministic-fst-inl.h"
334 
335 #endif
This class wraps an Fst, representing a language model, using the interface for "BackoffDeterministic...
std::vector< std::vector< Label > > state_vec_
fst::StdArc::StateId StateId
DeterministicOnDemandFst< StdArc > & det_fst_
virtual bool GetArc(StateId s, Label ilabel, Arc *oarc)=0
Note: ilabel must not be epsilon.
A hashing function-object for vectors.
Definition: stl-utils.h:216
virtual Weight Final(StateId s)=0
This class is for didactic purposes, it does not really do anything.
unordered_map< std::vector< Label >, StateId, kaldi::VectorHasher< Label > > MapType
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
Class ScaleDeterministicOnDemandFst takes another DeterministicOnDemandFst and scales the weights (li...
void ComposeDeterministicOnDemand(const Fst< Arc > &fst1, DeterministicOnDemandFst< Arc > *fst2, MutableFst< Arc > *fst_composed)
fst::StdArc StdArc
ScaleDeterministicOnDemandFst(float scale, DeterministicOnDemandFst< StdArc > *det_fst)
unordered_map< std::vector< Label >, StateId, kaldi::VectorHasher< Label > > MapType
virtual StateId Start()=0
std::vector< std::vector< Label > > state_vec_
DeterministicOnDemandFst< Arc > * fst1_
void ComposeDeterministicOnDemandInverse(const Fst< Arc > &right, DeterministicOnDemandFst< Arc > *left, MutableFst< Arc > *fst_composed)
This function does &#39;*fst_composed = Compose(Inverse(*fst2), fst1)&#39; Note that the arguments are revers...
class DeterministicOnDemandFst is an "FST-like" base-class.
unordered_map< std::pair< StateId, StateId >, StateId, kaldi::PairHasher< StateId > > MapType
virtual Weight Final(StateId s)
We don&#39;t bother caching the final-probs, just the arcs.
struct rnnlm::@11::@12 n
std::vector< std::pair< StateId, Arc > > cached_arcs_
DeterministicOnDemandFst< Arc > * fst_
bool GetArc(StateId s, Label ilabel, StdArc *oarc)
fst::StdArc::Label Label
fst::StdArc::Weight Weight
DeterministicOnDemandFst< Arc > * fst2_
The class UnweightedNgramFst is a DeterministicOnDemandFst whose states encode an n-gram history...
std::vector< std::pair< StateId, StateId > > state_vec_
A hashing function-object for pairs of ints.
Definition: stl-utils.h:235