Cambridge SMT System
task.readfst.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use these files except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 // Copyright 2012 - Gonzalo Iglesias, AdriĆ  de Gispert, William Byrne
14 
15 #ifndef TASK_READFST_HPP
16 #define TASK_READFST_HPP
17 
25 namespace ucam {
26 namespace fsttools {
27 
32 template <class Data, class Arc = fst::StdArc >
34 
35  private:
38 
40  std::string previousfile_;
42  std::string fstkey_;
44  boost::scoped_ptr< fst::VectorFst<Arc> > fst_;
45 
46  public:
49  const std::string& fstkey
50  ) :
51  fstkey_ ( fstkey ),
52  fstfile_ ( rg.get<std::string> ( fstkey ) ) {
53  };
54 
56  static ReadFstTask * init ( const ucam::util::RegistryPO& rg ,
57  const std::string& fstkey
58  ) {
59  if ( rg.exists ( fstkey ) ) return new ReadFstTask ( rg, fstkey );
60  return NULL;
61  }
62 
69  bool run ( Data& d ) {
70  fst_.reset();
71  if ( fstfile_ ( d.sidx ) != "" && fstfile_ ( d.sidx ) != previousfile_ ) {
72  LINFO ( "Loading ... " << fstfile_ ( d.sidx ) << " with key=" << fstkey_ );
73  fst_.reset ( fst::VectorFstRead<Arc> ( fstfile_ ( d.sidx ) ) );
74  d.fsts[fstkey_] = fst_.get();
75  previousfile_ = fstfile_ ( d.sidx );
76  }
77  return false;
78  };
79 
81  //fst_->DeleteStates();
82  fst_.reset();
83  LINFO ("Shutdown!");
84  }
85 
86  private:
87 
88  ZDISALLOW_COPY_AND_ASSIGN ( ReadFstTask );
89 
90 };
91 
95 template <class Data>
96 static ucam::util::TaskInterface<Data> * ReadFstInit ( const
98  const std::string& fstkey ,
99  const std::string& arctype = HifstConstants::kHifstSemiringStdArc
100  ) {
101  if ( rg.exists ( fstkey ) )
102  if (arctype == HifstConstants::kHifstSemiringStdArc) return new
103  ReadFstTask<Data, fst::StdArc> ( rg, fstkey );
104  else if (arctype == HifstConstants::kHifstSemiringLexStdArc ) return
105  static_cast< ucam::util::TaskInterface<Data> * > (new
106  ReadFstTask<Data, fst::LexStdArc> ( rg, fstkey ) );
107  else {
108  LERROR ("Unknown arc type:" << arctype);
109  exit (EXIT_FAILURE);
110  }
111  return NULL;
112 }
113 
114 }
115 } // End namespaces
116 
117 #endif
class that expands a wildcard into its actual value. This is useful e.g. for filenames ranging severa...
#define LINFO(msg)
Templated (hybrid) Interface for Task classes.
std::string const kHifstSemiringLexStdArc
std::string const kHifstSemiringStdArc
static ReadFstTask * init(const ucam::util::RegistryPO &rg, const std::string &fstkey)
Static constructor. May return NULL if relevant parameters in registrypo object tell that there will ...
bool exists(const std::string &key) const
Determines whether a program option (key) has been defined by the user.
Definition: registrypo.hpp:235
bool run(Data &d)
Method inherited from TaskInterface. Loads an fst and stores a pointer into Data structure using a ke...
ReadFstTask(const ucam::util::RegistryPO &rg, const std::string &fstkey)
Constructor with RegistryPO object and fstkey to access the registry object.
Convenience class that loads an fst using a key defined in the constructor and delivers it to the dat...
#define LERROR(msg)
Definition: bleu.hpp:14