FST  openfst-1.8.3
OpenFst Library
farscript.cc
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 // Definitions of 'scriptable' versions of FAR operations, that is,
19 // those that can be called with FstClass-type arguments.
20 
22 
23 #include <cstdint>
24 #include <string>
25 #include <vector>
26 
28 #include <fst/extensions/far/far.h>
30 #include <fst/arc.h>
31 #include <fst/cache.h>
32 #include <fst/error-weight.h>
33 #include <fst/float-weight.h>
34 #include <fst/string.h>
36 #include <fst/script/script-impl.h>
37 #include <string_view>
38 
39 #define REGISTER_FST_OPERATION_4ARCS(Op, ArgPack) \
40  REGISTER_FST_OPERATION_3ARCS(Op, ArgPack); \
41  REGISTER_FST_OPERATION(Op, ErrorArc, ArgPack)
42 
43 namespace fst {
44 namespace script {
45 
46 void CompileStrings(const std::vector<std::string> &sources,
47  FarWriterClass &writer, std::string_view fst_type,
48  int32_t generate_keys, FarEntryType fet, TokenType tt,
49  const std::string &symbols_source,
50  const std::string &unknown_symbol, bool keep_symbols,
51  bool initial_symbols, const std::string &key_prefix,
52  const std::string &key_suffix) {
53  FarCompileStringsArgs args{sources,
54  writer,
55  fst_type,
56  generate_keys,
57  fet,
58  tt,
59  symbols_source,
60  unknown_symbol,
61  keep_symbols,
62  initial_symbols,
63  key_prefix,
64  key_suffix};
65  Apply<Operation<FarCompileStringsArgs>>("CompileStrings", writer.ArcType(),
66  &args);
67 }
68 
70 
71 void Convert(FarReaderClass &reader, FarWriterClass &writer,
72  std::string_view fst_type) {
73  FarConvertArgs args{reader, writer, fst_type};
74  Apply<Operation<FarConvertArgs>>("Convert", reader.ArcType(), &args);
75 }
76 
78 
79 void Create(const std::vector<std::string> &sources, FarWriterClass &writer,
80  const int32_t generate_keys, const std::string &key_prefix,
81  const std::string &key_suffix) {
82  FarCreateArgs args{sources, writer, generate_keys, key_prefix, key_suffix};
83  Apply<Operation<FarCreateArgs>>("Create", writer.ArcType(), &args);
84 }
85 
87 
88 void Decode(FarReaderClass &reader, FarWriterClass &writer,
89  const EncodeMapperClass &encoder) {
90  if (!internal::ArcTypesMatch(reader, encoder, "Decode") ||
91  !internal::ArcTypesMatch(writer, encoder, "Decode")) {
92  return;
93  }
94  FarDecodeArgs args{reader, writer, encoder};
95  Apply<Operation<FarDecodeArgs>>("Decode", reader.ArcType(), &args);
96 }
97 
99 
100 void Encode(FarReaderClass &reader, FarWriterClass &writer,
101  EncodeMapperClass *encoder) {
102  if (!internal::ArcTypesMatch(reader, *encoder, "Encode") ||
103  !internal::ArcTypesMatch(writer, *encoder, "Encode")) {
104  return;
105  }
106  FarEncodeArgs args{reader, writer, encoder};
107  Apply<Operation<FarEncodeArgs>>("Encode", reader.ArcType(), &args);
108 }
109 
111 
112 bool Equal(FarReaderClass &reader1, FarReaderClass &reader2, float delta,
113  std::string_view begin_key, std::string_view end_key) {
114  if (!internal::ArcTypesMatch(reader1, reader2, "Equal")) return false;
115  FarEqualInnerArgs args{reader1, reader2, delta, begin_key, end_key};
116  FarEqualArgs args_with_retval(args);
117  Apply<Operation<FarEqualArgs>>("Equal", reader1.ArcType(), &args_with_retval);
118  return args_with_retval.retval;
119 }
120 
122 
123 void Extract(FarReaderClass &reader, int32_t generate_sources,
124  const std::string &keys, const std::string &key_separator,
125  const std::string &range_delimiter,
126  const std::string &source_prefix,
127  const std::string &source_suffix) {
128  FarExtractArgs args{reader, generate_sources, keys,
129  key_separator, range_delimiter, source_prefix,
130  source_suffix};
131  Apply<Operation<FarExtractArgs>>("Extract", reader.ArcType(), &args);
132 }
133 
135 
136 void GetInfo(const std::vector<std::string> &sources,
137  const std::string &arc_type, const std::string &begin_key,
138  const std::string &end_key, bool list_fsts, FarInfoData *data) {
139  FarGetInfoArgs args{sources, begin_key, end_key, list_fsts, data};
140  Apply<Operation<FarGetInfoArgs>>("GetInfo", arc_type, &args);
141 }
142 
144 
145 void Info(const std::vector<std::string> &sources, const std::string &arc_type,
146  const std::string &begin_key, const std::string &end_key,
147  bool list_fsts) {
148  FarInfoArgs args{sources, begin_key, end_key, list_fsts};
149  Apply<Operation<FarInfoArgs>>("Info", arc_type, &args);
150 }
151 
153 
154 bool Isomorphic(FarReaderClass &reader1, FarReaderClass &reader2, float delta,
155  std::string_view begin_key, std::string_view end_key) {
156  if (!internal::ArcTypesMatch(reader1, reader2, "Equal")) return false;
157  FarIsomorphicInnerArgs args{reader1, reader2, delta, begin_key, end_key};
158  FarIsomorphicArgs args_with_retval(args);
159  Apply<Operation<FarIsomorphicArgs>>("Isomorphic", reader1.ArcType(),
160  &args_with_retval);
161  return args_with_retval.retval;
162 }
163 
165 
166 void PrintStrings(FarReaderClass &reader, const FarEntryType entry_type,
167  const TokenType token_type, const std::string &begin_key,
168  const std::string &end_key, bool print_key, bool print_weight,
169  const std::string &symbols_source, bool initial_symbols,
170  const int32_t generate_sources,
171  const std::string &source_prefix,
172  const std::string &source_suffix) {
173  FarPrintStringsArgs args{reader, entry_type, token_type,
174  begin_key, end_key, print_key,
175  print_weight, symbols_source, initial_symbols,
176  generate_sources, source_prefix, source_suffix};
177  Apply<Operation<FarPrintStringsArgs>>("PrintStrings", reader.ArcType(),
178  &args);
179 }
180 
182 
183 } // namespace script
184 } // namespace fst
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
std::tuple< FarReaderClass &, FarWriterClass &, std::string_view > FarConvertArgs
Definition: farscript.h:88
std::tuple< const std::vector< std::string > &, const std::string &, const std::string &, const bool, FarInfoData * > FarGetInfoArgs
Definition: farscript.h:203
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
bool ArcTypesMatch(const M &m, const N &n, const std::string &op_name)
Definition: script-impl.h:195
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
std::tuple< FarReaderClass &, FarWriterClass &, const EncodeMapperClass & > FarDecodeArgs
Definition: farscript.h:123
const std::string & ArcType() const
Definition: far-class.h:244
void Encode(FarReaderClass &reader, FarWriterClass &writer, EncodeMapperClass *encoder)
Definition: farscript.cc:100
TokenType
Definition: string.h:49
REGISTER_FST_OPERATION_4ARCS(CompileStrings, FarCompileStringsArgs)
std::tuple< FarReaderClass &, FarReaderClass &, float, std::string_view, std::string_view > FarEqualInnerArgs
Definition: farscript.h:151
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 & ArcType() const
Definition: far-class.h:106
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
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