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