FST  openfst-1.8.3
OpenFst Library
info-impl.cc
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 #include <fst/script/info-impl.h>
19 
20 #include <cstdint>
21 #include <ios>
22 #include <iostream>
23 #include <ostream>
24 #include <string>
25 
26 #include <fst/fst.h>
27 #include <fst/properties.h>
29 #include <string_view>
30 
31 namespace fst {
32 
33 // Column width for property names.
34 constexpr int kWidth = 50;
35 
36 void FstInfo::Info() const {
37  std::ostream &ostrm = std::cout;
38  const auto old = ostrm.setf(std::ios::left);
39  ostrm.width(kWidth);
40  ostrm << "fst type" << FstType() << std::endl;
41  ostrm.width(kWidth);
42  ostrm << "arc type" << ArcType() << std::endl;
43  ostrm.width(kWidth);
44  ostrm << "input symbol table" << InputSymbols() << std::endl;
45  ostrm.width(kWidth);
46  ostrm << "output symbol table" << OutputSymbols() << std::endl;
47  if (!LongInfo()) {
48  ostrm.setf(old);
49  return;
50  }
51  ostrm.width(kWidth);
52  ostrm << "# of states" << NumStates() << std::endl;
53  ostrm.width(kWidth);
54  ostrm << "# of arcs" << NumArcs() << std::endl;
55  ostrm.width(kWidth);
56  ostrm << "initial state" << Start() << std::endl;
57  ostrm.width(kWidth);
58  ostrm << "# of final states" << NumFinal() << std::endl;
59  ostrm.width(kWidth);
60  ostrm << "# of input/output epsilons" << NumEpsilons() << std::endl;
61  ostrm.width(kWidth);
62  ostrm << "# of input epsilons" << NumInputEpsilons() << std::endl;
63  ostrm.width(kWidth);
64  ostrm << "# of output epsilons" << NumOutputEpsilons() << std::endl;
65  ostrm.width(kWidth);
66  ostrm << "input label multiplicity" << InputLabelMultiplicity() << std::endl;
67  ostrm.width(kWidth);
68  ostrm << "output label multiplicity" << OutputLabelMultiplicity()
69  << std::endl;
70  ostrm.width(kWidth);
71  std::string arc_type = "";
72  switch (ArcFilterType()) {
74  break;
76  arc_type = "epsilon ";
77  break;
78  }
80  arc_type = "input-epsilon ";
81  break;
82  }
84  arc_type = "output-epsilon ";
85  break;
86  }
87  }
88  const auto accessible_label = "# of " + arc_type + "accessible states";
89  ostrm.width(kWidth);
90  ostrm << accessible_label << NumAccessible() << std::endl;
91  const auto coaccessible_label = "# of " + arc_type + "coaccessible states";
92  ostrm.width(kWidth);
93  ostrm << coaccessible_label << NumCoAccessible() << std::endl;
94  const auto connected_label = "# of " + arc_type + "connected states";
95  ostrm.width(kWidth);
96  ostrm << connected_label << NumConnected() << std::endl;
97  const auto numcc_label = "# of " + arc_type + "connected components";
98  ostrm.width(kWidth);
99  ostrm << numcc_label << NumCc() << std::endl;
100  const auto numscc_label = "# of " + arc_type + "strongly conn components";
101  ostrm.width(kWidth);
102  ostrm << numscc_label << NumScc() << std::endl;
103  ostrm.width(kWidth);
104  ostrm << "input matcher"
105  << (InputMatchType() == MATCH_INPUT
106  ? 'y'
107  : InputMatchType() == MATCH_NONE ? 'n' : '?')
108  << std::endl;
109  ostrm.width(kWidth);
110  ostrm << "output matcher"
112  ? 'y'
113  : OutputMatchType() == MATCH_NONE ? 'n' : '?')
114  << std::endl;
115  ostrm.width(kWidth);
116  ostrm << "input lookahead" << (InputLookAhead() ? 'y' : 'n') << std::endl;
117  ostrm.width(kWidth);
118  ostrm << "output lookahead" << (OutputLookAhead() ? 'y' : 'n') << std::endl;
119  PrintProperties(ostrm, Properties());
120  ostrm.setf(old);
121 }
122 
123 void PrintProperties(std::ostream &ostrm, const uint64_t properties) {
124  uint64_t prop = 1;
125  for (auto i = 0; i < 64; ++i, prop <<= 1) {
126  if (prop & kBinaryProperties) {
127  const char value = properties & prop ? 'y' : 'n';
128  ostrm.width(kWidth);
129  ostrm << internal::PropertyNames[i] << value << std::endl;
130  } else if (prop & kPosTrinaryProperties) {
131  char value = '?';
132  if (properties & prop) {
133  value = 'y';
134  } else if (properties & prop << 1) {
135  value = 'n';
136  }
137  ostrm.width(kWidth);
138  ostrm << internal::PropertyNames[i] << value << std::endl;
139  }
140  }
141 }
142 
143 void PrintHeader(std::ostream &ostrm, const FstHeader &header) {
144  const auto old = ostrm.setf(std::ios::left);
145  ostrm.width(kWidth);
146  ostrm << "fst type" << header.FstType() << std::endl;
147  ostrm.width(kWidth);
148  ostrm << "arc type" << header.ArcType() << std::endl;
149  ostrm.width(kWidth);
150  ostrm << "version" << header.Version() << std::endl;
151 
152  // Flags
153  const auto flags = header.GetFlags();
154  ostrm.width(kWidth);
155  ostrm << "input symbol table" << (flags & FstHeader::HAS_ISYMBOLS ? 'y' : 'n')
156  << std::endl;
157  ostrm.width(kWidth);
158  ostrm << "output symbol table"
159  << (flags & FstHeader::HAS_OSYMBOLS ? 'y' : 'n') << std::endl;
160  ostrm.width(kWidth);
161  ostrm << "aligned" << (flags & FstHeader::IS_ALIGNED ? 'y' : 'n')
162  << std::endl;
163 
164  ostrm.width(kWidth);
165  ostrm << "initial state" << header.Start() << std::endl;
166  ostrm.width(kWidth);
167  ostrm << "# of states" << header.NumStates() << std::endl;
168  ostrm.width(kWidth);
169  ostrm << "# of arcs" << header.NumArcs() << std::endl;
170 
171  PrintProperties(ostrm, header.Properties());
172  ostrm.setf(old);
173 }
174 
175 } // namespace fst
const std::string & OutputSymbols() const
Definition: info-impl.h:206
constexpr uint64_t kBinaryProperties
Definition: properties.h:310
MatchType InputMatchType() const
Definition: info-impl.h:214
size_t NumFinal() const
Definition: info-impl.h:249
void PrintHeader(std::ostream &ostrm, const FstHeader &header)
Definition: info-impl.cc:143
void PrintProperties(std::ostream &ostrm, uint64_t properties)
Definition: info-impl.cc:123
size_t NumConnected() const
Definition: info-impl.h:289
script::ArcFilterType ArcFilterType() const
Definition: info-impl.h:210
bool InputLookAhead() const
Definition: info-impl.h:224
size_t NumCc() const
Definition: info-impl.h:294
uint32_t GetFlags() const
Definition: fst.h:142
constexpr int kWidth
Definition: info-impl.cc:34
bool LongInfo() const
Definition: info-impl.h:208
const std::string & FstType() const
Definition: fst.h:136
size_t NumScc() const
Definition: info-impl.h:299
double OutputLabelMultiplicity() const
Definition: info-impl.h:274
int64_t NumStates() const
Definition: info-impl.h:234
size_t NumOutputEpsilons() const
Definition: info-impl.h:264
size_t NumInputEpsilons() const
Definition: info-impl.h:259
size_t NumAccessible() const
Definition: info-impl.h:279
double InputLabelMultiplicity() const
Definition: info-impl.h:269
uint64_t Properties() const
Definition: info-impl.h:304
const std::string & InputSymbols() const
Definition: info-impl.h:204
size_t NumEpsilons() const
Definition: info-impl.h:254
void Info() const
Definition: info-impl.cc:36
size_t NumCoAccessible() const
Definition: info-impl.h:284
const std::string & FstType() const
Definition: info-impl.h:200
int64_t Start() const
Definition: info-impl.h:244
int64_t NumArcs() const
Definition: fst.h:150
const std::string & ArcType() const
Definition: info-impl.h:202
int64_t NumStates() const
Definition: fst.h:148
bool OutputLookAhead() const
Definition: info-impl.h:229
const std::string_view PropertyNames[]
Definition: properties.cc:429
constexpr uint64_t kPosTrinaryProperties
Definition: properties.h:318
int64_t Start() const
Definition: fst.h:146
int32_t Version() const
Definition: fst.h:140
MatchType OutputMatchType() const
Definition: info-impl.h:219
size_t NumArcs() const
Definition: info-impl.h:239
const std::string & ArcType() const
Definition: fst.h:138
uint64_t Properties() const
Definition: fst.h:144