• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // Convenience file for including all of the FAR operations,
18 // or registering them for new arc types.
19 
20 #ifndef FST_EXTENSIONS_FAR_FARSCRIPT_H_
21 #define FST_EXTENSIONS_FAR_FARSCRIPT_H_
22 
23 #include <vector>
24 using std::vector;
25 #include <string>
26 
27 #include <fst/script/arg-packs.h>
28 #include <fst/extensions/far/compile-strings.h>
29 #include <fst/extensions/far/create.h>
30 #include <fst/extensions/far/equal.h>
31 #include <fst/extensions/far/extract.h>
32 #include <fst/extensions/far/info.h>
33 #include <fst/extensions/far/print-strings.h>
34 #include <fst/extensions/far/far.h>
35 
36 #include <fst/types.h>
37 
38 namespace fst {
39 namespace script {
40 
41 // Note: it is safe to pass these strings as references because
42 // this struct is only used to pass them deeper in the call graph.
43 // Be sure you understand why this is so before using this struct
44 // for anything else!
45 struct FarCompileStringsArgs {
46   const vector<string> &in_fnames;
47   const string &out_fname;
48   const string &fst_type;
49   const FarType &far_type;
50   const int32 generate_keys;
51   const FarEntryType fet;
52   const FarTokenType tt;
53   const string &symbols_fname;
54   const string &unknown_symbol;
55   const bool keep_symbols;
56   const bool initial_symbols;
57   const bool allow_negative_labels;
58   const bool file_list_input;
59   const string &key_prefix;
60   const string &key_suffix;
61 
FarCompileStringsArgsFarCompileStringsArgs62   FarCompileStringsArgs(const vector<string> &in_fnames,
63                         const string &out_fname,
64                         const string &fst_type,
65                         const FarType &far_type,
66                         int32 generate_keys,
67                         FarEntryType fet,
68                         FarTokenType tt,
69                         const string &symbols_fname,
70                         const string &unknown_symbol,
71                         bool keep_symbols,
72                         bool initial_symbols,
73                         bool allow_negative_labels,
74                         bool file_list_input,
75                         const string &key_prefix,
76                         const string &key_suffix) :
77       in_fnames(in_fnames), out_fname(out_fname), fst_type(fst_type),
78       far_type(far_type), generate_keys(generate_keys), fet(fet),
79       tt(tt), symbols_fname(symbols_fname), unknown_symbol(unknown_symbol),
80       keep_symbols(keep_symbols), initial_symbols(initial_symbols),
81       allow_negative_labels(allow_negative_labels),
82       file_list_input(file_list_input), key_prefix(key_prefix),
83       key_suffix(key_suffix) { }
84 };
85 
86 template <class Arc>
FarCompileStrings(FarCompileStringsArgs * args)87 void FarCompileStrings(FarCompileStringsArgs *args) {
88   fst::FarCompileStrings<Arc>(
89       args->in_fnames, args->out_fname, args->fst_type, args->far_type,
90       args->generate_keys, args->fet, args->tt, args->symbols_fname,
91       args->unknown_symbol, args->keep_symbols, args->initial_symbols,
92       args->allow_negative_labels, args->file_list_input,
93       args->key_prefix, args->key_suffix);
94 }
95 
96 void FarCompileStrings(
97     const vector<string> &in_fnames,
98     const string &out_fname,
99     const string &arc_type,
100     const string &fst_type,
101     const FarType &far_type,
102     int32 generate_keys,
103     FarEntryType fet,
104     FarTokenType tt,
105     const string &symbols_fname,
106     const string &unknown_symbol,
107     bool keep_symbols,
108     bool initial_symbols,
109     bool allow_negative_labels,
110     bool file_list_input,
111     const string &key_prefix,
112     const string &key_suffix);
113 
114 
115 // Note: it is safe to pass these strings as references because
116 // this struct is only used to pass them deeper in the call graph.
117 // Be sure you understand why this is so before using this struct
118 // for anything else!
119 struct FarCreateArgs {
120   const vector<string> &in_fnames;
121   const string &out_fname;
122   const int32 generate_keys;
123   const bool file_list_input;
124   const FarType &far_type;
125   const string &key_prefix;
126   const string &key_suffix;
127 
FarCreateArgsFarCreateArgs128   FarCreateArgs(
129       const vector<string> &in_fnames, const string &out_fname,
130       const int32 generate_keys, const bool file_list_input,
131       const FarType &far_type, const string &key_prefix,
132       const string &key_suffix)
133       : in_fnames(in_fnames), out_fname(out_fname),
134         generate_keys(generate_keys), file_list_input(file_list_input),
135         far_type(far_type), key_prefix(key_prefix), key_suffix(key_suffix) { }
136 };
137 
138 template<class Arc>
FarCreate(FarCreateArgs * args)139 void FarCreate(FarCreateArgs *args) {
140   fst::FarCreate<Arc>(args->in_fnames, args->out_fname, args->generate_keys,
141                           args->file_list_input, args->far_type,
142                           args->key_prefix, args->key_suffix);
143 }
144 
145 void FarCreate(const vector<string> &in_fnames,
146                const string &out_fname,
147                const string &arc_type,
148                const int32 generate_keys,
149                const bool file_list_input,
150                const FarType &far_type,
151                const string &key_prefix,
152                const string &key_suffix);
153 
154 
155 typedef args::Package<const string &, const string &, float,
156                       const string &, const string &> FarEqualInnerArgs;
157 typedef args::WithReturnValue<bool, FarEqualInnerArgs> FarEqualArgs;
158 
159 template <class Arc>
FarEqual(FarEqualArgs * args)160 void FarEqual(FarEqualArgs *args) {
161   args->retval = fst::FarEqual<Arc>(
162       args->args.arg1, args->args.arg2, args->args.arg3,
163       args->args.arg4, args->args.arg5);
164 }
165 
166 bool FarEqual(const string &filename1,
167               const string &filename2,
168               const string &arc_type,
169               float delta = kDelta,
170               const string &begin_key = string(),
171               const string &end_key = string());
172 
173 
174 typedef args::Package<const vector<string> &, int32,
175                       const string&, const string&, const string&,
176                       const string&, const string&> FarExtractArgs;
177 
178 template<class Arc>
FarExtract(FarExtractArgs * args)179 void FarExtract(FarExtractArgs *args) {
180   fst::FarExtract<Arc>(
181       args->arg1, args->arg2, args->arg3, args->arg4, args->arg5, args->arg6,
182       args->arg7);
183 }
184 
185 void FarExtract(const vector<string> &ifilenames,
186                 const string &arc_type,
187                 int32 generate_filenames,
188                 const string &keys,
189                 const string &key_separator,
190                 const string &range_delimiter,
191                 const string &filename_prefix,
192                 const string &filename_suffix);
193 
194 typedef args::Package<const vector<string> &, const string &,
195                       const string &, const bool> FarInfoArgs;
196 
197 template <class Arc>
FarInfo(FarInfoArgs * args)198 void FarInfo(FarInfoArgs *args) {
199   fst::FarInfo<Arc>(args->arg1, args->arg2, args->arg3, args->arg4);
200 }
201 
202 void FarInfo(const vector<string> &filenames,
203              const string &arc_type,
204              const string &begin_key,
205              const string &end_key,
206              const bool list_fsts);
207 
208 struct FarPrintStringsArgs {
209   const vector<string> &ifilenames;
210   const FarEntryType entry_type;
211   const FarTokenType token_type;
212   const string &begin_key;
213   const string &end_key;
214   const bool print_key;
215   const bool print_weight;
216   const string &symbols_fname;
217   const bool initial_symbols;
218   const int32 generate_filenames;
219   const string &filename_prefix;
220   const string &filename_suffix;
221 
FarPrintStringsArgsFarPrintStringsArgs222   FarPrintStringsArgs(
223       const vector<string> &ifilenames, const FarEntryType entry_type,
224       const FarTokenType token_type, const string &begin_key,
225       const string &end_key,  const bool print_key, const bool print_weight,
226       const string &symbols_fname, const bool initial_symbols,
227       const int32 generate_filenames,
228       const string &filename_prefix, const string &filename_suffix) :
229       ifilenames(ifilenames), entry_type(entry_type), token_type(token_type),
230       begin_key(begin_key), end_key(end_key),
231       print_key(print_key), print_weight(print_weight),
232       symbols_fname(symbols_fname), initial_symbols(initial_symbols),
233       generate_filenames(generate_filenames), filename_prefix(filename_prefix),
234       filename_suffix(filename_suffix) { }
235 };
236 
237 template <class Arc>
FarPrintStrings(FarPrintStringsArgs * args)238 void FarPrintStrings(FarPrintStringsArgs *args) {
239   fst::FarPrintStrings<Arc>(
240       args->ifilenames, args->entry_type, args->token_type,
241       args->begin_key, args->end_key, args->print_key, args->print_weight,
242       args->symbols_fname, args->initial_symbols, args->generate_filenames,
243       args->filename_prefix, args->filename_suffix);
244 }
245 
246 
247 void FarPrintStrings(const vector<string> &ifilenames,
248                      const string &arc_type,
249                      const FarEntryType entry_type,
250                      const FarTokenType token_type,
251                      const string &begin_key,
252                      const string &end_key,
253                      const bool print_key,
254                      const bool print_weight,
255                      const string &symbols_fname,
256                      const bool initial_symbols,
257                      const int32 generate_filenames,
258                      const string &filename_prefix,
259                      const string &filename_suffix);
260 
261 }  // namespace script
262 }  // namespace fst
263 
264 
265 #define REGISTER_FST_FAR_OPERATIONS(ArcType)                            \
266   REGISTER_FST_OPERATION(FarCompileStrings, ArcType, FarCompileStringsArgs); \
267   REGISTER_FST_OPERATION(FarCreate, ArcType, FarCreateArgs);            \
268   REGISTER_FST_OPERATION(FarEqual, ArcType, FarEqualArgs);              \
269   REGISTER_FST_OPERATION(FarExtract, ArcType, FarExtractArgs);          \
270   REGISTER_FST_OPERATION(FarInfo, ArcType, FarInfoArgs);                \
271   REGISTER_FST_OPERATION(FarPrintStrings, ArcType, FarPrintStringsArgs)
272 
273 #endif  // FST_EXTENSIONS_FAR_FARSCRIPT_H_
274