FST  openfst-1.7.2
OpenFst Library
expanded-fst.h
Go to the documentation of this file.
1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
3 //
4 // Generic FST augmented with state count-interface class definition.
5 
6 #ifndef FST_EXPANDED_FST_H_
7 #define FST_EXPANDED_FST_H_
8 
9 #include <sys/types.h>
10 #include <istream>
11 #include <memory>
12 #include <string>
13 
14 #include <fst/log.h>
15 #include <fstream>
16 
17 #include <fst/fst.h>
18 
19 
20 namespace fst {
21 
22 // A generic FST plus state count.
23 template <class A>
24 class ExpandedFst : public Fst<A> {
25  public:
26  using Arc = A;
27  using StateId = typename Arc::StateId;
28 
29  virtual StateId NumStates() const = 0; // State count
30 
31  // Get a copy of this ExpandedFst. See Fst<>::Copy() for further doc.
32  ExpandedFst<Arc> *Copy(bool safe = false) const override = 0;
33 
34  // Read an ExpandedFst from an input stream; return NULL on error.
35  static ExpandedFst<Arc> *Read(std::istream &strm,
36  const FstReadOptions &opts) {
37  FstReadOptions ropts(opts);
38  FstHeader hdr;
39  if (ropts.header) {
40  hdr = *opts.header;
41  } else {
42  if (!hdr.Read(strm, opts.source)) return nullptr;
43  ropts.header = &hdr;
44  }
45  if (!(hdr.Properties() & kExpanded)) {
46  LOG(ERROR) << "ExpandedFst::Read: Not an ExpandedFst: " << ropts.source;
47  return nullptr;
48  }
49  const auto reader =
50  FstRegister<Arc>::GetRegister()->GetReader(hdr.FstType());
51  if (!reader) {
52  LOG(ERROR) << "ExpandedFst::Read: Unknown FST type \"" << hdr.FstType()
53  << "\" (arc type = \"" << A::Type() << "\"): " << ropts.source;
54  return nullptr;
55  }
56  auto *fst = reader(strm, ropts);
57  if (!fst) return nullptr;
58  return static_cast<ExpandedFst<Arc> *>(fst);
59  }
60 
61  // Read an ExpandedFst from a file; return NULL on error.
62  // Empty filename reads from standard input.
63  static ExpandedFst<Arc> *Read(const string &filename) {
64  if (!filename.empty()) {
65  std::ifstream strm(filename,
66  std::ios_base::in | std::ios_base::binary);
67  if (!strm) {
68  LOG(ERROR) << "ExpandedFst::Read: Can't open file: " << filename;
69  return nullptr;
70  }
71  return Read(strm, FstReadOptions(filename));
72  } else {
73  return Read(std::cin, FstReadOptions("standard input"));
74  }
75  }
76 };
77 
78 namespace internal {
79 
80 // ExpandedFst<A> case - abstract methods.
81 template <class Arc>
82 inline typename Arc::Weight Final(const ExpandedFst<Arc> &fst,
83  typename Arc::StateId s) {
84  return fst.Final(s);
85 }
86 
87 template <class Arc>
88 inline ssize_t NumArcs(const ExpandedFst<Arc> &fst, typename Arc::StateId s) {
89  return fst.NumArcs(s);
90 }
91 
92 template <class Arc>
93 inline ssize_t NumInputEpsilons(const ExpandedFst<Arc> &fst,
94  typename Arc::StateId s) {
95  return fst.NumInputEpsilons(s);
96 }
97 
98 template <class Arc>
99 inline ssize_t NumOutputEpsilons(const ExpandedFst<Arc> &fst,
100  typename Arc::StateId s) {
101  return fst.NumOutputEpsilons(s);
102 }
103 
104 } // namespace internal
105 
106 // A useful alias when using StdArc.
108 
109 // This is a helper class template useful for attaching an ExpandedFst
110 // interface to its implementation, handling reference counting. It
111 // delegates to ImplToFst the handling of the Fst interface methods.
112 template <class Impl, class FST = ExpandedFst<typename Impl::Arc>>
113 class ImplToExpandedFst : public ImplToFst<Impl, FST> {
114  public:
115  using Arc = typename FST::Arc;
116  using StateId = typename Arc::StateId;
117  using Weight = typename Arc::Weight;
118 
119  StateId NumStates() const override { return GetImpl()->NumStates(); }
120 
121  protected:
123 
124  explicit ImplToExpandedFst(std::shared_ptr<Impl> impl)
125  : ImplToFst<Impl, FST>(impl) {}
126 
128  : ImplToFst<Impl, FST>(fst, safe) {}
129 
130  static Impl *Read(std::istream &strm, const FstReadOptions &opts) {
131  return Impl::Read(strm, opts);
132  }
133 
134  // Read FST implementation from a file; return NULL on error.
135  // Empty filename reads from standard input.
136  static Impl *Read(const string &filename) {
137  if (!filename.empty()) {
138  std::ifstream strm(filename,
139  std::ios_base::in | std::ios_base::binary);
140  if (!strm) {
141  LOG(ERROR) << "ExpandedFst::Read: Can't open file: " << filename;
142  return nullptr;
143  }
144  return Impl::Read(strm, FstReadOptions(filename));
145  } else {
146  return Impl::Read(std::cin, FstReadOptions("standard input"));
147  }
148  }
149 };
150 
151 // Function to return the number of states in an FST, counting them
152 // if necessary.
153 template <class Arc>
154 typename Arc::StateId CountStates(const Fst<Arc> &fst) {
155  if (fst.Properties(kExpanded, false)) {
156  const auto *efst = static_cast<const ExpandedFst<Arc> *>(&fst);
157  return efst->NumStates();
158  } else {
159  typename Arc::StateId nstates = 0;
160  for (StateIterator<Fst<Arc>> siter(fst); !siter.Done(); siter.Next()) {
161  ++nstates;
162  }
163  return nstates;
164  }
165 }
166 
167 // Function to return the number of arcs in an FST.
168 template <class Arc>
169 typename Arc::StateId CountArcs(const Fst<Arc> &fst) {
170  size_t narcs = 0;
171  for (StateIterator<Fst<Arc>> siter(fst); !siter.Done(); siter.Next()) {
172  narcs += fst.NumArcs(siter.Value());
173  }
174  return narcs;
175 }
176 
177 } // namespace fst
178 
179 #endif // FST_EXPANDED_FST_H_
static Impl * Read(std::istream &strm, const FstReadOptions &opts)
Definition: expanded-fst.h:130
const FstHeader * header
Definition: fst.h:58
virtual size_t NumArcs(StateId) const =0
typename Arc::Weight Weight
Definition: fst.h:190
#define LOG(type)
Definition: log.h:48
virtual Weight Final(StateId) const =0
constexpr uint64 kExpanded
Definition: properties.h:27
virtual uint64 Properties(uint64 mask, bool test) const =0
virtual size_t NumInputEpsilons(StateId) const =0
ImplToExpandedFst(const ImplToExpandedFst< Impl, FST > &fst, bool safe)
Definition: expanded-fst.h:127
bool Read(std::istream &strm, const string &source, bool rewind=false)
Definition: fst.cc:58
ImplToExpandedFst(std::shared_ptr< Impl > impl)
Definition: expanded-fst.h:124
StateId NumStates() const override
Definition: expanded-fst.h:119
string source
Definition: fst.h:57
typename Arc::StateId StateId
Definition: expanded-fst.h:27
static ExpandedFst< Arc > * Read(std::istream &strm, const FstReadOptions &opts)
Definition: expanded-fst.h:35
const string & FstType() const
Definition: fst.h:119
A Arc
Definition: fst.h:188
Arc::StateId CountArcs(const Fst< Arc > &fst)
Definition: expanded-fst.h:169
Arc::StateId CountStates(const Fst< Arc > &fst)
Definition: expanded-fst.h:154
static ExpandedFst< Arc > * Read(const string &filename)
Definition: expanded-fst.h:63
static Impl * Read(const string &filename)
Definition: expanded-fst.h:136
typename Arc::StateId StateId
Definition: fst.h:189
ExpandedFst< Arc > * Copy(bool safe=false) const override=0
virtual StateId NumStates() const =0
virtual size_t NumOutputEpsilons(StateId) const =0
uint64 Properties() const
Definition: fst.h:127