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