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