Cambridge SMT System
task.writefst.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_WRITEFST_HPP
16 #define TASK_WRITEFST_HPP
17 
25 namespace ucam {
26 namespace fsttools {
27 
35 template <class Data, class Arc = fst::StdArc >
37  typedef typename Arc::Label Label;
38  typedef typename Arc::Weight Weight;
39  typedef std::vector<pair<Label, Label> > VectorPair;
40  private:
42  std::string fstkey_;
43  std::string readfstkey_;
46 
47  public:
50  , const std::string& fstkey
51  , const std::string& readfstkey = ""
52  )
53  : fstkey_ ( fstkey )
54  , readfstkey_ (readfstkey != "" ? readfstkey : fstkey)
55  , fstfile_ ( rg.get<std::string> ( fstkey ) ) {
56  };
57 
58  inline static WriteFstTask * init ( const ucam::util::RegistryPO& rg
59  , const std::string& fstkey
60  , const std::string& readfstkey = ""
61  ) {
62  if ( rg.exists ( fstkey ) )
63  return new WriteFstTask ( rg, fstkey, readfstkey );
64  return NULL;
65  };
66 
75  inline bool run ( Data& d ) {
76  if ( fstfile_ ( d.sidx ) == "" ) return false;
77  if ( d.fsts.find ( readfstkey_ ) == d.fsts.end() ) {
78  LERROR ( "fst with key=" << readfstkey_ << " does not exist!" );
79  exit ( EXIT_FAILURE );
80  }
81 
82  FORCELINFO ("Writing lattice " << d.sidx << " to ... "
83  << fstfile_( d.sidx ) );
84 
85  using namespace fst;
86  FstWrite<Arc>
87  ( * ( static_cast< Fst<Arc> *>
88  ( d.fsts[readfstkey_] ) ), fstfile_ ( d.sidx ) );
89  std::string parenskey = readfstkey_ + ".parens";
90  if ( d.fsts.find ( parenskey ) != d.fsts.end() ) {
91  WriteLabelPairs (fstfile_ ( d.sidx ) + ".parens",
92  * (static_cast< VectorPair * > ( d.fsts[parenskey] ) ) );
93  }
94  return false;
95  };
96 
97  private:
99 };
100 
101 }} // end namespaces
102 
103 #endif
#define ZDISALLOW_COPY_AND_ASSIGN(TypeName)
static WriteFstTask * init(const ucam::util::RegistryPO &rg, const std::string &fstkey, const std::string &readfstkey="")
class that expands a wildcard into its actual value. This is useful e.g. for filenames ranging severa...
Definition: fstio.hpp:27
#define FORCELINFO(msg)
Convenience class that inherits Taskinterface behaviour and writes an fst to [file] using a key defin...
Templated (hybrid) Interface for Task classes.
bool run(Data &d)
Method inherited from TaskInterface. Stores fst to [file]. The fst is accessed via data object using ...
bool exists(const std::string &key) const
Determines whether a program option (key) has been defined by the user.
Definition: registrypo.hpp:235
WriteFstTask(const ucam::util::RegistryPO &rg, const std::string &fstkey, const std::string &readfstkey="")
Constructor with RegistryPO object.
#define LERROR(msg)
Definition: bleu.hpp:14