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