Blame view

src/kwsbin/transcripts-to-fsts.cc 5.19 KB
8dcb6dfcb   Yannick Estève   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
  // kwsbin/transcripts-to-fsts.cc
  
  // Copyright 2012  Johns Hopkins University (Author: Guoguo Chen)
  
  // See ../../COPYING for clarification regarding multiple authors
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  //  http://www.apache.org/licenses/LICENSE-2.0
  //
  // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  // MERCHANTABLITY OR NON-INFRINGEMENT.
  // See the Apache 2 License for the specific language governing permissions and
  // limitations under the License.
  
  
  #include "base/kaldi-common.h"
  #include "util/common-utils.h"
  #include "fstext/kaldi-fst-io.h"
  #include "fstext/fstext-utils.h"
  
  namespace kaldi {
  void SetLinearAcceptorWeight(double cost, fst::VectorFst<fst::StdArc> *fst) {
    typedef typename fst::StdArc::Label Label;
    typedef typename fst::StdArc::Weight Weight;
    typedef typename fst::StdArc::StateId StateId;
  
    StateId start = fst->Start();
    fst::MutableArcIterator<fst::VectorFst<fst::StdArc> > aiter(fst, start);
    fst::StdArc arc = aiter.Value();
    arc.weight = cost;
    aiter.SetValue(arc);
  }
  }  // namespace kaldi
  
  int main(int argc, char *argv[]) {
    try {
      using namespace kaldi;
      using namespace fst;
      typedef kaldi::int32 int32;
      typedef kaldi::uint64 uint64;
  
      const char *usage =
          "Build a linear acceptor for each transcription in the archive. "
          "Read in the transcriptions in archive format and write out the linear "
          "acceptors in archive format with the same key. The costs of "
          "the arcs are set to be zero. The cost of the acceptor can be changed
  "
          "by supplying the costs archive. In that case, the first arc's cost
  "
          "will be set to the value obtained from the archive, i.e. the total
  "
          "cost will be equal to cost. The cost archive can be sparse, i.e.
  "
          "does not have to include zero-cost transcriptions. It is prefered
  "
          "for the archive to be sorted (for efficiency).
  "
          "
  "
          "Usage: 
  "
          " transcripts-to-fsts [options]  <transcriptions-rspecifier>"
          " [<costs-rspecifier>] <fsts-wspecifier>
  "
          "e.g.: 
  "
          " transcripts-to-fsts ark:train.tra ark,s,cs,t:costs.txt "
          " ark:train.fsts
  ";
  
      ParseOptions po(usage);
  
      std::string left_compose = "";
      std::string right_compose = "";
      bool project_input = false;
      bool project_output = false;
  
      po.Register("left-compose", &left_compose,
          "Compose the given FST to the left");
      po.Register("right-compose", &right_compose,
          "Compose the given FST to the right");
      po.Register("project-input", &project_input,
          "Project input labels if true "
          "(makes sense only with connection to left|right composition)");
      po.Register("project-output", &project_output,
          "Project output labels if true"
          "(makes sense only with connection to left|right composition)");
  
      po.Read(argc, argv);
  
      if (po.NumArgs() < 2 || po.NumArgs() > 3) {
        po.PrintUsage();
        exit(1);
      }
  
      std::string transcript_rspecifier,
                  costs_rspecifier,
                  fst_wspecifier;
  
      if ( po.NumArgs() == 2 ) {
        transcript_rspecifier  = po.GetArg(1);
        fst_wspecifier = po.GetArg(2);
      } else {
        transcript_rspecifier  = po.GetArg(1);
        costs_rspecifier = po.GetArg(2);
        fst_wspecifier = po.GetArg(3);
      }
  
  
      SequentialInt32VectorReader transcript_reader(transcript_rspecifier);
      RandomAccessDoubleReader costs_reader(costs_rspecifier);
      TableWriter<VectorFstHolder> fst_writer(fst_wspecifier);
  
      // Read the possible given FSTs
      VectorFst<StdArc> *lfst = NULL;
      VectorFst<StdArc> *rfst = NULL;
      if (left_compose != "") {
        lfst = ReadFstKaldi(left_compose);
      }
      if (right_compose != "") {
        rfst = ReadFstKaldi(right_compose);
      }
  
      int32 n_done = 0;
      for (; !transcript_reader.Done(); transcript_reader.Next()) {
        std::string key = transcript_reader.Key();
        std::vector<int32> transcript = transcript_reader.Value();
        transcript_reader.FreeCurrent();
  
        VectorFst<StdArc> fst;
        MakeLinearAcceptor(transcript, &fst);
        if (costs_reader.IsOpen() && costs_reader.HasKey(key)) {
          double cost = costs_reader.Value(key);
          SetLinearAcceptorWeight(cost, &fst);
        }
  
        if (lfst != NULL) {
          VectorFst<StdArc> composed_fst;
          Compose(*lfst, fst, &composed_fst);
          fst = composed_fst;
        }
  
        if (rfst != NULL) {
          VectorFst<StdArc> composed_fst;
          Compose(fst, *rfst, &composed_fst);
          fst = composed_fst;
        }
  
        if (project_input) {
          Project(&fst, PROJECT_INPUT);
        }
  
        if (project_output) {
          Project(&fst, PROJECT_OUTPUT);
        }
  
        fst_writer.Write(key, fst);
  
        n_done++;
      }
  
      delete lfst;
      delete rfst;
  
      KALDI_LOG << "Done " << n_done << " transcriptions";
      return (n_done != 0 ? 0 : 1);
    } catch(const std::exception &e) {
      std::cerr << e.what();
      return -1;
    }
  }