word-align-lattice-lexicon.h
Go to the documentation of this file.
1 // lat/word-align-lattice-lexicon.h
2 
3 // Copyright 2013 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_LEXICON_H_
21 #define KALDI_LAT_WORD_ALIGN_LATTICE_LEXICON_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 
49 bool ReadLexiconForWordAlign (std::istream &is,
50  std::vector<std::vector<int32> > *lexicon);
51 
52 
53 
57  public:
58  WordAlignLatticeLexiconInfo(const std::vector<std::vector<int32> > &lexicon);
59 
62  bool IsValidEntry(const std::vector<int32> &entry) const;
63 
67  int32 EquivalenceClassOf(int32 word) const;
68  protected:
70 
71  void UpdateViabilityMap(const std::vector<int32> &lexicon_entry);
72  void UpdateLexiconMap(const std::vector<int32> &lexicon_entry);
73  void UpdateNumPhonesMap(const std::vector<int32> &lexicon_entry);
74  void UpdateEquivalenceMap(const std::vector<std::vector<int32> > &lexicon);
75 
76  void FinalizeViabilityMap(); // sorts the vectors.
77 
85  typedef unordered_map<std::vector<int32>,
86  std::vector<int32>,
88 
91  typedef unordered_map<std::vector<int32>, int32,
93 
97  typedef unordered_map<int32, std::pair<int32, int32> > NumPhonesMap;
98 
101  typedef unordered_map<int32, int32> EquivalenceMap;
102 
103  // The following three variables represent various types of information
104  // gathered from the lexicon.
105  LexiconMap lexicon_map_;
106  NumPhonesMap num_phones_map_;
107  ViabilityMap viability_map_;
108 
109  // As lexicon_map but in reverse sense w.r.t. words [we only
110  // do this for asymmetric entries.] Used only in testing code.
112 
113  // This is used only in testing code; it defines a mapping from a word
114  // to the primary member of that word's equivalence-class. If an index
115  // is not present in the map, it's assumed to map to itself.
116  EquivalenceMap equivalence_map_;
117 };
118 
119 
122  bool reorder;
123  bool test;
126 
127  WordAlignLatticeLexiconOpts(): partial_word_label(0), reorder(true),
128  test(false), allow_duplicate_paths(false),
129  max_expand(-1.0) { }
130 
131  void Register(OptionsItf *opts) {
132  opts->Register("partial-word-label", &partial_word_label, "Numeric id of "
133  "word symbol that is to be used for arcs in the word-aligned "
134  "lattice corresponding to partial words at the end of "
135  "\"forced-out\" utterances (zero is OK)");
136  opts->Register("reorder", &reorder, "True if the lattices were generated "
137  "from graphs that had the --reorder option true, relating to "
138  "reordering self-loops (typically true)");
139  opts->Register("test", &test, "If true, testing code will be activated "
140  "(the purpose of this is to validate the algorithm).");
141  opts->Register("allow-duplicate-paths", &allow_duplicate_paths, "Only "
142  "has an effect if --test=true. If true, does not die "
143  "(only prints warnings) if duplicate paths are found. "
144  "This should only happen with very pathological lexicons, "
145  "e.g. as encountered in testing code.");
146  opts->Register("max-expand", &max_expand, "If >0.0, the maximum ratio "
147  "by which we allow the lattice-alignment code to increase the #states"
148  "in a lattice (vs. the phone-aligned lattice) before we fail and "
149  "refuse to align the lattice. This is helpful in order to "
150  "prevent 'pathological' lattices from causing the program to "
151  "exhaust memory. Actual max-states is 1000 + max-expand * "
152  "orig-num-states.");
153  }
154 };
155 
156 
164  const TransitionModel &tmodel,
165  const WordAlignLatticeLexiconInfo &lexicon_info,
166  const WordAlignLatticeLexiconOpts &opts,
167  CompactLattice *lat_out);
168 
169 
180  const TransitionModel &tmodel,
181  const std::vector<std::vector<int32> > &lexicon,
182  const CompactLattice &aligned_lat,
183  bool allow_duplicate_paths);
184 
185 } // end namespace kaldi
186 #endif
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
A hashing function-object for vectors.
Definition: stl-utils.h:216
void UpdateViabilityMap(const std::vector< int32 > &lexicon_entry)
bool ReadLexiconForWordAlign(std::istream &is, std::vector< std::vector< int32 > > *lexicon)
Read the lexicon in the special format required for word alignment.
unordered_map< std::vector< int32 >, int32, VectorHasher< int32 > > LexiconMap
This is a map from a vector (orig-word-symbol phone1 phone2 ...
bool IsValidEntry(const std::vector< int32 > &entry) const
Returns true if this lexicon-entry can appear, intepreted as (output-word phone1 phone2 ...
kaldi::int32 int32
bool WordAlignLatticeLexicon(const CompactLattice &lat, const TransitionModel &tmodel, const WordAlignLatticeLexiconInfo &lexicon_info, const WordAlignLatticeLexiconOpts &opts, CompactLattice *lat_out)
Align lattice so that each arc has the transition-ids on it that correspond to the word that is on th...
unordered_map< int32, int32 > EquivalenceMap
This is used only in testing code; it defines a mapping from a word to the primary member of that wor...
virtual void Register(const std::string &name, bool *ptr, const std::string &doc)=0
unordered_map< int32, std::pair< int32, int32 > > NumPhonesMap
This is a map from the word-id (as present in the original lattice) to the minimum and maximum #phone...
void TestWordAlignedLatticeLexicon(const CompactLattice &lat, const TransitionModel &tmodel, const std::vector< std::vector< int32 > > &lexicon, const CompactLattice &aligned_lat, bool allow_duplicate_paths)
This function is designed to crash if something went wrong with the word-alignment of the lattice...
unordered_map< std::vector< int32 >, std::vector< int32 >, VectorHasher< int32 > > ViabilityMap
The type ViabilityMap maps from sequences of phones (excluding the empty sequence), to the sets of all word-labels [on the input lattice] that could correspond to phone sequences that start with s [but are longer than s].
void UpdateEquivalenceMap(const std::vector< std::vector< int32 > > &lexicon)
fst::VectorFst< CompactLatticeArc > CompactLattice
Definition: kaldi-lattice.h:46
This class extracts some information from the lexicon and stores it in a suitable form for the word-a...
void UpdateNumPhonesMap(const std::vector< int32 > &lexicon_entry)
WordAlignLatticeLexiconInfo(const std::vector< std::vector< int32 > > &lexicon)
void UpdateLexiconMap(const std::vector< int32 > &lexicon_entry)
Update the map from a vector (orig-word-symbol phone1 phone2 ...
int32 EquivalenceClassOf(int32 word) const
Purely for the testing code, we map words into equivalence classes derived from the mappings in the f...