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