FST  openfst-1.7.2
OpenFst Library
reverse.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 // Expands a PDT to an FST.
5 
6 #ifndef FST_EXTENSIONS_PDT_REVERSE_H_
7 #define FST_EXTENSIONS_PDT_REVERSE_H_
8 
9 #include <vector>
10 
11 #include <fst/mutable-fst.h>
12 #include <fst/relabel.h>
13 #include <fst/reverse.h>
14 
15 namespace fst {
16 
17 // Reverses a pushdown transducer (PDT) encoded as an FST.
18 template <class Arc, class RevArc>
19 void Reverse(const Fst<Arc> &ifst,
20  const std::vector<
21  std::pair<typename Arc::Label, typename Arc::Label>> &parens,
22  MutableFst<RevArc> *ofst) {
23  using Label = typename Arc::Label;
24  // Reverses FST component.
25  Reverse(ifst, ofst);
26  // Exchanges open and close parenthesis pairs.
27  std::vector<std::pair<Label, Label>> relabel_pairs;
28  relabel_pairs.reserve(2 * parens.size());
29  for (const auto &pair : parens) {
30  relabel_pairs.emplace_back(pair.first, pair.second);
31  relabel_pairs.emplace_back(pair.second, pair.first);
32  }
33  Relabel(ofst, relabel_pairs, relabel_pairs);
34 }
35 
36 } // namespace fst
37 
38 #endif // FST_EXTENSIONS_PDT_REVERSE_H_
void Relabel(MutableFst< Arc > *fst, const std::vector< std::pair< typename Arc::Label, typename Arc::Label >> &ipairs, const std::vector< std::pair< typename Arc::Label, typename Arc::Label >> &opairs)
Definition: relabel.h:29
void Reverse(const Fst< Arc > &ifst, const std::vector< std::pair< typename Arc::Label, typename Arc::Label >> &parens, std::vector< typename Arc::Label > *assignments, MutableFst< RevArc > *ofst)
Definition: reverse.h:20