Cambridge SMT System
main-run.createssgrammar.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 MAIN_RUN_CREATESSGRAMMAR_HPP
16 #define MAIN_RUN_CREATESSGRAMMAR_HPP
17 
25 namespace ucam {
26 namespace hifst {
27 
28 using boost::asio::ip::tcp;
29 const int max_length = 1024;
30 typedef boost::shared_ptr<tcp::socket> socket_ptr;
31 
36 template <class Data = HifstTaskData>
39 
40  //Private variables are shown here. Private methods go after public methods
41  private:
48 
50  const ucam::util::RegistryPO& rg_;
51  public:
57  &rg ) :
58  fastforwardread_ ( new ucam::util::iszfstream ( rg.get<std::string>
59  ( HifstConstants::kSourceLoad ) ) ),
60  rg_ ( rg ) {
61  };
62 
67  bool run ( Data& d ) {
68  boost::scoped_ptr < LoadGrammar> grammartask ( new LoadGrammar ( rg_ ) );
69  grammartask->appendTask
71  ( new PrePro ( rg_ ) )
72  ( new PatternsToInstances ( rg_ ) )
73  ( new SentenceSpecificGrammar ( rg_ ) )
74  ;
75  bool finished = false;
77  !ir->done ();
78  ir->next () ) {
79  d.sidx = ir->get ();
80  finished = fastforwardread_ ( d.sidx ,
81  &d.originalsentence ); //Move to whichever next sentence and read
82  FORCELINFO ( "=====Translate sentence " << d.sidx << ":" <<
83  d.originalsentence );
84  grammartask->chainrun ( d ); //Run translation!
85  if ( finished ) break;
86  }
87  return false;
88  };
89 
90  inline bool operator() () {
91  Data d;
92  return run ( d );
93  }
94 
95  private:
96 
97  DISALLOW_COPY_AND_ASSIGN ( SingleThreadedCreateSentenceSpecificGrammarTask );
98 
99 };
100 
105 template <class Data = HifstTaskData>
108 
109  private:
116 
118  const ucam::util::RegistryPO& rg_;
119  unsigned threadcount_;
120  public:
126  &rg ) :
127  fastforwardread_ ( new ucam::util::iszfstream ( rg.get<std::string>
128  ( HifstConstants::kSourceLoad ) ) ),
129  threadcount_ ( rg.get<unsigned> ( HifstConstants::kNThreads) ),
130  rg_ ( rg ) {
131  };
132 
137  bool run ( Data& original_data ) {
138  boost::scoped_ptr < LoadGrammar >grammartask ( new LoadGrammar ( rg_ ) );
139  grammartask->appendTask
141  ;
142  //Load grammar and language model
143  grammartask->chainrun ( original_data );
144  std::vector < boost::shared_ptr<std::string> >translations;
145  {
146  ucam::util::TrivialThreadPool tp ( threadcount_ );
147  bool finished = false;
150  !ir->done();
151  ir->next() ) {
152  Data *d = new Data; //( original_data ); // reset.
153  d->grammar = original_data.grammar;
154  d->sidx = ir->get();
155  d->wm = original_data.wm;
156  finished = fastforwardread_ ( d->sidx ,
157  & ( d->originalsentence ) ); //Move to whichever next sentence and read
158  FORCELINFO ( "=====Translate sentence " << d->sidx << ":" <<
159  d->originalsentence );
160  PrePro *p = new PrePro ( rg_ );
161  p->appendTask
162  ( new PatternsToInstances ( rg_ ) )
163  ( new SentenceSpecificGrammar ( rg_ ) )
164  ;
165  tp ( ucam::util::TaskFunctor<Data> ( p, d ) );
166  if ( finished ) break;
167  }
168  }
169  return false;
170  };
171 
173  inline bool operator() () {
174  Data d;
175  return run ( d );
176  }
177 
178  private:
179 
180  DISALLOW_COPY_AND_ASSIGN ( MultiThreadedCreateSentenceSpecificGrammarTask );
181 
182 };
183 
184 }
185 } // end namespaces
186 
187 #endif
bool run(Data &d)
Reads an input sentence, tokenizes and integer-maps.
Reads text file, performs tokenization and integer-mapping.
Definition: task.prepro.hpp:33
static LoadWordMapTask * init(const ucam::util::RegistryPO &rg, const std::string &key, bool reverse=false)
Static constructor, will return NULL if there is no need for word-mapping.
Converts patterns to instanced patterns.
const std::string kPreproWordmapLoad
#define FORCELINFO(msg)
boost::scoped_ptr< NumberRangeInterface< unsigned > > IntRangePtr
Definition: range.hpp:214
boost::shared_ptr< tcp::socket > socket_ptr
#define IntRangeFactory
Definition: range.hpp:213
Trivial implementation of a threadpool based on boost::asio methods When initiated, creates a threadpool of n threads (n <= number of cpus). Jobs should be submitted with the templated operator(). When the object is deleted it will wait for all threads to finish.
MultiThreadedCreateSentenceSpecificGrammarTask(const ucam::util::RegistryPO &rg)
Constructor.
Loads wordmap in constructor and delivers pointer to data object during run time. ...
Task class that loads a grammar into memory.
Templated (hybrid) Interface for Task classes.
Simple functor that accepts an interface and pointer to the data object in which it will have to run ...
TaskInterface & appendTask(TaskInterface *t)
Appends a task class. If there is no task, append here, otherwise delegate in next task...
const std::string kNThreads
This class uses instantiated patterns to analyze the grammar and deliver two hashes providing candida...
const std::string kSourceLoad
SingleThreadedCreateSentenceSpecificGrammarTask(const ucam::util::RegistryPO &rg)
Constructor.
const std::string kRangeOne
Definition: range.hpp:26
Definition: bleu.hpp:14
bool run(Data &original_data)
Reads an input sentence, tokenizes and integer-maps.