FST  openfst-1.8.3
OpenFst Library
mpdtscript.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 // Convenience file for including all MPDT operations at once, and/or
19 // registering them for new arc types.
20 
21 #ifndef FST_EXTENSIONS_MPDT_MPDTSCRIPT_H_
22 #define FST_EXTENSIONS_MPDT_MPDTSCRIPT_H_
23 
24 #include <algorithm>
25 #include <cstdint>
26 #include <tuple>
27 #include <utility>
28 #include <vector>
29 
30 #include <fst/log.h>
35 #include <fst/extensions/pdt/pdtscript.h> // For LabelClassPair,
36 #include <fst/compose.h> // for ComposeOptions
37 #include <fst/fst.h>
38 #include <fst/mutable-fst.h>
39 #include <fst/util.h>
40 #include <fst/script/arg-packs.h>
41 #include <fst/script/fst-class.h>
43 // FstClassPair, and to detect
44 // any collisions.
45 
46 namespace fst {
47 namespace script {
48 
49 using MPdtComposeArgs =
50  std::tuple<const FstClass &, const FstClass &,
51  const std::vector<std::pair<int64_t, int64_t>> &,
52  const std::vector<int64_t> &, MutableFstClass *,
53  const MPdtComposeOptions &, bool>;
54 
55 template <class Arc>
56 void Compose(MPdtComposeArgs *args) {
57  const Fst<Arc> &ifst1 = *(std::get<0>(*args).GetFst<Arc>());
58  const Fst<Arc> &ifst2 = *(std::get<1>(*args).GetFst<Arc>());
59  MutableFst<Arc> *ofst = std::get<4>(*args)->GetMutableFst<Arc>();
60  std::vector<std::pair<typename Arc::Label, typename Arc::Label>> typed_parens(
61  std::get<2>(*args).size());
62  std::copy(std::get<2>(*args).begin(), std::get<2>(*args).end(),
63  typed_parens.begin());
64  using Level = typename Arc::Label;
65  std::vector<Level> typed_assignments(std::get<3>(*args).size());
66  std::copy(std::get<3>(*args).begin(), std::get<3>(*args).end(),
67  typed_assignments.begin());
68  if (std::get<6>(*args)) {
69  Compose(ifst1, typed_parens, typed_assignments, ifst2, ofst,
70  std::get<5>(*args));
71  } else {
72  Compose(ifst1, ifst2, typed_parens, typed_assignments, ofst,
73  std::get<5>(*args));
74  }
75 }
76 
77 void Compose(const FstClass &ifst1, const FstClass &ifst2,
78  const std::vector<std::pair<int64_t, int64_t>> &parens,
79  const std::vector<int64_t> &assignments, MutableFstClass *ofst,
80  const MPdtComposeOptions &copts, bool left_pdt);
81 
82 using MPdtExpandArgs = std::tuple<
83  const FstClass &, const std::vector<std::pair<int64_t, int64_t>> &,
84  const std::vector<int64_t> &, MutableFstClass *, const MPdtExpandOptions &>;
85 
86 template <class Arc>
87 void Expand(MPdtExpandArgs *args) {
88  const Fst<Arc> &fst = *(std::get<0>(*args).GetFst<Arc>());
89  MutableFst<Arc> *ofst = std::get<3>(*args)->GetMutableFst<Arc>();
90  // In case Arc::Label is not the same as FstClass::Label, we make copies.
91  // Truncation may occur if FstClass::Label has more precision than
92  // Arc::Label.
93  std::vector<std::pair<typename Arc::Label, typename Arc::Label>> typed_parens(
94  std::get<1>(*args).size());
95  std::copy(std::get<1>(*args).begin(), std::get<1>(*args).end(),
96  typed_parens.begin());
97  using Level = typename Arc::Label;
98  std::vector<Level> typed_assignments(std::get<2>(*args).size());
99  std::copy(std::get<2>(*args).begin(), std::get<2>(*args).end(),
100  typed_assignments.begin());
101  Expand(fst, typed_parens, typed_assignments, ofst,
102  MPdtExpandOptions(std::get<4>(*args).connect,
103  std::get<4>(*args).keep_parentheses));
104 }
105 
106 void Expand(const FstClass &ifst,
107  const std::vector<std::pair<int64_t, int64_t>> &parens,
108  const std::vector<int64_t> &assignments, MutableFstClass *ofst,
109  const MPdtExpandOptions &opts);
110 
111 using MPdtReverseArgs =
112  std::tuple<const FstClass &,
113  const std::vector<std::pair<int64_t, int64_t>> &,
114  std::vector<int64_t> *, MutableFstClass *>;
115 
116 template <class Arc>
118  const Fst<Arc> &fst = *(std::get<0>(*args).GetFst<Arc>());
119  MutableFst<Arc> *ofst = std::get<3>(*args)->GetMutableFst<Arc>();
120  // In case Arc::Label is not the same as FstClass::Label, we make copies.
121  // Truncation may occur if FstClass::Label has more precision than
122  // Arc::Label.
123  std::vector<std::pair<typename Arc::Label, typename Arc::Label>> typed_parens(
124  std::get<1>(*args).size());
125  std::copy(std::get<1>(*args).begin(), std::get<1>(*args).end(),
126  typed_parens.begin());
127  using Level = typename Arc::Label;
128  std::vector<Level> typed_assignments(std::get<2>(*args)->size());
129  std::copy(std::get<2>(*args)->begin(), std::get<2>(*args)->end(),
130  typed_assignments.begin());
131  Reverse(fst, typed_parens, &typed_assignments, ofst);
132  // Reassign stack assignments to input assignment vector.
133  std::copy(typed_assignments.begin(), typed_assignments.end(),
134  std::get<2>(*args)->begin());
135 }
136 
137 void Reverse(const FstClass &ifst,
138  const std::vector<std::pair<int64_t, int64_t>> &parens,
139  std::vector<int64_t> *assignments, MutableFstClass *ofst);
140 
141 using MPdtInfoArgs =
142  std::tuple<const FstClass &,
143  const std::vector<std::pair<int64_t, int64_t>> &,
144  const std::vector<int64_t> &>;
145 
146 template <class Arc>
147 void Info(MPdtInfoArgs *args) {
148  const Fst<Arc> &fst = *(std::get<0>(*args).GetFst<Arc>());
149  // In case Arc::Label is not the same as FstClass::Label, we make copies.
150  // Truncation may occur if FstClass::Label has more precision than
151  // Arc::Label.
152  std::vector<std::pair<typename Arc::Label, typename Arc::Label>> typed_parens(
153  std::get<1>(*args).size());
154  std::copy(std::get<1>(*args).begin(), std::get<1>(*args).end(),
155  typed_parens.begin());
156  using Level = typename Arc::Label;
157  std::vector<Level> typed_assignments(std::get<2>(*args).size());
158  std::copy(std::get<2>(*args).begin(), std::get<2>(*args).end(),
159  typed_assignments.begin());
160  MPdtInfo<Arc> mpdtinfo(fst, typed_parens, typed_assignments);
161  mpdtinfo.Print();
162 }
163 
164 void Info(const FstClass &ifst,
165  const std::vector<std::pair<int64_t, int64_t>> &parens,
166  const std::vector<int64_t> &assignments);
167 
168 } // namespace script
169 } // namespace fst
170 
171 #endif // FST_EXTENSIONS_MPDT_MPDTSCRIPT_H_
MutableFst< Arc > * GetMutableFst()
Definition: fst-class.h:534
void Reverse(const FstClass &ifst, const std::vector< std::pair< int64_t, int64_t >> &parens, std::vector< int64_t > *assignments, MutableFstClass *ofst)
Definition: mpdtscript.cc:72
std::tuple< const FstClass &, const std::vector< std::pair< int64_t, int64_t >> &, const std::vector< int64_t > & > MPdtInfoArgs
Definition: mpdtscript.h:144
std::tuple< const FstClass &, const FstClass &, const std::vector< std::pair< int64_t, int64_t >> &, const std::vector< int64_t > &, MutableFstClass *, const MPdtComposeOptions &, bool > MPdtComposeArgs
Definition: mpdtscript.h:53
void Info(const std::vector< std::string > &sources, const std::string &arc_type, const std::string &begin_key, const std::string &end_key, bool list_fsts)
Definition: farscript.cc:145
std::tuple< const FstClass &, const std::vector< std::pair< int64_t, int64_t >> &, std::vector< int64_t > *, MutableFstClass * > MPdtReverseArgs
Definition: mpdtscript.h:114
void Expand(const FstClass &ifst, const std::vector< std::pair< int64_t, int64_t >> &parens, const std::vector< int64_t > &assignments, MutableFstClass *ofst, const MPdtExpandOptions &opts)
Definition: mpdtscript.cc:55
std::tuple< const FstClass &, const std::vector< std::pair< int64_t, int64_t >> &, const std::vector< int64_t > &, MutableFstClass *, const MPdtExpandOptions & > MPdtExpandArgs
Definition: mpdtscript.h:84
void Compose(const FstClass &ifst1, const FstClass &ifst2, const std::vector< std::pair< int64_t, int64_t >> &parens, const std::vector< int64_t > &assignments, MutableFstClass *ofst, const MPdtComposeOptions &copts, bool left_pdt)
Definition: mpdtscript.cc:41