You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.6 KiB

  1. // Copyright (c) 2021 Xingchen Song sxc19@mails.tsinghua.edu.cn
  2. // 2023 Jing Du (thuduj12@163.com)
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License
  15. #ifndef POST_PROCESSOR_POST_PROCESSOR_H_
  16. #define POST_PROCESSOR_POST_PROCESSOR_H_
  17. #include <memory>
  18. #include <string>
  19. #include <utility>
  20. #include "utils/utils.h"
  21. namespace wenet {
  22. enum LanguageType {
  23. // spaces between **mandarin words** should be removed.
  24. // cases of processing spaces with mandarin-only, english-only
  25. // and mandarin-english code-switch can be found in post_processor_test.cc
  26. kMandarinEnglish = 0x00,
  27. // spaces should be kept for most of the
  28. // Indo-European languages (i.e., deutsch or english-deutsch code-switch).
  29. // cases of those languages can be found in post_processor_test.cc
  30. kIndoEuropean = 0x01
  31. };
  32. struct PostProcessOptions {
  33. // space options
  34. // The decoded result may contain spaces (' ' or '_'),
  35. // we will process those spaces according to language_type. More details can
  36. // be found in
  37. // https://github.com/wenet-e2e/wenet/issues/583#issuecomment-907994058
  38. LanguageType language_type = kMandarinEnglish;
  39. // whether lowercase letters are required
  40. bool lowercase = true;
  41. bool itn = false;
  42. };
  43. // TODO(xcsong): add punctuation related resource
  44. // Post Processor
  45. class PostProcessor {
  46. public:
  47. explicit PostProcessor(PostProcessOptions&& opts) : opts_(std::move(opts)) {}
  48. explicit PostProcessor(const PostProcessOptions& opts) : opts_(opts) {}
  49. // call other functions to do post processing
  50. std::string Process(const std::string& str, bool finish);
  51. // process spaces according to configurations
  52. std::string ProcessSpace(const std::string& str);
  53. std::string ProcessSymbols(const std::string& str);
  54. // TODO(xcsong): add punctuation
  55. // void Punctuate(const std::string& str);
  56. void InitITNResource(const std::string& tagger_path,
  57. const std::string& verbalizer_path);
  58. private:
  59. const PostProcessOptions opts_;
  60. public:
  61. WENET_DISALLOW_COPY_AND_ASSIGN(PostProcessor);
  62. };
  63. } // namespace wenet
  64. #endif // POST_PROCESSOR_POST_PROCESSOR_H_