Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef FST_SCRIPT_FST_CLASS_H_
00018 #define FST_SCRIPT_FST_CLASS_H_
00019
00020 #include <string>
00021
00022 #include <fst/fst.h>
00023 #include <fst/mutable-fst.h>
00024 #include <fst/vector-fst.h>
00025 #include <iostream>
00026 #include <fstream>
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 namespace fst {
00037 namespace script {
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 class FstClassBase {
00048 public:
00049 virtual const string &ArcType() const = 0;
00050 virtual const string &FstType() const = 0;
00051 virtual const string &WeightType() const = 0;
00052 virtual const SymbolTable *InputSymbols() const = 0;
00053 virtual const SymbolTable *OutputSymbols() const = 0;
00054 virtual void Write(const string& fname) const = 0;
00055 virtual uint64 Properties(uint64 mask, bool test) const = 0;
00056 virtual ~FstClassBase() { }
00057 };
00058
00059 class FstClassImplBase : public FstClassBase {
00060 public:
00061 virtual FstClassImplBase *Copy() = 0;
00062 virtual void SetInputSymbols(SymbolTable *is) = 0;
00063 virtual void SetOutputSymbols(SymbolTable *is) = 0;
00064 virtual ~FstClassImplBase() { }
00065 };
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 template<class Arc>
00077 class FstClassImpl : public FstClassImplBase {
00078 public:
00079 explicit FstClassImpl(Fst<Arc> *impl,
00080 bool should_own = false) :
00081 impl_(should_own ? impl : impl->Copy()) { }
00082
00083 virtual const string &ArcType() const {
00084 return Arc::Type();
00085 }
00086
00087 virtual const string &FstType() const {
00088 return impl_->Type();
00089 }
00090
00091 virtual const string &WeightType() const {
00092 return Arc::Weight::Type();
00093 }
00094
00095 virtual const SymbolTable *InputSymbols() const {
00096 return impl_->InputSymbols();
00097 }
00098
00099 virtual const SymbolTable *OutputSymbols() const {
00100 return impl_->OutputSymbols();
00101 }
00102
00103
00104 virtual void SetInputSymbols(SymbolTable *is) {
00105 static_cast<MutableFst<Arc> *>(impl_)->SetInputSymbols(is);
00106 }
00107
00108
00109 virtual void SetOutputSymbols(SymbolTable *os) {
00110 static_cast<MutableFst<Arc> *>(impl_)->SetOutputSymbols(os);
00111 }
00112
00113 virtual void Write(const string &fname) const {
00114 impl_->Write(fname);
00115 }
00116
00117 virtual uint64 Properties(uint64 mask, bool test) const {
00118 return impl_->Properties(mask, test);
00119 }
00120
00121 virtual ~FstClassImpl() { delete impl_; }
00122
00123 Fst<Arc> *GetImpl() { return impl_; }
00124
00125 virtual FstClassImpl *Copy() {
00126 return new FstClassImpl<Arc>(impl_);
00127 }
00128
00129 private:
00130 Fst<Arc> *impl_;
00131 };
00132
00133
00134
00135
00136
00137 class MutableFstClass;
00138
00139 class FstClass : public FstClassBase {
00140 public:
00141 template<class Arc>
00142 static FstClass *Read(istream &stream,
00143 const FstReadOptions &opts) {
00144 CHECK(opts.header);
00145 const FstHeader &hdr = *opts.header;
00146
00147 if (hdr.Properties() & kMutable) {
00148 return ReadTypedFst<MutableFstClass, MutableFst<Arc> >(stream, opts);
00149 } else {
00150 return ReadTypedFst<FstClass, Fst<Arc> >(stream, opts);
00151 }
00152 }
00153
00154 template<class Arc>
00155 explicit FstClass(Fst<Arc> *fst) : impl_(new FstClassImpl<Arc>(fst)) { }
00156
00157 explicit FstClass(const FstClass &other) : impl_(other.impl_->Copy()) { }
00158
00159 static FstClass *Read(const string &fname);
00160
00161 virtual const string &ArcType() const {
00162 return impl_->ArcType();
00163 }
00164
00165 virtual const string& FstType() const {
00166 return impl_->FstType();
00167 }
00168
00169 virtual const SymbolTable *InputSymbols() const {
00170 return impl_->InputSymbols();
00171 }
00172
00173 virtual const SymbolTable *OutputSymbols() const {
00174 return impl_->OutputSymbols();
00175 }
00176
00177 virtual const string& WeightType() const {
00178 return impl_->WeightType();
00179 }
00180
00181 virtual void Write(const string &fname) const {
00182 impl_->Write(fname);
00183 }
00184
00185 virtual uint64 Properties(uint64 mask, bool test) const {
00186 return impl_->Properties(mask, test);
00187 }
00188
00189 template<class Arc>
00190 const Fst<Arc> *GetFst() const {
00191 if (Arc::Type() != ArcType()) {
00192 return NULL;
00193 } else {
00194 FstClassImpl<Arc> *typed_impl = static_cast<FstClassImpl<Arc> *>(impl_);
00195 return typed_impl->GetImpl();
00196 }
00197 }
00198
00199 virtual ~FstClass() { delete impl_; }
00200
00201
00202 template<class Arc>
00203 static FstClassImplBase *Convert(const FstClass &other) {
00204 LOG(ERROR) << "Doesn't make sense to convert any class to type FstClass.";
00205 return 0;
00206 }
00207
00208 template<class Arc>
00209 static FstClassImplBase *Create() {
00210 LOG(ERROR) << "Doesn't make sense to create an FstClass with a "
00211 << "particular arc type.";
00212 return 0;
00213 }
00214 protected:
00215 explicit FstClass(FstClassImplBase *impl) : impl_(impl) { }
00216
00217
00218
00219
00220 template<class FstClassT, class UnderlyingT>
00221 static FstClassT* ReadTypedFst(istream &stream,
00222 const FstReadOptions &opts) {
00223 UnderlyingT *u = UnderlyingT::Read(stream, opts);
00224 if (!u) {
00225 return 0;
00226 } else {
00227 FstClassT *r = new FstClassT(u);
00228 delete u;
00229 return r;
00230 }
00231 }
00232
00233 FstClassImplBase *GetImpl() { return impl_; }
00234 private:
00235 FstClassImplBase *impl_;
00236 };
00237
00238
00239
00240
00241
00242 class MutableFstClass : public FstClass {
00243 public:
00244 template<class Arc>
00245 explicit MutableFstClass(MutableFst<Arc> *fst) :
00246 FstClass(fst) { }
00247
00248 template<class Arc>
00249 MutableFst<Arc> *GetMutableFst() {
00250 Fst<Arc> *fst = const_cast<Fst<Arc> *>(this->GetFst<Arc>());
00251 MutableFst<Arc> *mfst = static_cast<MutableFst<Arc> *>(fst);
00252
00253 return mfst;
00254 }
00255
00256 template<class Arc>
00257 static MutableFstClass *Read(istream &stream,
00258 const FstReadOptions &opts) {
00259 CHECK(opts.header);
00260 const FstHeader &hdr = *opts.header;
00261
00262 if (hdr.Properties() & kMutable) {
00263 MutableFst<Arc> *f = MutableFst<Arc>::Read(stream, opts);
00264 MutableFstClass *retval = new MutableFstClass(f);
00265 delete f;
00266 return retval;
00267 } else {
00268 LOG(ERROR) << "Attempt to read a MutableFstClass from a file that doesn't"
00269 << " contain a mutable FST type.";
00270 return 0;
00271 }
00272 }
00273
00274 virtual void SetInputSymbols(SymbolTable *is) {
00275 GetImpl()->SetInputSymbols(is);
00276 }
00277
00278 virtual void SetOutputSymbols(SymbolTable *os) {
00279 GetImpl()->SetOutputSymbols(os);
00280 }
00281
00282
00283 template<class Arc>
00284 static FstClassImplBase *Convert(const FstClass &other) {
00285 LOG(ERROR) << "Doesn't make sense to convert any class to type "
00286 << "MutableFstClass.";
00287 return 0;
00288 }
00289
00290 template<class Arc>
00291 static FstClassImplBase *Create() {
00292 LOG(ERROR) << "Doesn't make sense to create a MutableFstClass with a "
00293 << "particular arc type.";
00294 return 0;
00295 }
00296
00297 protected:
00298 explicit MutableFstClass(FstClassImplBase *impl) : FstClass(impl) { }
00299 };
00300
00301
00302 class VectorFstClass : public MutableFstClass {
00303 public:
00304 explicit VectorFstClass(const FstClass &other);
00305 explicit VectorFstClass(const string &arc_type);
00306
00307 template<class Arc>
00308 explicit VectorFstClass(VectorFst<Arc> *fst) :
00309 MutableFstClass(fst) { }
00310
00311 template<class Arc>
00312 static VectorFstClass *Read(istream &stream,
00313 const FstReadOptions &opts) {
00314 VectorFst<Arc> *vfst = VectorFst<Arc>::Read(stream, opts);
00315 if (!vfst) {
00316 return 0;
00317 } else {
00318 VectorFstClass *retval = new VectorFstClass(vfst);
00319 delete vfst;
00320 return retval;
00321 }
00322 }
00323
00324
00325 template<class Arc>
00326 static FstClassImplBase *Convert(const FstClass &other) {
00327 return new FstClassImpl<Arc>(new VectorFst<Arc>(
00328 *other.GetFst<Arc>()), true);
00329 }
00330
00331 template<class Arc>
00332 static FstClassImplBase *Create() {
00333 return new FstClassImpl<Arc>(new VectorFst<Arc>(), true);
00334 }
00335 };
00336
00337 }
00338 }
00339
00340
00341 #endif /// FST_SCRIPT_FST_CLASS_H_
00342