// expanded-fst.h // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // Copyright 2005-2010 Google, Inc. // Author: riley@google.com (Michael Riley) // // \file // Generic FST augmented with state count - interface class definition. // #ifndef FST_LIB_EXPANDED_FST_H__ #define FST_LIB_EXPANDED_FST_H__ #include #include #include namespace fst { // A generic FST plus state count. template class ExpandedFst : public Fst { public: typedef A Arc; typedef typename A::StateId StateId; virtual StateId NumStates() const = 0; // State count // Get a copy of this ExpandedFst. See Fst<>::Copy() for further doc. virtual ExpandedFst *Copy(bool safe = false) const = 0; // Read an ExpandedFst from an input stream; return NULL on error. static ExpandedFst *Read(istream &strm, const FstReadOptions &opts) { FstReadOptions ropts(opts); FstHeader hdr; if (ropts.header) hdr = *opts.header; else { if (!hdr.Read(strm, opts.source)) return 0; ropts.header = &hdr; } if (!(hdr.Properties() & kExpanded)) { LOG(ERROR) << "ExpandedFst::Read: Not an ExpandedFst: " << ropts.source; return 0; } FstRegister *registr = FstRegister::GetRegister(); const typename FstRegister::Reader reader = registr->GetReader(hdr.FstType()); if (!reader) { LOG(ERROR) << "ExpandedFst::Read: Unknown FST type \"" << hdr.FstType() << "\" (arc type = \"" << A::Type() << "\"): " << ropts.source; return 0; } Fst *fst = reader(strm, ropts); if (!fst) return 0; 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()) { ifstream strm(filename.c_str(), ifstream::in | ifstream::binary); if (!strm) { LOG(ERROR) << "ExpandedFst::Read: Can't open file: " << filename; return 0; } return Read(strm, FstReadOptions(filename)); } else { return Read(cin, FstReadOptions("standard input")); } } }; namespace internal { // ExpandedFst case - abstract methods. template inline typename A::Weight Final(const ExpandedFst &fst, typename A::StateId s) { return fst.Final(s); } template inline ssize_t NumArcs(const ExpandedFst &fst, typename A::StateId s) { return fst.NumArcs(s); } template inline ssize_t NumInputEpsilons(const ExpandedFst &fst, typename A::StateId s) { return fst.NumInputEpsilons(s); } template inline ssize_t NumOutputEpsilons(const ExpandedFst &fst, typename A::StateId s) { return fst.NumOutputEpsilons(s); } } // namespace internal // A useful alias when using StdArc. typedef ExpandedFst StdExpandedFst; // 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 I, class F = ExpandedFst > class ImplToExpandedFst : public ImplToFst { public: typedef typename I::Arc Arc; typedef typename Arc::Weight Weight; typedef typename Arc::StateId StateId; using ImplToFst::GetImpl; virtual StateId NumStates() const { return GetImpl()->NumStates(); } protected: ImplToExpandedFst() : ImplToFst() {} ImplToExpandedFst(I *impl) : ImplToFst(impl) {} ImplToExpandedFst(const ImplToExpandedFst &fst) : ImplToFst(fst) {} ImplToExpandedFst(const ImplToExpandedFst &fst, bool safe) : ImplToFst(fst, safe) {} // Read FST implementation from a file; return NULL on error. // Empty filename reads from standard input. static I *Read(const string &filename) { if (!filename.empty()) { ifstream strm(filename.c_str(), ifstream::in | ifstream::binary); if (!strm) { LOG(ERROR) << "ExpandedFst::Read: Can't open file: " << filename; return 0; } return I::Read(strm, FstReadOptions(filename)); } else { return I::Read(cin, FstReadOptions("standard input")); } } private: // Disallow ImplToExpandedFst &operator=(const ImplToExpandedFst &fst); ImplToExpandedFst &operator=(const Fst &fst) { FSTERROR() << "ImplToExpandedFst: Assignment operator disallowed"; GetImpl()->SetProperties(kError, kError); return *this; } }; // 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 ExpandedFst *efst = static_cast *>(&fst); return efst->NumStates(); } else { typename Arc::StateId nstates = 0; for (StateIterator< Fst > siter(fst); !siter.Done(); siter.Next()) ++nstates; return nstates; } } } // namespace fst #endif // FST_LIB_EXPANDED_FST_H__