FST  openfst-1.8.1
OpenFst Library
pdtscript.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 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 <string>
27 #include <utility>
28 #include <vector>
29 
30 #include <fst/types.h>
36 #include <fst/script/script-impl.h>
37 
38 namespace fst {
39 namespace script {
40 
41 void PdtCompose(const FstClass &ifst1, const FstClass &ifst2,
42  const std::vector<std::pair<int64, int64>> &parens,
43  MutableFstClass *ofst, const PdtComposeOptions &copts,
44  bool left_pdt) {
45  if (!internal::ArcTypesMatch(ifst1, ifst2, "PdtCompose") ||
46  !internal::ArcTypesMatch(ifst1, *ofst, "PdtCompose")) {
47  return;
48  }
49  PdtComposeArgs args(ifst1, ifst2, parens, ofst, copts, left_pdt);
50  Apply<Operation<PdtComposeArgs>>("PdtCompose", ifst1.ArcType(), &args);
51 }
52 
54 
55 void PdtExpand(const FstClass &ifst,
56  const std::vector<std::pair<int64, int64>> &parens,
57  MutableFstClass *ofst, const PdtExpandOptions &opts) {
58  PdtExpandArgs args(ifst, parens, ofst, opts);
59  Apply<Operation<PdtExpandArgs>>("PdtExpand", ifst.ArcType(), &args);
60 }
61 
63 
64 void PdtExpand(const FstClass &ifst,
65  const std::vector<std::pair<int64, int64>> &parens,
66  MutableFstClass *ofst, bool connect, bool keep_parentheses,
67  const WeightClass &weight_threshold) {
68  PdtExpand(ifst, parens, ofst,
69  PdtExpandOptions(connect, keep_parentheses, weight_threshold));
70 }
71 
72 void PdtReplace(const std::vector<std::pair<int64, const FstClass *>> &pairs,
73  MutableFstClass *ofst,
74  std::vector<std::pair<int64, int64>> *parens, int64 root,
75  PdtParserType parser_type, int64 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  "PdtReplace"))
81  return;
82  }
83  if (!internal::ArcTypesMatch(*pairs[0].second, *ofst, "PdtReplace")) return;
84  PdtReplaceArgs args(pairs, ofst, parens, root, parser_type,
85  start_paren_labels, left_paren_prefix,
86  right_paren_prefix);
87  Apply<Operation<PdtReplaceArgs>>("PdtReplace", ofst->ArcType(), &args);
88 }
89 
91 
92 void PdtReverse(const FstClass &ifst,
93  const std::vector<std::pair<int64, int64>> &parens,
94  MutableFstClass *ofst) {
95  PdtReverseArgs args(ifst, parens, ofst);
96  Apply<Operation<PdtReverseArgs>>("PdtReverse", ifst.ArcType(), &args);
97 }
98 
100 
101 void PdtShortestPath(const FstClass &ifst,
102  const std::vector<std::pair<int64, int64>> &parens,
103  MutableFstClass *ofst,
104  const PdtShortestPathOptions &opts) {
105  PdtShortestPathArgs args(ifst, parens, ofst, opts);
106  Apply<Operation<PdtShortestPathArgs>>("PdtShortestPath", ifst.ArcType(),
107  &args);
108 }
109 
111 
112 void PrintPdtInfo(const FstClass &ifst,
113  const std::vector<std::pair<int64, int64>> &parens) {
114  PrintPdtInfoArgs args(ifst, parens);
115  Apply<Operation<PrintPdtInfoArgs>>("PrintPdtInfo", ifst.ArcType(), &args);
116 }
117 
119 
120 } // namespace script
121 } // namespace fst
void PdtShortestPath(const FstClass &ifst, const std::vector< std::pair< int64, int64 >> &parens, MutableFstClass *ofst, const PdtShortestPathOptions &opts)
Definition: pdtscript.cc:101
REGISTER_FST_OPERATION_3ARCS(Compress, CompressArgs)
PdtParserType
Definition: replace.h:59
const std::string & ArcType() const final
Definition: fst-class.h:320
void PdtReplace(const std::vector< std::pair< int64, const FstClass * >> &pairs, MutableFstClass *ofst, std::vector< std::pair< int64, int64 >> *parens, int64 root, PdtParserType parser_type, int64 start_paren_labels, const std::string &left_paren_prefix, const std::string &right_paren_prefix)
Definition: pdtscript.cc:72
bool ArcTypesMatch(const M &m, const N &n, const std::string &op_name)
Definition: script-impl.h:190
void PdtCompose(const FstClass &ifst1, const FstClass &ifst2, const std::vector< std::pair< int64, int64 >> &parens, MutableFstClass *ofst, const PdtComposeOptions &copts, bool left_pdt)
Definition: pdtscript.cc:41
int64_t int64
Definition: types.h:27
void PdtExpand(const FstClass &ifst, const std::vector< std::pair< int64, int64 >> &parens, MutableFstClass *ofst, const PdtExpandOptions &opts)
Definition: pdtscript.cc:55
std::tuple< const FstClass &, const FstClass &, const std::vector< std::pair< int64, int64 >> &, MutableFstClass *, const PdtComposeOptions &, bool > PdtComposeArgs
Definition: pdtscript.h:48
void PrintPdtInfo(const FstClass &ifst, const std::vector< std::pair< int64, int64 >> &parens)
Definition: pdtscript.cc:112
std::tuple< const FstClass &, const std::vector< std::pair< int64, int64 >> &, MutableFstClass * > PdtReverseArgs
Definition: pdtscript.h:151
std::pair< const FstClass &, const std::vector< std::pair< int64, int64 >> & > PrintPdtInfoArgs
Definition: pdtscript.h:234
std::tuple< const FstClass &, const std::vector< std::pair< int64, int64 >> &, MutableFstClass *, const PdtShortestPathOptions & > PdtShortestPathArgs
Definition: pdtscript.h:185
std::tuple< const FstClass &, const std::vector< std::pair< int64, int64 >> &, MutableFstClass *, const PdtExpandOptions & > PdtExpandArgs
Definition: pdtscript.h:85
void PdtReverse(const FstClass &ifst, const std::vector< std::pair< int64, int64 >> &parens, MutableFstClass *ofst)
Definition: pdtscript.cc:92
std::tuple< const std::vector< std::pair< int64, const FstClass * >> &, MutableFstClass *, std::vector< std::pair< int64, int64 >> *, int64, PdtParserType, int64, const std::string &, const std::string & > PdtReplaceArgs
Definition: pdtscript.h:117