kws-functions.cc
14.3 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// kws/kws-functions.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 <algorithm>
#include "lat/lattice-functions.h"
#include "kws/kws-functions.h"
#include "fstext/determinize-star.h"
#include "fstext/epsilon-property.h"
// note: this .cc file does not include everything declared in kws-functions.h;
// the remainder are defined in kws-functions2.cc (for compilation speed and
// to avoid generating too-large object files on cygwin).
namespace kaldi {
bool CompareInterval(const Interval &i1,
const Interval &i2) {
return (i1.Start() < i2.Start() ? true :
i1.Start() > i2.Start() ? false:
i1.End() < i2.End() ? true: false);
}
bool ClusterLattice(CompactLattice *clat,
const std::vector<int32> &state_times) {
using namespace fst;
typedef CompactLattice::StateId StateId;
// Hashmap to store the cluster heads.
unordered_map<StateId, std::vector<Interval> > head;
// Step 1: Iterate over the lattice to get the arcs
StateId max_id = 0;
for (StateIterator<CompactLattice> siter(*clat); !siter.Done();
siter.Next()) {
StateId state_id = siter.Value();
for (ArcIterator<CompactLattice> aiter(*clat, state_id); !aiter.Done();
aiter.Next()) {
CompactLatticeArc arc = aiter.Value();
if (state_id >= state_times.size() || arc.nextstate >= state_times.size())
return false;
if (state_id > max_id)
max_id = state_id;
if (arc.nextstate > max_id)
max_id = arc.nextstate;
head[arc.ilabel].push_back(Interval(state_times[state_id],
state_times[arc.nextstate]));
}
}
// Check if alignments and the states match
if (state_times.size() != max_id+1)
return false;
// Step 2: Iterates over the hashmap to get the cluster heads.
// We sort all the words on their start-time, and the process for getting
// the cluster heads is to take the first one as a cluster head; then go
// till we find the next one that doesn't overlap in time with the current
// cluster head, and so on.
unordered_map<StateId, std::vector<Interval> >::iterator iter;
for (iter = head.begin(); iter != head.end(); ++iter) {
// For this ilabel, sort all the arcs on time, from first to last.
sort(iter->second.begin(), iter->second.end(), CompareInterval);
std::vector<Interval> tmp;
tmp.push_back(iter->second[0]);
for (int32 i = 1; i < iter->second.size(); i++) {
if (tmp.back().End() <= iter->second[i].Start())
tmp.push_back(iter->second[i]);
}
iter->second = tmp;
}
// Step 3: Cluster arcs according to the maximum overlap: attach
// each arc to the cluster-head (as identified in Step 2) which
// has the most temporal overlap with the current arc.
for (StateIterator<CompactLattice> siter(*clat); !siter.Done();
siter.Next()) {
CompactLatticeArc::StateId state_id = siter.Value();
for (MutableArcIterator<CompactLattice> aiter(clat, state_id);
!aiter.Done(); aiter.Next()) {
CompactLatticeArc arc = aiter.Value();
// We don't cluster the epsilon arcs
if (arc.ilabel == 0)
continue;
// We cluster the non-epsilon arcs
Interval interval(state_times[state_id], state_times[arc.nextstate]);
int32 max_overlap = 0;
size_t olabel = 1;
for (int32 i = 0; i < head[arc.ilabel].size(); i++) {
int32 overlap = interval.Overlap(head[arc.ilabel][i]);
if (overlap > max_overlap) {
max_overlap = overlap;
olabel = i + 1; // need non-epsilon label.
}
}
arc.olabel = olabel;
aiter.SetValue(arc);
}
}
return true;
}
class CompactLatticeToKwsProductFstMapper {
public:
typedef CompactLatticeArc FromArc;
typedef CompactLatticeWeight FromWeight;
typedef KwsProductArc ToArc;
typedef KwsProductWeight ToWeight;
CompactLatticeToKwsProductFstMapper() {}
ToArc operator()(const FromArc &arc) const {
return ToArc(arc.ilabel,
arc.olabel,
(arc.weight == FromWeight::Zero() ?
ToWeight::Zero() :
ToWeight(arc.weight.Weight().Value1()
+arc.weight.Weight().Value2(),
(arc.weight.Weight() == LatticeWeight::Zero() ?
StdXStdprimeWeight::Zero() :
StdXStdprimeWeight::One()))),
arc.nextstate);
}
fst::MapFinalAction FinalAction() const {
return fst::MAP_NO_SUPERFINAL;
}
fst::MapSymbolsAction InputSymbolsAction() const {
return fst::MAP_COPY_SYMBOLS;
}
fst::MapSymbolsAction OutputSymbolsAction() const {
return fst::MAP_COPY_SYMBOLS;
}
uint64 Properties(uint64 props) const {
return props;
}
};
bool CreateFactorTransducer(const CompactLattice &clat,
const std::vector<int32> &state_times,
int32 utterance_id,
KwsProductFst *factor_transducer) {
using namespace fst;
typedef KwsProductArc::StateId StateId;
// We first compute the alphas and betas
bool success = false;
std::vector<double> alpha;
std::vector<double> beta;
success = ComputeCompactLatticeAlphas(clat, &alpha);
success = success && ComputeCompactLatticeBetas(clat, &beta);
if (!success)
return false;
// Now we map the CompactLattice to VectorFst<KwsProductArc>. We drop the
// alignment information and only keep the negated log-probs
Map(clat, factor_transducer, CompactLatticeToKwsProductFstMapper());
// Now do the weight pushing manually on the CompactLattice format. Note that
// the alphas and betas in Kaldi are stored as the log-probs, not the negated
// log-probs, so the equation for weight pushing is a little different from
// the original algorithm (pay attention to the sign). We push the weight to
// initial and remove the total weight, i.e., the sum of all the outgoing
// transitions and final weight at any state is equal to One() (push only the
// negated log-prob, not the alignments)
for (StateIterator<KwsProductFst>
siter(*factor_transducer); !siter.Done(); siter.Next()) {
KwsProductArc::StateId state_id = siter.Value();
for (MutableArcIterator<KwsProductFst>
aiter(factor_transducer, state_id); !aiter.Done(); aiter.Next()) {
KwsProductArc arc = aiter.Value();
BaseFloat w = arc.weight.Value1().Value();
w += beta[state_id] - beta[arc.nextstate];
KwsProductWeight weight(w, arc.weight.Value2());
arc.weight = weight;
aiter.SetValue(arc);
}
// Weight of final state
if (factor_transducer->Final(state_id) != KwsProductWeight::Zero()) {
BaseFloat w = factor_transducer->Final(state_id).Value1().Value();
w += beta[state_id];
KwsProductWeight weight(w, factor_transducer->Final(state_id).Value2());
factor_transducer->SetFinal(state_id, weight);
}
}
// Modify the alphas and set betas to zero. After that, we get the alphas and
// betas for the pushed FST. Since I will not use beta anymore, here I don't
// set them to zero. This can be derived from the weight pushing formula.
for (int32 s = 0; s < alpha.size(); s++) {
alpha[s] += beta[s] - beta[0];
if (alpha[s] > 0.1) {
KALDI_WARN << "Positive alpha " << alpha[s];
}
}
// to understand the next part, look at the comment in
// ../kwsbin/lattice-to-kws-index.cc just above the call to
// EnsureEpsilonProperty(). We use the bool has_epsilon_property mainly to
// handle the case when someone comments out that call. It should always be
// true in the normal case.
std::vector<char> state_properties;
ComputeStateInfo(*factor_transducer, &state_properties);
bool has_epsilon_property = true;
for (size_t i = 0; i < state_properties.size(); i++) {
char c = state_properties[i];
if ((c & kStateHasEpsilonArcsEntering) != 0 &&
(c & kStateHasNonEpsilonArcsEntering) != 0)
has_epsilon_property = false;
if ((c & kStateHasEpsilonArcsLeaving) != 0 &&
(c & kStateHasNonEpsilonArcsLeaving) != 0)
has_epsilon_property = false;
}
if (!has_epsilon_property) {
KALDI_WARN << "Epsilon property does not hold, reverting to old behavior.";
}
// OK, after the above preparation, we finally come to the factor generation
// step.
StateId ns = factor_transducer->NumStates();
StateId ss = factor_transducer->AddState();
StateId fs = factor_transducer->AddState();
factor_transducer->SetStart(ss);
factor_transducer->SetFinal(fs, KwsProductWeight::One());
for (StateId s = 0; s < ns; s++) {
// Add arcs from initial state to current state
if (!has_epsilon_property ||
(state_properties[s] & kStateHasNonEpsilonArcsLeaving))
factor_transducer->AddArc(ss, KwsProductArc(0, 0, KwsProductWeight(-alpha[s], StdXStdprimeWeight(state_times[s], ArcticWeight::One())), s));
// Add arcs from current state to final state
if (!has_epsilon_property ||
(state_properties[s] & kStateHasNonEpsilonArcsEntering))
factor_transducer->AddArc(s, KwsProductArc(0, utterance_id, KwsProductWeight(0, StdXStdprimeWeight(TropicalWeight::One(), state_times[s])), fs));
// The old final state is not final any more
if (factor_transducer->Final(s) != KwsProductWeight::Zero())
factor_transducer->SetFinal(s, KwsProductWeight::Zero());
}
return true;
}
void RemoveLongSilences(int32 max_silence_frames,
const std::vector<int32> &state_times,
KwsProductFst *factor_transducer) {
using namespace fst;
typedef KwsProductArc::StateId StateId;
StateId ns = factor_transducer->NumStates();
StateId ss = factor_transducer->Start();
StateId bad_state = factor_transducer->AddState();
for (StateId s = 0; s < ns; s++) {
// Skip arcs start from the initial state
if (s == ss)
continue;
for (MutableArcIterator<KwsProductFst>
aiter(factor_transducer, s); !aiter.Done(); aiter.Next()) {
KwsProductArc arc = aiter.Value();
// Skip arcs end with the final state
if (factor_transducer->Final(arc.nextstate) != KwsProductWeight::Zero())
continue;
// Non-silence arcs
if (arc.ilabel != 0)
continue;
// Short silence arcs
if (state_times[arc.nextstate]-state_times[s] <= max_silence_frames)
continue;
// The rest are the long silence arcs, we point their nextstate to
// bad_state
arc.nextstate = bad_state;
aiter.SetValue(arc);
}
}
// Trim the unsuccessful paths
Connect(factor_transducer);
}
template<class Arc>
static void DifferenceWrapper(const fst::VectorFst<Arc> &fst1,
const fst::VectorFst<Arc> &fst2,
fst::VectorFst<Arc> *difference) {
using namespace fst;
if (!fst2.Properties(kAcceptor, true)) {
// make it an acceptor by encoding the weights.
EncodeMapper<Arc> encoder(kEncodeLabels, ENCODE);
VectorFst<Arc> fst1_copy(fst1);
VectorFst<Arc> fst2_copy(fst2);
Encode(&fst1_copy, &encoder);
Encode(&fst2_copy, &encoder);
DifferenceWrapper(fst1_copy, fst2_copy, difference);
Decode(difference, encoder);
} else {
VectorFst<Arc> fst2_copy(fst2);
RmEpsilon(&fst2_copy); // or Difference will crash.
RemoveWeights(&fst2_copy); // or Difference will crash.
Difference(fst1, fst2_copy, difference);
}
}
void MaybeDoSanityCheck(const KwsLexicographicFst &index_transducer) {
typedef KwsLexicographicFst::Arc::Label Label;
if (GetVerboseLevel() < 2) return;
KwsLexicographicFst temp_transducer;
ShortestPath(index_transducer, &temp_transducer);
std::vector<Label> isymbols, osymbols;
KwsLexicographicWeight weight;
GetLinearSymbolSequence(temp_transducer, &isymbols, &osymbols, &weight);
std::ostringstream os;
for (size_t i = 0; i < isymbols.size(); i++)
os << isymbols[i] << ' ';
BaseFloat best_cost = weight.Value1().Value();
KALDI_VLOG(3) << "Best path: " << isymbols.size() << " isymbols " << ", "
<< osymbols.size() << " osymbols, isymbols are " << os.str()
<< ", best cost is " << best_cost;
// Now get second-best path. This will exclude the best path, which
// will generally correspond to the empty word sequence (there will
// be isymbols and osymbols anyway though, because of the utterance-id
// having been encoded as an osymbol (and later, the EncodeFst turning it
// into a transducer).
KwsLexicographicFst difference_transducer;
DifferenceWrapper(index_transducer, temp_transducer, &difference_transducer);
ShortestPath(difference_transducer, &temp_transducer);
GetLinearSymbolSequence(temp_transducer, &isymbols, &osymbols, &weight);
std::ostringstream os2;
for (size_t i = 0; i < isymbols.size(); i++)
os2 << isymbols[i] << ' ';
BaseFloat second_best_cost = weight.Value1().Value();
KALDI_VLOG(3) << "Second-best path: " << isymbols.size()
<< " isymbols " << ", "
<< osymbols.size() << " osymbols, isymbols are " << os2.str()
<< ", second-best cost is " << second_best_cost;
if (second_best_cost < -0.01) {
KALDI_WARN << "Negative second-best cost found " << second_best_cost;
}
}
void MaybeDoSanityCheck(const KwsProductFst &product_transducer) {
if (GetVerboseLevel() < 2) return;
KwsLexicographicFst index_transducer;
Map(product_transducer,
&index_transducer,
KwsProductFstToKwsLexicographicFstMapper());
MaybeDoSanityCheck(index_transducer);
}
} // end namespace kaldi