FST  openfst-1.7.2
OpenFst Library
compile.h
Go to the documentation of this file.
1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
3 
4 #ifndef FST_SCRIPT_COMPILE_H_
5 #define FST_SCRIPT_COMPILE_H_
6 
7 #include <istream>
8 #include <memory>
9 
10 #include <fst/script/arg-packs.h>
12 #include <fst/script/fst-class.h>
13 
14 namespace fst {
15 namespace script {
16 
17 // This operation exists in two forms. 1 is a void operation which writes the
18 // compiled machine to disk; 2 returns an FstClass. I/O should normally be done
19 // using the binary format for efficiency, so users are STRONGLY ENCOURAGED to
20 // use 1 or to construct FSTs using the C++ FST mutation operations.
21 
22 // Note: it is safe to pass these strings as references because
23 // this struct is only used to pass them deeper in the call graph.
24 // Be sure you understand why this is so before using this struct
25 // for anything else!
27  std::istream &istrm;
28  const string &source;
29  const string &fst_type;
33  const bool accep;
34  const bool ikeep;
35  const bool okeep;
36  const bool nkeep;
38 
39  CompileFstInnerArgs(std::istream &istrm, const string &source,
40  const string &fst_type, const fst::SymbolTable *isyms,
41  const fst::SymbolTable *osyms,
42  const fst::SymbolTable *ssyms, bool accep, bool ikeep,
43  bool okeep, bool nkeep,
44  bool allow_negative_labels = false)
45  : istrm(istrm),
46  source(source),
47  fst_type(fst_type),
48  isyms(isyms),
49  osyms(osyms),
50  ssyms(ssyms),
51  accep(accep),
52  ikeep(ikeep),
53  okeep(okeep),
54  nkeep(nkeep),
55  allow_negative_labels(allow_negative_labels) {}
56 };
57 
59 
60 template <class Arc>
62  using fst::Convert;
63  using fst::Fst;
64  using fst::FstCompiler;
65  FstCompiler<Arc> fstcompiler(
66  args->args.istrm, args->args.source, args->args.isyms, args->args.osyms,
67  args->args.ssyms, args->args.accep, args->args.ikeep, args->args.okeep,
68  args->args.nkeep, args->args.allow_negative_labels);
69  const Fst<Arc> *fst = &fstcompiler.Fst();
70  std::unique_ptr<const Fst<Arc>> owned_fst;
71  if (args->args.fst_type != "vector") {
72  owned_fst.reset(Convert<Arc>(*fst, args->args.fst_type));
73  if (!owned_fst) {
74  FSTERROR() << "Failed to convert FST to desired type: "
75  << args->args.fst_type;
76  }
77  fst = owned_fst.get();
78  }
79  args->retval = fst ? new FstClass(*fst) : nullptr;
80 }
81 
82 void CompileFst(std::istream &istrm, const string &source, const string &dest,
83  const string &fst_type, const string &arc_type,
84  const SymbolTable *isyms, const SymbolTable *osyms,
85  const SymbolTable *ssyms, bool accep, bool ikeep, bool okeep,
86  bool nkeep, bool allow_negative_labels);
87 
88 FstClass *CompileFstInternal(std::istream &istrm, const string &source,
89  const string &fst_type, const string &arc_type,
90  const SymbolTable *isyms, const SymbolTable *osyms,
91  const SymbolTable *ssyms, bool accep, bool ikeep,
92  bool okeep, bool nkeep,
93  bool allow_negative_labels);
94 
95 } // namespace script
96 } // namespace fst
97 
98 #endif // FST_SCRIPT_COMPILE_H_
Fst< Arc > * Convert(const Fst< Arc > &fst, const string &fst_type)
Definition: register.h:102
const fst::SymbolTable * osyms
Definition: compile.h:31
void CompileFst(std::istream &istrm, const string &source, const string &dest, const string &fst_type, const 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:14
#define FSTERROR()
Definition: util.h:35
const fst::SymbolTable * ssyms
Definition: compile.h:32
CompileFstInnerArgs(std::istream &istrm, const string &source, const string &fst_type, const fst::SymbolTable *isyms, const fst::SymbolTable *osyms, const fst::SymbolTable *ssyms, bool accep, bool ikeep, bool okeep, bool nkeep, bool allow_negative_labels=false)
Definition: compile.h:39
const fst::SymbolTable * isyms
Definition: compile.h:30
void CompileFstInternal(CompileFstArgs *args)
Definition: compile.h:61
const VectorFst< Arc > & Fst() const
Definition: compile-impl.h:135