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