FST  openfst-1.8.2.post1
OpenFst Library
info.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 // Prints information about an MPDT.
19 
20 #ifndef FST_EXTENSIONS_MPDT_INFO_H_
21 #define FST_EXTENSIONS_MPDT_INFO_H_
22 
23 #include <cstdint>
24 #include <string>
25 #include <vector>
26 
28 #include <fst/fst.h>
29 #include <unordered_map>
30 #include <unordered_set>
31 
32 namespace fst {
33 
34 // Compute various information about MPDTs, helper class for mpdtinfo.cc.
35 template <class Arc, typename Arc::Label nlevels = 2>
36 class MPdtInfo {
37  public:
38  using Label = typename Arc::Label;
39  using StateId = typename Arc::StateId;
40  using Weight = typename Arc::Weight;
41 
42  MPdtInfo(const Fst<Arc> &fst,
43  const std::vector<std::pair<Label, Label>> &parens,
44  const std::vector<Label> &assignments);
45 
46  const std::string &FstType() const { return fst_type_; }
47 
48  const std::string &ArcType() const { return Arc::Type(); }
49 
50  int64_t NumStates() const { return nstates_; }
51 
52  int64_t NumArcs() const { return narcs_; }
53 
54  int64_t NumLevels() const { return nlevels; }
55 
56  int64_t NumOpenParens(Label level) const { return nopen_parens_[level]; }
57 
58  int64_t NumCloseParens(Label level) const { return nclose_parens_[level]; }
59 
60  int64_t NumUniqueOpenParens(Label level) const {
61  return nuniq_open_parens_[level];
62  }
63 
64  int64_t NumUniqueCloseParens(Label level) const {
65  return nuniq_close_parens_[level];
66  }
67  int64_t NumOpenParenStates(Label level) const {
68  return nopen_paren_states_[level];
69  }
70 
71  int64_t NumCloseParenStates(Label level) const {
72  return nclose_paren_states_[level];
73  }
74 
75  void Print();
76 
77  private:
78  std::string fst_type_;
79  int64_t nstates_;
80  int64_t narcs_;
81  int64_t nopen_parens_[nlevels];
82  int64_t nclose_parens_[nlevels];
83  int64_t nuniq_open_parens_[nlevels];
84  int64_t nuniq_close_parens_[nlevels];
85  int64_t nopen_paren_states_[nlevels];
86  int64_t nclose_paren_states_[nlevels];
87  bool error_;
88 };
89 
90 template <class Arc, typename Arc::Label nlevels>
92  const Fst<Arc> &fst,
93  const std::vector<std::pair<typename Arc::Label, typename Arc::Label>>
94  &parens,
95  const std::vector<typename Arc::Label> &assignments)
96  : fst_type_(fst.Type()), nstates_(0), narcs_(0), error_(false) {
97  std::unordered_map<Label, size_t> paren_map;
98  std::unordered_set<Label> paren_set;
99  std::unordered_map<Label, int> paren_levels;
100  std::unordered_set<StateId> open_paren_state_set;
101  std::unordered_set<StateId> close_paren_state_set;
102  if (parens.size() != assignments.size()) {
103  FSTERROR() << "MPdtInfo: Parens of different size from assignments";
104  error_ = true;
105  return;
106  }
107  for (Label i = 0; i < assignments.size(); ++i) {
108  // Assignments here start at 0, so assuming the human-readable version has
109  // them starting at 1, we should subtract 1 here.
110  Label level = assignments[i] - 1;
111  if (level < 0 || level >= nlevels) {
112  FSTERROR() << "MPdtInfo: Specified level " << level << " out of bounds";
113  error_ = true;
114  return;
115  }
116  const auto &pair = parens[i];
117  paren_levels[pair.first] = level;
118  paren_levels[pair.second] = level;
119  paren_map[pair.first] = i;
120  paren_map[pair.second] = i;
121  }
122  for (Label i = 0; i < nlevels; ++i) {
123  nopen_parens_[i] = 0;
124  nclose_parens_[i] = 0;
125  nuniq_open_parens_[i] = 0;
126  nuniq_close_parens_[i] = 0;
127  nopen_paren_states_[i] = 0;
128  nclose_paren_states_[i] = 0;
129  }
130  for (StateIterator<Fst<Arc>> siter(fst); !siter.Done(); siter.Next()) {
131  ++nstates_;
132  const auto s = siter.Value();
133  for (ArcIterator<Fst<Arc>> aiter(fst, s); !aiter.Done(); aiter.Next()) {
134  const auto &arc = aiter.Value();
135  ++narcs_;
136  const auto it = paren_map.find(arc.ilabel);
137  if (it != paren_map.end()) {
138  const auto open_paren = parens[it->second].first;
139  const auto close_paren = parens[it->second].second;
140  const auto level = paren_levels[arc.ilabel];
141  if (arc.ilabel == open_paren) {
142  ++nopen_parens_[level];
143  if (paren_set.insert(open_paren).second) {
144  ++nuniq_open_parens_[level];
145  }
146  if (open_paren_state_set.insert(arc.nextstate).second) {
147  ++nopen_paren_states_[level];
148  }
149  } else {
150  ++nclose_parens_[level];
151  if (paren_set.insert(close_paren).second) {
152  ++nuniq_close_parens_[level];
153  }
154  if (close_paren_state_set.insert(s).second) {
155  ++nclose_paren_states_[level];
156  }
157  }
158  }
159  }
160  }
161 }
162 
163 template <class Arc, typename Arc::Label nlevels>
165  const auto old = std::cout.setf(std::ios::left);
166  std::cout.width(50);
167  std::cout << "fst type" << FstType() << std::endl;
168  std::cout.width(50);
169  std::cout << "arc type" << ArcType() << std::endl;
170  std::cout.width(50);
171  std::cout << "# of states" << NumStates() << std::endl;
172  std::cout.width(50);
173  std::cout << "# of arcs" << NumArcs() << std::endl;
174  std::cout.width(50);
175  std::cout << "# of levels" << NumLevels() << std::endl;
176  std::cout.width(50);
177  for (typename Arc::Label i = 0; i < nlevels; ++i) {
178  int level = i + 1;
179  std::cout << "# of open parentheses at level " << level << "\t"
180  << NumOpenParens(i) << std::endl;
181  std::cout.width(50);
182  std::cout << "# of close parentheses at level " << level << "\t"
183  << NumCloseParens(i) << std::endl;
184  std::cout.width(50);
185  std::cout << "# of unique open parentheses at level " << level << "\t"
186  << NumUniqueOpenParens(i) << std::endl;
187  std::cout.width(50);
188  std::cout << "# of unique close parentheses at level " << level << "\t"
189  << NumUniqueCloseParens(i) << std::endl;
190  std::cout.width(50);
191  std::cout << "# of open parenthesis dest. states at level " << level << "\t"
192  << NumOpenParenStates(i) << std::endl;
193  std::cout.width(50);
194  std::cout << "# of close parenthesis source states at level " << level
195  << "\t" << NumCloseParenStates(i) << std::endl;
196  std::cout.width(50);
197  }
198  std::cout.setf(old);
199 }
200 
201 } // namespace fst
202 
203 #endif // FST_EXTENSIONS_MPDT_INFO_H_
MPdtInfo(const Fst< Arc > &fst, const std::vector< std::pair< Label, Label >> &parens, const std::vector< Label > &assignments)
Definition: info.h:91
int64_t NumArcs() const
Definition: info.h:52
int64_t NumOpenParens(Label level) const
Definition: info.h:56
typename Arc::Weight Weight
Definition: info.h:40
int64_t NumStates() const
Definition: info.h:50
typename Arc::Label Label
Definition: info.h:38
int64_t NumLevels() const
Definition: info.h:54
int64_t NumCloseParens(Label level) const
Definition: info.h:58
#define FSTERROR()
Definition: util.h:53
int64_t NumUniqueOpenParens(Label level) const
Definition: info.h:60
const std::string & FstType() const
Definition: info.h:46
typename Arc::StateId StateId
Definition: info.h:39
const std::string & ArcType() const
Definition: info.h:48
int64_t NumCloseParenStates(Label level) const
Definition: info.h:71
int64_t NumOpenParenStates(Label level) const
Definition: info.h:67
int64_t NumUniqueCloseParens(Label level) const
Definition: info.h:64
void Print()
Definition: info.h:164