// See www.openfst.org for extensive documentation on this weighted // finite-state transducer library. // // Generic FST augmented with state count-interface class definition. #ifndef FST_EXPANDED_FST_H_ #define FST_EXPANDED_FST_H_ #include #include #include #include #include #include namespace fst { // A generic FST plus state count. template class ExpandedFst : public Fst { public: using Arc = A; using StateId = typename Arc::StateId; virtual StateId NumStates() const = 0; // State count // Get a copy of this ExpandedFst. See Fst<>::Copy() for further doc. ExpandedFst *Copy(bool safe = false) const override = 0; // Read an ExpandedFst from an input stream; return NULL on error. static ExpandedFst *Read(std::istream &strm, const FstReadOptions &opts) { FstReadOptions ropts(opts); FstHeader hdr; if (ropts.header) { hdr = *opts.header; } else { if (!hdr.Read(strm, opts.source)) return nullptr; ropts.header = &hdr; } if (!(hdr.Properties() & kExpanded)) { LOG(ERROR) << "ExpandedFst::Read: Not an ExpandedFst: " << ropts.source; return nullptr; } const auto reader = FstRegister::GetRegister()->GetReader(hdr.FstType()); if (!reader) { LOG(ERROR) << "ExpandedFst::Read: Unknown FST type \"" << hdr.FstType() << "\" (arc type = \"" << A::Type() << "\"): " << ropts.source; return nullptr; } auto *fst = reader(strm, ropts); if (!fst) return nullptr; return static_cast *>(fst); } // Read an ExpandedFst from a file; return NULL on error. // Empty filename reads from standard input. static ExpandedFst *Read(const string &filename) { if (!filename.empty()) { std::ifstream strm(filename, std::ios_base::in | std::ios_base::binary); if (!strm) { LOG(ERROR) << "ExpandedFst::Read: Can't open file: " << filename; return nullptr; } return Read(strm, FstReadOptions(filename)); } else { return Read(std::cin, FstReadOptions("standard input")); } } }; namespace internal { // ExpandedFst case - abstract methods. template inline typename Arc::Weight Final(const ExpandedFst &fst, typename Arc::StateId s) { return fst.Final(s); } template inline ssize_t NumArcs(const ExpandedFst &fst, typename Arc::StateId s) { return fst.NumArcs(s); } template inline ssize_t NumInputEpsilons(const ExpandedFst &fst, typename Arc::StateId s) { return fst.NumInputEpsilons(s); } template inline ssize_t NumOutputEpsilons(const ExpandedFst &fst, typename Arc::StateId s) { return fst.NumOutputEpsilons(s); } } // namespace internal // A useful alias when using StdArc. using StdExpandedFst = ExpandedFst; // This is a helper class template useful for attaching an ExpandedFst // interface to its implementation, handling reference counting. It // delegates to ImplToFst the handling of the Fst interface methods. template > class ImplToExpandedFst : public ImplToFst { public: using Arc = typename FST::Arc; using StateId = typename Arc::StateId; using Weight = typename Arc::Weight; using ImplToFst::operator=; StateId NumStates() const override { return GetImpl()->NumStates(); } protected: using ImplToFst::GetImpl; explicit ImplToExpandedFst(std::shared_ptr impl) : ImplToFst(impl) {} ImplToExpandedFst(const ImplToExpandedFst &fst) : ImplToFst(fst) {} ImplToExpandedFst(const ImplToExpandedFst &fst, bool safe) : ImplToFst(fst, safe) {} static Impl *Read(std::istream &strm, const FstReadOptions &opts) { return Impl::Read(strm, opts); } // Read FST implementation from a file; return NULL on error. // Empty filename reads from standard input. static Impl *Read(const string &filename) { if (!filename.empty()) { std::ifstream strm(filename, std::ios_base::in | std::ios_base::binary); if (!strm) { LOG(ERROR) << "ExpandedFst::Read: Can't open file: " << filename; return nullptr; } return Impl::Read(strm, FstReadOptions(filename)); } else { return Impl::Read(std::cin, FstReadOptions("standard input")); } } }; // Function to return the number of states in an FST, counting them // if necessary. template typename Arc::StateId CountStates(const Fst &fst) { if (fst.Properties(kExpanded, false)) { const auto *efst = static_cast *>(&fst); return efst->NumStates(); } else { typename Arc::StateId nstates = 0; for (StateIterator> siter(fst); !siter.Done(); siter.Next()) { ++nstates; } return nstates; } } // Function to return the number of arcs in an FST. template typename Arc::StateId CountArcs(const Fst &fst) { size_t narcs = 0; for (StateIterator> siter(fst); !siter.Done(); siter.Next()) { narcs += fst.NumArcs(siter.Value()); } return narcs; } } // namespace fst #endif // FST_EXPANDED_FST_H_