• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // mutable-fst.h
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 //
16 // \file
17 // Expanded FST augmented with mutators - interface class definition
18 // and mutable arc iterator interface.
19 
20 #ifndef FST_LIB_MUTABLE_FST_H__
21 #define FST_LIB_MUTABLE_FST_H__
22 
23 #include <vector>
24 
25 #include "fst/lib/expanded-fst.h"
26 
27 namespace fst {
28 
29 template <class A> class MutableArcIteratorData;
30 
31 // An expanded FST plus mutators (use MutableArcIterator to modify arcs).
32 template <class A>
33 class MutableFst : public ExpandedFst<A> {
34  public:
35   typedef A Arc;
36   typedef typename A::Weight Weight;
37   typedef typename A::StateId StateId;
38 
39   virtual MutableFst<A> &operator=(const Fst<A> &fst) = 0;
40 
41   virtual void SetStart(StateId) = 0;           // Set the initial state
42   virtual void SetFinal(StateId, Weight) = 0;   // Set a state's final weight
43   virtual void SetProperties(uint64 props,
44                              uint64 mask) = 0;  // Set property bits wrt mask
45   virtual StateId AddState() = 0;               // Add a state, return its ID
46   virtual void AddArc(StateId, const A &arc) = 0;   // Add an arc to state
47 
48   virtual void DeleteStates(const vector<StateId>&) = 0;  // Delete some states
49   virtual void DeleteStates() = 0;              // Delete all states
50   virtual void DeleteArcs(StateId, size_t n) = 0;  // Delete some arcs at state
51   virtual void DeleteArcs(StateId) = 0;         // Delete all arcs at state
52 
53   // Return input label symbol table; return NULL if not specified
54   virtual const SymbolTable* InputSymbols() const = 0;
55   // Return output label symbol table; return NULL if not specified
56   virtual const SymbolTable* OutputSymbols() const = 0;
57 
58   // Return input label symbol table; return NULL if not specified
59   virtual SymbolTable* InputSymbols() = 0;
60   // Return output label symbol table; return NULL if not specified
61   virtual SymbolTable* OutputSymbols() = 0;
62 
63   // Set input label symbol table; NULL signifies not unspecified
64   virtual void SetInputSymbols(const SymbolTable* isyms) = 0;
65   // Set output label symbol table; NULL signifies not unspecified
66   virtual void SetOutputSymbols(const SymbolTable* osyms) = 0;
67 
68   // Get a copy of this MutableFst.
69   virtual MutableFst<A> *Copy() const = 0;
70   // Read an MutableFst from an input stream; return NULL on error.
Read(istream & strm,const FstReadOptions & opts)71   static MutableFst<A> *Read(istream &strm, const FstReadOptions &opts) {
72     FstReadOptions ropts(opts);
73     FstHeader hdr;
74     if (ropts.header)
75       hdr = *opts.header;
76     else {
77       if (!hdr.Read(strm, opts.source))
78         return 0;
79       ropts.header = &hdr;
80     }
81     if (!(hdr.Properties() & kMutable)) {
82       LOG(ERROR) << "MutableFst::Read: Not an MutableFst: " << ropts.source;
83       return 0;
84     }
85     FstRegister<A> *registr = FstRegister<A>::GetRegister();
86     const typename FstRegister<A>::Reader reader =
87       registr->GetReader(hdr.FstType());
88     if (!reader) {
89       LOG(ERROR) << "MutableFst::Read: Unknown FST type \"" << hdr.FstType()
90                  << "\" (arc type = \"" << A::Type()
91                  << "\"): " << ropts.source;
92       return 0;
93     }
94     Fst<A> *fst = reader(strm, ropts);
95     if (!fst) return 0;
96     return down_cast<MutableFst<A> *>(fst);
97   }
98   // Read an MutableFst from a file; returns NULL on error.
Read(const string & filename)99   static MutableFst<A> *Read(const string &filename) {
100     ifstream strm(filename.c_str());
101     if (!strm) {
102       LOG(ERROR) << "MutableFst::Read: Can't open file: " << filename;
103       return 0;
104     }
105     return Read(strm, FstReadOptions(filename));
106   }
107 
108   // For generic mutuble arc iterator construction; not normally called
109   // directly by users.
110   virtual void InitMutableArcIterator(StateId s,
111                                       MutableArcIteratorData<A> *) = 0;
112 };
113 
114 // Mutable arc iterator interface, templated on the Arc definition.
115 template <class A>
116 class MutableArcIteratorBase : public ArcIteratorBase<A> {
117  public:
118   typedef A Arc;
119   virtual void SetValue(const A &arc) = 0;  // Set current arc's contents
120 };
121 
122 template <class A>
123 struct MutableArcIteratorData {
124   MutableArcIteratorBase<A> *base;  // Specific iterator
125 };
126 
127 // Generic mutable arc iterator, templated on the FST definition
128 // - a wrapper around pointer to specific one.
129 // Here is a typical use: \code
130 //   for (MutableArcIterator<StdFst> aiter(&fst, s));
131 //        !aiter.Done();
132 //         aiter.Next()) {
133 //     StdArc arc = aiter.Value();
134 //     arc.ilabel = 7;
135 //     aiter.SetValue(arc);
136 //     ...
137 //   } \endcode
138 // This version requires function calls.
139 template <class F>
140 class MutableArcIterator : public MutableArcIteratorBase<typename F::Arc> {
141  public:
142   typedef typename F::Arc Arc;
143   typedef typename Arc::StateId StateId;
144 
MutableArcIterator(F * fst,StateId s)145   MutableArcIterator(F *fst, StateId s) {
146     fst->InitMutableArcIterator(s, &data_);
147   }
~MutableArcIterator()148   ~MutableArcIterator() { delete data_.base; }
149 
Done()150   bool Done() const { return data_.base->Done(); }
Value()151   const Arc& Value() const { return data_.base->Value(); }
Next()152   void Next() { data_.base->Next(); }
Reset()153   void Reset() { data_.base->Reset(); }
Seek(size_t a)154   void Seek(size_t a) { data_.base->Seek(a); }
SetValue(const Arc & a)155   void SetValue(const Arc &a) { data_.base->SetValue(a); }
156 
157  private:
158   MutableArcIteratorData<Arc> data_;
159   DISALLOW_EVIL_CONSTRUCTORS(MutableArcIterator);
160 };
161 
162 // A useful alias when using StdArc.
163 typedef MutableFst<StdArc> StdMutableFst;
164 
165 }  // namespace fst;
166 
167 #endif  // FST_LIB_MUTABLE_FST_H__
168