0520b74c37a1318aa322ef55708a49ef7fbe1bef
[platform/upstream/openfst.git] / src / include / fst / script / compile.h
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
9 #include <fst/script/arg-packs.h>
10 #include <fst/script/compile-impl.h>
11 #include <fst/script/fst-class.h>
12
13 namespace fst {
14 namespace script {
15
16 // This operation exists in two forms. 1 is a void operation which writes the
17 // compiled machine to disk; 2 returns an FstClass. I/O should normally be done
18 // using the binary format for efficiency, so users are STRONGLY ENCOURAGED to
19 // use 1 or to construct FSTs using the C++ FST mutation operations.
20
21 // Note: it is safe to pass these strings as references because
22 // this struct is only used to pass them deeper in the call graph.
23 // Be sure you understand why this is so before using this struct
24 // for anything else!
25 struct CompileFstInnerArgs {
26   std::istream &istrm;
27   const string &source;
28   const string &fst_type;
29   const fst::SymbolTable *isyms;
30   const fst::SymbolTable *osyms;
31   const fst::SymbolTable *ssyms;
32   const bool accep;
33   const bool ikeep;
34   const bool okeep;
35   const bool nkeep;
36   const bool allow_negative_labels;
37
38   CompileFstInnerArgs(std::istream &istrm, const string &source,
39                       const string &fst_type, const fst::SymbolTable *isyms,
40                       const fst::SymbolTable *osyms,
41                       const fst::SymbolTable *ssyms, bool accep, bool ikeep,
42                       bool okeep, bool nkeep,
43                       bool allow_negative_labels = false)
44       : istrm(istrm),
45         source(source),
46         fst_type(fst_type),
47         isyms(isyms),
48         osyms(osyms),
49         ssyms(ssyms),
50         accep(accep),
51         ikeep(ikeep),
52         okeep(okeep),
53         nkeep(nkeep),
54         allow_negative_labels(allow_negative_labels) {}
55 };
56
57 // 1
58 using CompileFstArgs = args::WithReturnValue<FstClass *, CompileFstInnerArgs>;
59
60 // 2
61 template <class Arc>
62 void CompileFstInternal(CompileFstArgs *args) {
63   using fst::Convert;
64   using fst::Fst;
65   using fst::FstCompiler;
66   FstCompiler<Arc> fstcompiler(
67       args->args.istrm, args->args.source, args->args.isyms, args->args.osyms,
68       args->args.ssyms, args->args.accep, args->args.ikeep, args->args.okeep,
69       args->args.nkeep, args->args.allow_negative_labels);
70   const Fst<Arc> *fst = &fstcompiler.Fst();
71   if (args->args.fst_type != "vector") {
72     fst = Convert<Arc>(*fst, args->args.fst_type);
73     if (!fst) {
74       FSTERROR() << "Failed to convert FST to desired type: "
75                  << args->args.fst_type;
76     }
77   }
78   args->retval = fst ? new FstClass(*fst) : nullptr;
79 }
80
81 // 1
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 // 2
89 FstClass *CompileFstInternal(std::istream &istrm, const string &source,
90                              const string &fst_type, const string &arc_type,
91                              const SymbolTable *isyms, const SymbolTable *osyms,
92                              const SymbolTable *ssyms, bool accep, bool ikeep,
93                              bool okeep, bool nkeep,
94                              bool allow_negative_labels);
95
96 }  // namespace script
97 }  // namespace fst
98
99 #endif  // FST_SCRIPT_COMPILE_H_