FST  openfst-1.8.2
OpenFst Library
print-impl.h
Go to the documentation of this file.
1 // Copyright 2005-2020 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 // Stand-alone class to print out binary FSTs in the AT&T format, a helper
19 // class for fstprint.cc.
20 
21 #ifndef FST_SCRIPT_PRINT_IMPL_H_
22 #define FST_SCRIPT_PRINT_IMPL_H_
23 
24 #include <ostream>
25 #include <sstream>
26 #include <string>
27 
28 #include <fst/fstlib.h>
29 #include <fst/util.h>
30 
31 namespace fst {
32 
33 // Print a binary FST in textual format (helper class for fstprint.cc).
34 // WARNING: Stand-alone use of this class not recommended, most code should
35 // read/write using the binary format which is much more efficient.
36 template <class Arc>
37 class FstPrinter {
38  public:
39  using StateId = typename Arc::StateId;
40  using Label = typename Arc::Label;
41  using Weight = typename Arc::Weight;
42 
43  explicit FstPrinter(const Fst<Arc> &fst, const SymbolTable *isyms,
44  const SymbolTable *osyms, const SymbolTable *ssyms,
45  bool accept, bool show_weight_one,
46  const std::string &field_separator,
47  const std::string &missing_symbol = "")
48  : fst_(fst),
49  isyms_(isyms),
50  osyms_(osyms),
51  ssyms_(ssyms),
52  accept_(accept && (fst.Properties(kAcceptor, true) == kAcceptor)),
53  show_weight_one_(show_weight_one),
54  sep_(field_separator),
55  missing_symbol_(missing_symbol) {}
56 
57  // Prints FST to an output stream.
58  void Print(std::ostream &ostrm, const std::string &dest) {
59  dest_ = dest;
60  const auto start = fst_.Start();
61  if (start == kNoStateId) return;
62  // Initial state first.
63  PrintState(ostrm, start);
64  for (StateIterator<Fst<Arc>> siter(fst_); !siter.Done(); siter.Next()) {
65  const auto s = siter.Value();
66  if (s != start) PrintState(ostrm, s);
67  }
68  }
69 
70  private:
71  std::string FormatId(StateId id, const SymbolTable *syms) const {
72  if (syms) {
73  std::string symbol = syms->Find(id);
74  if (symbol.empty()) {
75  if (missing_symbol_.empty()) {
76  FSTERROR() << "FstPrinter: Integer " << id
77  << " is not mapped to any textual symbol"
78  << ", symbol table = " << syms->Name()
79  << ", destination = " << dest_;
80  symbol = "?";
81  } else {
82  symbol = missing_symbol_;
83  }
84  }
85  return symbol;
86  } else {
87  return std::to_string(id);
88  }
89  }
90 
91  std::string FormatStateId(StateId s) const { return FormatId(s, ssyms_); }
92 
93  std::string FormatILabel(Label l) const { return FormatId(l, isyms_); }
94 
95  std::string FormatOLabel(Label l) const { return FormatId(l, osyms_); }
96 
97  void PrintState(std::ostream &ostrm, StateId s) const {
98  bool output = false;
99  for (ArcIterator<Fst<Arc>> aiter(fst_, s); !aiter.Done(); aiter.Next()) {
100  const auto &arc = aiter.Value();
101  ostrm << FormatStateId(s) << sep_ << FormatStateId(arc.nextstate)
102  << sep_ << FormatILabel(arc.ilabel);
103  if (!accept_) {
104  ostrm << sep_ << FormatOLabel(arc.olabel);
105  }
106  if (show_weight_one_ || arc.weight != Weight::One()) {
107  ostrm << sep_ << arc.weight;
108  }
109  ostrm << "\n";
110  output = true;
111  }
112  const auto weight = fst_.Final(s);
113  if (weight != Weight::Zero() || !output) {
114  ostrm << FormatStateId(s);
115  if (show_weight_one_ || weight != Weight::One()) {
116  ostrm << sep_ << weight;
117  }
118  ostrm << "\n";
119  }
120  }
121 
122  const Fst<Arc> &fst_;
123  const SymbolTable *isyms_; // ilabel symbol table.
124  const SymbolTable *osyms_; // olabel symbol table.
125  const SymbolTable *ssyms_; // slabel symbol table.
126  bool accept_; // Print as acceptor when possible?
127  std::string dest_; // Text FST destination name.
128  bool show_weight_one_; // Print weights equal to Weight::One()?
129  std::string sep_; // Separator character between fields.
130  std::string missing_symbol_; // Symbol to print when lookup fails (default
131  // "" means raise error).
132 
133  FstPrinter(const FstPrinter &) = delete;
134  FstPrinter &operator=(const FstPrinter &) = delete;
135 };
136 
137 } // namespace fst
138 
139 #endif // FST_SCRIPT_PRINT_IMPL_H_
const std::string & Name() const
Definition: symbol-table.h:462
typename Arc::StateId StateId
Definition: print-impl.h:39
virtual Weight Final(StateId) const =0
constexpr int kNoStateId
Definition: fst.h:202
void Print(std::ostream &ostrm, const std::string &dest)
Definition: print-impl.h:58
#define FSTERROR()
Definition: util.h:53
virtual StateId Start() const =0
typename Arc::Label Label
Definition: print-impl.h:40
typename Arc::Weight Weight
Definition: print-impl.h:41
std::string Find(int64_t key) const
Definition: symbol-table.h:446
FstPrinter(const Fst< Arc > &fst, const SymbolTable *isyms, const SymbolTable *osyms, const SymbolTable *ssyms, bool accept, bool show_weight_one, const std::string &field_separator, const std::string &missing_symbol="")
Definition: print-impl.h:43
constexpr uint64_t kAcceptor
Definition: properties.h:63