15 #ifndef APPLYLMTASK_HPP 16 #define APPLYLMTASK_HPP 35 template <
class Data ,
class Arc >
38 typedef typename Arc::Label Label;
39 typedef typename Arc::Weight Weight;
46 const std::string lmkey_;
47 const std::string latticeloadkey_;
48 const std::string latticestorekey_;
50 fst::VectorFst<Arc> mylmfst_;
53 typedef boost::shared_ptr<ApplyLanguageModelOnTheFlyInterfaceType> ApplyLanguageModelOnTheFlyInterfacePtrType;
54 std::vector<ApplyLanguageModelOnTheFlyInterfacePtrType> almotf_;
60 const std::string& latticeloadkey =
"lm.lattice.load",
61 const std::string& latticestorekey =
"lm.lattice.store",
62 bool deletelmscores =
false 66 , latticeloadkey_ ( latticeloadkey )
67 , latticestorekey_ ( latticestorekey )
69 , deletelmscores_ (deletelmscores)
77 if (almotf_.size())
return;
78 almotf_.resize(d.klm[lmkey_].size());
80 unordered_set<Label> epsilons;
82 epsilons.insert (
DR );
83 epsilons.insert (
OOV );
85 epsilons.insert (
SEP );
86 for (
unsigned k = 0; k < d.klm[lmkey_].size(); ++k ) {
88 "Language model " << k <<
" not available!" );
89 almotf_[k].reset(assignKenLmHandler<Arc>(rg_,lmkey_, epsilons
102 mylmfst_.DeleteStates();
104 "No language models available" ) )
return true;
105 if ( !
USER_CHECK ( d.klm.find ( lmkey_ ) != d.klm.end() ,
106 "No language models available (key not initialized) " ) )
return true;
107 if ( !
USER_CHECK ( d.fsts.find ( latticeloadkey_ ) != d.fsts.end() ,
108 " Input fst not available!" ) )
return true;
111 mylmfst_ = * (
static_cast<fst::VectorFst<Arc> *
> ( d.fsts[latticeloadkey_] ) );
112 if (deletelmscores_) {
113 LINFO (
"Delete old LM scores first" );
116 fst::Map<Arc> ( &mylmfst_,
119 LINFO (
"Input lattice loaded with key=" << latticeloadkey_ <<
", NS=" <<
120 mylmfst_.NumStates() );
121 boost::shared_ptr<fst::VectorFst<Arc> > p;
122 for (
unsigned k = 0; k < almotf_.size(); ++k ) {
124 p.reset(almotf_[k]->
run(mylmfst_));
128 LDEBUG ( mylmfst_.NumStates() );
130 d.fsts[latticestorekey_] = &mylmfst_;
143 template <
class Data ,
class Arc >
146 typedef typename Arc::Label Label;
147 typedef typename Arc::Weight Weight;
150 bool deletelmscores_;
153 const std::string lmkey_;
154 const std::string latticeloadkey_;
155 const std::string latticestorekey_;
157 fst::VectorFst<Arc> mylmfst_;
160 typedef boost::shared_ptr<ApplyLanguageModelOnTheFlyInterfaceType> ApplyLanguageModelOnTheFlyInterfacePtrType;
161 std::vector<ApplyLanguageModelOnTheFlyInterfacePtrType> almotf_;
164 unsigned srcWindowsSize_;
169 ,
const std::string& lmkey
170 ,
const std::string& latticeloadkey
171 ,
const std::string& latticestorekey
172 ,
bool deletelmscores
176 , latticeloadkey_ ( latticeloadkey )
177 , latticestorekey_ ( latticestorekey )
179 , deletelmscores_ (deletelmscores)
187 if (almotf_.size())
return;
188 almotf_.resize(d.klm[lmkey_].size());
190 unordered_set<Label> epsilons;
192 epsilons.insert (
DR );
193 epsilons.insert (
OOV );
195 epsilons.insert (
SEP );
197 ,
"Only ONE bilingual model supported. Sorry for my limitations!");
200 (assignKenLmHandlerBilingual<Arc>(rg_,lmkey_, epsilons
201 , *(d.klm[lmkey_][k])
213 mylmfst_.DeleteStates();
215 "No language models available" ) )
return true;
216 if ( !
USER_CHECK ( d.klm.find ( lmkey_ ) != d.klm.end() ,
217 "No language models available (key not initialized) " ) )
return true;
218 if ( !
USER_CHECK ( d.fsts.find ( latticeloadkey_ ) != d.fsts.end() ,
219 " Input fst not available!" ) )
return true;
221 mylmfst_ = * (
static_cast<fst::VectorFst<Arc> *
> ( d.fsts[latticeloadkey_] ) );
229 LINFO (
"Input lattice loaded with key=" << latticeloadkey_ <<
", NS=" <<
230 mylmfst_.NumStates() );
231 boost::shared_ptr<fst::VectorFst<Arc> > p;
232 for (
unsigned k = 0; k < almotf_.size(); ++k ) {
234 p.reset(almotf_[k]->
run(mylmfst_, srcWindowsSize_, d.sourceWindows) );
238 LDEBUG ( mylmfst_.NumStates() );
240 d.fsts[latticestorekey_] = &mylmfst_;
#define ZDISALLOW_COPY_AND_ASSIGN(TypeName)
std::string toString(const T &x, uint pr=2)
Converts an arbitrary type to string Converts to string integers, floats, doubles Quits execution if ...
std::string const kLmLogTen
templated Mapper that modifies weights over an FST, passing through the other values of the arc...
Templated (hybrid) Interface for Task classes.
bool exists(const std::string &source, const std::string &needle)
Convenience function to find out whether a needle exists in a text.
Templated functor that creates a weight given a float.
Wrapper to ApplyLanguageModelOnTheFly to apply different kenlm models.
Templated functor that creates a weight given a float.
#define USER_CHECK(exp, comment)
Tests whether exp is true. If not, comment is printed and program ends.
std::string const kLmLoad
std::string const kUseBilingualModelSourceSize