FST  openfst-1.8.3
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/arc.h>
35 #include <fst/cache.h>
36 #include <fst/float-weight.h>
37 #include <fst/script/fst-class.h>
38 #include <fst/script/script-impl.h>
40 
41 namespace fst {
42 namespace script {
43 
44 void Compose(const FstClass &ifst1, const FstClass &ifst2,
45  const std::vector<std::pair<int64_t, int64_t>> &parens,
46  MutableFstClass *ofst, const PdtComposeOptions &copts,
47  bool left_pdt) {
48  if (!internal::ArcTypesMatch(ifst1, ifst2, "Compose") ||
49  !internal::ArcTypesMatch(ifst1, *ofst, "Compose")) {
50  return;
51  }
52  PdtComposeArgs args{ifst1, ifst2, parens, ofst, copts, left_pdt};
53  Apply<Operation<PdtComposeArgs>>("Compose", ifst1.ArcType(), &args);
54 }
55 
57 
58 void Expand(const FstClass &ifst,
59  const std::vector<std::pair<int64_t, int64_t>> &parens,
60  MutableFstClass *ofst, const PdtExpandOptions &opts) {
61  PdtExpandArgs args{ifst, parens, ofst, opts};
62  Apply<Operation<PdtExpandArgs>>("Expand", ifst.ArcType(), &args);
63 }
64 
66 
67 void Expand(const FstClass &ifst,
68  const std::vector<std::pair<int64_t, int64_t>> &parens,
69  MutableFstClass *ofst, bool connect, bool keep_parentheses,
70  const WeightClass &weight_threshold) {
71  Expand(ifst, parens, ofst,
72  PdtExpandOptions(connect, keep_parentheses, weight_threshold));
73 }
74 
75 void Replace(const std::vector<std::pair<int64_t, const FstClass *>> &pairs,
76  MutableFstClass *ofst,
77  std::vector<std::pair<int64_t, int64_t>> *parens, int64_t root,
78  PdtParserType parser_type, int64_t start_paren_labels,
79  const std::string &left_paren_prefix,
80  const std::string &right_paren_prefix) {
81  for (size_t i = 1; i < pairs.size(); ++i) {
82  if (!internal::ArcTypesMatch(*pairs[i - 1].second, *pairs[i].second,
83  "Replace"))
84  return;
85  }
86  if (!internal::ArcTypesMatch(*pairs[0].second, *ofst, "PdtReplace")) return;
87  PdtReplaceArgs args{pairs,
88  ofst,
89  parens,
90  root,
91  parser_type,
92  start_paren_labels,
93  left_paren_prefix,
94  right_paren_prefix};
95  Apply<Operation<PdtReplaceArgs>>("Replace", ofst->ArcType(), &args);
96 }
97 
99 
100 void Reverse(const FstClass &ifst,
101  const std::vector<std::pair<int64_t, int64_t>> &parens,
102  MutableFstClass *ofst) {
103  PdtReverseArgs args{ifst, parens, ofst};
104  Apply<Operation<PdtReverseArgs>>("Reverse", ifst.ArcType(), &args);
105 }
106 
108 
109 void ShortestPath(const FstClass &ifst,
110  const std::vector<std::pair<int64_t, int64_t>> &parens,
111  MutableFstClass *ofst, const PdtShortestPathOptions &opts) {
112  PdtShortestPathArgs args{ifst, parens, ofst, opts};
113  Apply<Operation<PdtShortestPathArgs>>("ShortestPath", ifst.ArcType(), &args);
114 }
115 
117 
118 void Info(const FstClass &ifst,
119  const std::vector<std::pair<int64_t, int64_t>> &parens) {
120  PdtInfoArgs args(ifst, parens);
121  Apply<Operation<PdtInfoArgs>>("Info", ifst.ArcType(), &args);
122 }
123 
125 
126 } // namespace script
127 } // 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:72
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:75
REGISTER_FST_OPERATION_3ARCS(Compress, CompressArgs)
const std::string & ArcType() const final
Definition: fst-class.h:329
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:109
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:55
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:41
std::tuple< const FstClass &, const std::vector< std::pair< int64_t, int64_t >> &, MutableFstClass *, const PdtExpandOptions & > PdtExpandArgs
Definition: pdtscript.h:95