21 #ifndef FST_SCRIPT_INFO_IMPL_H_ 22 #define FST_SCRIPT_INFO_IMPL_H_ 44 #include <string_view> 56 template <
typename Arc>
59 std::string_view info_type =
"auto",
bool verify =
true)
60 : fst_type_(fst.Type()),
81 input_lookahead_(false),
82 output_lookahead_(false),
84 arc_filter_type_(arc_filter_type),
86 arc_type_(Arc::Type()) {
87 using Label =
typename Arc::Label;
88 using StateId =
typename Arc::StateId;
89 using Weight =
typename Arc::Weight;
90 if (info_type ==
"long") {
92 }
else if (info_type ==
"short") {
94 }
else if (info_type ==
"auto") {
97 FSTERROR() <<
"Bad info type: " << info_type;
100 if (!long_info_)
return;
102 if (verify && !
Verify(fst)) {
103 FSTERROR() <<
"FstInfo: Verify: FST not well-formed";
106 start_ = fst.
Start();
110 const auto s = siter.Value();
111 if (fst.
Final(s) != Weight::Zero()) ++nfinal_;
112 std::map<Label, size_t> ilabel_count;
113 std::map<Label, size_t> olabel_count;
115 const auto &arc = aiter.Value();
117 if (arc.ilabel == 0 && arc.olabel == 0) ++nepsilons_;
118 if (arc.ilabel == 0) ++niepsilons_;
119 if (arc.olabel == 0) ++noepsilons_;
120 ++ilabel_count[arc.ilabel];
121 ++olabel_count[arc.olabel];
123 for (
auto it = ilabel_count.begin(); it != ilabel_count.end(); ++it) {
124 ilabel_mult_ += it->second * it->second;
126 for (
auto it = olabel_count.begin(); it != olabel_count.end(); ++it) {
127 olabel_mult_ += it->second * it->second;
131 ilabel_mult_ /= narcs_;
132 olabel_mult_ /= narcs_;
135 std::vector<StateId> cc;
138 switch (arc_filter_type) {
140 Visit(fst, &cc_visitor, &fifo_queue);
156 for (StateId s = 0; s < cc.size(); ++s) {
157 if (cc[s] >= ncc_) ncc_ = cc[s] + 1;
161 std::vector<StateId> scc;
162 std::vector<bool> access, coaccess;
165 switch (arc_filter_type) {
183 for (StateId s = 0; s < scc.size(); ++s) {
184 if (access[s]) ++naccess_;
185 if (coaccess[s]) ++ncoaccess_;
186 if (access[s] && coaccess[s]) ++nconnect_;
187 if (scc[s] >= nscc_) nscc_ = scc[s] + 1;
191 input_match_type_ = imatcher.
Type(test_properties);
194 output_match_type_ = omatcher.
Type(test_properties);
200 const std::string &
FstType()
const {
return fst_type_; }
202 const std::string &
ArcType()
const {
return arc_type_; }
216 return input_match_type_;
221 return output_match_type_;
226 return input_lookahead_;
231 return output_lookahead_;
312 void CheckLong()
const {
314 FSTERROR() <<
"FstInfo: Method only available with long info signature";
317 std::string fst_type_;
318 std::string input_symbols_;
319 std::string output_symbols_;
336 bool input_lookahead_;
337 bool output_lookahead_;
338 uint64_t properties_;
341 std::string arc_type_;
352 #endif // FST_SCRIPT_INFO_IMPL_H_
const std::string & OutputSymbols() const
virtual uint64_t Properties(uint64_t mask, bool test) const =0
MatchType InputMatchType() const
void Visit(const FST &fst, Visitor *visitor, Queue *queue, ArcFilter filter, bool access_only=false)
void DfsVisit(const FST &fst, Visitor *visitor, ArcFilter filter, bool access_only=false)
virtual Weight Final(StateId) const =0
void PrintHeader(std::ostream &ostrm, const FstHeader &header)
void PrintProperties(std::ostream &ostrm, uint64_t properties)
size_t NumConnected() const
MatchType Type(bool test) const
script::ArcFilterType ArcFilterType() const
bool InputLookAhead() const
constexpr uint32_t kOutputLookAheadMatcher
double OutputLabelMultiplicity() const
int64_t NumStates() const
size_t NumOutputEpsilons() const
size_t NumInputEpsilons() const
constexpr uint32_t kInputLookAheadMatcher
size_t NumAccessible() const
double InputLabelMultiplicity() const
virtual StateId Start() const =0
uint64_t Properties() const
const std::string & InputSymbols() const
size_t NumEpsilons() const
size_t NumCoAccessible() const
const std::string & FstType() const
bool Verify(const Fst< Arc > &fst, bool allow_negative_labels=false)
constexpr uint64_t kFstProperties
const std::string & ArcType() const
bool OutputLookAhead() const
FstInfo(const Fst< Arc > &fst, bool test_properties, script::ArcFilterType arc_filter_type=script::ArcFilterType::ANY, std::string_view info_type="auto", bool verify=true)
MatchType OutputMatchType() const
constexpr uint64_t kExpanded