compile.h revision f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2
1
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6//     http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13//
14// Copyright 2005-2010 Google, Inc.
15// Author: jpr@google.com (Jake Ratkiewicz)
16
17#ifndef FST_SCRIPT_COMPILE_H_
18#define FST_SCRIPT_COMPILE_H_
19
20#include <fst/script/arg-packs.h>
21#include <fst/script/fst-class.h>
22#include <fst/script/compile-impl.h>
23
24namespace fst {
25namespace script {
26
27// Note: it is safe to pass these strings as references because
28// this struct is only used to pass them deeper in the call graph.
29// Be sure you understand why this is so before using this struct
30// for anything else!
31struct FstCompileArgs {
32  fst::istream &istrm;
33  const string &source;
34  const string &dest;
35  const string &fst_type;
36  const fst::SymbolTable *isyms;
37  const fst::SymbolTable *osyms;
38  const fst::SymbolTable *ssyms;
39  const bool accep;
40  const bool ikeep;
41  const bool okeep;
42  const bool nkeep;
43  const bool allow_negative_labels;
44
45  FstCompileArgs(istream &istrm, const string &source, const string &dest,
46                 const string &fst_type, const fst::SymbolTable *isyms,
47                 const fst::SymbolTable *osyms,
48                 const fst::SymbolTable *ssyms,
49                 bool accep, bool ikeep, bool okeep, bool nkeep,
50                 bool allow_negative_labels = false) :
51      istrm(istrm), source(source), dest(dest), fst_type(fst_type),
52      isyms(isyms), osyms(osyms), ssyms(ssyms), accep(accep), ikeep(ikeep),
53      okeep(okeep), nkeep(nkeep),
54      allow_negative_labels(allow_negative_labels) { }
55};
56
57template<class Arc>
58void CompileFst(FstCompileArgs *args) {
59  using fst::FstCompiler;
60  using fst::Convert;
61  using fst::Fst;
62
63  FstCompiler<Arc> fstcompiler(args->istrm, args->source, args->isyms,
64                               args->osyms, args->ssyms,
65                               args->accep, args->ikeep,
66                               args->okeep, args->nkeep,
67                               args->allow_negative_labels);
68
69  const Fst<Arc> *fst = &fstcompiler.Fst();
70  if (args->fst_type != "vector") {
71    fst = Convert<Arc>(*fst, args->fst_type);
72    if (!fst) {
73      FSTERROR() << "Failed to convert FST to desired type: "
74                 << args->fst_type;
75      return;
76    }
77  }
78
79  fst->Write(args->dest);
80}
81
82void CompileFst(istream &istrm, const string &source, const string &dest,
83                const string &fst_type, const string &arc_type,
84                const SymbolTable *isyms,
85                const SymbolTable *osyms, const SymbolTable *ssyms,
86                bool accep, bool ikeep, bool okeep, bool nkeep,
87                bool allow_negative_labels);
88
89}  // namespace script
90}  // namespace fst
91
92#endif  // FST_SCRIPT_COMPILE_H_
93