endpoint.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * Copyright 2022 (authors: Pingfeng Luo)
  3. *
  4. * See LICENSE for clarification regarding multiple authors
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "sherpa-ncnn/csrc/endpoint.h"
  19. #include <sstream>
  20. #include <string>
  21. namespace sherpa_ncnn {
  22. static bool RuleActivated(const EndpointRule &rule,
  23. const std::string &rule_name,
  24. const float trailing_silence,
  25. const float utterance_length) {
  26. bool contain_nonsilence = utterance_length > trailing_silence;
  27. bool ans = (contain_nonsilence || !rule.must_contain_nonsilence) &&
  28. trailing_silence >= rule.min_trailing_silence &&
  29. utterance_length >= rule.min_utterance_length;
  30. return ans;
  31. }
  32. std::string EndpointRule::ToString() const {
  33. std::ostringstream os;
  34. os << "EndpointRule(";
  35. os << "must_contain_nonsilence="
  36. << (must_contain_nonsilence ? "True" : "False") << ", ";
  37. os << "min_trailing_silence=" << min_trailing_silence << ", ";
  38. os << "min_utterance_length=" << min_utterance_length << ")";
  39. return os.str();
  40. }
  41. std::string EndpointConfig::ToString() const {
  42. std::ostringstream os;
  43. os << "EndpointConfig(";
  44. os << "rule1=" << rule1.ToString() << ", ";
  45. os << "rule2=" << rule2.ToString() << ", ";
  46. os << "rule3=" << rule3.ToString() << ")";
  47. return os.str();
  48. }
  49. bool Endpoint::IsEndpoint(const int num_frames_decoded,
  50. const int trailing_silence_frames,
  51. const float frame_shift_in_seconds) const {
  52. float utterance_length = num_frames_decoded * frame_shift_in_seconds;
  53. float trailing_silence = trailing_silence_frames * frame_shift_in_seconds;
  54. if (RuleActivated(config_.rule1, "rule1", trailing_silence,
  55. utterance_length) ||
  56. RuleActivated(config_.rule2, "rule2", trailing_silence,
  57. utterance_length) ||
  58. RuleActivated(config_.rule3, "rule3", trailing_silence,
  59. utterance_length)) {
  60. return true;
  61. }
  62. return false;
  63. }
  64. } // namespace sherpa_ncnn