FST  openfst-1.8.2.post1
OpenFst Library
pdtscript.h
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 // Convenience file for including all PDT operations at once, and/or
19 // registering them for new arc types.
20 
21 #ifndef FST_EXTENSIONS_PDT_PDTSCRIPT_H_
22 #define FST_EXTENSIONS_PDT_PDTSCRIPT_H_
23 
24 #include <algorithm>
25 #include <cstdint>
26 #include <string>
27 #include <utility>
28 #include <vector>
29 
30 #include <fst/log.h>
37 #include <fst/compose.h> // for ComposeOptions
38 #include <fst/util.h>
39 #include <fst/script/arg-packs.h>
40 #include <fst/script/fstscript.h>
42 
43 namespace fst {
44 namespace script {
45 
46 using PdtComposeArgs =
47  std::tuple<const FstClass &, const FstClass &,
48  const std::vector<std::pair<int64_t, int64_t>> &,
50 
51 template <class Arc>
52 void Compose(PdtComposeArgs *args) {
53  const Fst<Arc> &ifst1 = *(std::get<0>(*args).GetFst<Arc>());
54  const Fst<Arc> &ifst2 = *(std::get<1>(*args).GetFst<Arc>());
55  MutableFst<Arc> *ofst = std::get<3>(*args)->GetMutableFst<Arc>();
56  // In case Arc::Label is not the same as FstClass::Label, we make a
57  // copy. Truncation may occur if FstClass::Label has more precision than
58  // Arc::Label.
59  std::vector<std::pair<typename Arc::Label, typename Arc::Label>> typed_parens(
60  std::get<2>(*args).size());
61  std::copy(std::get<2>(*args).begin(), std::get<2>(*args).end(),
62  typed_parens.begin());
63  if (std::get<5>(*args)) {
64  Compose(ifst1, typed_parens, ifst2, ofst, std::get<4>(*args));
65  } else {
66  Compose(ifst1, ifst2, typed_parens, ofst, std::get<4>(*args));
67  }
68 }
69 
70 void Compose(const FstClass &ifst1, const FstClass &ifst2,
71  const std::vector<std::pair<int64_t, int64_t>> &parens,
72  MutableFstClass *ofst, const PdtComposeOptions &opts,
73  bool left_pdt);
74 
76  bool connect;
79 
80  PdtExpandOptions(bool c, bool k, const WeightClass &w)
81  : connect(c), keep_parentheses(k), weight_threshold(w) {}
82 };
83 
84 using PdtExpandArgs =
85  std::tuple<const FstClass &,
86  const std::vector<std::pair<int64_t, int64_t>> &,
88 
89 template <class Arc>
90 void Expand(PdtExpandArgs *args) {
91  const Fst<Arc> &fst = *(std::get<0>(*args).GetFst<Arc>());
92  MutableFst<Arc> *ofst = std::get<2>(*args)->GetMutableFst<Arc>();
93  // In case Arc::Label is not the same as FstClass::Label, we make a
94  // copy. Truncation may occur if FstClass::Label has more precision than
95  // Arc::Label.
96  std::vector<std::pair<typename Arc::Label, typename Arc::Label>> typed_parens(
97  std::get<1>(*args).size());
98  std::copy(std::get<1>(*args).begin(), std::get<1>(*args).end(),
99  typed_parens.begin());
100  Expand(fst, typed_parens, ofst,
102  std::get<3>(*args).connect, std::get<3>(*args).keep_parentheses,
103  *(std::get<3>(*args)
104  .weight_threshold.GetWeight<typename Arc::Weight>())));
105 }
106 
107 void Expand(const FstClass &ifst,
108  const std::vector<std::pair<int64_t, int64_t>> &parens,
109  MutableFstClass *ofst, const PdtExpandOptions &opts);
110 
111 void Expand(const FstClass &ifst,
112  const std::vector<std::pair<int64_t, int64_t>> &parens,
113  MutableFstClass *ofst, bool connect, bool keep_parentheses,
115 
116 using PdtReplaceArgs =
117  std::tuple<const std::vector<std::pair<int64_t, const FstClass *>> &,
118  MutableFstClass *, std::vector<std::pair<int64_t, int64_t>> *,
119  int64_t, PdtParserType, int64_t, const std::string &,
120  const std::string &>;
121 
122 template <class Arc>
123 void Replace(PdtReplaceArgs *args) {
124  const auto &untyped_pairs = std::get<0>(*args);
125  auto size = untyped_pairs.size();
126  std::vector<std::pair<typename Arc::Label, const Fst<Arc> *>> typed_pairs(
127  size);
128  for (size_t i = 0; i < size; ++i) {
129  typed_pairs[i].first = untyped_pairs[i].first;
130  typed_pairs[i].second = untyped_pairs[i].second->GetFst<Arc>();
131  }
132  MutableFst<Arc> *ofst = std::get<1>(*args)->GetMutableFst<Arc>();
133  std::vector<std::pair<typename Arc::Label, typename Arc::Label>> typed_parens;
134  const PdtReplaceOptions<Arc> opts(std::get<3>(*args), std::get<4>(*args),
135  std::get<5>(*args), std::get<6>(*args),
136  std::get<7>(*args));
137  Replace(typed_pairs, ofst, &typed_parens, opts);
138  // Copies typed parens into arg3.
139  std::get<2>(*args)->resize(typed_parens.size());
140  std::copy(typed_parens.begin(), typed_parens.end(),
141  std::get<2>(*args)->begin());
142 }
143 
144 void Replace(const std::vector<std::pair<int64_t, const FstClass *>> &pairs,
145  MutableFstClass *ofst,
146  std::vector<std::pair<int64_t, int64_t>> *parens, int64_t root,
147  PdtParserType parser_type = PdtParserType::LEFT,
148  int64_t start_paren_labels = kNoLabel,
149  const std::string &left_paren_prefix = "(_",
150  const std::string &right_paren_prefix = "_)");
151 
152 using PdtReverseArgs =
153  std::tuple<const FstClass &,
154  const std::vector<std::pair<int64_t, int64_t>> &,
155  MutableFstClass *>;
156 
157 template <class Arc>
158 void Reverse(PdtReverseArgs *args) {
159  const Fst<Arc> &fst = *(std::get<0>(*args).GetFst<Arc>());
160  MutableFst<Arc> *ofst = std::get<2>(*args)->GetMutableFst<Arc>();
161  // In case Arc::Label is not the same as FstClass::Label, we make a
162  // copy. Truncation may occur if FstClass::Label has more precision than
163  // Arc::Label.
164  std::vector<std::pair<typename Arc::Label, typename Arc::Label>> typed_parens(
165  std::get<1>(*args).size());
166  std::copy(std::get<1>(*args).begin(), std::get<1>(*args).end(),
167  typed_parens.begin());
168  Reverse(fst, typed_parens, ofst);
169 }
170 
171 void Reverse(const FstClass &ifst,
172  const std::vector<std::pair<int64_t, int64_t>> &,
173  MutableFstClass *ofst);
174 
175 // PDT SHORTESTPATH
176 
180  bool path_gc;
181 
182  explicit PdtShortestPathOptions(QueueType qt = FIFO_QUEUE, bool kp = false,
183  bool gc = true)
184  : queue_type(qt), keep_parentheses(kp), path_gc(gc) {}
185 };
186 
187 using PdtShortestPathArgs =
188  std::tuple<const FstClass &,
189  const std::vector<std::pair<int64_t, int64_t>> &,
190  MutableFstClass *, const PdtShortestPathOptions &>;
191 
192 template <class Arc>
194  const Fst<Arc> &fst = *(std::get<0>(*args).GetFst<Arc>());
195  MutableFst<Arc> *ofst = std::get<2>(*args)->GetMutableFst<Arc>();
196  const PdtShortestPathOptions &opts = std::get<3>(*args);
197  // In case Arc::Label is not the same as FstClass::Label, we make a
198  // copy. Truncation may occur if FstClass::Label has more precision than
199  // Arc::Label.
200  std::vector<std::pair<typename Arc::Label, typename Arc::Label>> typed_parens(
201  std::get<1>(*args).size());
202  std::copy(std::get<1>(*args).begin(), std::get<1>(*args).end(),
203  typed_parens.begin());
204  switch (opts.queue_type) {
205  default:
206  FSTERROR() << "Unknown queue type: " << opts.queue_type;
207  case FIFO_QUEUE: {
208  using Queue = FifoQueue<typename Arc::StateId>;
209  fst::PdtShortestPathOptions<Arc, Queue> spopts(opts.keep_parentheses,
210  opts.path_gc);
211  ShortestPath(fst, typed_parens, ofst, spopts);
212  return;
213  }
214  case LIFO_QUEUE: {
215  using Queue = LifoQueue<typename Arc::StateId>;
216  fst::PdtShortestPathOptions<Arc, Queue> spopts(opts.keep_parentheses,
217  opts.path_gc);
218  ShortestPath(fst, typed_parens, ofst, spopts);
219  return;
220  }
221  case STATE_ORDER_QUEUE: {
223  fst::PdtShortestPathOptions<Arc, Queue> spopts(opts.keep_parentheses,
224  opts.path_gc);
225  ShortestPath(fst, typed_parens, ofst, spopts);
226  return;
227  }
228  }
229 }
230 
231 void ShortestPath(
232  const FstClass &ifst,
233  const std::vector<std::pair<int64_t, int64_t>> &parens,
234  MutableFstClass *ofst,
235  const PdtShortestPathOptions &opts = PdtShortestPathOptions());
236 
237 // PRINT INFO
238 
239 using PdtInfoArgs = std::pair<const FstClass &,
240  const std::vector<std::pair<int64_t, int64_t>> &>;
241 
242 template <class Arc>
243 void Info(PdtInfoArgs *args) {
244  const Fst<Arc> &fst = *(std::get<0>(*args).GetFst<Arc>());
245  // In case Arc::Label is not the same as FstClass::Label, we make a
246  // copy. Truncation may occur if FstClass::Label has more precision than
247  // Arc::Label.
248  std::vector<std::pair<typename Arc::Label, typename Arc::Label>> typed_parens(
249  std::get<1>(*args).size());
250  std::copy(std::get<1>(*args).begin(), std::get<1>(*args).end(),
251  typed_parens.begin());
252  PdtInfo<Arc> pdtinfo(fst, typed_parens);
253  pdtinfo.Print();
254 }
255 
256 void Info(const FstClass &ifst,
257  const std::vector<std::pair<int64_t, int64_t>> &parens);
258 
259 } // namespace script
260 } // namespace fst
261 
262 #define REGISTER_FST_PDT_OPERATIONS(ArcType) \
263  REGISTER_FST_OPERATION(PdtCompose, ArcType, PdtComposeArgs); \
264  REGISTER_FST_OPERATION(PdtExpand, ArcType, PdtExpandArgs); \
265  REGISTER_FST_OPERATION(PdtReplace, ArcType, PdtReplaceArgs); \
266  REGISTER_FST_OPERATION(PdtReverse, ArcType, PdtReverseArgs); \
267  REGISTER_FST_OPERATION(PdtShortestPath, ArcType, PdtShortestPathArgs); \
268  REGISTER_FST_OPERATION(PrintPdtInfo, ArcType, PrintPdtInfoArgs)
269 #endif // FST_EXTENSIONS_PDT_PDTSCRIPT_H_
PdtShortestPathOptions(QueueType qt=FIFO_QUEUE, bool kp=false, bool gc=true)
Definition: pdtscript.h:182
constexpr int kNoLabel
Definition: fst.h:201
MutableFst< Arc > * GetMutableFst()
Definition: fst-class.h:527
PdtExpandOptions(bool c, bool k, const WeightClass &w)
Definition: pdtscript.h:80
QueueType
Definition: queue.h:70
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:70
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
PdtParserType
Definition: replace.h:60
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:120
std::tuple< const FstClass &, const std::vector< std::pair< int64_t, int64_t >> &, MutableFstClass * > PdtReverseArgs
Definition: pdtscript.h:155
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
#define FSTERROR()
Definition: util.h:53
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:53
std::pair< const FstClass &, const std::vector< std::pair< int64_t, int64_t >> & > PdtInfoArgs
Definition: pdtscript.h:240
const WeightClass & weight_threshold
Definition: pdtscript.h:78
std::tuple< const FstClass &, const std::vector< std::pair< int64_t, int64_t >> &, MutableFstClass *, const PdtShortestPathOptions & > PdtShortestPathArgs
Definition: pdtscript.h:190
std::tuple< const FstClass &, const FstClass &, const std::vector< std::pair< int64_t, int64_t >> &, MutableFstClass *, const PdtComposeOptions &, bool > PdtComposeArgs
Definition: pdtscript.h:49
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:39
std::tuple< const FstClass &, const std::vector< std::pair< int64_t, int64_t >> &, MutableFstClass *, const PdtExpandOptions & > PdtExpandArgs
Definition: pdtscript.h:87