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