FST  openfst-1.7.1
OpenFst Library
mpdtscript.h
Go to the documentation of this file.
1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
3 //
4 // Convenience file for including all MPDT operations at once, and/or
5 // registering them for new arc types.
6 
7 #ifndef FST_EXTENSIONS_MPDT_MPDTSCRIPT_H_
8 #define FST_EXTENSIONS_MPDT_MPDTSCRIPT_H_
9 
10 #include <algorithm>
11 #include <utility>
12 #include <vector>
13 
14 #include <fst/log.h>
15 #include <fst/compose.h> // for ComposeOptions
16 #include <fst/util.h>
17 
18 #include <fst/script/arg-packs.h>
19 #include <fst/script/fst-class.h>
21 
26 
27 #include <fst/extensions/pdt/pdtscript.h> // For LabelClassPair,
28  // FstClassPair, and to detect
29  // any collisions.
30 
31 namespace fst {
32 namespace script {
33 
34 using MPdtComposeArgs =
35  std::tuple<const FstClass &, const FstClass &,
36  const std::vector<LabelPair> &, const std::vector<int64> &,
38 
39 template <class Arc>
41  const Fst<Arc> &ifst1 = *(std::get<0>(*args).GetFst<Arc>());
42  const Fst<Arc> &ifst2 = *(std::get<1>(*args).GetFst<Arc>());
43  MutableFst<Arc> *ofst = std::get<4>(*args)->GetMutableFst<Arc>();
44  std::vector<std::pair<typename Arc::Label, typename Arc::Label>> typed_parens(
45  std::get<2>(*args).size());
46  std::copy(std::get<2>(*args).begin(), std::get<2>(*args).end(),
47  typed_parens.begin());
48  using Level = typename Arc::Label;
49  std::vector<Level> typed_assignments(std::get<3>(*args).size());
50  std::copy(std::get<3>(*args).begin(), std::get<3>(*args).end(),
51  typed_assignments.begin());
52  if (std::get<6>(*args)) {
53  Compose(ifst1, typed_parens, typed_assignments, ifst2, ofst,
54  std::get<5>(*args));
55  } else {
56  Compose(ifst1, ifst2, typed_parens, typed_assignments, ofst,
57  std::get<5>(*args));
58  }
59 }
60 
61 void MPdtCompose(const FstClass &ifst1, const FstClass &ifst2,
62  const std::vector<LabelPair> &parens,
63  const std::vector<int64> &assignments, MutableFstClass *ofst,
64  const MPdtComposeOptions &copts, bool left_pdt);
65 
66 using MPdtExpandArgs =
67  std::tuple<const FstClass &, const std::vector<LabelPair> &,
68  const std::vector<int64> &, MutableFstClass *,
70 
71 template <class Arc>
73  const Fst<Arc> &fst = *(std::get<0>(*args).GetFst<Arc>());
74  MutableFst<Arc> *ofst = std::get<3>(*args)->GetMutableFst<Arc>();
75  // In case Arc::Label is not the same as FstClass::Label, we make copies.
76  // Truncation may occur if FstClass::Label has more precision than
77  // Arc::Label.
78  std::vector<std::pair<typename Arc::Label, typename Arc::Label>> typed_parens(
79  std::get<1>(*args).size());
80  std::copy(std::get<1>(*args).begin(), std::get<1>(*args).end(),
81  typed_parens.begin());
82  using Level = typename Arc::Label;
83  std::vector<Level> typed_assignments(std::get<2>(*args).size());
84  std::copy(std::get<2>(*args).begin(), std::get<2>(*args).end(),
85  typed_assignments.begin());
86  Expand(fst, typed_parens, typed_assignments, ofst,
87  MPdtExpandOptions(std::get<4>(*args).connect,
88  std::get<4>(*args).keep_parentheses));
89 }
90 
91 void MPdtExpand(const FstClass &ifst, const std::vector<LabelPair> &parens,
92  const std::vector<int64> &assignments, MutableFstClass *ofst,
93  const MPdtExpandOptions &opts);
94 
95 using MPdtReverseArgs =
96  std::tuple<const FstClass &, const std::vector<LabelPair> &,
97  std::vector<int64> *, MutableFstClass *>;
98 
99 template <class Arc>
101  const Fst<Arc> &fst = *(std::get<0>(*args).GetFst<Arc>());
102  MutableFst<Arc> *ofst = std::get<3>(*args)->GetMutableFst<Arc>();
103  // In case Arc::Label is not the same as FstClass::Label, we make copies.
104  // Truncation may occur if FstClass::Label has more precision than
105  // Arc::Label.
106  std::vector<std::pair<typename Arc::Label, typename Arc::Label>> typed_parens(
107  std::get<1>(*args).size());
108  std::copy(std::get<1>(*args).begin(), std::get<1>(*args).end(),
109  typed_parens.begin());
110  using Level = typename Arc::Label;
111  std::vector<Level> typed_assignments(std::get<2>(*args)->size());
112  std::copy(std::get<2>(*args)->begin(), std::get<2>(*args)->end(),
113  typed_assignments.begin());
114  Reverse(fst, typed_parens, &typed_assignments, ofst);
115  // Reassign stack assignments to input assignment vector.
116  std::copy(typed_assignments.begin(), typed_assignments.end(),
117  std::get<2>(*args)->begin());
118 }
119 
120 void MPdtReverse(const FstClass &ifst, const std::vector<LabelPair> &parens,
121  std::vector<int64> *assignments, MutableFstClass *ofst);
122 
123 using PrintMPdtInfoArgs =
124  std::tuple<const FstClass &, const std::vector<LabelPair> &,
125  const std::vector<int64> &>;
126 
127 template <class Arc>
129  const Fst<Arc> &fst = *(std::get<0>(*args).GetFst<Arc>());
130  // In case Arc::Label is not the same as FstClass::Label, we make copies.
131  // Truncation may occur if FstClass::Label has more precision than
132  // Arc::Label.
133  std::vector<std::pair<typename Arc::Label, typename Arc::Label>> typed_parens(
134  std::get<1>(*args).size());
135  std::copy(std::get<1>(*args).begin(), std::get<1>(*args).end(),
136  typed_parens.begin());
137  using Level = typename Arc::Label;
138  std::vector<Level> typed_assignments(std::get<2>(*args).size());
139  std::copy(std::get<2>(*args).begin(), std::get<2>(*args).end(),
140  typed_assignments.begin());
141  MPdtInfo<Arc> mpdtinfo(fst, typed_parens, typed_assignments);
142  mpdtinfo.Print();
143 }
144 
145 void PrintMPdtInfo(const FstClass &ifst, const std::vector<LabelPair> &parens,
146  const std::vector<int64> &assignments);
147 
148 } // namespace script
149 } // namespace fst
150 
151 #define REGISTER_FST_MPDT_OPERATIONS(ArcType) \
152  REGISTER_FST_OPERATION(MPdtCompose, ArcType, MPdtComposeArgs); \
153  REGISTER_FST_OPERATION(MPdtExpand, ArcType, MPdtExpandArgs); \
154  REGISTER_FST_OPERATION(MPdtReverse, ArcType, MPdtReverseArgs); \
155  REGISTER_FST_OPERATION(PrintMPdtInfo, ArcType, PrintMPdtInfoArgs)
156 #endif // FST_EXTENSIONS_MPDT_MPDTSCRIPT_H_
std::tuple< const FstClass &, const std::vector< LabelPair > &, std::vector< int64 > *, MutableFstClass * > MPdtReverseArgs
Definition: mpdtscript.h:97
MutableFst< Arc > * GetMutableFst()
Definition: fst-class.h:478
void MPdtCompose(const FstClass &ifst1, const FstClass &ifst2, const std::vector< LabelPair > &parens, const std::vector< int64 > &assignments, MutableFstClass *ofst, const MPdtComposeOptions &copts, bool left_pdt)
Definition: mpdtscript.cc:22
std::tuple< const FstClass &, const std::vector< LabelPair > &, const std::vector< int64 > &, MutableFstClass *, const MPdtExpandOptions & > MPdtExpandArgs
Definition: mpdtscript.h:69
void MPdtReverse(const FstClass &ifst, const std::vector< LabelPair > &parens, std::vector< int64 > *assignments, MutableFstClass *ofst)
Definition: mpdtscript.cc:46
void Compose(ComposeArgs *args)
Definition: compose.h:19
void MPdtExpand(const FstClass &ifst, const std::vector< LabelPair > &parens, const std::vector< int64 > &assignments, MutableFstClass *ofst, const MPdtExpandOptions &opts)
Definition: mpdtscript.cc:33
std::tuple< const FstClass &, const FstClass &, const std::vector< LabelPair > &, const std::vector< int64 > &, MutableFstClass *, const MPdtComposeOptions &, bool > MPdtComposeArgs
Definition: mpdtscript.h:37
void Reverse(ReverseArgs *args)
Definition: reverse.h:18
void PrintMPdtInfo(const FstClass &ifst, const std::vector< LabelPair > &parens, const std::vector< int64 > &assignments)
Definition: mpdtscript.cc:52
std::tuple< const FstClass &, const std::vector< LabelPair > &, const std::vector< int64 > & > PrintMPdtInfoArgs
Definition: mpdtscript.h:125
void Expand(const Fst< Arc > &ifst, const std::vector< std::pair< typename Arc::Label, typename Arc::Label >> &parens, const std::vector< typename Arc::Label > &assignments, MutableFst< Arc > *ofst, const MPdtExpandOptions &opts)
Definition: expand.h:302