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.

102 lines
3.5 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_COMPILE_H_
  18. #define FST_SCRIPT_COMPILE_H_
  19. #include <istream>
  20. #include <memory>
  21. #include <string>
  22. #include <utility>
  23. #include <fst/log.h>
  24. #include <fst/fst.h>
  25. #include <fst/register.h>
  26. #include <fst/symbol-table.h>
  27. #include <fst/util.h>
  28. #include <fst/script/arg-packs.h>
  29. #include <fst/script/compile-impl.h>
  30. #include <fst/script/fst-class.h>
  31. namespace fst {
  32. namespace script {
  33. // This operation exists in two forms. 1 is a void operation which writes the
  34. // compiled machine to disk; 2 returns an FstClass. I/O should normally be done
  35. // using the binary format for efficiency, so users are STRONGLY ENCOURAGED to
  36. // use 1 or to construct FSTs using the C++ FST mutation operations.
  37. // Note: it is safe to pass these strings as references because
  38. // this struct is only used to pass them deeper in the call graph.
  39. // Be sure you understand why this is so before using this struct
  40. // for anything else!
  41. struct FstCompileInnerArgs {
  42. std::istream &istrm;
  43. const std::string &source;
  44. const std::string &fst_type;
  45. const fst::SymbolTable *isyms;
  46. const fst::SymbolTable *osyms;
  47. const fst::SymbolTable *ssyms;
  48. const bool accep;
  49. const bool ikeep;
  50. const bool okeep;
  51. const bool nkeep;
  52. };
  53. using FstCompileArgs =
  54. WithReturnValue<std::unique_ptr<FstClass>, FstCompileInnerArgs>;
  55. template <class Arc>
  56. void CompileInternal(FstCompileArgs *args) {
  57. using fst::Convert;
  58. using fst::Fst;
  59. using fst::FstCompiler;
  60. FstCompiler<Arc> fstcompiler(
  61. args->args.istrm, args->args.source, args->args.isyms, args->args.osyms,
  62. args->args.ssyms, args->args.accep, args->args.ikeep, args->args.okeep,
  63. args->args.nkeep);
  64. std::unique_ptr<Fst<Arc>> fst;
  65. if (args->args.fst_type != "vector") {
  66. std::unique_ptr<Fst<Arc>> tmp_fst(
  67. Convert<Arc>(fstcompiler.Fst(), args->args.fst_type));
  68. if (!tmp_fst) {
  69. FSTERROR() << "Failed to convert FST to desired type: "
  70. << args->args.fst_type;
  71. }
  72. fst = std::move(tmp_fst);
  73. } else {
  74. fst = fst::WrapUnique(fstcompiler.Fst().Copy());
  75. }
  76. args->retval = fst ? std::make_unique<FstClass>(std::move(fst)) : nullptr;
  77. }
  78. void Compile(std::istream &istrm, const std::string &source,
  79. const std::string &dest, const std::string &fst_type,
  80. const std::string &arc_type, const SymbolTable *isyms,
  81. const SymbolTable *osyms, const SymbolTable *ssyms, bool accep,
  82. bool ikeep, bool okeep, bool nkeep);
  83. std::unique_ptr<FstClass> CompileInternal(
  84. std::istream &istrm, const std::string &source, const std::string &fst_type,
  85. const std::string &arc_type, const SymbolTable *isyms,
  86. const SymbolTable *osyms, const SymbolTable *ssyms, bool accep, bool ikeep,
  87. bool okeep, bool nkeep);
  88. } // namespace script
  89. } // namespace fst
  90. #endif // FST_SCRIPT_COMPILE_H_