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