/usr/include/fst/script/info-impl.h is in libfst-dev 1.5.3+r3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 | // See www.openfst.org for extensive documentation on this weighted
// finite-state transducer library.
//
// Class to compute various information about FSTs, a helper class for
// fstinfo.cc.
#ifndef FST_SCRIPT_INFO_IMPL_H_
#define FST_SCRIPT_INFO_IMPL_H_
#include <map>
#include <string>
#include <vector>
#include <fst/connect.h>
#include <fst/dfs-visit.h>
#include <fst/fst.h>
#include <fst/lookahead-matcher.h>
#include <fst/matcher.h>
#include <fst/queue.h>
#include <fst/test-properties.h>
#include <fst/verify.h>
#include <fst/visit.h>
namespace fst {
// Compute various information about FSTs, helper class for fstinfo.cc.
// WARNING: Stand-alone use of this class is not recommended, most code
// should call directly the relevant library functions: Fst<A>::NumStates,
// Fst<A>::NumArcs, TestProperties, ...
template <class A>
class FstInfo {
public:
typedef A Arc;
typedef typename A::StateId StateId;
typedef typename A::Label Label;
typedef typename A::Weight Weight;
// When info_type is "short" (or "auto" and not an ExpandedFst)
// then only minimal info is computed and can be requested.
FstInfo(const Fst<A>& fst, bool test_properties,
const string& arc_filter_type = "any", string info_type = "auto",
bool verify = true)
: fst_type_(fst.Type()),
input_symbols_(fst.InputSymbols() ? fst.InputSymbols()->Name()
: "none"),
output_symbols_(fst.OutputSymbols() ? fst.OutputSymbols()->Name()
: "none"),
nstates_(0),
narcs_(0),
start_(kNoStateId),
nfinal_(0),
nepsilons_(0),
niepsilons_(0),
noepsilons_(0),
ilabel_mult_(0.0),
olabel_mult_(0.0),
naccess_(0),
ncoaccess_(0),
nconnect_(0),
ncc_(0),
nscc_(0),
input_match_type_(MATCH_NONE),
output_match_type_(MATCH_NONE),
input_lookahead_(false),
output_lookahead_(false),
properties_(0),
arc_filter_type_(arc_filter_type),
long_info_(true) {
if (info_type == "long") {
long_info_ = true;
} else if (info_type == "short") {
long_info_ = false;
} else if (info_type == "auto") {
long_info_ = fst.Properties(kExpanded, false);
} else {
FSTERROR() << "Bad info type: " << info_type;
return;
}
if (!long_info_) return;
// If the FST is not sane, we return.
if (verify && !Verify(fst)) {
FSTERROR() << "FstInfo: Verify: FST not well-formed.";
return;
}
start_ = fst.Start();
properties_ = fst.Properties(kFstProperties, test_properties);
for (StateIterator<Fst<A>> siter(fst); !siter.Done(); siter.Next()) {
++nstates_;
StateId s = siter.Value();
if (fst.Final(s) != Weight::Zero()) ++nfinal_;
std::map<Label, size_t> ilabel_count;
std::map<Label, size_t> olabel_count;
for (ArcIterator<Fst<A>> aiter(fst, s); !aiter.Done(); aiter.Next()) {
const A& arc = aiter.Value();
++narcs_;
if (arc.ilabel == 0 && arc.olabel == 0) ++nepsilons_;
if (arc.ilabel == 0) ++niepsilons_;
if (arc.olabel == 0) ++noepsilons_;
++ilabel_count[arc.ilabel];
++olabel_count[arc.olabel];
}
for (auto it = ilabel_count.begin(); it != ilabel_count.end(); ++it) {
ilabel_mult_ += it->second * it->second;
}
for (auto it = olabel_count.begin(); it != olabel_count.end(); ++it) {
olabel_mult_ += it->second * it->second;
}
}
if (narcs_ > 0) {
ilabel_mult_ /= narcs_;
olabel_mult_ /= narcs_;
}
{
std::vector<StateId> cc;
CcVisitor<Arc> cc_visitor(&cc);
FifoQueue<StateId> fifo_queue;
if (arc_filter_type == "any") {
Visit(fst, &cc_visitor, &fifo_queue);
} else if (arc_filter_type == "epsilon") {
Visit(fst, &cc_visitor, &fifo_queue, EpsilonArcFilter<Arc>());
} else if (arc_filter_type == "iepsilon") {
Visit(fst, &cc_visitor, &fifo_queue, InputEpsilonArcFilter<Arc>());
} else if (arc_filter_type == "oepsilon") {
Visit(fst, &cc_visitor, &fifo_queue, OutputEpsilonArcFilter<Arc>());
} else {
FSTERROR() << "Bad arc filter type: " << arc_filter_type;
return;
}
for (StateId s = 0; s < cc.size(); ++s) {
if (cc[s] >= ncc_) ncc_ = cc[s] + 1;
}
}
{
std::vector<StateId> scc;
std::vector<bool> access, coaccess;
uint64 props = 0;
SccVisitor<Arc> scc_visitor(&scc, &access, &coaccess, &props);
if (arc_filter_type == "any") {
DfsVisit(fst, &scc_visitor);
} else if (arc_filter_type == "epsilon") {
DfsVisit(fst, &scc_visitor, EpsilonArcFilter<Arc>());
} else if (arc_filter_type == "iepsilon") {
DfsVisit(fst, &scc_visitor, InputEpsilonArcFilter<Arc>());
} else if (arc_filter_type == "oepsilon") {
DfsVisit(fst, &scc_visitor, OutputEpsilonArcFilter<Arc>());
} else {
FSTERROR() << "Bad arc filter type: " << arc_filter_type;
return;
}
for (StateId s = 0; s < scc.size(); ++s) {
if (access[s]) ++naccess_;
if (coaccess[s]) ++ncoaccess_;
if (access[s] && coaccess[s]) ++nconnect_;
if (scc[s] >= nscc_) nscc_ = scc[s] + 1;
}
}
LookAheadMatcher<Fst<A>> imatcher(fst, MATCH_INPUT);
input_match_type_ = imatcher.Type(test_properties);
input_lookahead_ = imatcher.Flags() & kInputLookAheadMatcher;
LookAheadMatcher<Fst<A>> omatcher(fst, MATCH_OUTPUT);
output_match_type_ = omatcher.Type(test_properties);
output_lookahead_ = omatcher.Flags() & kOutputLookAheadMatcher;
}
// Short info
const string& FstType() const { return fst_type_; }
const string& ArcType() const { return A::Type(); }
const string& InputSymbols() const { return input_symbols_; }
const string& OutputSymbols() const { return output_symbols_; }
bool LongInfo() const { return long_info_; }
const string& ArcFilterType() const { return arc_filter_type_; }
// Long info
MatchType InputMatchType() const {
CheckLong();
return input_match_type_;
}
MatchType OutputMatchType() const {
CheckLong();
return output_match_type_;
}
bool InputLookAhead() const {
CheckLong();
return input_lookahead_;
}
bool OutputLookAhead() const {
CheckLong();
return output_lookahead_;
}
StateId NumStates() const {
CheckLong();
return nstates_;
}
size_t NumArcs() const {
CheckLong();
return narcs_;
}
StateId Start() const {
CheckLong();
return start_;
}
size_t NumFinal() const {
CheckLong();
return nfinal_;
}
size_t NumEpsilons() const {
CheckLong();
return nepsilons_;
}
size_t NumInputEpsilons() const {
CheckLong();
return niepsilons_;
}
size_t NumOutputEpsilons() const {
CheckLong();
return noepsilons_;
}
double InputLabelMultiplicity() const {
CheckLong();
return ilabel_mult_;
}
double OutputLabelMultiplicity() const {
CheckLong();
return olabel_mult_;
}
size_t NumAccessible() const {
CheckLong();
return naccess_;
}
size_t NumCoAccessible() const {
CheckLong();
return ncoaccess_;
}
size_t NumConnected() const {
CheckLong();
return nconnect_;
}
size_t NumCc() const {
CheckLong();
return ncc_;
}
size_t NumScc() const {
CheckLong();
return nscc_;
}
uint64 Properties() const {
CheckLong();
return properties_;
}
private:
void CheckLong() const {
if (!long_info_)
FSTERROR() << "FstInfo: Method only available with long info signature";
}
string fst_type_;
string input_symbols_;
string output_symbols_;
StateId nstates_;
size_t narcs_;
StateId start_;
size_t nfinal_;
size_t nepsilons_;
size_t niepsilons_;
size_t noepsilons_;
double ilabel_mult_;
double olabel_mult_;
size_t naccess_;
size_t ncoaccess_;
size_t nconnect_;
size_t ncc_;
size_t nscc_;
MatchType input_match_type_;
MatchType output_match_type_;
bool input_lookahead_;
bool output_lookahead_;
uint64 properties_;
string arc_filter_type_;
bool long_info_;
DISALLOW_COPY_AND_ASSIGN(FstInfo);
};
template <class A>
void PrintFstInfo(const FstInfo<A>& fstinfo, bool pipe = false) {
ostream& os = pipe ? std::cerr : std::cout;
ios_base::fmtflags old = os.setf(ios::left);
os.width(50);
os << "fst type" << fstinfo.FstType() << std::endl;
os.width(50);
os << "arc type" << fstinfo.ArcType() << std::endl;
os.width(50);
os << "input symbol table" << fstinfo.InputSymbols() << std::endl;
os.width(50);
os << "output symbol table" << fstinfo.OutputSymbols() << std::endl;
if (!fstinfo.LongInfo()) {
os.setf(old);
return;
}
os.width(50);
os << "# of states" << fstinfo.NumStates() << std::endl;
os.width(50);
os << "# of arcs" << fstinfo.NumArcs() << std::endl;
os.width(50);
os << "initial state" << fstinfo.Start() << std::endl;
os.width(50);
os << "# of final states" << fstinfo.NumFinal() << std::endl;
os.width(50);
os << "# of input/output epsilons" << fstinfo.NumEpsilons() << std::endl;
os.width(50);
os << "# of input epsilons" << fstinfo.NumInputEpsilons() << std::endl;
os.width(50);
os << "# of output epsilons" << fstinfo.NumOutputEpsilons() << std::endl;
os.width(50);
os << "input label multiplicity" << fstinfo.InputLabelMultiplicity()
<< std::endl;
os.width(50);
os << "output label multiplicity" << fstinfo.OutputLabelMultiplicity()
<< std::endl;
os.width(50);
string arc_type = "";
if (fstinfo.ArcFilterType() == "epsilon")
arc_type = "epsilon ";
else if (fstinfo.ArcFilterType() == "iepsilon")
arc_type = "input-epsilon ";
else if (fstinfo.ArcFilterType() == "oepsilon")
arc_type = "output-epsilon ";
string accessible_label = "# of " + arc_type + "accessible states";
os.width(50);
os << accessible_label << fstinfo.NumAccessible() << std::endl;
string coaccessible_label = "# of " + arc_type + "coaccessible states";
os.width(50);
os << coaccessible_label << fstinfo.NumCoAccessible() << std::endl;
string connected_label = "# of " + arc_type + "connected states";
os.width(50);
os << connected_label << fstinfo.NumConnected() << std::endl;
string numcc_label = "# of " + arc_type + "connected components";
os.width(50);
os << numcc_label << fstinfo.NumCc() << std::endl;
string numscc_label = "# of " + arc_type + "strongly conn components";
os.width(50);
os << numscc_label << fstinfo.NumScc() << std::endl;
os.width(50);
os << "input matcher"
<< (fstinfo.InputMatchType() == MATCH_INPUT
? 'y'
: fstinfo.InputMatchType() == MATCH_NONE ? 'n' : '?')
<< std::endl;
os.width(50);
os << "output matcher"
<< (fstinfo.OutputMatchType() == MATCH_OUTPUT
? 'y'
: fstinfo.OutputMatchType() == MATCH_NONE ? 'n' : '?')
<< std::endl;
os.width(50);
os << "input lookahead" << (fstinfo.InputLookAhead() ? 'y' : 'n')
<< std::endl;
os.width(50);
os << "output lookahead" << (fstinfo.OutputLookAhead() ? 'y' : 'n')
<< std::endl;
uint64 prop = 1;
for (int i = 0; i < 64; ++i, prop <<= 1) {
if (prop & kBinaryProperties) {
char value = 'n';
if (fstinfo.Properties() & prop) value = 'y';
os.width(50);
os << PropertyNames[i] << value << std::endl;
} else if (prop & kPosTrinaryProperties) {
char value = '?';
if (fstinfo.Properties() & prop)
value = 'y';
else if (fstinfo.Properties() & prop << 1)
value = 'n';
os.width(50);
os << PropertyNames[i] << value << std::endl;
}
}
os.setf(old);
}
} // namespace fst
#endif // FST_SCRIPT_INFO_IMPL_H_
|