21 #ifndef FST_EXTENSIONS_PDT_PDTSCRIPT_H_ 22 #define FST_EXTENSIONS_PDT_PDTSCRIPT_H_ 55 std::tuple<
const FstClass &,
const FstClass &,
56 const std::vector<std::pair<int64_t, int64_t>> &,
61 const Fst<Arc> &ifst1 = *(std::get<0>(*args).GetFst<Arc>());
62 const Fst<Arc> &ifst2 = *(std::get<1>(*args).GetFst<Arc>());
67 std::vector<std::pair<typename Arc::Label, typename Arc::Label>> typed_parens(
68 std::get<2>(*args).size());
69 std::copy(std::get<2>(*args).begin(), std::get<2>(*args).end(),
70 typed_parens.begin());
71 if (std::get<5>(*args)) {
72 Compose(ifst1, typed_parens, ifst2, ofst, std::get<4>(*args));
74 Compose(ifst1, ifst2, typed_parens, ofst, std::get<4>(*args));
78 void Compose(
const FstClass &ifst1,
const FstClass &ifst2,
79 const std::vector<std::pair<int64_t, int64_t>> &parens,
89 : connect(c), keep_parentheses(k), weight_threshold(w) {}
93 std::tuple<
const FstClass &,
94 const std::vector<std::pair<int64_t, int64_t>> &,
99 const Fst<Arc> &
fst = *(std::get<0>(*args).GetFst<Arc>());
104 std::vector<std::pair<typename Arc::Label, typename Arc::Label>> typed_parens(
105 std::get<1>(*args).size());
106 std::copy(std::get<1>(*args).begin(), std::get<1>(*args).end(),
107 typed_parens.begin());
108 Expand(fst, typed_parens, ofst,
110 std::get<3>(*args).connect, std::get<3>(*args).keep_parentheses,
112 .weight_threshold.GetWeight<
typename Arc::Weight>())));
115 void Expand(
const FstClass &ifst,
116 const std::vector<std::pair<int64_t, int64_t>> &parens,
119 void Expand(
const FstClass &ifst,
120 const std::vector<std::pair<int64_t, int64_t>> &parens,
125 std::tuple<const std::vector<std::pair<int64_t, const FstClass *>> &,
128 const std::string &>;
132 const auto &untyped_pairs = std::get<0>(*args);
133 auto size = untyped_pairs.size();
134 std::vector<std::pair<typename Arc::Label, const Fst<Arc> *>> typed_pairs(
136 for (
size_t i = 0; i < size; ++i) {
137 typed_pairs[i].first = untyped_pairs[i].first;
138 typed_pairs[i].second = untyped_pairs[i].second->GetFst<Arc>();
141 std::vector<std::pair<typename Arc::Label, typename Arc::Label>> typed_parens;
143 std::get<5>(*args), std::get<6>(*args),
145 Replace(typed_pairs, ofst, &typed_parens, opts);
147 std::get<2>(*args)->resize(typed_parens.size());
148 std::copy(typed_parens.begin(), typed_parens.end(),
149 std::get<2>(*args)->begin());
152 void Replace(
const std::vector<std::pair<int64_t, const FstClass *>> &pairs,
153 MutableFstClass *ofst,
154 std::vector<std::pair<int64_t, int64_t>> *parens, int64_t root,
156 int64_t start_paren_labels =
kNoLabel,
157 const std::string &left_paren_prefix =
"(_",
158 const std::string &right_paren_prefix =
"_)");
161 std::tuple<
const FstClass &,
162 const std::vector<std::pair<int64_t, int64_t>> &,
167 const Fst<Arc> &
fst = *(std::get<0>(*args).GetFst<Arc>());
172 std::vector<std::pair<typename Arc::Label, typename Arc::Label>> typed_parens(
173 std::get<1>(*args).size());
174 std::copy(std::get<1>(*args).begin(), std::get<1>(*args).end(),
175 typed_parens.begin());
176 Reverse(fst, typed_parens, ofst);
179 void Reverse(
const FstClass &ifst,
180 const std::vector<std::pair<int64_t, int64_t>> &,
181 MutableFstClass *ofst);
192 : queue_type(qt), keep_parentheses(kp), path_gc(gc) {}
196 std::tuple<
const FstClass &,
197 const std::vector<std::pair<int64_t, int64_t>> &,
202 const Fst<Arc> &
fst = *(std::get<0>(*args).GetFst<Arc>());
204 const PdtShortestPathOptions &opts = std::get<3>(*args);
208 std::vector<std::pair<typename Arc::Label, typename Arc::Label>> typed_parens(
209 std::get<1>(*args).size());
210 std::copy(std::get<1>(*args).begin(), std::get<1>(*args).end(),
211 typed_parens.begin());
212 switch (opts.queue_type) {
214 FSTERROR() <<
"Unknown queue type: " << opts.queue_type;
241 const FstClass &ifst,
242 const std::vector<std::pair<int64_t, int64_t>> &parens,
243 MutableFstClass *ofst,
244 const PdtShortestPathOptions &opts = PdtShortestPathOptions());
249 const std::vector<std::pair<int64_t, int64_t>> &>;
253 const Fst<Arc> &
fst = *(std::get<0>(*args).GetFst<Arc>());
257 std::vector<std::pair<typename Arc::Label, typename Arc::Label>> typed_parens(
258 std::get<1>(*args).size());
259 std::copy(std::get<1>(*args).begin(), std::get<1>(*args).end(),
260 typed_parens.begin());
265 void Info(
const FstClass &ifst,
266 const std::vector<std::pair<int64_t, int64_t>> &parens);
271 #define REGISTER_FST_PDT_OPERATIONS(ArcType) \ 272 REGISTER_FST_OPERATION(PdtCompose, ArcType, PdtComposeArgs); \ 273 REGISTER_FST_OPERATION(PdtExpand, ArcType, PdtExpandArgs); \ 274 REGISTER_FST_OPERATION(PdtReplace, ArcType, PdtReplaceArgs); \ 275 REGISTER_FST_OPERATION(PdtReverse, ArcType, PdtReverseArgs); \ 276 REGISTER_FST_OPERATION(PdtShortestPath, ArcType, PdtShortestPathArgs); \ 277 REGISTER_FST_OPERATION(PrintPdtInfo, ArcType, PrintPdtInfoArgs) 278 #endif // FST_EXTENSIONS_PDT_PDTSCRIPT_H_
PdtShortestPathOptions(QueueType qt=FIFO_QUEUE, bool kp=false, bool gc=true)
MutableFst< Arc > * GetMutableFst()
PdtExpandOptions(bool c, bool k, const WeightClass &w)
void Reverse(const FstClass &ifst, const std::vector< std::pair< int64_t, int64_t >> &parens, std::vector< int64_t > *assignments, MutableFstClass *ofst)
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)
void ShortestPath(const FstClass &ifst, const std::vector< std::pair< int64_t, int64_t >> &parens, MutableFstClass *ofst, const PdtShortestPathOptions &opts)
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
std::tuple< const FstClass &, const std::vector< std::pair< int64_t, int64_t >> &, MutableFstClass * > PdtReverseArgs
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)
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)
std::pair< const FstClass &, const std::vector< std::pair< int64_t, int64_t >> & > PdtInfoArgs
const WeightClass & weight_threshold
std::tuple< const FstClass &, const std::vector< std::pair< int64_t, int64_t >> &, MutableFstClass *, const PdtShortestPathOptions & > PdtShortestPathArgs
std::tuple< const FstClass &, const FstClass &, const std::vector< std::pair< int64_t, int64_t >> &, MutableFstClass *, const PdtComposeOptions &, bool > PdtComposeArgs
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)
std::tuple< const FstClass &, const std::vector< std::pair< int64_t, int64_t >> &, MutableFstClass *, const PdtExpandOptions & > PdtExpandArgs