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