FST  openfst-1.8.4
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>> & parens)
87  : fst_type_(fst.Type()),
88  nstates_(0),
89  narcs_(0),
90  nopen_parens_(0),
91  nclose_parens_(0),
92  nuniq_open_parens_(0),
93  nuniq_close_parens_(0),
94  nopen_paren_states_(0),
95  nclose_paren_states_(0) {
96  std::unordered_map<Label, size_t> paren_map;
97  std::unordered_set<Label> paren_set;
98  std::unordered_set<StateId> open_paren_state_set;
99  std::unordered_set<StateId> close_paren_state_set;
100  for (size_t i = 0; i < parens.size(); ++i) {
101  const auto &pair = parens[i];
102  paren_map[pair.first] = i;
103  paren_map[pair.second] = i;
104  }
105  for (StateIterator<Fst<Arc>> siter(fst); !siter.Done(); siter.Next()) {
106  ++nstates_;
107  const auto s = siter.Value();
108  for (ArcIterator<Fst<Arc>> aiter(fst, s); !aiter.Done(); aiter.Next()) {
109  const auto &arc = aiter.Value();
110  ++narcs_;
111  const auto it = paren_map.find(arc.ilabel);
112  if (it != paren_map.end()) {
113  const auto open_paren = parens[it->second].first;
114  const auto close_paren = parens[it->second].second;
115  if (arc.ilabel == open_paren) {
116  ++nopen_parens_;
117  if (paren_set.insert(open_paren).second) {
118  ++nuniq_open_parens_;
119  }
120  if (open_paren_state_set.insert(arc.nextstate).second) {
121  ++nopen_paren_states_;
122  }
123  } else {
124  ++nclose_parens_;
125  if (paren_set.insert(close_paren).second) {
126  ++nuniq_close_parens_;
127  }
128  if (close_paren_state_set.insert(s).second) {
129  ++nclose_paren_states_;
130  }
131  }
132  }
133  }
134  }
135 }
136 
137 template <class Arc>
139  const auto old = std::cout.setf(std::ios::left);
140  std::cout.width(50);
141  std::cout << "fst type" << FstType() << std::endl;
142  std::cout.width(50);
143  std::cout << "arc type" << ArcType() << std::endl;
144  std::cout.width(50);
145  std::cout << "# of states" << NumStates() << std::endl;
146  std::cout.width(50);
147  std::cout << "# of arcs" << NumArcs() << std::endl;
148  std::cout.width(50);
149  std::cout << "# of open parentheses" << NumOpenParens() << std::endl;
150  std::cout.width(50);
151  std::cout << "# of close parentheses" << NumCloseParens() << std::endl;
152  std::cout.width(50);
153  std::cout << "# of unique open parentheses" << NumUniqueOpenParens()
154  << std::endl;
155  std::cout.width(50);
156  std::cout << "# of unique close parentheses" << NumUniqueCloseParens()
157  << std::endl;
158  std::cout.width(50);
159  std::cout << "# of open parenthesis dest. states" << NumOpenParenStates()
160  << std::endl;
161  std::cout.width(50);
162  std::cout << "# of close parenthesis source states" << NumCloseParenStates()
163  << std::endl;
164  std::cout.setf(old);
165 }
166 
167 } // namespace fst
168 
169 #endif // FST_EXTENSIONS_PDT_INFO_H_
int64_t NumOpenParenStates() const
Definition: info.h:65
void Print()
Definition: info.h:138
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