FST  openfst-1.8.2.post1
OpenFst Library
compile.h
Go to the documentation of this file.
1 // Copyright 2005-2020 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 
25 #include <fst/script/arg-packs.h>
27 #include <fst/script/fst-class.h>
28 
29 namespace fst {
30 namespace script {
31 
32 // This operation exists in two forms. 1 is a void operation which writes the
33 // compiled machine to disk; 2 returns an FstClass. I/O should normally be done
34 // using the binary format for efficiency, so users are STRONGLY ENCOURAGED to
35 // use 1 or to construct FSTs using the C++ FST mutation operations.
36 
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!
42  std::istream &istrm;
43  const std::string &source;
44  const std::string &fst_type;
48  const bool accep;
49  const bool ikeep;
50  const bool okeep;
51  const bool nkeep;
53 };
54 
55 using FstCompileArgs =
57 
58 template <class Arc>
60  using fst::Convert;
61  using fst::Fst;
62  using fst::FstCompiler;
63  FstCompiler<Arc> fstcompiler(
64  args->args.istrm, args->args.source, args->args.isyms, args->args.osyms,
65  args->args.ssyms, args->args.accep, args->args.ikeep, args->args.okeep,
66  args->args.nkeep, args->args.allow_negative_labels);
67  std::unique_ptr<Fst<Arc>> fst;
68  if (args->args.fst_type != "vector") {
69  std::unique_ptr<Fst<Arc>> tmp_fst(
70  Convert<Arc>(fstcompiler.Fst(), args->args.fst_type));
71  if (!tmp_fst) {
72  FSTERROR() << "Failed to convert FST to desired type: "
73  << args->args.fst_type;
74  }
75  fst = std::move(tmp_fst);
76  } else {
77  fst = fst::WrapUnique(fstcompiler.Fst().Copy());
78  }
79  args->retval = fst ? std::make_unique<FstClass>(std::move(fst)) : nullptr;
80 }
81 
82 void Compile(std::istream &istrm, const std::string &source,
83  const std::string &dest, const std::string &fst_type,
84  const std::string &arc_type, const SymbolTable *isyms,
85  const SymbolTable *osyms, const SymbolTable *ssyms, bool accep,
86  bool ikeep, bool okeep, bool nkeep, bool allow_negative_labels);
87 
88 std::unique_ptr<FstClass> CompileInternal(
89  std::istream &istrm, const std::string &source, const std::string &fst_type,
90  const std::string &arc_type, const SymbolTable *isyms,
91  const SymbolTable *osyms, const SymbolTable *ssyms, bool accep, bool ikeep,
92  bool okeep, bool nkeep, bool allow_negative_labels);
93 
94 } // namespace script
95 } // namespace fst
96 
97 #endif // FST_SCRIPT_COMPILE_H_
void Convert(FarReader< Arc > &reader, FarWriter< Arc > &writer, std::string_view fst_type)
Definition: convert.h:27
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, bool allow_negative_labels)
Definition: compile.cc:28
std::unique_ptr< T > WrapUnique(T *ptr)
Definition: compat.h:125
#define FSTERROR()
Definition: util.h:53
const fst::SymbolTable * ssyms
Definition: compile.h:47
const fst::SymbolTable * osyms
Definition: compile.h:46
VectorFst * Copy(bool safe=false) const override
Definition: vector-fst.h:546
const std::string & fst_type
Definition: compile.h:44
void CompileInternal(FstCompileArgs *args)
Definition: compile.h:59
const fst::SymbolTable * isyms
Definition: compile.h:45
const std::string & source
Definition: compile.h:43
const VectorFst< Arc > & Fst() const
Definition: compile-impl.h:151