FST  openfst-1.8.2.post1
OpenFst Library
reverse.h
Go to the documentation of this file.
1 // Copyright 2005-2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the 'License');
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an 'AS IS' BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // See www.openfst.org for extensive documentation on this weighted
16 // finite-state transducer library.
17 //
18 // Expands a PDT to an FST.
19 
20 #ifndef FST_EXTENSIONS_PDT_REVERSE_H_
21 #define FST_EXTENSIONS_PDT_REVERSE_H_
22 
23 #include <vector>
24 
25 #include <fst/mutable-fst.h>
26 #include <fst/relabel.h>
27 #include <fst/reverse.h>
28 
29 namespace fst {
30 
31 // Reverses a pushdown transducer (PDT) encoded as an FST.
32 template <class Arc, class RevArc>
33 void Reverse(
34  const Fst<Arc> &ifst,
35  const std::vector<std::pair<typename Arc::Label, typename Arc::Label>>
36  &parens,
37  MutableFst<RevArc> *ofst) {
38  using Label = typename Arc::Label;
39  // Reverses FST component.
40  Reverse(ifst, ofst);
41  // Exchanges open and close parenthesis pairs.
42  std::vector<std::pair<Label, Label>> relabel_pairs;
43  relabel_pairs.reserve(2 * parens.size());
44  for (const auto &pair : parens) {
45  relabel_pairs.emplace_back(pair.first, pair.second);
46  relabel_pairs.emplace_back(pair.second, pair.first);
47  }
48  Relabel(ofst, relabel_pairs, relabel_pairs);
49 }
50 
51 } // namespace fst
52 
53 #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:42
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:34