Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 11 additions & 18 deletions openfst/extensions/far/info.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@
#include <vector>

#include "absl/log/log.h"
#include "absl/strings/str_join.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "openfst/extensions/far/far-reader.h"
#include "openfst/extensions/far/far.h"
#include "openfst/extensions/far/getters.h"
#include "openfst/lib/fst.h"
#include "openfst/lib/util.h"

namespace fst {

Expand Down Expand Up @@ -112,24 +114,15 @@ void Info(absl::Span<const std::string> sources, absl::string_view begin_key,
FarInfoData info;
GetInfo<Arc>(sources, begin_key, end_key, list_fsts, &info);
if (!list_fsts) {
std::cout << std::left << std::setw(50) << "far type" << info.far_type
<< '\n';
std::cout << std::left << std::setw(50) << "arc type" << Arc::Type()
<< std::endl;
std::cout << std::left << std::setw(50) << "fst type";
for (auto iter = info.fst_types.begin(); iter != info.fst_types.end();
++iter) {
if (iter != info.fst_types.begin()) std::cout << ",";
std::cout << *iter;
}
std::cout << '\n';
std::cout << std::left << std::setw(50) << "# of FSTs" << info.nfst << '\n';
std::cout << std::left << std::setw(50) << "total # of states"
<< info.nstate << '\n';
std::cout << std::left << std::setw(50) << "total # of arcs" << info.narc
<< '\n';
std::cout << std::left << std::setw(50) << "total # of final states"
<< info.nfinal << '\n';
const auto old = std::cout.setf(std::ios::left);
PrintField(std::cout, "far type", info.far_type);
PrintField(std::cout, "arc type", Arc::Type());
PrintField(std::cout, "fst type", absl::StrJoin(info.fst_types, ","));
PrintField(std::cout, "# of FSTs", info.nfst);
PrintField(std::cout, "total # of states", info.nstate);
PrintField(std::cout, "total # of arcs", info.narc);
PrintField(std::cout, "total # of final states", info.nfinal);
std::cout.setf(old);
} else {
// FIXME(kbg): Grok, then document this.
int wkey = 10;
Expand Down
1 change: 1 addition & 0 deletions openfst/extensions/mpdt/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ cc_library(
"//openfst/lib:util",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/strings",
],
)

Expand Down
55 changes: 26 additions & 29 deletions openfst/extensions/mpdt/info.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "absl/strings/str_cat.h"
#include "openfst/lib/fst.h"
#include "openfst/lib/util.h"

Expand Down Expand Up @@ -167,37 +168,33 @@ MPdtInfo<Arc, nlevels>::MPdtInfo(
template <class Arc, typename Arc::Label nlevels>
void MPdtInfo<Arc, nlevels>::Print() {
const auto old = std::cout.setf(std::ios::left);
std::cout.width(50);
std::cout << "fst type" << FstType() << std::endl;
std::cout.width(50);
std::cout << "arc type" << ArcType() << std::endl;
std::cout.width(50);
std::cout << "# of states" << NumStates() << std::endl;
std::cout.width(50);
std::cout << "# of arcs" << NumArcs() << std::endl;
std::cout.width(50);
std::cout << "# of levels" << NumLevels() << std::endl;
std::cout.width(50);
PrintField(std::cout, "fst type", FstType());
PrintField(std::cout, "arc type", ArcType());
PrintField(std::cout, "# of states", NumStates());
PrintField(std::cout, "# of arcs", NumArcs());
PrintField(std::cout, "# of levels", NumLevels());
for (typename Arc::Label i = 0; i < nlevels; ++i) {
int level = i + 1;
std::cout << "# of open parentheses at level " << level << "\t"
<< NumOpenParens(i) << std::endl;
std::cout.width(50);
std::cout << "# of close parentheses at level " << level << "\t"
<< NumCloseParens(i) << std::endl;
std::cout.width(50);
std::cout << "# of unique open parentheses at level " << level << "\t"
<< NumUniqueOpenParens(i) << std::endl;
std::cout.width(50);
std::cout << "# of unique close parentheses at level " << level << "\t"
<< NumUniqueCloseParens(i) << std::endl;
std::cout.width(50);
std::cout << "# of open parenthesis dest. states at level " << level << "\t"
<< NumOpenParenStates(i) << std::endl;
std::cout.width(50);
std::cout << "# of close parenthesis source states at level " << level
<< "\t" << NumCloseParenStates(i) << std::endl;
std::cout.width(50);
PrintField(std::cout,
absl::StrCat("# of open parentheses at level ", level),
NumOpenParens(i));
PrintField(std::cout,
absl::StrCat("# of close parentheses at level ", level),
NumCloseParens(i));
PrintField(std::cout,
absl::StrCat("# of unique open parentheses at level ", level),
NumUniqueOpenParens(i));
PrintField(std::cout,
absl::StrCat("# of unique close parentheses at level ", level),
NumUniqueCloseParens(i));
PrintField(
std::cout,
absl::StrCat("# of open parenthesis dest. states at level ", level),
NumOpenParenStates(i));
PrintField(
std::cout,
absl::StrCat("# of close parenthesis source states at level ", level),
NumCloseParenStates(i));
}
std::cout.setf(old);
}
Expand Down
1 change: 1 addition & 0 deletions openfst/extensions/pdt/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ cc_library(
hdrs = ["info.h"],
deps = [
"//openfst/lib",
"//openfst/lib:util",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/types:span",
Expand Down
38 changes: 14 additions & 24 deletions openfst/extensions/pdt/info.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "absl/container/flat_hash_set.h"
#include "absl/types/span.h"
#include "openfst/lib/fst.h"
#include "openfst/lib/util.h"

namespace fst {

Expand Down Expand Up @@ -137,30 +138,19 @@ PdtInfo<Arc>::PdtInfo(
template <class Arc>
void PdtInfo<Arc>::Print() {
const auto old = std::cout.setf(std::ios::left);
std::cout.width(50);
std::cout << "fst type" << FstType() << std::endl;
std::cout.width(50);
std::cout << "arc type" << ArcType() << std::endl;
std::cout.width(50);
std::cout << "# of states" << NumStates() << std::endl;
std::cout.width(50);
std::cout << "# of arcs" << NumArcs() << std::endl;
std::cout.width(50);
std::cout << "# of open parentheses" << NumOpenParens() << std::endl;
std::cout.width(50);
std::cout << "# of close parentheses" << NumCloseParens() << std::endl;
std::cout.width(50);
std::cout << "# of unique open parentheses" << NumUniqueOpenParens()
<< std::endl;
std::cout.width(50);
std::cout << "# of unique close parentheses" << NumUniqueCloseParens()
<< std::endl;
std::cout.width(50);
std::cout << "# of open parenthesis dest. states" << NumOpenParenStates()
<< std::endl;
std::cout.width(50);
std::cout << "# of close parenthesis source states" << NumCloseParenStates()
<< std::endl;
PrintField(std::cout, "fst type", FstType());
PrintField(std::cout, "arc type", ArcType());
PrintField(std::cout, "# of states", NumStates());
PrintField(std::cout, "# of arcs", NumArcs());
PrintField(std::cout, "# of open parentheses", NumOpenParens());
PrintField(std::cout, "# of close parentheses", NumCloseParens());
PrintField(std::cout, "# of unique open parentheses", NumUniqueOpenParens());
PrintField(std::cout, "# of unique close parentheses",
NumUniqueCloseParens());
PrintField(std::cout, "# of open parenthesis dest. states",
NumOpenParenStates());
PrintField(std::cout, "# of close parenthesis source states",
NumCloseParenStates());
std::cout.setf(old);
}

Expand Down
8 changes: 8 additions & 0 deletions openfst/lib/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,14 @@ void ConvertToLegalCSymbol(std::string* s);

// Utilities for stream I/O.

// Helper template for printing labeled fields to output streams.
template <typename T>
inline void PrintField(std::ostream& ostrm, absl::string_view label,
const T& value, int width = 50) {
ostrm.width(width);
ostrm << label << value << std::endl;
}

bool AlignInput(std::istream& strm, size_t align = MappedFile::kArchAlignment);
bool AlignOutput(std::ostream& strm, size_t align = MappedFile::kArchAlignment);

Expand Down
144 changes: 48 additions & 96 deletions openfst/script/info-impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,48 +27,31 @@
#include "absl/strings/string_view.h"
#include "openfst/lib/fst.h"
#include "openfst/lib/properties.h"
#include "openfst/lib/util.h"
#include "openfst/script/arcfilter-impl.h"

namespace fst {

// Column width for property names.
constexpr int kWidth = 50;

void FstInfo::Info() const {
std::ostream& ostrm = std::cout;
const auto old = ostrm.setf(std::ios::left);
ostrm.width(kWidth);
ostrm << "fst type" << FstType() << std::endl;
ostrm.width(kWidth);
ostrm << "arc type" << ArcType() << std::endl;
ostrm.width(kWidth);
ostrm << "input symbol table" << InputSymbols() << std::endl;
ostrm.width(kWidth);
ostrm << "output symbol table" << OutputSymbols() << std::endl;
PrintField(ostrm, "fst type", FstType());
PrintField(ostrm, "arc type", ArcType());
PrintField(ostrm, "input symbol table", InputSymbols());
PrintField(ostrm, "output symbol table", OutputSymbols());
if (!LongInfo()) {
ostrm.setf(old);
return;
}
ostrm.width(kWidth);
ostrm << "# of states" << NumStates() << std::endl;
ostrm.width(kWidth);
ostrm << "# of arcs" << NumArcs() << std::endl;
ostrm.width(kWidth);
ostrm << "initial state" << Start() << std::endl;
ostrm.width(kWidth);
ostrm << "# of final states" << NumFinal() << std::endl;
ostrm.width(kWidth);
ostrm << "# of input/output epsilons" << NumEpsilons() << std::endl;
ostrm.width(kWidth);
ostrm << "# of input epsilons" << NumInputEpsilons() << std::endl;
ostrm.width(kWidth);
ostrm << "# of output epsilons" << NumOutputEpsilons() << std::endl;
ostrm.width(kWidth);
ostrm << "input label multiplicity" << InputLabelMultiplicity() << std::endl;
ostrm.width(kWidth);
ostrm << "output label multiplicity" << OutputLabelMultiplicity()
<< std::endl;
ostrm.width(kWidth);
PrintField(ostrm, "# of states", NumStates());
PrintField(ostrm, "# of arcs", NumArcs());
PrintField(ostrm, "initial state", Start());
PrintField(ostrm, "# of final states", NumFinal());
PrintField(ostrm, "# of input/output epsilons", NumEpsilons());
PrintField(ostrm, "# of input epsilons", NumInputEpsilons());
PrintField(ostrm, "# of output epsilons", NumOutputEpsilons());
PrintField(ostrm, "input label multiplicity", InputLabelMultiplicity());
PrintField(ostrm, "output label multiplicity", OutputLabelMultiplicity());
std::string arc_type = "";
switch (ArcFilterType()) {
case script::ArcFilterType::ANY:
Expand All @@ -86,42 +69,26 @@ void FstInfo::Info() const {
break;
}
}
const auto accessible_label =
absl::StrCat("# of ", arc_type, "accessible states");
ostrm.width(kWidth);
ostrm << accessible_label << NumAccessible() << std::endl;
const auto coaccessible_label =
absl::StrCat("# of ", arc_type, "coaccessible states");
ostrm.width(kWidth);
ostrm << coaccessible_label << NumCoAccessible() << std::endl;
const auto connected_label =
absl::StrCat("# of ", arc_type, "connected states");
ostrm.width(kWidth);
ostrm << connected_label << NumConnected() << std::endl;
const auto numcc_label =
absl::StrCat("# of ", arc_type, "connected components");
ostrm.width(kWidth);
ostrm << numcc_label << NumCc() << std::endl;
const auto numscc_label =
absl::StrCat("# of ", arc_type, "strongly conn components");
ostrm.width(kWidth);
ostrm << numscc_label << NumScc() << std::endl;
ostrm.width(kWidth);
ostrm << "input matcher"
<< (InputMatchType() == MATCH_INPUT ? 'y'
: InputMatchType() == MATCH_NONE ? 'n'
: '?')
<< std::endl;
ostrm.width(kWidth);
ostrm << "output matcher"
<< (OutputMatchType() == MATCH_OUTPUT ? 'y'
: OutputMatchType() == MATCH_NONE ? 'n'
: '?')
<< std::endl;
ostrm.width(kWidth);
ostrm << "input lookahead" << (InputLookAhead() ? 'y' : 'n') << std::endl;
ostrm.width(kWidth);
ostrm << "output lookahead" << (OutputLookAhead() ? 'y' : 'n') << std::endl;
PrintField(ostrm, absl::StrCat("# of ", arc_type, "accessible states"),
NumAccessible());
PrintField(ostrm, absl::StrCat("# of ", arc_type, "coaccessible states"),
NumCoAccessible());
PrintField(ostrm, absl::StrCat("# of ", arc_type, "connected states"),
NumConnected());
PrintField(ostrm, absl::StrCat("# of ", arc_type, "connected components"),
NumCc());
PrintField(ostrm, absl::StrCat("# of ", arc_type, "strongly conn components"),
NumScc());
PrintField(ostrm, "input matcher",
(InputMatchType() == MATCH_INPUT ? 'y'
: InputMatchType() == MATCH_NONE ? 'n'
: '?'));
PrintField(ostrm, "output matcher",
(OutputMatchType() == MATCH_OUTPUT ? 'y'
: OutputMatchType() == MATCH_NONE ? 'n'
: '?'));
PrintField(ostrm, "input lookahead", (InputLookAhead() ? 'y' : 'n'));
PrintField(ostrm, "output lookahead", (OutputLookAhead() ? 'y' : 'n'));
PrintProperties(ostrm, Properties());
ostrm.setf(old);
}
Expand All @@ -131,49 +98,34 @@ void PrintProperties(std::ostream& ostrm, const uint64_t properties) {
for (auto i = 0; i < 64; ++i, prop <<= 1) {
if (prop & kBinaryProperties) {
const char value = properties & prop ? 'y' : 'n';
ostrm.width(kWidth);
ostrm << internal::PropertyNames[i] << value << std::endl;
PrintField(ostrm, internal::PropertyNames[i], value);
} else if (prop & kPosTrinaryProperties) {
char value = '?';
if (properties & prop) {
value = 'y';
} else if (properties & prop << 1) {
value = 'n';
}
ostrm.width(kWidth);
ostrm << internal::PropertyNames[i] << value << std::endl;
PrintField(ostrm, internal::PropertyNames[i], value);
}
}
}

void PrintHeader(std::ostream& ostrm, const FstHeader& header) {
const auto old = ostrm.setf(std::ios::left);
ostrm.width(kWidth);
ostrm << "fst type" << header.FstType() << std::endl;
ostrm.width(kWidth);
ostrm << "arc type" << header.ArcType() << std::endl;
ostrm.width(kWidth);
ostrm << "version" << header.Version() << std::endl;

// Flags
PrintField(ostrm, "fst type", header.FstType());
PrintField(ostrm, "arc type", header.ArcType());
PrintField(ostrm, "version", header.Version());
// Flags.
const auto flags = header.GetFlags();
ostrm.width(kWidth);
ostrm << "input symbol table" << (flags & FstHeader::HAS_ISYMBOLS ? 'y' : 'n')
<< std::endl;
ostrm.width(kWidth);
ostrm << "output symbol table"
<< (flags & FstHeader::HAS_OSYMBOLS ? 'y' : 'n') << std::endl;
ostrm.width(kWidth);
ostrm << "aligned" << (flags & FstHeader::IS_ALIGNED ? 'y' : 'n')
<< std::endl;

ostrm.width(kWidth);
ostrm << "initial state" << header.Start() << std::endl;
ostrm.width(kWidth);
ostrm << "# of states" << header.NumStates() << std::endl;
ostrm.width(kWidth);
ostrm << "# of arcs" << header.NumArcs() << std::endl;

PrintField(ostrm, "input symbol table",
(flags & FstHeader::HAS_ISYMBOLS ? 'y' : 'n'));
PrintField(ostrm, "output symbol table",
(flags & FstHeader::HAS_OSYMBOLS ? 'y' : 'n'));
PrintField(ostrm, "aligned", (flags & FstHeader::IS_ALIGNED ? 'y' : 'n'));
PrintField(ostrm, "initial state", header.Start());
PrintField(ostrm, "# of states", header.NumStates());
PrintField(ostrm, "# of arcs", header.NumArcs());
PrintProperties(ostrm, header.Properties());
ostrm.setf(old);
}
Expand Down
Loading