diff --git a/sherpa-onnx/csrc/hypothesis.h b/sherpa-onnx/csrc/hypothesis.h index 1487bfcc4..ada88c8ce 100644 --- a/sherpa-onnx/csrc/hypothesis.h +++ b/sherpa-onnx/csrc/hypothesis.h @@ -36,10 +36,12 @@ struct Hypothesis { // lm_probs[i] contains the lm score for each token in ys. // Used only in transducer mofified beam-search. + // Elements filled only if LM is used. std::vector lm_probs; // context_scores[i] contains the context-graph score for each token in ys. // Used only in transducer mofified beam-search. + // Elements filled only if `ContextGraph` is used. std::vector context_scores; // The total score of ys in log space. diff --git a/sherpa-onnx/csrc/online-recognizer.cc b/sherpa-onnx/csrc/online-recognizer.cc index 52749d171..7c0dd78d4 100644 --- a/sherpa-onnx/csrc/online-recognizer.cc +++ b/sherpa-onnx/csrc/online-recognizer.cc @@ -20,7 +20,7 @@ namespace sherpa_onnx { /// Helper for `OnlineRecognizerResult::AsJsonString()` template -const std::string& VecToString(const std::vector& vec, int32_t precision = 6) { +std::string VecToString(const std::vector& vec, int32_t precision = 6) { std::ostringstream oss; oss << std::fixed << std::setprecision(precision); oss << "[ "; @@ -35,9 +35,8 @@ const std::string& VecToString(const std::vector& vec, int32_t precision = 6) /// Helper for `OnlineRecognizerResult::AsJsonString()` template<> // explicit specialization for T = std::string -const std::string& VecToString(const std::vector& vec, - int32_t) // ignore 2nd arg -{ +std::string VecToString(const std::vector& vec, + int32_t) { // ignore 2nd arg std::ostringstream oss; oss << "[ "; std::string sep = ""; @@ -57,9 +56,10 @@ std::string OnlineRecognizerResult::AsJsonString() const { os << "\"timestamps\": " << VecToString(timestamps, 2) << ", "; os << "\"ys_probs\": " << VecToString(ys_probs, 6) << ", "; os << "\"lm_probs\": " << VecToString(lm_probs, 6) << ", "; - os << "\"constext_scores\": " << VecToString(context_scores, 6) << ", "; + os << "\"context_scores\": " << VecToString(context_scores, 6) << ", "; os << "\"segment\": " << segment << ", "; - os << "\"start_time\": " << std::fixed << std::setprecision(2) << start_time << ", "; + os << "\"start_time\": " << std::fixed << std::setprecision(2) + << start_time << ", "; os << "\"is_final\": " << (is_final ? "true" : "false"); os << "}"; return os.str(); diff --git a/sherpa-onnx/csrc/online-transducer-modified-beam-search-decoder.cc b/sherpa-onnx/csrc/online-transducer-modified-beam-search-decoder.cc index b4aa49114..e37ba63d4 100644 --- a/sherpa-onnx/csrc/online-transducer-modified-beam-search-decoder.cc +++ b/sherpa-onnx/csrc/online-transducer-modified-beam-search-decoder.cc @@ -190,17 +190,22 @@ void OnlineTransducerModifiedBeamSearchDecoder::Decode( if (new_token != 0 && new_token != unk_id_) { const Hypothesis& prev_i = prev[hyp_index]; // subtract 'prev[i]' path scores, which were added before - // for getting topk tokens + // getting topk tokens float y_prob = p_logprob[k] - prev_i.log_prob - prev_i.lm_log_prob; new_hyp.ys_probs.push_back(y_prob); - float lm_prob = new_hyp.lm_log_prob - prev_lm_log_prob; - if (lm_scale_ != 0.0) { - lm_prob /= lm_scale_; // remove lm-scale + if (lm_) { // export only when LM is used + float lm_prob = new_hyp.lm_log_prob - prev_lm_log_prob; + if (lm_scale_ != 0.0) { + lm_prob /= lm_scale_; // remove lm-scale + } + new_hyp.lm_probs.push_back(lm_prob); } - new_hyp.lm_probs.push_back(lm_prob); - new_hyp.context_scores.push_back(context_score); + // export only when `ContextGraph` is used + if (ss != nullptr && ss[b]->GetContextGraph() != nullptr) { + new_hyp.context_scores.push_back(context_score); + } } hyps.Add(std::move(new_hyp));