FST  openfst-1.8.4
OpenFst Library
pdtscript.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 pdt operations, that is,
19 // those that can be called with FstClass-type arguments.
20 //
21 // See comments in nlp/fst/script/script-impl.h for how the registration
22 // mechanism allows these to work with various arc types.
23 
25 
26 #include <cstddef>
27 #include <cstdint>
28 #include <string>
29 #include <utility>
30 #include <vector>
31 
34 #include <fst/script/fst-class.h>
35 #include <fst/script/script-impl.h>
37 
38 namespace fst {
39 namespace script {
40 
41 void Compose(const FstClass &ifst1, const FstClass &ifst2,
42  const std::vector<std::pair<int64_t, int64_t>> &parens,
43  MutableFstClass *ofst, const PdtComposeOptions &copts,
44  bool left_pdt) {
45  if (!internal::ArcTypesMatch(ifst1, ifst2, "Compose") ||
46  !internal::ArcTypesMatch(ifst1, *ofst, "Compose")) {
47  return;
48  }
49  PdtComposeArgs args{ifst1, ifst2, parens, ofst, copts, left_pdt};
50  Apply<Operation<PdtComposeArgs>>("Compose", ifst1.ArcType(), &args);
51 }
52 
54 
55 void Expand(const FstClass &ifst,
56  const std::vector<std::pair<int64_t, int64_t>> &parens,
57  MutableFstClass *ofst, const PdtExpandOptions &opts) {
58  PdtExpandArgs args{ifst, parens, ofst, opts};
59  Apply<Operation<PdtExpandArgs>>("Expand", ifst.ArcType(), &args);
60 }
61 
63 
64 void Expand(const FstClass &ifst,
65  const std::vector<std::pair<int64_t, int64_t>> &parens,
66  MutableFstClass *ofst, bool connect, bool keep_parentheses,
67  const WeightClass &weight_threshold) {
68  Expand(ifst, parens, ofst,
69  PdtExpandOptions(connect, keep_parentheses, weight_threshold));
70 }
71 
72 void Replace(const std::vector<std::pair<int64_t, const FstClass *>> &pairs,
73  MutableFstClass *ofst,
74  std::vector<std::pair<int64_t, int64_t>> *parens, int64_t root,
75  PdtParserType parser_type, int64_t start_paren_labels,
76  const std::string &left_paren_prefix,
77  const std::string &right_paren_prefix) {
78  for (size_t i = 1; i < pairs.size(); ++i) {
79  if (!internal::ArcTypesMatch(*pairs[i - 1].second, *pairs[i].second,
80  "Replace"))
81  return;
82  }
83  if (!internal::ArcTypesMatch(*pairs[0].second, *ofst, "PdtReplace")) return;
84  PdtReplaceArgs args{pairs,
85  ofst,
86  parens,
87  root,
88  parser_type,
89  start_paren_labels,
90  left_paren_prefix,
91  right_paren_prefix};
92  Apply<Operation<PdtReplaceArgs>>("Replace", ofst->ArcType(), &args);
93 }
94 
96 
97 void Reverse(const FstClass &ifst,
98  const std::vector<std::pair<int64_t, int64_t>> &parens,
99  MutableFstClass *ofst) {
100  PdtReverseArgs args{ifst, parens, ofst};
101  Apply<Operation<PdtReverseArgs>>("Reverse", ifst.ArcType(), &args);
102 }
103 
105 
106 void ShortestPath(const FstClass &ifst,
107  const std::vector<std::pair<int64_t, int64_t>> &parens,
108  MutableFstClass *ofst, const PdtShortestPathOptions &opts) {
109  PdtShortestPathArgs args{ifst, parens, ofst, opts};
110  Apply<Operation<PdtShortestPathArgs>>("ShortestPath", ifst.ArcType(), &args);
111 }
112 
114 
115 void Info(const FstClass &ifst,
116  const std::vector<std::pair<int64_t, int64_t>> &parens) {
117  PdtInfoArgs args(ifst, parens);
118  Apply<Operation<PdtInfoArgs>>("Info", ifst.ArcType(), &args);
119 }
120 
122 
123 } // namespace script
124 } // namespace fst
void Reverse(const FstClass &ifst, const std::vector< std::pair< int64_t, int64_t >> &parens, std::vector< int64_t > *assignments, MutableFstClass *ofst)
Definition: mpdtscript.cc:69
void Replace(const std::vector< std::pair< int64_t, const FstClass * >> &pairs, MutableFstClass *ofst, std::vector< std::pair< int64_t, int64_t >> *parens, int64_t root, PdtParserType parser_type, int64_t start_paren_labels, const std::string &left_paren_prefix, const std::string &right_paren_prefix)
Definition: pdtscript.cc:72
REGISTER_FST_OPERATION_3ARCS(Compress, CompressArgs)
const std::string & ArcType() const final
Definition: fst-class.h:335
PdtParserType
Definition: replace.h:69
void ShortestPath(const FstClass &ifst, const std::vector< std::pair< int64_t, int64_t >> &parens, MutableFstClass *ofst, const PdtShortestPathOptions &opts)
Definition: pdtscript.cc:106
std::tuple< const std::vector< std::pair< int64_t, const FstClass * >> &, MutableFstClass *, std::vector< std::pair< int64_t, int64_t >> *, int64_t, PdtParserType, int64_t, const std::string &, const std::string & > PdtReplaceArgs
Definition: pdtscript.h:128
std::tuple< const FstClass &, const std::vector< std::pair< int64_t, int64_t >> &, MutableFstClass * > PdtReverseArgs
Definition: pdtscript.h:163
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
void Expand(const FstClass &ifst, const std::vector< std::pair< int64_t, int64_t >> &parens, const std::vector< int64_t > &assignments, MutableFstClass *ofst, const MPdtExpandOptions &opts)
Definition: mpdtscript.cc:52
std::pair< const FstClass &, const std::vector< std::pair< int64_t, int64_t >> & > PdtInfoArgs
Definition: pdtscript.h:249
std::tuple< const FstClass &, const std::vector< std::pair< int64_t, int64_t >> &, MutableFstClass *, const PdtShortestPathOptions & > PdtShortestPathArgs
Definition: pdtscript.h:198
std::tuple< const FstClass &, const FstClass &, const std::vector< std::pair< int64_t, int64_t >> &, MutableFstClass *, const PdtComposeOptions &, bool > PdtComposeArgs
Definition: pdtscript.h:57
void Compose(const FstClass &ifst1, const FstClass &ifst2, const std::vector< std::pair< int64_t, int64_t >> &parens, const std::vector< int64_t > &assignments, MutableFstClass *ofst, const MPdtComposeOptions &copts, bool left_pdt)
Definition: mpdtscript.cc:38
std::tuple< const FstClass &, const std::vector< std::pair< int64_t, int64_t >> &, MutableFstClass *, const PdtExpandOptions & > PdtExpandArgs
Definition: pdtscript.h:95