FST  openfst-1.8.3
OpenFst Library
farscript.h
Go to the documentation of this file.
1 // Copyright 2005-2024 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 // Convenience file for including all of the FAR operations, or registering
19 // them for new arc types.
20 
21 #ifndef FST_EXTENSIONS_FAR_FARSCRIPT_H_
22 #define FST_EXTENSIONS_FAR_FARSCRIPT_H_
23 
24 #include <cstdint>
25 #include <string>
26 #include <tuple>
27 #include <vector>
28 
36 #include <fst/extensions/far/far.h>
41 #include <fst/encode.h>
42 #include <fst/string.h>
43 #include <fst/weight.h>
44 #include <fst/script/arg-packs.h>
46 #include <fst/script/script-impl.h>
47 #include <string_view>
48 
49 namespace fst {
50 namespace script {
51 
52 // Note: it is safe to pass these strings as references because this struct is
53 // only used to pass them deeper in the call graph. Be sure you understand why
54 // this is so before using this struct for anything else!
56  const std::vector<std::string> &sources;
58  std::string_view fst_type;
59  const int32_t generate_keys;
61  const TokenType tt;
62  const std::string &symbols_source;
63  const std::string &unknown_symbol;
64  const bool keep_symbols;
65  const bool initial_symbols;
66  const std::string &key_prefix;
67  const std::string &key_suffix;
68 };
69 
70 template <class Arc>
72  FarWriter<Arc> &writer = *args->writer.GetFarWriter<Arc>();
73  ::fst::CompileStrings<Arc>(
74  args->sources, writer, args->fst_type, args->generate_keys, args->fet,
75  args->tt, args->symbols_source, args->unknown_symbol, args->keep_symbols,
76  args->initial_symbols, args->key_prefix, args->key_suffix);
77 }
78 
79 void CompileStrings(const std::vector<std::string> &sources,
80  FarWriterClass &writer, std::string_view fst_type,
82  const std::string &symbols_source,
83  const std::string &unknown_symbol, bool keep_symbols,
84  bool initial_symbols, const std::string &key_prefix,
85  const std::string &key_suffix);
86 
87 using FarConvertArgs =
88  std::tuple<FarReaderClass &, FarWriterClass &, std::string_view>;
89 
90 template <class Arc>
91 void Convert(FarConvertArgs *args) {
92  FarReader<Arc> &reader = *std::get<0>(*args).GetFarReader<Arc>();
93  FarWriter<Arc> &writer = *std::get<1>(*args).GetFarWriter<Arc>();
94  ::fst::Convert<Arc>(reader, writer, std::get<2>(*args));
95 }
96 
97 void Convert(FarReaderClass &reader, FarWriterClass &writer,
98  std::string_view fst_type);
99 
100 // Note: it is safe to pass these strings as references because this struct is
101 // only used to pass them deeper in the call graph. Be sure you understand why
102 // this is so before using this struct for anything else!
104  const std::vector<std::string> &sources;
106  const int32_t generate_keys;
107  const std::string &key_prefix;
108  const std::string &key_suffix;
109 };
110 
111 template <class Arc>
112 void Create(FarCreateArgs *args) {
113  FarWriter<Arc> &writer = *args->writer.GetFarWriter<Arc>();
114  ::fst::Create<Arc>(args->sources, writer, args->generate_keys,
115  args->key_prefix, args->key_suffix);
116 }
117 
118 void Create(const std::vector<std::string> &sources, FarWriterClass &writer,
119  int32_t generate_keys, const std::string &key_prefix,
120  const std::string &key_suffix);
121 
122 using FarDecodeArgs =
123  std::tuple<FarReaderClass &, FarWriterClass &, const EncodeMapperClass &>;
124 
125 template <class Arc>
126 void Decode(FarDecodeArgs *args) {
127  FarReader<Arc> &reader = *std::get<0>(*args).GetFarReader<Arc>();
128  FarWriter<Arc> &writer = *std::get<1>(*args).GetFarWriter<Arc>();
129  const EncodeMapper<Arc> &mapper = *std::get<2>(*args).GetEncodeMapper<Arc>();
130  Decode(reader, writer, mapper);
131 }
132 
133 void Decode(FarReaderClass &reader, FarWriterClass &writer,
134  const EncodeMapperClass &encoder);
135 
136 using FarEncodeArgs =
137  std::tuple<FarReaderClass &, FarWriterClass &, EncodeMapperClass *>;
138 
139 template <class Arc>
140 void Encode(FarEncodeArgs *args) {
141  FarReader<Arc> &reader = *std::get<0>(*args).GetFarReader<Arc>();
142  FarWriter<Arc> &writer = *std::get<1>(*args).GetFarWriter<Arc>();
143  EncodeMapper<Arc> *mapper = std::get<2>(*args)->GetEncodeMapper<Arc>();
144  Encode(reader, writer, mapper);
145 }
146 
147 void Encode(FarReaderClass &reader, FarWriterClass &writer,
148  EncodeMapperClass *encoder);
149 
150 using FarEqualInnerArgs = std::tuple<FarReaderClass &, FarReaderClass &, float,
151  std::string_view, std::string_view>;
152 
154 
155 template <class Arc>
156 void Equal(FarEqualArgs *args) {
157  FarReader<Arc> &reader1 = *std::get<0>(args->args).GetFarReader<Arc>();
158  FarReader<Arc> &reader2 = *std::get<1>(args->args).GetFarReader<Arc>();
159  args->retval =
160  ::fst::Equal<Arc>(reader1, reader2, std::get<2>(args->args),
161  std::get<3>(args->args), std::get<4>(args->args));
162 }
163 
164 bool Equal(FarReaderClass &reader1, FarReaderClass &reader2,
165  float delta = kDelta, std::string_view begin_key = "",
166  std::string_view end_key = "");
167 
168 using FarExtractArgs =
169  std::tuple<FarReaderClass &, int32_t, const std::string &,
170  const std::string &, const std::string &, const std::string &,
171  const std::string &>;
172 
173 template <class Arc>
174 void Extract(FarExtractArgs *args) {
175  FarReader<Arc> &reader = *std::get<0>(*args).GetFarReader<Arc>();
176  ::fst::Extract<Arc>(reader, std::get<1>(*args), std::get<2>(*args),
177  std::get<3>(*args), std::get<4>(*args),
178  std::get<5>(*args), std::get<6>(*args));
179 }
180 
181 void Extract(FarReaderClass &reader, int32_t generate_sources,
182  const std::string &keys, const std::string &key_separator,
183  const std::string &range_delimiter,
184  const std::string &source_prefix,
185  const std::string &source_suffix);
186 
187 using FarInfoArgs =
188  std::tuple<const std::vector<std::string> &, const std::string &,
189  const std::string &, const bool>;
190 
191 template <class Arc>
192 void Info(FarInfoArgs *args) {
193  ::fst::Info<Arc>(std::get<0>(*args), std::get<1>(*args),
194  std::get<2>(*args), std::get<3>(*args));
195 }
196 
197 void Info(const std::vector<std::string> &sources, const std::string &arc_type,
198  const std::string &begin_key, const std::string &end_key,
199  const bool list_fsts);
200 
201 using FarGetInfoArgs =
202  std::tuple<const std::vector<std::string> &, const std::string &,
203  const std::string &, const bool, FarInfoData *>;
204 
205 template <class Arc>
206 void GetInfo(FarGetInfoArgs *args) {
207  ::fst::GetInfo<Arc>(std::get<0>(*args), std::get<1>(*args),
208  std::get<2>(*args), std::get<3>(*args),
209  std::get<4>(*args));
210 }
211 
212 void GetInfo(const std::vector<std::string> &sources,
213  const std::string &arc_type, const std::string &begin_key,
214  const std::string &end_key, const bool list_fsts, FarInfoData *);
215 
217  std::tuple<FarReaderClass &, FarReaderClass &, float, std::string_view,
218  std::string_view>;
219 
221 
222 template <class Arc>
224  FarReader<Arc> &reader1 = *std::get<0>(args->args).GetFarReader<Arc>();
225  FarReader<Arc> &reader2 = *std::get<1>(args->args).GetFarReader<Arc>();
226  args->retval = ::fst::Isomorphic<Arc>(
227  reader1, reader2, std::get<2>(args->args), std::get<3>(args->args),
228  std::get<4>(args->args));
229 }
230 
231 bool Isomorphic(FarReaderClass &reader1, FarReaderClass &reader2,
232  float delta = kDelta, std::string_view begin_key = "",
233  std::string_view end_key = "");
234 
236  FarReaderClass &reader;
239  const std::string &begin_key;
240  const std::string &end_key;
241  const bool print_key;
242  const bool print_weight;
243  const std::string &symbols_source;
244  const bool initial_symbols;
245  const int32_t generate_sources;
246  const std::string &source_prefix;
247  const std::string &source_suffix;
248 };
249 
250 template <class Arc>
252  FarReader<Arc> &reader = *args->reader.GetFarReader<Arc>();
253  ::fst::PrintStrings<Arc>(reader, args->entry_type, args->token_type,
254  args->begin_key, args->end_key, args->print_key,
255  args->print_weight, args->symbols_source,
256  args->initial_symbols, args->generate_sources,
257  args->source_prefix, args->source_suffix);
258 }
259 
260 void PrintStrings(FarReaderClass &reader, const FarEntryType entry_type,
261  const TokenType token_type, const std::string &begin_key,
262  const std::string &end_key, const bool print_key,
263  const bool print_weight, const std::string &symbols_source,
264  const bool initial_symbols, const int32_t generate_sources,
265  const std::string &source_prefix,
266  const std::string &source_suffix);
267 
268 } // namespace script
269 } // namespace fst
270 
271 #define REGISTER_FST_FAR_OPERATIONS(ArcType) \
272  REGISTER_FST_OPERATION(CompileStrings, ArcType, FarCompileStringsArgs); \
273  REGISTER_FST_OPERATION(Create, ArcType, FarCreateArgs); \
274  REGISTER_FST_OPERATION(Equal, ArcType, FarEqualArgs); \
275  REGISTER_FST_OPERATION(Extract, ArcType, FarExtractArgs); \
276  REGISTER_FST_OPERATION(Info, ArcType, FarInfoArgs); \
277  REGISTER_FST_OPERATION(Isomorphic, ArcType, FarIsomorphicArgs); \
278  REGISTER_FST_OPERATION(PrintStrings, ArcType, FarPrintStringsArgs); \
279  REGISTER_FST_OPERATION(GetInfo, ArcType, FarGetInfoArgs)
280 
281 #endif // FST_EXTENSIONS_FAR_FARSCRIPT_H_
const FarReader< Arc > * GetFarReader() const
Definition: far-class.h:127
const std::string & symbols_source
Definition: farscript.h:62
void PrintStrings(FarReaderClass &reader, const FarEntryType entry_type, const TokenType token_type, const std::string &begin_key, const std::string &end_key, bool print_key, bool print_weight, const std::string &symbols_source, bool initial_symbols, const int32_t generate_sources, const std::string &source_prefix, const std::string &source_suffix)
Definition: farscript.cc:166
void GetInfo(const std::vector< std::string > &sources, const std::string &arc_type, const std::string &begin_key, const std::string &end_key, bool list_fsts, FarInfoData *data)
Definition: farscript.cc:136
std::tuple< const std::vector< std::string > &, const std::string &, const std::string &, const bool > FarInfoArgs
Definition: farscript.h:189
bool Equal(FarReaderClass &reader1, FarReaderClass &reader2, float delta, std::string_view begin_key, std::string_view end_key)
Definition: farscript.cc:112
std::tuple< FarReaderClass &, FarReaderClass &, float, std::string_view, std::string_view > FarIsomorphicInnerArgs
Definition: farscript.h:218
bool Isomorphic(FarReaderClass &reader1, FarReaderClass &reader2, float delta, std::string_view begin_key, std::string_view end_key)
Definition: farscript.cc:154
std::tuple< FarReaderClass &, FarWriterClass &, EncodeMapperClass * > FarEncodeArgs
Definition: farscript.h:137
const std::vector< std::string > & sources
Definition: farscript.h:56
const std::string & unknown_symbol
Definition: farscript.h:63
std::tuple< FarReaderClass &, FarWriterClass &, std::string_view > FarConvertArgs
Definition: farscript.h:88
const std::string & key_suffix
Definition: farscript.h:108
std::tuple< const std::vector< std::string > &, const std::string &, const std::string &, const bool, FarInfoData * > FarGetInfoArgs
Definition: farscript.h:203
const std::vector< std::string > & sources
Definition: farscript.h:104
std::tuple< FarReaderClass &, int32_t, const std::string &, const std::string &, const std::string &, const std::string &, const std::string & > FarExtractArgs
Definition: farscript.h:171
void Decode(FarReaderClass &reader, FarWriterClass &writer, const EncodeMapperClass &encoder)
Definition: farscript.cc:88
const std::string & source_prefix
Definition: farscript.h:246
const std::string & begin_key
Definition: farscript.h:239
void Info(const std::vector< std::string > &sources, const std::string &arc_type, const std::string &begin_key, const std::string &end_key, bool list_fsts)
Definition: farscript.cc:145
FarWriterClass & writer
Definition: farscript.h:105
std::tuple< FarReaderClass &, FarWriterClass &, const EncodeMapperClass & > FarDecodeArgs
Definition: farscript.h:123
void Encode(FarReaderClass &reader, FarWriterClass &writer, EncodeMapperClass *encoder)
Definition: farscript.cc:100
const std::string & key_suffix
Definition: farscript.h:67
const int32_t generate_keys
Definition: farscript.h:106
TokenType
Definition: string.h:49
std::tuple< FarReaderClass &, FarReaderClass &, float, std::string_view, std::string_view > FarEqualInnerArgs
Definition: farscript.h:151
const FarWriter< Arc > * GetFarWriter() const
Definition: far-class.h:249
void Create(const std::vector< std::string > &sources, FarWriterClass &writer, const int32_t generate_keys, const std::string &key_prefix, const std::string &key_suffix)
Definition: farscript.cc:79
const std::string & symbols_source
Definition: farscript.h:243
const std::string & end_key
Definition: farscript.h:240
const std::string & source_suffix
Definition: farscript.h:247
const std::string & key_prefix
Definition: farscript.h:107
const FarEntryType entry_type
Definition: farscript.h:237
void Extract(FarReaderClass &reader, int32_t generate_sources, const std::string &keys, const std::string &key_separator, const std::string &range_delimiter, const std::string &source_prefix, const std::string &source_suffix)
Definition: farscript.cc:123
FarEntryType
Definition: far.h:49
constexpr float kDelta
Definition: weight.h:133
void CompileStrings(const std::vector< std::string > &sources, FarWriterClass &writer, std::string_view fst_type, int32_t generate_keys, FarEntryType fet, TokenType tt, const std::string &symbols_source, const std::string &unknown_symbol, bool keep_symbols, bool initial_symbols, const std::string &key_prefix, const std::string &key_suffix)
Definition: farscript.cc:46
void Convert(FarReaderClass &reader, FarWriterClass &writer, std::string_view fst_type)
Definition: farscript.cc:71
const std::string & key_prefix
Definition: farscript.h:66