FST  openfst-1.8.2
OpenFst Library
farscript.cc
Go to the documentation of this file.
1 // Copyright 2005-2020 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 
26 #include <fst/extensions/far/far.h>
27 #include <fst/arc.h>
28 #include <fst/script/script-impl.h>
29 
30 #define REGISTER_FST_OPERATION_4ARCS(Op, ArgPack) \
31  REGISTER_FST_OPERATION_3ARCS(Op, ArgPack); \
32  REGISTER_FST_OPERATION(Op, ErrorArc, ArgPack)
33 
34 namespace fst {
35 namespace script {
36 
37 void CompileStrings(const std::vector<std::string> &sources,
38  FarWriterClass &writer, std::string_view fst_type,
39  int32_t generate_keys, FarEntryType fet, TokenType tt,
40  const std::string &symbols_source,
41  const std::string &unknown_symbol, bool keep_symbols,
42  bool initial_symbols, bool allow_negative_labels,
43  const std::string &key_prefix,
44  const std::string &key_suffix) {
45  FarCompileStringsArgs args{sources,
46  writer,
47  fst_type,
48  generate_keys,
49  fet,
50  tt,
51  symbols_source,
52  unknown_symbol,
53  keep_symbols,
54  initial_symbols,
55  allow_negative_labels,
56  key_prefix,
57  key_suffix};
58  Apply<Operation<FarCompileStringsArgs>>("CompileStrings", writer.ArcType(),
59  &args);
60 }
61 
63 
64 void Convert(FarReaderClass &reader, FarWriterClass &writer,
65  std::string_view fst_type) {
66  FarConvertArgs args{reader, writer, fst_type};
67  Apply<Operation<FarConvertArgs>>("Convert", reader.ArcType(), &args);
68 }
69 
71 
72 void Create(const std::vector<std::string> &sources, FarWriterClass &writer,
73  const int32_t generate_keys, const std::string &key_prefix,
74  const std::string &key_suffix) {
75  FarCreateArgs args{sources, writer, generate_keys, key_prefix, key_suffix};
76  Apply<Operation<FarCreateArgs>>("Create", writer.ArcType(), &args);
77 }
78 
80 
81 void Decode(FarReaderClass &reader, FarWriterClass &writer,
82  const EncodeMapperClass &encoder) {
83  if (!internal::ArcTypesMatch(reader, encoder, "Decode") ||
84  !internal::ArcTypesMatch(writer, encoder, "Decode")) {
85  return;
86  }
87  FarDecodeArgs args{reader, writer, encoder};
88  Apply<Operation<FarDecodeArgs>>("Decode", reader.ArcType(), &args);
89 }
90 
92 
93 void Encode(FarReaderClass &reader, FarWriterClass &writer,
94  EncodeMapperClass *encoder) {
95  if (!internal::ArcTypesMatch(reader, *encoder, "Encode") ||
96  !internal::ArcTypesMatch(writer, *encoder, "Encode")) {
97  return;
98  }
99  FarEncodeArgs args{reader, writer, encoder};
100  Apply<Operation<FarEncodeArgs>>("Encode", reader.ArcType(), &args);
101 }
102 
104 
105 bool Equal(FarReaderClass &reader1, FarReaderClass &reader2, float delta,
106  std::string_view begin_key, std::string_view end_key) {
107  if (!internal::ArcTypesMatch(reader1, reader2, "Equal")) return false;
108  FarEqualInnerArgs args{reader1, reader2, delta, begin_key, end_key};
109  FarEqualArgs args_with_retval(args);
110  Apply<Operation<FarEqualArgs>>("Equal", reader1.ArcType(), &args_with_retval);
111  return args_with_retval.retval;
112 }
113 
115 
116 void Extract(FarReaderClass &reader, int32_t generate_sources,
117  const std::string &keys, const std::string &key_separator,
118  const std::string &range_delimiter,
119  const std::string &source_prefix,
120  const std::string &source_suffix) {
121  FarExtractArgs args{reader, generate_sources, keys,
122  key_separator, range_delimiter, source_prefix,
123  source_suffix};
124  Apply<Operation<FarExtractArgs>>("Extract", reader.ArcType(), &args);
125 }
126 
128 
129 void GetInfo(const std::vector<std::string> &sources,
130  const std::string &arc_type, const std::string &begin_key,
131  const std::string &end_key, bool list_fsts, FarInfoData *data) {
132  FarGetInfoArgs args{sources, begin_key, end_key, list_fsts, data};
133  Apply<Operation<FarGetInfoArgs>>("GetInfo", arc_type, &args);
134 }
135 
137 
138 void Info(const std::vector<std::string> &sources, const std::string &arc_type,
139  const std::string &begin_key, const std::string &end_key,
140  bool list_fsts) {
141  FarInfoArgs args{sources, begin_key, end_key, list_fsts};
142  Apply<Operation<FarInfoArgs>>("Info", arc_type, &args);
143 }
144 
146 
147 bool Isomorphic(FarReaderClass &reader1, FarReaderClass &reader2, float delta,
148  std::string_view begin_key, std::string_view end_key) {
149  if (!internal::ArcTypesMatch(reader1, reader2, "Equal")) return false;
150  FarIsomorphicInnerArgs args{reader1, reader2, delta, begin_key, end_key};
151  FarIsomorphicArgs args_with_retval(args);
152  Apply<Operation<FarIsomorphicArgs>>("Isomorphic", reader1.ArcType(),
153  &args_with_retval);
154  return args_with_retval.retval;
155 }
156 
158 
159 void PrintStrings(FarReaderClass &reader, const FarEntryType entry_type,
160  const TokenType token_type, const std::string &begin_key,
161  const std::string &end_key, bool print_key, bool print_weight,
162  const std::string &symbols_source, bool initial_symbols,
163  const int32_t generate_sources,
164  const std::string &source_prefix,
165  const std::string &source_suffix) {
166  FarPrintStringsArgs args{reader, entry_type, token_type,
167  begin_key, end_key, print_key,
168  print_weight, symbols_source, initial_symbols,
169  generate_sources, source_prefix, source_suffix};
170  Apply<Operation<FarPrintStringsArgs>>("PrintStrings", reader.ArcType(),
171  &args);
172 }
173 
175 
176 } // namespace script
177 } // 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:159
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:129
std::tuple< const std::vector< std::string > &, const std::string &, const std::string &, const bool > FarInfoArgs
Definition: farscript.h:186
bool Equal(FarReaderClass &reader1, FarReaderClass &reader2, float delta, std::string_view begin_key, std::string_view end_key)
Definition: farscript.cc:105
std::tuple< FarReaderClass &, FarReaderClass &, float, std::string_view, std::string_view > FarIsomorphicInnerArgs
Definition: farscript.h:215
bool Isomorphic(FarReaderClass &reader1, FarReaderClass &reader2, float delta, std::string_view begin_key, std::string_view end_key)
Definition: farscript.cc:147
std::tuple< FarReaderClass &, FarWriterClass &, EncodeMapperClass * > FarEncodeArgs
Definition: farscript.h:134
std::tuple< FarReaderClass &, FarWriterClass &, std::string_view > FarConvertArgs
Definition: farscript.h:85
std::tuple< const std::vector< std::string > &, const std::string &, const std::string &, const bool, FarInfoData * > FarGetInfoArgs
Definition: farscript.h:200
std::tuple< FarReaderClass &, int32_t, const std::string &, const std::string &, const std::string &, const std::string &, const std::string & > FarExtractArgs
Definition: farscript.h:168
void Decode(FarReaderClass &reader, FarWriterClass &writer, const EncodeMapperClass &encoder)
Definition: farscript.cc:81
bool ArcTypesMatch(const M &m, const N &n, const std::string &op_name)
Definition: script-impl.h:191
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:138
std::tuple< FarReaderClass &, FarWriterClass &, const EncodeMapperClass & > FarDecodeArgs
Definition: farscript.h:120
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, bool allow_negative_labels, const std::string &key_prefix, const std::string &key_suffix)
Definition: farscript.cc:37
const std::string & ArcType() const
Definition: far-class.h:238
void Encode(FarReaderClass &reader, FarWriterClass &writer, EncodeMapperClass *encoder)
Definition: farscript.cc:93
TokenType
Definition: string.h:47
REGISTER_FST_OPERATION_4ARCS(CompileStrings, FarCompileStringsArgs)
std::tuple< FarReaderClass &, FarReaderClass &, float, std::string_view, std::string_view > FarEqualInnerArgs
Definition: farscript.h:148
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:72
const std::string & ArcType() const
Definition: far-class.h:101
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:116
FarEntryType
Definition: far.h:41
void Convert(FarReaderClass &reader, FarWriterClass &writer, std::string_view fst_type)
Definition: farscript.cc:64