|
| 1 | +// online2/online-endpoing.cc |
| 2 | + |
| 3 | +// Copyright 2014 Johns Hopkins University (author: Daniel Povey) |
| 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 | +#include "online2/online-endpoint.h" |
| 21 | + |
| 22 | +namespace kaldi { |
| 23 | + |
| 24 | +static inline void GetBestPathStats(const TransitionModel &tmodel, |
| 25 | + const Lattice &best_path, |
| 26 | + const OnlineEndpointConfig &config, |
| 27 | + BaseFloat frame_shift_in_seconds, |
| 28 | + BaseFloat *utterance_length, |
| 29 | + BaseFloat *trailing_silence) { |
| 30 | + std::vector<int32> silence_phones; |
| 31 | + if (!SplitStringToIntegers(config.silence_phones, ":", false, &silence_phones)) |
| 32 | + KALDI_ERR << "Bad --silence-phones option in endpointing config: " |
| 33 | + << config.silence_phones; |
| 34 | + std::sort(silence_phones.begin(), silence_phones.end()); |
| 35 | + KALDI_ASSERT(IsSortedAndUniq(silence_phones) && |
| 36 | + "Duplicates in --silence-phones option in endpointing config"); |
| 37 | + KALDI_ASSERT(!silence_phones.empty() && |
| 38 | + "Endpointing requires nonempty --endpoint.silence-phones option"); |
| 39 | + ConstIntegerSet<int32> silence_set(silence_phones); |
| 40 | + |
| 41 | + std::vector<int32> alignment; |
| 42 | + |
| 43 | + fst::GetLinearSymbolSequence<LatticeArc,int32>(best_path, &alignment, NULL, NULL); |
| 44 | + |
| 45 | + *utterance_length = alignment.size() * frame_shift_in_seconds; |
| 46 | + if (alignment.empty()) { |
| 47 | + *trailing_silence = 0.0; |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + int32 num_sil_frames = 0; |
| 52 | + for (int32 n = alignment.size() - 1; n >= 0; n--) { |
| 53 | + int32 phone = tmodel.TransitionIdToPhone(alignment[n]); |
| 54 | + if (silence_set.count(phone) != 0) |
| 55 | + num_sil_frames++; |
| 56 | + else |
| 57 | + break; |
| 58 | + } |
| 59 | + *trailing_silence = num_sil_frames * frame_shift_in_seconds; |
| 60 | +} |
| 61 | + |
| 62 | + |
| 63 | +static bool RuleActivated(const OnlineEndpointRule &rule, |
| 64 | + const std::string &rule_name, |
| 65 | + BaseFloat trailing_silence, |
| 66 | + BaseFloat relative_cost, |
| 67 | + BaseFloat utterance_length) { |
| 68 | + bool contains_nonsilence = (utterance_length > trailing_silence); |
| 69 | + |
| 70 | + bool ans = (contains_nonsilence || !rule.must_contain_nonsilence) && |
| 71 | + trailing_silence >= rule.min_trailing_silence && |
| 72 | + relative_cost <= rule.max_relative_cost && |
| 73 | + utterance_length >= rule.min_utterance_length; |
| 74 | + if (ans) { |
| 75 | + KALDI_VLOG(2) << "Endpointing rule " << rule_name << " activated: " |
| 76 | + << (contains_nonsilence ? "true" : "false" ) << ',' |
| 77 | + << trailing_silence << ',' << relative_cost << ',' |
| 78 | + << utterance_length; |
| 79 | + } |
| 80 | + return ans; |
| 81 | +} |
| 82 | + |
| 83 | +bool EndpointDetected(const TransitionModel &tmodel, |
| 84 | + const Lattice &best_path, |
| 85 | + const OnlineEndpointConfig &config, |
| 86 | + BaseFloat frame_shift_in_seconds, |
| 87 | + BaseFloat final_relative_cost) { |
| 88 | + if (best_path.NumStates() == 0) return false; |
| 89 | + KALDI_ASSERT(final_relative_cost >= 0.0); |
| 90 | + BaseFloat utterance_length, trailing_silence; |
| 91 | + GetBestPathStats(tmodel, best_path, config, frame_shift_in_seconds, |
| 92 | + &utterance_length, &trailing_silence); |
| 93 | + |
| 94 | + if (RuleActivated(config.rule1, "rule1", |
| 95 | + trailing_silence, final_relative_cost, utterance_length)) |
| 96 | + return true; |
| 97 | + if (RuleActivated(config.rule2, "rule2", |
| 98 | + trailing_silence, final_relative_cost, utterance_length)) |
| 99 | + return true; |
| 100 | + if (RuleActivated(config.rule3, "rule3", |
| 101 | + trailing_silence, final_relative_cost, utterance_length)) |
| 102 | + return true; |
| 103 | + if (RuleActivated(config.rule4, "rule4", |
| 104 | + trailing_silence, final_relative_cost, utterance_length)) |
| 105 | + return true; |
| 106 | + if (RuleActivated(config.rule5, "rule5", |
| 107 | + trailing_silence, final_relative_cost, utterance_length)) |
| 108 | + return true; |
| 109 | + return false; |
| 110 | +} |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | +} // namespace kaldi |
0 commit comments