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