Blame view
src/kwsbin/kws-search.cc
13.2 KB
8dcb6dfcb 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 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 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 |
// kwsbin/kws-search.cc // Copyright 2012-2015 Johns Hopkins University (Authors: Guoguo Chen, // Daniel Povey. // Yenda Trmal) // 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 "kws/kaldi-kws.h" namespace kaldi { typedef KwsLexicographicArc Arc; typedef Arc::Weight Weight; typedef Arc::StateId StateId; // encode ilabel, olabel pair as a single 64bit (output) symbol uint64 EncodeLabel(StateId ilabel, StateId olabel) { return (static_cast<int64>(olabel) << 32) + static_cast<int64>(ilabel); } // extract the osymbol from the 64bit symbol. That represents the utterance id // in this setup -- we throw away the isymbol which is typically 0 or an // disambiguation symbol StateId DecodeLabelUid(uint64 osymbol) { return static_cast<StateId>(osymbol >> 32); } // this is a mapper adapter that helps converting // between the StdArc FST (i.e. tropical semiring FST) // to the KwsLexicographic FST. Structure will be kept, // the weights converted/recomputed class VectorFstToKwsLexicographicFstMapper { public: typedef fst::StdArc FromArc; typedef FromArc::Weight FromWeight; typedef KwsLexicographicArc ToArc; typedef KwsLexicographicWeight ToWeight; VectorFstToKwsLexicographicFstMapper() {} ToArc operator()(const FromArc &arc) const { return ToArc(arc.ilabel, arc.olabel, (arc.weight == FromWeight::Zero() ? ToWeight::Zero() : ToWeight(arc.weight.Value(), StdLStdWeight::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; } }; struct ActivePath { std::vector<KwsLexicographicArc::Label> path; KwsLexicographicArc::Weight weight; KwsLexicographicArc::Label last; }; bool GenerateActivePaths(const KwsLexicographicFst &proxy, std::vector<ActivePath> *paths, KwsLexicographicFst::StateId cur_state, std::vector<KwsLexicographicArc::Label> cur_path, KwsLexicographicArc::Weight cur_weight) { for (fst::ArcIterator<KwsLexicographicFst> aiter(proxy, cur_state); !aiter.Done(); aiter.Next()) { const Arc &arc = aiter.Value(); Weight temp_weight = Times(arc.weight, cur_weight); cur_path.push_back(arc.ilabel); if ( arc.olabel != 0 ) { ActivePath path; path.path = cur_path; path.weight = temp_weight; path.last = arc.olabel; paths->push_back(path); } else { GenerateActivePaths(proxy, paths, arc.nextstate, cur_path, temp_weight); } cur_path.pop_back(); } return true; } } // namespace kaldi typedef kaldi::TableWriter< kaldi::BasicVectorHolder<double> > VectorOfDoublesWriter; void OutputDetailedStatistics(const std::string &kwid, const kaldi::KwsLexicographicFst &keyword, const unordered_map<uint32, uint64> &label_decoder, VectorOfDoublesWriter *output ) { std::vector<kaldi::ActivePath> paths; if (keyword.Start() == fst::kNoStateId) return; kaldi::GenerateActivePaths(keyword, &paths, keyword.Start(), std::vector<kaldi::KwsLexicographicArc::Label>(), kaldi::KwsLexicographicArc::Weight::One()); for (int i = 0; i < paths.size(); ++i) { std::vector<double> out; double score; int32 tbeg, tend, uid; uint64 osymbol = label_decoder.find(paths[i].last)->second; uid = kaldi::DecodeLabelUid(osymbol); tbeg = paths[i].weight.Value2().Value1().Value(); tend = paths[i].weight.Value2().Value2().Value(); score = paths[i].weight.Value1().Value(); out.push_back(uid); out.push_back(tbeg); out.push_back(tend); out.push_back(score); for (int j = 0; j < paths[i].path.size(); ++j) { out.push_back(paths[i].path[j]); } output->Write(kwid, out); } } int main(int argc, char *argv[]) { try { using namespace kaldi; using namespace fst; typedef kaldi::int32 int32; typedef kaldi::uint32 uint32; typedef kaldi::uint64 uint64; const char *usage = "Search the keywords over the index. This program can be executed " "in parallel, either on the index side or the keywords side; we use " "a script to combine the final search results. Note that the index " "archive has a single key \"global\". " "Search has one or two outputs. The first one is mandatory and will " "contain the seach output, i.e. list of all found keyword instances " "The file is in the following format: " "kw_id utt_id beg_frame end_frame neg_logprob " " e.g.: " "KW105-0198 7 335 376 1.91254 " "The second parameter is optional and allows the user to gather more " "statistics about the individual instances from the posting list. " "Remember \"keyword\" is an FST and as such, there can be multiple " "paths matching in the keyword and in the lattice index in that given " "time period. The stats output will provide all matching paths " "each with the appropriate score. " "The format is as follows: " "kw_id utt_id beg_frame end_frame neg_logprob 0 w_id1 w_id2 ... 0 " " e.g.: " "KW105-0198 7 335 376 16.01254 0 5766 5659 0 " " " "Usage: kws-search [options] <index-rspecifier> <keywords-rspecifier> " "<results-wspecifier> [<stats_wspecifier>] " " e.g.: kws-search ark:index.idx ark:keywords.fsts " "ark:results ark:stats "; ParseOptions po(usage); int32 n_best = -1; int32 keyword_nbest = -1; bool strict = true; double negative_tolerance = -0.1; double keyword_beam = -1; int32 frame_subsampling_factor = 1; po.Register("frame-subsampling-factor", &frame_subsampling_factor, "Frame subsampling factor. (Default value 1)"); po.Register("nbest", &n_best, "Return the best n hypotheses."); po.Register("keyword-nbest", &keyword_nbest, "Pick the best n keywords if the FST contains " "multiple keywords."); po.Register("strict", &strict, "Affects the return status of the program."); po.Register("negative-tolerance", &negative_tolerance, "The program will print a warning if we get negative score " "smaller than this tolerance."); po.Register("keyword-beam", &keyword_beam, "Prune the FST with the given beam if the FST contains " "multiple keywords."); if (n_best < 0 && n_best != -1) { KALDI_ERR << "Bad number for nbest"; exit(1); } if (keyword_nbest < 0 && keyword_nbest != -1) { KALDI_ERR << "Bad number for keyword-nbest"; exit(1); } if (keyword_beam < 0 && keyword_beam != -1) { KALDI_ERR << "Bad number for keyword-beam"; exit(1); } po.Read(argc, argv); if (po.NumArgs() < 3 || po.NumArgs() > 4) { po.PrintUsage(); exit(1); } std::string index_rspecifier = po.GetArg(1), keyword_rspecifier = po.GetArg(2), result_wspecifier = po.GetArg(3), stats_wspecifier = po.GetOptArg(4); RandomAccessTableReader< VectorFstTplHolder<KwsLexicographicArc> > index_reader(index_rspecifier); SequentialTableReader<VectorFstHolder> keyword_reader(keyword_rspecifier); VectorOfDoublesWriter result_writer(result_wspecifier); VectorOfDoublesWriter stats_writer(stats_wspecifier); // Index has key "global" KwsLexicographicFst index = index_reader.Value("global"); // First we have to remove the disambiguation symbols. But rather than // removing them totally, we actually move them from input side to output // side, making the output symbol a "combined" symbol of the disambiguation // symbols and the utterance id's. // Note that in Dogan and Murat's original paper, they simply remove the // disambiguation symbol on the input symbol side, which will not allow us // to do epsilon removal after composition with the keyword FST. They have // to traverse the resulting FST. int32 label_count = 1; unordered_map<uint64, uint32> label_encoder; unordered_map<uint32, uint64> label_decoder; for (StateIterator<KwsLexicographicFst> siter(index); !siter.Done(); siter.Next()) { StateId state_id = siter.Value(); for (MutableArcIterator<KwsLexicographicFst> aiter(&index, state_id); !aiter.Done(); aiter.Next()) { KwsLexicographicArc arc = aiter.Value(); // Skip the non-final arcs if (index.Final(arc.nextstate) == Weight::Zero()) continue; // Encode the input and output label of the final arc, and this is the // new output label for this arc; set the input label to <epsilon> uint64 osymbol = EncodeLabel(arc.ilabel, arc.olabel); arc.ilabel = 0; if (label_encoder.find(osymbol) == label_encoder.end()) { arc.olabel = label_count; label_encoder[osymbol] = label_count; label_decoder[label_count] = osymbol; label_count++; } else { arc.olabel = label_encoder[osymbol]; } aiter.SetValue(arc); } } ArcSort(&index, fst::ILabelCompare<KwsLexicographicArc>()); int32 n_done = 0; int32 n_fail = 0; for (; !keyword_reader.Done(); keyword_reader.Next()) { std::string key = keyword_reader.Key(); VectorFst<StdArc> keyword = keyword_reader.Value(); keyword_reader.FreeCurrent(); // Process the case where we have confusion for keywords if (keyword_beam != -1) { Prune(&keyword, keyword_beam); } if (keyword_nbest != -1) { VectorFst<StdArc> tmp; ShortestPath(keyword, &tmp, keyword_nbest, true, true); keyword = tmp; } KwsLexicographicFst keyword_fst; KwsLexicographicFst result_fst; Map(keyword, &keyword_fst, VectorFstToKwsLexicographicFstMapper()); Compose(keyword_fst, index, &result_fst); if (stats_wspecifier != "") { KwsLexicographicFst matched_seq(result_fst); OutputDetailedStatistics(key, matched_seq, label_decoder, &stats_writer); } Project(&result_fst, PROJECT_OUTPUT); Minimize(&result_fst, (KwsLexicographicFst *) nullptr, kDelta, true); ShortestPath(result_fst, &result_fst, n_best); RmEpsilon(&result_fst); // No result found if (result_fst.Start() == kNoStateId) continue; // Got something here double score; int32 tbeg, tend, uid; for (ArcIterator<KwsLexicographicFst> aiter(result_fst, result_fst.Start()); !aiter.Done(); aiter.Next()) { const KwsLexicographicArc &arc = aiter.Value(); // We're expecting a two-state FST if (result_fst.Final(arc.nextstate) != Weight::One()) { KALDI_WARN << "The resulting FST does not have " << "the expected structure for key " << key; n_fail++; continue; } uint64 osymbol = label_decoder[arc.olabel]; uid = static_cast<int32>(DecodeLabelUid(osymbol)); tbeg = arc.weight.Value2().Value1().Value(); tend = arc.weight.Value2().Value2().Value(); score = arc.weight.Value1().Value(); if (score < 0) { if (score < negative_tolerance) { KALDI_WARN << "Score out of expected range: " << score; } score = 0.0; } vector<double> result; result.push_back(uid); result.push_back(tbeg * frame_subsampling_factor); result.push_back(tend * frame_subsampling_factor); result.push_back(score); result_writer.Write(key, result); } n_done++; } KALDI_LOG << "Done " << n_done << " keywords"; if (strict == true) return (n_done != 0 ? 0 : 1); else return 0; } catch(const std::exception &e) { std::cerr << e.what(); return -1; } } |