FST  openfst-1.8.3
OpenFst Library
relabel.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 #ifndef FST_SCRIPT_RELABEL_H_
19 #define FST_SCRIPT_RELABEL_H_
20 
21 #include <algorithm>
22 #include <cstdint>
23 #include <string>
24 #include <tuple>
25 #include <utility>
26 #include <vector>
27 
28 #include <fst/mutable-fst.h>
29 #include <fst/relabel.h>
30 #include <fst/symbol-table.h>
31 #include <fst/script/fst-class.h>
32 
33 namespace fst {
34 namespace script {
35 
36 using FstRelabelArgs1 =
37  std::tuple<MutableFstClass *, const SymbolTable *, const SymbolTable *,
38  const std::string &, bool, const SymbolTable *,
39  const SymbolTable *, const std::string &, bool>;
40 
41 template <class Arc>
42 void Relabel(FstRelabelArgs1 *args) {
43  MutableFst<Arc> *ofst = std::get<0>(*args)->GetMutableFst<Arc>();
44  Relabel(ofst, std::get<1>(*args), std::get<2>(*args), std::get<3>(*args),
45  std::get<4>(*args), std::get<5>(*args), std::get<6>(*args),
46  std::get<7>(*args), std::get<8>(*args));
47 }
48 
49 using FstRelabelArgs2 =
50  std::tuple<MutableFstClass *,
51  const std::vector<std::pair<int64_t, int64_t>> &,
52  const std::vector<std::pair<int64_t, int64_t>> &>;
53 
54 template <class Arc>
55 void Relabel(FstRelabelArgs2 *args) {
56  MutableFst<Arc> *ofst = std::get<0>(*args)->GetMutableFst<Arc>();
57  using LabelPair = std::pair<typename Arc::Label, typename Arc::Label>;
58  // In case the MutableFstClass::Label is not the same as Arc::Label,
59  // make a copy.
60  std::vector<LabelPair> typed_ipairs(std::get<1>(*args).size());
61  std::copy(std::get<1>(*args).begin(), std::get<1>(*args).end(),
62  typed_ipairs.begin());
63  std::vector<LabelPair> typed_opairs(std::get<2>(*args).size());
64  std::copy(std::get<2>(*args).begin(), std::get<2>(*args).end(),
65  typed_opairs.begin());
66  Relabel(ofst, typed_ipairs, typed_opairs);
67 }
68 
69 void Relabel(MutableFstClass *ofst, const SymbolTable *old_isymbols,
70  const SymbolTable *new_isymbols,
71  const std::string &unknown_isymbol, bool attach_new_isymbols,
72  const SymbolTable *old_osymbols, const SymbolTable *new_osymbols,
73  const std::string &unknown_osymbol, bool attach_new_osymbols);
74 
75 void Relabel(MutableFstClass *ofst,
76  const std::vector<std::pair<int64_t, int64_t>> &ipairs,
77  const std::vector<std::pair<int64_t, int64_t>> &opairs);
78 
79 } // namespace script
80 } // namespace fst
81 
82 #endif // FST_SCRIPT_RELABEL_H_
void Relabel(FstRelabelArgs1 *args)
Definition: relabel.h:42
std::tuple< MutableFstClass *, const SymbolTable *, const SymbolTable *, const std::string &, bool, const SymbolTable *, const SymbolTable *, const std::string &, bool > FstRelabelArgs1
Definition: relabel.h:39
std::tuple< MutableFstClass *, const std::vector< std::pair< int64_t, int64_t >> &, const std::vector< std::pair< int64_t, int64_t >> & > FstRelabelArgs2
Definition: relabel.h:52