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.

88 lines
3.1 KiB

  1. // Copyright 2005-2024 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the 'License');
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an 'AS IS' BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // See www.openfst.org for extensive documentation on this weighted
  16. // finite-state transducer library.
  17. #ifndef FST_SCRIPT_PRINT_H_
  18. #define FST_SCRIPT_PRINT_H_
  19. #include <ostream>
  20. #include <string>
  21. #include <fst/flags.h>
  22. #include <fst/fst.h>
  23. #include <fst/symbol-table.h>
  24. #include <fst/script/fst-class.h>
  25. #include <fst/script/print-impl.h>
  26. #include <string_view>
  27. DECLARE_string(fst_field_separator);
  28. namespace fst {
  29. namespace script {
  30. // Note: it is safe to pass these strings as references because this struct is
  31. // only used to pass them deeper in the call graph. Be sure you understand why
  32. // this is so before using this struct for anything else!
  33. struct FstPrintArgs {
  34. const FstClass &fst;
  35. const SymbolTable *isyms;
  36. const SymbolTable *osyms;
  37. const SymbolTable *ssyms;
  38. const bool accept;
  39. const bool show_weight_one;
  40. std::ostream &ostrm;
  41. const std::string &dest;
  42. const std::string &sep;
  43. const std::string &missing_symbol;
  44. };
  45. template <class Arc>
  46. void Print(FstPrintArgs *args) {
  47. const Fst<Arc> &fst = *args->fst.GetFst<Arc>();
  48. FstPrinter<Arc> fstprinter(fst, args->isyms, args->osyms, args->ssyms,
  49. args->accept, args->show_weight_one, args->sep,
  50. args->missing_symbol);
  51. fstprinter.Print(args->ostrm, args->dest);
  52. }
  53. void Print(const FstClass &fst, std::ostream &ostrm, const std::string &dest,
  54. const SymbolTable *isyms = nullptr,
  55. const SymbolTable *osyms = nullptr,
  56. const SymbolTable *ssyms = nullptr, bool accept = true,
  57. bool show_weight_one = true, const std::string &missing_sym = "");
  58. // TODO(kbg,2019-09-01): Deprecated.
  59. void PrintFst(const FstClass &fst, std::ostream &ostrm, const std::string &dest,
  60. const SymbolTable *isyms, const SymbolTable *osyms,
  61. const SymbolTable *ssyms, bool accept, bool show_weight_one,
  62. const std::string &missing_sym = "");
  63. // The same, but with more sensible defaults, and for arc-templated FSTs only.
  64. // TODO(kbg,2019-09-01): Deprecated.
  65. template <class Arc>
  66. void PrintFst(const Fst<Arc> &fst, std::ostream &ostrm,
  67. std::string_view dest = "", const SymbolTable *isyms = nullptr,
  68. const SymbolTable *osyms = nullptr,
  69. const SymbolTable *ssyms = nullptr) {
  70. const std::string sep = FST_FLAGS_fst_field_separator.substr(0, 1);
  71. FstPrinter<Arc> fstprinter(fst, isyms, osyms, ssyms, true, true, sep);
  72. fstprinter.Print(ostrm, dest);
  73. }
  74. } // namespace script
  75. } // namespace fst
  76. #endif // FST_SCRIPT_PRINT_H_