transcripts-to-fsts.cc
Go to the documentation of this file.
1 // kwsbin/transcripts-to-fsts.cc
2 
3 // Copyright 2012 Johns Hopkins University (Author: Guoguo Chen)
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 
21 #include "base/kaldi-common.h"
22 #include "util/common-utils.h"
23 #include "fstext/kaldi-fst-io.h"
24 #include "fstext/fstext-utils.h"
25 
26 namespace kaldi {
27 void SetLinearAcceptorWeight(double cost, fst::VectorFst<fst::StdArc> *fst) {
28  typedef typename fst::StdArc::Label Label;
29  typedef typename fst::StdArc::Weight Weight;
30  typedef typename fst::StdArc::StateId StateId;
31 
32  StateId start = fst->Start();
33  fst::MutableArcIterator<fst::VectorFst<fst::StdArc> > aiter(fst, start);
34  fst::StdArc arc = aiter.Value();
35  arc.weight = cost;
36  aiter.SetValue(arc);
37 }
38 } // namespace kaldi
39 
40 int main(int argc, char *argv[]) {
41  try {
42  using namespace kaldi;
43  using namespace fst;
44  typedef kaldi::int32 int32;
45  typedef kaldi::uint64 uint64;
46 
47  const char *usage =
48  "Build a linear acceptor for each transcription in the archive. "
49  "Read in the transcriptions in archive format and write out the linear "
50  "acceptors in archive format with the same key. The costs of "
51  "the arcs are set to be zero. The cost of the acceptor can be changed\n"
52  "by supplying the costs archive. In that case, the first arc's cost\n"
53  "will be set to the value obtained from the archive, i.e. the total\n"
54  "cost will be equal to cost. The cost archive can be sparse, i.e.\n"
55  "does not have to include zero-cost transcriptions. It is prefered\n"
56  "for the archive to be sorted (for efficiency).\n"
57  "\n"
58  "Usage: \n"
59  " transcripts-to-fsts [options] <transcriptions-rspecifier>"
60  " [<costs-rspecifier>] <fsts-wspecifier>\n"
61  "e.g.: \n"
62  " transcripts-to-fsts ark:train.tra ark,s,cs,t:costs.txt "
63  " ark:train.fsts\n";
64 
65  ParseOptions po(usage);
66 
67  std::string left_compose = "";
68  std::string right_compose = "";
69  bool project_input = false;
70  bool project_output = false;
71 
72  po.Register("left-compose", &left_compose,
73  "Compose the given FST to the left");
74  po.Register("right-compose", &right_compose,
75  "Compose the given FST to the right");
76  po.Register("project-input", &project_input,
77  "Project input labels if true "
78  "(makes sense only with connection to left|right composition)");
79  po.Register("project-output", &project_output,
80  "Project output labels if true"
81  "(makes sense only with connection to left|right composition)");
82 
83  po.Read(argc, argv);
84 
85  if (po.NumArgs() < 2 || po.NumArgs() > 3) {
86  po.PrintUsage();
87  exit(1);
88  }
89 
90  std::string transcript_rspecifier,
91  costs_rspecifier,
92  fst_wspecifier;
93 
94  if ( po.NumArgs() == 2 ) {
95  transcript_rspecifier = po.GetArg(1);
96  fst_wspecifier = po.GetArg(2);
97  } else {
98  transcript_rspecifier = po.GetArg(1);
99  costs_rspecifier = po.GetArg(2);
100  fst_wspecifier = po.GetArg(3);
101  }
102 
103 
104  SequentialInt32VectorReader transcript_reader(transcript_rspecifier);
105  RandomAccessDoubleReader costs_reader(costs_rspecifier);
106  TableWriter<VectorFstHolder> fst_writer(fst_wspecifier);
107 
108  // Read the possible given FSTs
109  VectorFst<StdArc> *lfst = NULL;
110  VectorFst<StdArc> *rfst = NULL;
111  if (left_compose != "") {
112  lfst = ReadFstKaldi(left_compose);
113  }
114  if (right_compose != "") {
115  rfst = ReadFstKaldi(right_compose);
116  }
117 
118  int32 n_done = 0;
119  for (; !transcript_reader.Done(); transcript_reader.Next()) {
120  std::string key = transcript_reader.Key();
121  std::vector<int32> transcript = transcript_reader.Value();
122  transcript_reader.FreeCurrent();
123 
124  VectorFst<StdArc> fst;
125  MakeLinearAcceptor(transcript, &fst);
126  if (costs_reader.IsOpen() && costs_reader.HasKey(key)) {
127  double cost = costs_reader.Value(key);
128  SetLinearAcceptorWeight(cost, &fst);
129  }
130 
131  if (lfst != NULL) {
132  VectorFst<StdArc> composed_fst;
133  Compose(*lfst, fst, &composed_fst);
134  fst = composed_fst;
135  }
136 
137  if (rfst != NULL) {
138  VectorFst<StdArc> composed_fst;
139  Compose(fst, *rfst, &composed_fst);
140  fst = composed_fst;
141  }
142 
143  if (project_input) {
144  Project(&fst, PROJECT_INPUT);
145  }
146 
147  if (project_output) {
148  Project(&fst, PROJECT_OUTPUT);
149  }
150 
151  fst_writer.Write(key, fst);
152 
153  n_done++;
154  }
155 
156  delete lfst;
157  delete rfst;
158 
159  KALDI_LOG << "Done " << n_done << " transcriptions";
160  return (n_done != 0 ? 0 : 1);
161  } catch(const std::exception &e) {
162  std::cerr << e.what();
163  return -1;
164  }
165 }
fst::StdArc::StateId StateId
fst::StdArc::Label Label
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
Lattice::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
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
void Write(const std::string &key, const T &value) const
void Register(const std::string &name, bool *ptr, const std::string &doc)
int main(int argc, char *argv[])
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
void MakeLinearAcceptor(const std::vector< I > &labels, MutableFst< Arc > *ofst)
Creates unweighted linear acceptor from symbol sequence.
void SetLinearAcceptorWeight(double cost, fst::VectorFst< fst::StdArc > *fst)
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.
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::Label Label
fst::StdArc::Weight Weight
int NumArgs() const
Number of positional parameters (c.f. argc-1).
Arc::Weight Weight
Definition: kws-search.cc:31
void ReadFstKaldi(std::istream &is, bool binary, VectorFst< Arc > *fst)
#define KALDI_LOG
Definition: kaldi-error.h:153