align-equal-compiled.cc
Go to the documentation of this file.
1 // bin/align-equal-compiled.cc
2 
3 // Copyright 2009-2013 Microsoft Corporation
4 // Johns Hopkins University (Author: Daniel Povey)
5 
6 // See ../../COPYING for clarification regarding multiple authors
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 // MERCHANTABLITY OR NON-INFRINGEMENT.
18 // See the Apache 2 License for the specific language governing permissions and
19 // limitations under the License.
20 
21 #include "base/kaldi-common.h"
22 #include "util/common-utils.h"
23 #include "tree/context-dep.h"
24 #include "hmm/transition-model.h"
25 #include "fstext/fstext-lib.h"
27 
28 
31 int main(int argc, char *argv[]) {
32  try {
33  using namespace kaldi;
34  typedef kaldi::int32 int32;
35  using fst::SymbolTable;
36  using fst::VectorFst;
37  using fst::StdArc;
38 
39  const char *usage = "Write an equally spaced alignment (for getting training started)"
40  "Usage: align-equal-compiled <graphs-rspecifier> <features-rspecifier> <alignments-wspecifier>\n"
41  "e.g.: \n"
42  " align-equal-compiled 1.fsts scp:train.scp ark:equal.ali\n";
43 
44  ParseOptions po(usage);
45  bool binary = true;
46  po.Register("binary", &binary, "Write output in binary mode");
47  po.Read(argc, argv);
48 
49  if (po.NumArgs() != 3) {
50  po.PrintUsage();
51  exit(1);
52  }
53 
54  std::string
55  fst_rspecifier = po.GetArg(1),
56  feature_rspecifier = po.GetArg(2),
57  alignment_wspecifier = po.GetArg(3);
58 
59 
60  SequentialTableReader<fst::VectorFstHolder> fst_reader(fst_rspecifier);
61  RandomAccessBaseFloatMatrixReader feature_reader(feature_rspecifier);
62  Int32VectorWriter alignment_writer(alignment_wspecifier);
63 
64  int32 done = 0, no_feat = 0, error = 0;
65 
66  for (; !fst_reader.Done(); fst_reader.Next()) {
67  std::string key = fst_reader.Key();
68  if (!feature_reader.HasKey(key)) {
69  KALDI_WARN << "No features for utterance " << key;
70  no_feat++;
71  } else {
72  const Matrix<BaseFloat> &features = feature_reader.Value(key);
73  VectorFst<StdArc> decode_fst(fst_reader.Value());
74  fst_reader.FreeCurrent(); // this stops copy-on-write of the fst
75  // by deleting the fst inside the reader, since we're about to mutate
76  // the fst by adding transition probs.
77 
78  if (features.NumRows() == 0) {
79  KALDI_WARN << "Zero-length utterance: " << key;
80  error++;
81  continue;
82  }
83  if (decode_fst.Start() == fst::kNoStateId) {
84  KALDI_WARN << "Empty decoding graph for " << key;
85  error++;
86  continue;
87  }
88 
89  VectorFst<StdArc> path;
90  int32 rand_seed = StringHasher()(key); // StringHasher() produces new anonymous
91  // object of type StringHasher; we then call operator () on it, with "key".
92  if (EqualAlign(decode_fst, features.NumRows(), rand_seed, &path) ) {
93  std::vector<int32> aligned_seq, words;
95  GetLinearSymbolSequence(path, &aligned_seq, &words, &w);
96  KALDI_ASSERT(aligned_seq.size() == features.NumRows());
97  alignment_writer.Write(key, aligned_seq);
98  done++;
99  } else {
100  KALDI_WARN << "AlignEqual: did not align utterence " << key;
101  error++;
102  }
103  }
104  }
105 
106  if (done != 0 && no_feat == 0 && error == 0) {
107  KALDI_LOG << "Success: done " << done << " utterances.";
108  } else {
109  KALDI_WARN << "Computed " << done << " alignments; " << no_feat
110  << " lacked features, " << error
111  << " had other errors.";
112  }
113  if (done != 0) return 0;
114  else return 1;
115  } catch(const std::exception &e) {
116  std::cerr << e.what();
117  return -1;
118  }
119 }
120 
121 
int32 words[kMaxOrder]
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
fst::StdArc StdArc
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
int main(int argc, char *argv[])
Write an equally spaced alignment (for getting training started).
A hashing function object for strings.
Definition: stl-utils.h:248
bool GetLinearSymbolSequence(const Fst< Arc > &fst, std::vector< I > *isymbols_out, std::vector< I > *osymbols_out, typename Arc::Weight *tot_weight_out)
GetLinearSymbolSequence gets the symbol sequence from a linear FST.
void Write(const std::string &key, const T &value) const
void Register(const std::string &name, bool *ptr, const std::string &doc)
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
bool EqualAlign(const Fst< Arc > &ifst, typename Arc::StateId length, int rand_seed, MutableFst< Arc > *ofst, int num_retries)
EqualAlign is similar to RandGen, but it generates a sequence with exactly "length" input symbols...
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
const T & Value(const std::string &key)
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
#define KALDI_WARN
Definition: kaldi-error.h:150
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
bool HasKey(const std::string &key)
fst::StdArc::Weight Weight
int NumArgs() const
Number of positional parameters (c.f. argc-1).
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
#define KALDI_LOG
Definition: kaldi-error.h:153