transcripts-to-fsts.cc File Reference
Include dependency graph for transcripts-to-fsts.cc:

Go to the source code of this file.

Namespaces

 kaldi
 This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for mispronunciations detection tasks, the reference:
 

Functions

void SetLinearAcceptorWeight (double cost, fst::VectorFst< fst::StdArc > *fst)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 40 of file transcripts-to-fsts.cc.

References SequentialTableReader< Holder >::Done(), SequentialTableReader< Holder >::FreeCurrent(), ParseOptions::GetArg(), RandomAccessTableReader< Holder >::HasKey(), RandomAccessTableReader< Holder >::IsOpen(), KALDI_LOG, SequentialTableReader< Holder >::Key(), fst::MakeLinearAcceptor(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), fst::ReadFstKaldi(), ParseOptions::Register(), kaldi::SetLinearAcceptorWeight(), RandomAccessTableReader< Holder >::Value(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

40  {
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 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
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
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
void ReadFstKaldi(std::istream &is, bool binary, VectorFst< Arc > *fst)
#define KALDI_LOG
Definition: kaldi-error.h:153