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.

78 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 "wetext_processor.h"
  21. #include "../../utils/utils.h"
  22. namespace wenet {
  23. enum LanguageType {
  24. // spaces between **mandarin words** should be removed.
  25. // cases of processing spaces with mandarin-only, english-only
  26. // and mandarin-english code-switch can be found in post_processor_test.cc
  27. kMandarinEnglish = 0x00,
  28. // spaces should be kept for most of the
  29. // Indo-European languages (i.e., deutsch or english-deutsch code-switch).
  30. // cases of those languages can be found in post_processor_test.cc
  31. kIndoEuropean = 0x01
  32. };
  33. struct PostProcessOptions {
  34. // space options
  35. // The decoded result may contain spaces (' ' or '_'),
  36. // we will process those spaces according to language_type. More details can
  37. // be found in
  38. // https://github.com/wenet-e2e/wenet/issues/583#issuecomment-907994058
  39. LanguageType language_type = kMandarinEnglish;
  40. // whether lowercase letters are required
  41. bool lowercase = true;
  42. bool itn = false;
  43. };
  44. // TODO(xcsong): add punctuation related resource
  45. // Post Processor
  46. class PostProcessor {
  47. public:
  48. explicit PostProcessor(PostProcessOptions&& opts) : opts_(std::move(opts)) {}
  49. explicit PostProcessor(const PostProcessOptions& opts) : opts_(opts) {}
  50. // call other functions to do post processing
  51. std::string Process(const std::string& str, bool finish);
  52. // process spaces according to configurations
  53. std::string ProcessSpace(const std::string& str);
  54. std::string ProcessSymbols(const std::string& str);
  55. // TODO(xcsong): add punctuation
  56. // void Punctuate(const std::string& str);
  57. void InitITNResource(const std::string& tagger_path,
  58. const std::string& verbalizer_path);
  59. private:
  60. std::shared_ptr<wetext::Processor> itn_resource = nullptr;
  61. const PostProcessOptions opts_;
  62. public:
  63. WENET_DISALLOW_COPY_AND_ASSIGN(PostProcessor);
  64. };
  65. } // namespace wenet
  66. #endif // POST_PROCESSOR_POST_PROCESSOR_H_