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 a PDT.
19 
20 #ifndef FST_EXTENSIONS_PDT_INFO_H_
21 #define FST_EXTENSIONS_PDT_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/extensions/pdt/pdt.h>
32 #include <fst/fst.h>
33 #include <unordered_map>
34 #include <unordered_set>
35 
36 namespace fst {
37 
38 // Compute various information about PDTs.
39 template <class Arc>
40 class PdtInfo {
41  public:
42  using Label = typename Arc::Label;
43  using StateId = typename Arc::StateId;
44  using Weight = typename Arc::Weight;
45 
46  PdtInfo(const Fst<Arc> &fst,
47  const std::vector<std::pair<Label, Label>> &parents);
48 
49  const std::string &FstType() const { return fst_type_; }
50 
51  const std::string &ArcType() const { return Arc::Type(); }
52 
53  int64_t NumStates() const { return nstates_; }
54 
55  int64_t NumArcs() const { return narcs_; }
56 
57  int64_t NumOpenParens() const { return nopen_parens_; }
58 
59  int64_t NumCloseParens() const { return nclose_parens_; }
60 
61  int64_t NumUniqueOpenParens() const { return nuniq_open_parens_; }
62 
63  int64_t NumUniqueCloseParens() const { return nuniq_close_parens_; }
64 
65  int64_t NumOpenParenStates() const { return nopen_paren_states_; }
66 
67  int64_t NumCloseParenStates() const { return nclose_paren_states_; }
68 
69  void Print();
70 
71  private:
72  std::string fst_type_;
73  int64_t nstates_;
74  int64_t narcs_;
75  int64_t nopen_parens_;
76  int64_t nclose_parens_;
77  int64_t nuniq_open_parens_;
78  int64_t nuniq_close_parens_;
79  int64_t nopen_paren_states_;
80  int64_t nclose_paren_states_;
81 };
82 
83 template <class Arc>
85  const Fst<Arc> &fst,
86  const std::vector<std::pair<typename Arc::Label, typename Arc::Label>>
87  &parens)
88  : fst_type_(fst.Type()),
89  nstates_(0),
90  narcs_(0),
91  nopen_parens_(0),
92  nclose_parens_(0),
93  nuniq_open_parens_(0),
94  nuniq_close_parens_(0),
95  nopen_paren_states_(0),
96  nclose_paren_states_(0) {
97  std::unordered_map<Label, size_t> paren_map;
98  std::unordered_set<Label> paren_set;
99  std::unordered_set<StateId> open_paren_state_set;
100  std::unordered_set<StateId> close_paren_state_set;
101  for (size_t i = 0; i < parens.size(); ++i) {
102  const auto &pair = parens[i];
103  paren_map[pair.first] = i;
104  paren_map[pair.second] = i;
105  }
106  for (StateIterator<Fst<Arc>> siter(fst); !siter.Done(); siter.Next()) {
107  ++nstates_;
108  const auto s = siter.Value();
109  for (ArcIterator<Fst<Arc>> aiter(fst, s); !aiter.Done(); aiter.Next()) {
110  const auto &arc = aiter.Value();
111  ++narcs_;
112  const auto it = paren_map.find(arc.ilabel);
113  if (it != paren_map.end()) {
114  const auto open_paren = parens[it->second].first;
115  const auto close_paren = parens[it->second].second;
116  if (arc.ilabel == open_paren) {
117  ++nopen_parens_;
118  if (paren_set.insert(open_paren).second) {
119  ++nuniq_open_parens_;
120  }
121  if (open_paren_state_set.insert(arc.nextstate).second) {
122  ++nopen_paren_states_;
123  }
124  } else {
125  ++nclose_parens_;
126  if (paren_set.insert(close_paren).second) {
127  ++nuniq_close_parens_;
128  }
129  if (close_paren_state_set.insert(s).second) {
130  ++nclose_paren_states_;
131  }
132  }
133  }
134  }
135  }
136 }
137 
138 template <class Arc>
140  const auto old = std::cout.setf(std::ios::left);
141  std::cout.width(50);
142  std::cout << "fst type" << FstType() << std::endl;
143  std::cout.width(50);
144  std::cout << "arc type" << ArcType() << std::endl;
145  std::cout.width(50);
146  std::cout << "# of states" << NumStates() << std::endl;
147  std::cout.width(50);
148  std::cout << "# of arcs" << NumArcs() << std::endl;
149  std::cout.width(50);
150  std::cout << "# of open parentheses" << NumOpenParens() << std::endl;
151  std::cout.width(50);
152  std::cout << "# of close parentheses" << NumCloseParens() << std::endl;
153  std::cout.width(50);
154  std::cout << "# of unique open parentheses" << NumUniqueOpenParens()
155  << std::endl;
156  std::cout.width(50);
157  std::cout << "# of unique close parentheses" << NumUniqueCloseParens()
158  << std::endl;
159  std::cout.width(50);
160  std::cout << "# of open parenthesis dest. states" << NumOpenParenStates()
161  << std::endl;
162  std::cout.width(50);
163  std::cout << "# of close parenthesis source states" << NumCloseParenStates()
164  << std::endl;
165  std::cout.setf(old);
166 }
167 
168 } // namespace fst
169 
170 #endif // FST_EXTENSIONS_PDT_INFO_H_
int64_t NumOpenParenStates() const
Definition: info.h:65
void Print()
Definition: info.h:139
typename Arc::StateId StateId
Definition: info.h:43
int64_t NumCloseParens() const
Definition: info.h:59
int64_t NumStates() const
Definition: info.h:53
int64_t NumUniqueOpenParens() const
Definition: info.h:61
typename Arc::Weight Weight
Definition: info.h:44
const std::string & ArcType() const
Definition: info.h:51
int64_t NumArcs() const
Definition: info.h:55
typename Arc::Label Label
Definition: info.h:42
int64_t NumCloseParenStates() const
Definition: info.h:67
int64_t NumOpenParens() const
Definition: info.h:57
const std::string & FstType() const
Definition: info.h:49
PdtInfo(const Fst< Arc > &fst, const std::vector< std::pair< Label, Label >> &parents)
Definition: info.h:84
int64_t NumUniqueCloseParens() const
Definition: info.h:63