14a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project// expanded-fst.h 24a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project// 34a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project// Licensed under the Apache License, Version 2.0 (the "License"); 44a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project// you may not use this file except in compliance with the License. 54a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project// You may obtain a copy of the License at 64a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project// 74a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project// http://www.apache.org/licenses/LICENSE-2.0 84a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project// 94a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project// Unless required by applicable law or agreed to in writing, software 104a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project// distributed under the License is distributed on an "AS IS" BASIS, 114a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project// See the License for the specific language governing permissions and 134a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project// limitations under the License. 144a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project// 154a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project// 164a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project// \file 174a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project// Generic FST augmented with state count - interface class definition. 184a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project 194a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project#ifndef FST_LIB_EXPANDED_FST_H__ 204a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project#define FST_LIB_EXPANDED_FST_H__ 214a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project 224a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project#include "fst/lib/fst.h" 234a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project 244a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Projectnamespace fst { 254a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project 264a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project// A generic FST plus state count. 274a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Projecttemplate <class A> 284a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Projectclass ExpandedFst : public Fst<A> { 294a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project public: 304a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project typedef A Arc; 314a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project typedef typename A::StateId StateId; 324a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project 334a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project virtual StateId NumStates() const = 0; // State count 344a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project 354a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project // Get a copy of this ExpandedFst. 364a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project virtual ExpandedFst<A> *Copy() const = 0; 374a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project // Read an ExpandedFst from an input stream; return NULL on error. 384a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project static ExpandedFst<A> *Read(istream &strm, const FstReadOptions &opts) { 394a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project FstReadOptions ropts(opts); 404a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project FstHeader hdr; 414a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project if (ropts.header) 424a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project hdr = *opts.header; 434a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project else { 444a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project if (!hdr.Read(strm, opts.source)) 454a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project return 0; 464a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project ropts.header = &hdr; 474a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project } 484a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project if (!(hdr.Properties() & kExpanded)) { 494a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project LOG(ERROR) << "ExpandedFst::Read: Not an ExpandedFst: " << ropts.source; 504a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project return 0; 514a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project } 524a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project FstRegister<A> *registr = FstRegister<A>::GetRegister(); 534a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project const typename FstRegister<A>::Reader reader = 544a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project registr->GetReader(hdr.FstType()); 554a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project if (!reader) { 564a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project LOG(ERROR) << "ExpandedFst::Read: Unknown FST type \"" << hdr.FstType() 574a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project << "\" (arc type = \"" << A::Type() 584a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project << "\"): " << ropts.source; 594a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project return 0; 604a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project } 614a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project Fst<A> *fst = reader(strm, ropts); 624a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project if (!fst) return 0; 634a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project return down_cast<ExpandedFst<A> *>(fst); 644a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project } 654a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project // Read an ExpandedFst from a file; return NULL on error. 664a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project static ExpandedFst<A> *Read(const string &filename) { 674a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project ifstream strm(filename.c_str()); 684a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project if (!strm) { 694a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project LOG(ERROR) << "ExpandedFst::Read: Can't open file: " << filename; 704a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project return 0; 714a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project } 724a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project return Read(strm, FstReadOptions(filename)); 734a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project } 744a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project}; 754a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project 764a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project// A useful alias when using StdArc. 774a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Projecttypedef ExpandedFst<StdArc> StdExpandedFst; 784a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project 794a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project// Function to return the number of states in an FST, counting them 804a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project// if necessary. 814a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Projecttemplate <class Arc> 824a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Projecttypename Arc::StateId CountStates(const Fst<Arc> &fst) { 834a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project if (fst.Properties(kExpanded, false)) { 844a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project const ExpandedFst<Arc> *efst = down_cast<const ExpandedFst<Arc> *>(&fst); 854a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project return efst->NumStates(); 864a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project } else { 874a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project typename Arc::StateId nstates = 0; 884a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project for (StateIterator< Fst<Arc> > siter(fst); !siter.Done(); siter.Next()) 894a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project ++nstates; 904a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project return nstates; 914a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project } 924a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project} 934a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project 944a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project} // FST_LIB_FST_H__ 954a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project 964a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project#endif // FST_LIB_EXPANDED_FST_H__ 97