grammar-context-fst.cc
Go to the documentation of this file.
1 // fstext/grammar-context-fst.cc
2 
3 // Copyright 2018 Johns Hopkins University (author: Daniel Povey)
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 
21 #include "base/kaldi-error.h"
22 #include "util/stl-utils.h"
23 
24 namespace fst {
25 using std::vector;
26 
28  Label nonterm_phones_offset,
29  const vector<int32>& phones,
30  const vector<int32>& disambig_syms):
31  nonterm_phones_offset_(nonterm_phones_offset),
32  phone_syms_(phones),
33  disambig_syms_(disambig_syms) {
34 
35  { // This block does some checks.
36  std::vector<int32> all_inputs(phones);
37  all_inputs.insert(all_inputs.end(), disambig_syms.begin(),
38  disambig_syms.end());
39  all_inputs.push_back(nonterm_phones_offset);
40  size_t size = all_inputs.size();
41  kaldi::SortAndUniq(&all_inputs);
42  if (all_inputs.size() != size) {
43  KALDI_ERR << "There was overlap between disambig symbols, phones, "
44  "and/or --nonterm-phones-offset";
45  }
46  if (all_inputs.front() <= 0)
47  KALDI_ERR << "Symbols <= 0 were passed in as phones, disambig-syms, "
48  "or nonterm-phones-offset.";
49  if (all_inputs.back() != nonterm_phones_offset) {
50  // the value passed --nonterm-phones-offset is not higher numbered
51  // than all the phones and disambig syms... do some more checking.
52  for (int32 i = 1; i < 4; i++) {
53  int32 symbol = nonterm_phones_offset + i;
54  // None of the symbols --nonterm-phones-offset + {kNontermBos, kNontermBegin,
55  // kNontermEnd, kNontermReenter, kNontermUserDefined}
56  // (i.e. the special symbols plus the first user-defined symbol) may be
57  // listed as phones or disambig symbols... this doesn't make sense. We
58  // do allow disambig symbols to be higher-numbered than the nonterminal
59  // sybols, just in case that happens to be needed, but they can't overlap.
60  if (std::binary_search(all_inputs.begin(), all_inputs.end(), symbol)) {
61  KALDI_ERR << "The symbol " << symbol
62  << " = --nonterm-phones-offset + " << i
63  << " was listed as a phone or disambig symbol.";
64  }
65  }
66  }
67  if (phone_syms_.empty())
68  KALDI_WARN << "Context FST created but there are no phone symbols: probably "
69  "input FST was empty.";
70  }
71 
72  // empty vector, will be the ilabel_info vector that corresponds to epsilon,
73  // in case our FST needs to output epsilons.
74  vector<int32> empty_vec;
75  Label epsilon_label = FindLabel(empty_vec);
76  // Make sure that a label is assigned for epsilon.
77  KALDI_ASSERT(epsilon_label == 0);
78 }
79 
80 
82  if (s == 0 || phone_syms_.count(s) != 0 ||
84  return Weight::One();
85  else
86  return Weight::Zero();
87 }
88 
90  StateId s, Label ilabel, Arc *arc) {
91  // it's a rule of the DeterministicOnDemandFst that the ilabel cannot be zero.q
92  KALDI_ASSERT(ilabel != 0);
93 
94  arc->ilabel = ilabel;
95  arc->weight = Weight::One();
96 
97  if (s == 0 || phone_syms_.count(s) != 0) {
98  // This is an epsilon or phone state.
99  if (phone_syms_.count(ilabel) != 0) {
100  // The ilabel is a phone.
101  std::vector<int32> context_window(2);
102  context_window[0] = s;
103  context_window[1] = ilabel;
104  arc->olabel = FindLabel(context_window);
105  arc->nextstate = ilabel;
106  return true;
107  } else if (disambig_syms_.count(ilabel) != 0) {
108  // the ilabel is a disambiguation symbol. Make a self-loop arc that
109  // replicates the disambiguation symbol on the input.
110  // The ilabel-info vector for disambig symbols is just a single element
111  // consisting of the negative of the disambig symbols (for easier
112  // identification from code).
113  std::vector<int32> this_ilabel_info(1);
114  this_ilabel_info[0] = -ilabel;
115  arc->olabel = FindLabel(this_ilabel_info);
116  arc->nextstate = s;
117  return true;
118  } else if (ilabel == GetPhoneSymbolFor(kNontermBegin) &&
119  s == 0) {
120  // We were at the start state and saw the symbol #nonterm_begin.
121  // Output nothing, but transition to the special #nonterm_begin state.
122  // when we're in that state, arcs for phones generate special
123  // osymbols corresponding to pairs like (#nonterm_begin, p1).
124  arc->olabel = 0;
125  arc->nextstate = GetPhoneSymbolFor(kNontermBegin);
126  return true;
127  } else if (ilabel == GetPhoneSymbolFor(kNontermEnd)) {
128  // we saw #nonterm_end.
129  std::vector<int32> this_ilabel_info(2);
130  this_ilabel_info[0] = -(GetPhoneSymbolFor(kNontermEnd));
131  this_ilabel_info[1] = (s != 0 ? s : GetPhoneSymbolFor(kNontermBos));
132  arc->olabel = FindLabel(this_ilabel_info);
133  arc->nextstate = GetPhoneSymbolFor(kNontermEnd);
134  return true;
135  } else if (ilabel >= GetPhoneSymbolFor(kNontermUserDefined)) {
136  // Assume this ilabel is a user-defined nonterminal.
137  // Transition to the state kNontermUserDefined, with an olabel
138  // (#nonterm:foo, p1) where 'p1' is the current left-context.
139  std::vector<int32> this_ilabel_info(2);
140  this_ilabel_info[0] = -ilabel;
141  this_ilabel_info[1] = (s != 0 ? s : GetPhoneSymbolFor(kNontermBos));
142  arc->olabel = FindLabel(this_ilabel_info);
143  // the destination state is not specific to this user-defined symbol, it's
144  // a generic destination state.
145  arc->nextstate = GetPhoneSymbolFor(kNontermUserDefined);
146  return true;
147  } else {
148  return false;
149  }
150  } else if (s == GetPhoneSymbolFor(kNontermBegin)) {
151  if (phone_syms_.count(ilabel) != 0 || ilabel == GetPhoneSymbolFor(kNontermBos)) {
152  std::vector<int32> this_ilabel_info(2);
153  this_ilabel_info[0] = -GetPhoneSymbolFor(kNontermBegin);
154  this_ilabel_info[1] = ilabel;
155  arc->nextstate = (ilabel == GetPhoneSymbolFor(kNontermBos) ? 0 : ilabel);
156  arc->olabel = FindLabel(this_ilabel_info);
157  return true;
158  } else {
159  return false;
160  }
161  } else if (s == GetPhoneSymbolFor(kNontermEnd)) {
162  return false;
163  } else if (s == GetPhoneSymbolFor(kNontermUserDefined)) {
164  if (phone_syms_.count(ilabel) != 0 || ilabel == GetPhoneSymbolFor(kNontermBos)) {
165  std::vector<int32> this_ilabel_info(2);
166  this_ilabel_info[0] = -GetPhoneSymbolFor(kNontermReenter);
167  this_ilabel_info[1] = ilabel;
168  arc->nextstate = (ilabel == GetPhoneSymbolFor(kNontermBos) ? 0 : ilabel);
169  arc->olabel = FindLabel(this_ilabel_info);
170  return true;
171  } else {
172  return false;
173  }
174  } else {
175  // likely code error.
176  KALDI_ERR << "Invalid state encountered";
177  return false; // won't get here. suppress compiler error.
178  }
179 }
180 
182  // Finds the ilabel corresponding to this vector (creates a new ilabel if
183  // necessary).
184  VectorToLabelMap::const_iterator iter = ilabel_map_.find(label_vec);
185  if (iter == ilabel_map_.end()) { // Not already in map.
186  Label this_label = ilabel_info_.size();
187  ilabel_info_.push_back(label_vec);
188  ilabel_map_[label_vec] = this_label;
189  return this_label;
190  } else {
191  return iter->second;
192  }
193 }
194 
195 
197  int32 nonterm_phones_offset,
198  const vector<int32> &disambig_syms_in,
199  const VectorFst<StdArc> &ifst,
200  VectorFst<StdArc> *ofst,
201  std::vector<std::vector<int32> > *ilabels) {
202 
203  vector<int32> disambig_syms(disambig_syms_in);
204  std::sort(disambig_syms.begin(), disambig_syms.end());
205 
206  vector<int32> all_syms;
207  GetInputSymbols(ifst, false/*no eps*/, &all_syms);
208  std::sort(all_syms.begin(), all_syms.end());
209  vector<int32> phones;
210  for (size_t i = 0; i < all_syms.size(); i++)
211  if (!std::binary_search(disambig_syms.begin(),
212  disambig_syms.end(), all_syms[i]) &&
213  all_syms[i] < nonterm_phones_offset)
214  phones.push_back(all_syms[i]);
215 
216 
217  InverseLeftBiphoneContextFst inv_c(nonterm_phones_offset,
218  phones, disambig_syms);
219 
220  // The following statement is equivalent to the following
221  // (if FSTs had the '*' operator for composition):
222  // (*ofst) = inv(inv_c) * (*ifst)
223  ComposeDeterministicOnDemandInverse(ifst, &inv_c, ofst);
224 
225  inv_c.SwapIlabelInfo(ilabels);
226 }
227 
228 } // end namespace fst
void SwapIlabelInfo(std::vector< std::vector< int32 > > *vec)
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
kaldi::int32 int32
void SortAndUniq(std::vector< T > *vec)
Sorts and uniq&#39;s (removes duplicates) from a vector.
Definition: stl-utils.h:39
void GetInputSymbols(const Fst< Arc > &fst, bool include_eps, std::vector< I > *symbols)
GetInputSymbols gets the list of symbols on the input of fst (including epsilon, if include_eps == tr...
InverseLeftBiphoneContextFst(Label nonterm_phones_offset, const std::vector< int32 > &phones, const std::vector< int32 > &disambig_syms)
Constructor.
virtual bool GetArc(StateId s, Label ilabel, Arc *arc)
Note: ilabel must not be epsilon.
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...
std::vector< std::vector< int32 > > ilabel_info_
void ComposeContextLeftBiphone(int32 nonterm_phones_offset, const vector< int32 > &disambig_syms_in, const VectorFst< StdArc > &ifst, VectorFst< StdArc > *ofst, std::vector< std::vector< int32 > > *ilabels)
This is a variant of the function ComposeContext() which is to be used with our "grammar FST" framewo...
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
fst::StdArc::Label Label
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
Label FindLabel(const std::vector< int32 > &label_info)
Finds the label index corresponding to this context-window of phones (likely of width context_width_)...
kaldi::ConstIntegerSet< Label > disambig_syms_
int32 GetPhoneSymbolFor(enum NonterminalValues n)
kaldi::ConstIntegerSet< Label > phone_syms_