word-align-lattice.h
Go to the documentation of this file.
1 // lat/word-align-lattice.h
2 
3 // Copyright 2009-2012 Microsoft Corporation 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 
20 #ifndef KALDI_LAT_WORD_ALIGN_LATTICE_H_
21 #define KALDI_LAT_WORD_ALIGN_LATTICE_H_
22 #include <fst/fstlib.h>
23 #include <fst/fst-decl.h>
24 
25 #include "base/kaldi-common.h"
26 #include "util/common-utils.h"
27 #include "fstext/fstext-lib.h"
28 #include "hmm/transition-model.h"
29 #include "lat/kaldi-lattice.h"
30 
31 namespace kaldi {
32 
33 
35  // Note: use of this structure
36  // is deprecated, see WordBoundaryInfoNewOpts.
37 
38  // Note: this structure (and the code in word-align-lattice.{h,cc}
39  // makes stronger assumptions than the rest of the Kaldi toolkit:
40  // that is, it assumes you have word-position-dependent phones,
41  // with disjoint subsets of phones for (word-begin, word-end,
42  // word-internal, word-begin-and-end), and of course silence,
43  // which is assumed not to be inside a word [it will just print
44  // a warning if it is, though, and should give the right output
45  // as long as it's not at the beginning or end of a word].
46 
47  std::string wbegin_phones;
48  std::string wend_phones;
49  std::string wbegin_and_end_phones;
50  std::string winternal_phones;
51  std::string silence_phones;
54  bool reorder;
57 
58  WordBoundaryInfoOpts(): silence_label(0), partial_word_label(0),
59  reorder(true), silence_may_be_word_internal(false),
60  silence_has_olabels(false) { }
61 
62  void Register(OptionsItf *opts) {
63  opts->Register("wbegin-phones", &wbegin_phones, "Colon-separated list of "
64  "numeric ids of phones that begin a word");
65  opts->Register("wend-phones", &wend_phones, "Colon-separated list of "
66  "numeric ids of phones that end a word");
67  opts->Register("winternal-phones", &winternal_phones, "Colon-separated list "
68  "of numeric ids of phones that are internal to a word");
69  opts->Register("wbegin-and-end-phones", &wbegin_and_end_phones, "Colon-separated "
70  "list of numeric ids of phones that are used for "
71  "single-phone words.");
72  opts->Register("silence-phones", &silence_phones, "Colon-separated list of "
73  "numeric ids of phones that are used for silence (and other "
74  "non-word events such as noise - anything that doesn't have "
75  "a corresponding symbol in the lexicon.");
76  opts->Register("silence-label", &silence_label, "Numeric id of word symbol "
77  "that is to be used for silence arcs in the word-aligned "
78  "lattice (zero is OK)");
79  opts->Register("partial-word-label", &partial_word_label, "Numeric id of "
80  "word symbol that is to be used for arcs in the word-aligned "
81  "lattice corresponding to partial words at the end of "
82  "\"forced-out\" utterances (zero is OK)");
83  opts->Register("reorder", &reorder, "True if the lattices were generated "
84  "from graphs that had the --reorder option true, relating to "
85  "reordering self-loops (typically true)");
86  opts->Register("silence-may-be-word-internal", &silence_may_be_word_internal,
87  "If true, silence may appear inside words' prons (but not at begin/end!)\n");
88  opts->Register("silence-has-olabels", &silence_has_olabels,
89  "If true, silence phones have output labels in the lattice, just\n"
90  "like regular words. [This means you can't have un-labeled silences]");
91  }
92 };
93 
94 
95 // This structure is to be used for newer code, from s5 scripts on.
99  bool reorder;
100 
101  WordBoundaryInfoNewOpts(): silence_label(0), partial_word_label(0),
102  reorder(true) { }
103 
104  void Register(OptionsItf *opts) {
105  opts->Register("silence-label", &silence_label, "Numeric id of word symbol "
106  "that is to be used for silence arcs in the word-aligned "
107  "lattice (zero is OK)");
108  opts->Register("partial-word-label", &partial_word_label, "Numeric id of "
109  "word symbol that is to be used for arcs in the word-aligned "
110  "lattice corresponding to partial words at the end of "
111  "\"forced-out\" utterances (zero is OK)");
112  opts->Register("reorder", &reorder, "True if the lattices were generated "
113  "from graphs that had the --reorder option true, relating to "
114  "reordering self-loops (typically true)");
115  }
116 };
117 
118 
120  // This initializer will be deleted eventually.
121  WordBoundaryInfo(const WordBoundaryInfoOpts &opts); // Initialize from
122  // options class. Note: this throws. Don't try to catch this error
123  // and continue; catching errors thrown from initializers is dangerous.
124  // Note: the following vectors are initialized from the corresponding
125  // options strings in the options class, but if silence_may_be_word_internal=true
126  // or silence_has_olabels=true, we modify them as needed to make
127  // silence phones behave in this way.
128 
129  // This initializer is to be used in future.
132  std::string word_boundary_file);
133 
134  void Init(std::istream &stream);
135 
136  enum PhoneType {
137  kNoPhone = 0,
142  kNonWordPhone // non-word phones are typically silence phones; but the point
143  // is that there is
144  // no word label associated with them in the lattice. If a silence phone
145  // had a word label with it, we'd have to call it kWordBeginAndEndPhone.
146  };
148  if ((p < 0 || p > phone_to_type.size()))
149  KALDI_ERR << "Phone " << p << " was not specified in "
150  "word-boundary file (or options)";
151  return phone_to_type[p];
152  }
153 
154  std::vector<PhoneType> phone_to_type;
155 
156  int32 silence_label; // The integer label we give to silence words.
157  // (May be zero).
158  int32 partial_word_label; // The label we give to partially
159  // formed words that we might get at the end of the utterance
160  // if the lattice was "forced out" (no end state was reached).
161 
162  bool reorder; // True if the "reordering" of self-loops versus
163  // forward-transition was done during graph creation (will
164  // normally be true.
165 
166  private:
167  // This is to be removed eventually, when we all move to s5 scripts.
168  void SetOptions(const std::string int_list, PhoneType phone_type);
169 };
170 
191 bool WordAlignLattice(const CompactLattice &lat,
192  const TransitionModel &tmodel,
193  const WordBoundaryInfo &info,
194  int32 max_states,
195  CompactLattice *lat_out);
196 
197 
198 
205 void TestWordAlignedLattice(const CompactLattice &lat,
206  const TransitionModel &tmodel,
207  const WordBoundaryInfo &info,
208  const CompactLattice &aligned_lat);
209 
210 } // end namespace kaldi
211 #endif
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
std::vector< PhoneType > phone_to_type
kaldi::int32 int32
bool WordAlignLattice(const CompactLattice &lat, const TransitionModel &tmodel, const WordBoundaryInfo &info, int32 max_states, CompactLattice *lat_out)
Align lattice so that each arc has the transition-ids on it that correspond to the word that is on th...
static bool TestWordAlignedLattice(const WordAlignLatticeLexiconInfo &lexicon_info, const TransitionModel &tmodel, CompactLattice clat, CompactLattice aligned_clat, bool allow_duplicate_paths)
virtual void Register(const std::string &name, bool *ptr, const std::string &doc)=0
PhoneType TypeOfPhone(int32 p) const
void Register(OptionsItf *opts)
void Register(OptionsItf *opts)
#define KALDI_ERR
Definition: kaldi-error.h:147
fst::VectorFst< CompactLatticeArc > CompactLattice
Definition: kaldi-lattice.h:46