FST  openfst-1.8.3
OpenFst Library
reverse.h
Go to the documentation of this file.
1 // Copyright 2005-2024 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 <utility>
24 #include <vector>
25 
26 #include <fst/fst.h>
27 #include <fst/mutable-fst.h>
28 #include <fst/relabel.h>
29 #include <fst/reverse.h>
30 
31 namespace fst {
32 
33 // Reverses a pushdown transducer (PDT) encoded as an FST.
34 template <class Arc, class RevArc>
35 void Reverse(
36  const Fst<Arc> &ifst,
37  const std::vector<std::pair<typename Arc::Label, typename Arc::Label>>
38  &parens,
39  MutableFst<RevArc> *ofst) {
40  using Label = typename Arc::Label;
41  // Reverses FST component.
42  Reverse(ifst, ofst);
43  // Exchanges open and close parenthesis pairs.
44  std::vector<std::pair<Label, Label>> relabel_pairs;
45  relabel_pairs.reserve(2 * parens.size());
46  for (const auto &pair : parens) {
47  relabel_pairs.emplace_back(pair.first, pair.second);
48  relabel_pairs.emplace_back(pair.second, pair.first);
49  }
50  Relabel(ofst, relabel_pairs, relabel_pairs);
51 }
52 
53 } // namespace fst
54 
55 #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:49
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:36