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