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