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 #ifndef FST_EXTENSIONS_FAR_INFO_H_
5 #define FST_EXTENSIONS_FAR_INFO_H_
6 
7 #include <iomanip>
8 #include <memory>
9 #include <set>
10 #include <string>
11 #include <utility>
12 #include <vector>
13 
14 #include <fst/extensions/far/far.h>
16 
17 namespace fst {
18 
19 template <class Arc>
20 void AccumulateStatesAndArcs(const Fst<Arc> &fst, size_t *nstate, size_t *narc,
21  size_t *nfinal) {
22  for (StateIterator<Fst<Arc>> siter(fst); !siter.Done();
23  siter.Next(), ++(*nstate)) {
24  ArcIterator<Fst<Arc>> aiter(fst, siter.Value());
25  for (; !aiter.Done(); aiter.Next(), ++(*narc)) {
26  }
27  if (fst.Final(siter.Value()) != Arc::Weight::Zero()) ++(*nfinal);
28  }
29 }
30 
31 struct KeyInfo {
32  string key;
33  string type;
34  size_t nstate = 0;
35  size_t narc = 0;
36  size_t nfinal = 0;
37 };
38 
39 struct FarInfoData {
40  std::vector<KeyInfo> key_infos;
41  string far_type;
42  string arc_type;
43  size_t nfst = 0;
44  size_t nstate = 0;
45  size_t narc = 0;
46  size_t nfinal = 0;
47  std::set<string> fst_types;
48 };
49 
50 template <class Arc>
51 void GetFarInfo(const std::vector<string> &filenames, const string &begin_key,
52  const string &end_key, const bool list_fsts,
53  FarInfoData *far_info) {
54  *far_info = FarInfoData();
55  std::unique_ptr<FarReader<Arc>> reader(FarReader<Arc>::Open(filenames));
56  if (!reader) {
57  LOG(ERROR) << "GetFarInfo: failed to create far reader.";
58  return;
59  }
60  if (!begin_key.empty()) reader->Find(begin_key);
61 
62  for (; !reader->Done(); reader->Next()) {
63  const auto &key = reader->GetKey();
64  if (!end_key.empty() && end_key < key) break;
65  ++far_info->nfst;
66  const auto *fst = reader->GetFst();
67  far_info->fst_types.insert(fst->Type());
68  if (list_fsts) {
69  KeyInfo info;
70  info.key = key;
71  info.type = fst->Type();
72  AccumulateStatesAndArcs(*fst, &info.nstate, &info.narc, &info.nfinal);
73  far_info->nstate += info.nstate;
74  far_info->narc += info.narc;
75  far_info->nfinal += info.nfinal;
76  far_info->key_infos.push_back(info);
77  } else {
78  AccumulateStatesAndArcs(*fst, &far_info->nstate, &far_info->narc,
79  &far_info->nfinal);
80  }
81  }
82  far_info->far_type = GetFarTypeString(reader->Type());
83  far_info->arc_type = Arc::Type();
84 }
85 
86 template <class Arc>
87 void FarInfo(const std::vector<string> &filenames, const string &begin_key,
88  const string &end_key, const bool list_fsts) {
89  FarInfoData info;
90  GetFarInfo<Arc>(filenames, begin_key, end_key, list_fsts, &info);
91  if (!list_fsts) {
92  std::cout << std::left << std::setw(50) << "far type" << info.far_type
93  << std::endl;
94  std::cout << std::left << std::setw(50) << "arc type" << Arc::Type()
95  << std::endl;
96  std::cout << std::left << std::setw(50) << "fst type";
97  for (auto iter = info.fst_types.begin(); iter != info.fst_types.end();
98  ++iter) {
99  if (iter != info.fst_types.begin()) std::cout << ",";
100  std::cout << *iter;
101  }
102  std::cout << std::endl;
103  std::cout << std::left << std::setw(50) << "# of FSTs" << info.nfst
104  << std::endl;
105  std::cout << std::left << std::setw(50) << "total # of states"
106  << info.nstate << std::endl;
107  std::cout << std::left << std::setw(50) << "total # of arcs" << info.narc
108  << std::endl;
109  std::cout << std::left << std::setw(50) << "total # of final states"
110  << info.nfinal << std::endl;
111  } else {
112  // FIXME(kbg): Grok, then document this.
113  int wkey = 10;
114  int wtype = 10;
115  int wnstate = 14;
116  int wnarc = 12;
117  int wnfinal = 20;
118  for (const auto &key_info : info.key_infos) {
119  if (key_info.key.size() + 2 > wkey) wkey = key_info.key.size() + 2;
120  if (key_info.type.size() + 2 > wtype) wtype = key_info.type.size() + 2;
121  if (ceil(log10(key_info.nstate)) + 2 > wnstate) {
122  wnstate = ceil(log10(key_info.nstate)) + 2;
123  }
124  if (ceil(log10(key_info.narc)) + 2 > wnarc) {
125  wnarc = ceil(log10(key_info.narc)) + 2;
126  }
127  if (ceil(log10(key_info.nfinal)) + 2 > wnfinal) {
128  wnfinal = ceil(log10(key_info.nfinal)) + 2;
129  }
130  }
131  std::cout << std::left << std::setw(wkey) << "key" << std::setw(wtype)
132  << "type" << std::right << std::setw(wnstate) << "# of states"
133  << std::setw(wnarc) << "# of arcs" << std::setw(wnfinal)
134  << "# of final states" << std::endl;
135  for (const auto &key_info : info.key_infos) {
136  std::cout << std::left << std::setw(wkey) << key_info.key
137  << std::setw(wtype) << key_info.type << std::right
138  << std::setw(wnstate) << key_info.nstate << std::setw(wnarc)
139  << key_info.narc << std::setw(wnfinal) << key_info.nfinal
140  << std::endl;
141  }
142  }
143 }
144 
145 } // namespace fst
146 
147 #endif // FST_EXTENSIONS_FAR_INFO_H_
size_t nfinal
Definition: info.h:36
void GetFarInfo(const std::vector< string > &filenames, const string &begin_key, const string &end_key, const bool list_fsts, FarInfoData *far_info)
Definition: info.h:51
void AccumulateStatesAndArcs(const Fst< Arc > &fst, size_t *nstate, size_t *narc, size_t *nfinal)
Definition: info.h:20
#define LOG(type)
Definition: log.h:48
virtual Weight Final(StateId) const =0
size_t nstate
Definition: info.h:34
string key
Definition: info.h:32
size_t narc
Definition: info.h:45
string GetFarTypeString(FarType type)
Definition: getters.cc:60
void FarInfo(const std::vector< string > &filenames, const string &begin_key, const string &end_key, const bool list_fsts)
Definition: info.h:87
string far_type
Definition: info.h:41
std::set< string > fst_types
Definition: info.h:47
size_t nstate
Definition: info.h:44
string arc_type
Definition: info.h:42
std::vector< KeyInfo > key_infos
Definition: info.h:40
size_t nfinal
Definition: info.h:46
string type
Definition: info.h:33
size_t nfst
Definition: info.h:43
size_t narc
Definition: info.h:35