mosesdecoder/moses/FF/Factory.cpp

265 lines
7.6 KiB
C++
Raw Normal View History

2013-07-19 01:54:52 +04:00
#include "moses/FF/Factory.h"
#include "moses/StaticData.h"
#include "moses/TranslationModel/PhraseDictionaryTreeAdaptor.h"
#include "moses/TranslationModel/RuleTable/PhraseDictionaryOnDisk.h"
#include "moses/TranslationModel/PhraseDictionaryMemory.h"
#include "moses/TranslationModel/PhraseDictionaryMultiModel.h"
#include "moses/TranslationModel/PhraseDictionaryMultiModelCounts.h"
#include "moses/TranslationModel/RuleTable/PhraseDictionaryALSuffixArray.h"
#include "moses/TranslationModel/PhraseDictionaryDynSuffixArray.h"
#include "moses/TranslationModel/PhraseDictionaryScope3.h"
#include "moses/TranslationModel/PhraseDictionaryTransliteration.h"
2013-07-19 01:54:52 +04:00
2013-08-30 19:28:28 +04:00
#include "moses/FF/LexicalReordering/LexicalReordering.h"
2013-07-19 01:54:52 +04:00
#include "moses/FF/BleuScoreFeature.h"
#include "moses/FF/TargetWordInsertionFeature.h"
#include "moses/FF/SourceWordDeletionFeature.h"
#include "moses/FF/GlobalLexicalModel.h"
#include "moses/FF/GlobalLexicalModelUnlimited.h"
#include "moses/FF/UnknownWordPenaltyProducer.h"
#include "moses/FF/WordTranslationFeature.h"
#include "moses/FF/TargetBigramFeature.h"
#include "moses/FF/TargetNgramFeature.h"
#include "moses/FF/PhraseBoundaryFeature.h"
#include "moses/FF/PhrasePairFeature.h"
#include "moses/FF/PhraseLengthFeature.h"
#include "moses/FF/DistortionScoreProducer.h"
2013-09-14 20:22:43 +04:00
#include "moses/FF/SparseHieroReorderingFeature.h"
2013-07-19 01:54:52 +04:00
#include "moses/FF/WordPenaltyProducer.h"
#include "moses/FF/InputFeature.h"
#include "moses/FF/PhrasePenalty.h"
#include "moses/FF/OSM-Feature/OpSequenceModel.h"
#include "moses/FF/ControlRecombination.h"
2013-09-12 11:48:17 +04:00
#include "moses/FF/ExternalFeature.h"
#include "moses/FF/ConstrainedDecoding.h"
2013-11-29 12:27:43 +04:00
#include "moses/FF/CoveredReferenceFeature.h"
#include "moses/FF/TreeStructureFeature.h"
#include "moses/FF/SoftMatchingFeature.h"
#include "moses/FF/HyperParameterAsWeight.h"
2014-04-06 19:57:54 +04:00
#include "moses/FF/SetSourcePhrase.h"
2014-04-08 20:19:26 +04:00
#include "CountNonTerms.h"
2014-04-11 00:34:34 +04:00
#include "ReferenceComparison.h"
2014-05-16 15:47:43 +04:00
#include "RuleScope.h"
#include "MaxSpanFreeNonTermSource.h"
2014-05-20 23:02:49 +04:00
#include "NieceTerminal.h"
2013-09-12 11:48:17 +04:00
2013-09-01 19:58:23 +04:00
#include "moses/FF/SkeletonStatelessFF.h"
#include "moses/FF/SkeletonStatefulFF.h"
2013-09-25 01:26:51 +04:00
#include "moses/LM/SkeletonLM.h"
2013-09-25 19:57:01 +04:00
#include "moses/TranslationModel/SkeletonPT.h"
2013-07-19 01:54:52 +04:00
#ifdef HAVE_CMPH
#include "moses/TranslationModel/CompactPT/PhraseDictionaryCompact.h"
#endif
#ifdef PT_UG
2014-02-08 22:22:33 +04:00
#include "moses/TranslationModel/UG/mmsapt.h"
#endif
2013-07-19 01:54:52 +04:00
#include "moses/LM/Ken.h"
#ifdef LM_IRST
#include "moses/LM/IRST.h"
#endif
#ifdef LM_SRI
#include "moses/LM/SRI.h"
#endif
#ifdef LM_MAXENT_SRI
#include "moses/LM/MaxEntSRI.h"
#endif
2013-07-19 01:54:52 +04:00
#ifdef LM_RAND
#include "moses/LM/Rand.h"
#endif
#ifdef HAVE_SYNLM
#include "moses/SyntacticLanguageModel.h"
#endif
2013-10-29 03:21:02 +04:00
#ifdef LM_NEURAL
#include "moses/LM/NeuralLMWrapper.h"
2013-10-29 02:11:37 +04:00
#endif
2013-11-05 18:37:56 +04:00
#ifdef LM_DALM
2013-11-21 23:19:34 +04:00
#include "moses/LM/DALMWrapper.h"
2013-11-05 18:37:56 +04:00
#endif
2013-07-19 01:54:52 +04:00
#include "util/exception.hh"
#include <vector>
2013-07-19 16:56:02 +04:00
namespace Moses
{
2013-07-19 01:54:52 +04:00
2013-07-19 16:56:02 +04:00
class FeatureFactory
{
public:
virtual ~FeatureFactory() {}
2013-07-19 01:54:52 +04:00
2013-07-19 16:56:02 +04:00
virtual void Create(const std::string &line) = 0;
2013-07-19 01:54:52 +04:00
2013-07-19 16:56:02 +04:00
protected:
template <class F> static void DefaultSetup(F *feature);
2013-07-19 01:54:52 +04:00
2013-07-19 16:56:02 +04:00
FeatureFactory() {}
2013-07-19 01:54:52 +04:00
};
2013-07-19 16:56:02 +04:00
template <class F> void FeatureFactory::DefaultSetup(F *feature)
{
2013-07-19 01:54:52 +04:00
StaticData &static_data = StaticData::InstanceNonConst();
2013-11-11 19:32:58 +04:00
const string &featureName = feature->GetScoreProducerDescription();
std::vector<float> weights = static_data.GetParameter()->GetWeights(featureName);
if (feature->IsTuneable() || weights.size()) {
2013-07-19 16:56:02 +04:00
// if it's tuneable, ini file MUST have weights
// even it it's not tuneable, people can still set the weights in the ini file
static_data.SetWeights(feature, weights);
} else if (feature->GetNumScoreComponents() > 0) {
std::vector<float> defaultWeights = feature->DefaultWeights();
static_data.SetWeights(feature, defaultWeights);
}
2013-07-19 01:54:52 +04:00
}
2013-07-19 16:56:02 +04:00
namespace
{
2013-07-19 01:54:52 +04:00
2013-07-19 16:56:02 +04:00
template <class F> class DefaultFeatureFactory : public FeatureFactory
{
public:
void Create(const std::string &line) {
DefaultSetup(new F(line));
}
2013-07-19 01:54:52 +04:00
};
2013-07-19 16:56:02 +04:00
class KenFactory : public FeatureFactory
{
public:
void Create(const std::string &line) {
DefaultSetup(ConstructKenLM(line));
}
2013-07-19 01:54:52 +04:00
};
} // namespace
2013-07-19 16:56:02 +04:00
FeatureRegistry::FeatureRegistry()
{
2013-07-19 01:54:52 +04:00
// Feature with same name as class
#define MOSES_FNAME(name) Add(#name, new DefaultFeatureFactory< name >());
// Feature with different name than class.
#define MOSES_FNAME2(name, type) Add(name, new DefaultFeatureFactory< type >());
MOSES_FNAME(GlobalLexicalModel);
//MOSES_FNAME(GlobalLexicalModelUnlimited); This was commented out in the original
MOSES_FNAME(SourceWordDeletionFeature);
MOSES_FNAME(TargetWordInsertionFeature);
MOSES_FNAME(PhraseBoundaryFeature);
MOSES_FNAME(PhraseLengthFeature);
MOSES_FNAME(WordTranslationFeature);
MOSES_FNAME(TargetBigramFeature);
MOSES_FNAME(TargetNgramFeature);
MOSES_FNAME(PhrasePairFeature);
MOSES_FNAME(LexicalReordering);
MOSES_FNAME2("Generation", GenerationDictionary);
MOSES_FNAME(BleuScoreFeature);
MOSES_FNAME2("Distortion", DistortionScoreProducer);
MOSES_FNAME2("WordPenalty", WordPenaltyProducer);
MOSES_FNAME(InputFeature);
MOSES_FNAME2("PhraseDictionaryBinary", PhraseDictionaryTreeAdaptor);
MOSES_FNAME(PhraseDictionaryOnDisk);
MOSES_FNAME(PhraseDictionaryMemory);
MOSES_FNAME(PhraseDictionaryScope3);
2013-07-19 01:54:52 +04:00
MOSES_FNAME(PhraseDictionaryMultiModel);
MOSES_FNAME(PhraseDictionaryMultiModelCounts);
MOSES_FNAME(PhraseDictionaryALSuffixArray);
MOSES_FNAME(PhraseDictionaryDynSuffixArray);
MOSES_FNAME(PhraseDictionaryTransliteration);
2013-07-19 01:54:52 +04:00
MOSES_FNAME(OpSequenceModel);
MOSES_FNAME(PhrasePenalty);
MOSES_FNAME2("UnknownWordPenalty", UnknownWordPenaltyProducer);
MOSES_FNAME(ControlRecombination);
MOSES_FNAME(ConstrainedDecoding);
2013-11-29 12:27:43 +04:00
MOSES_FNAME(CoveredReferenceFeature);
MOSES_FNAME(ExternalFeature);
MOSES_FNAME(TreeStructureFeature);
MOSES_FNAME(SoftMatchingFeature);
MOSES_FNAME(HyperParameterAsWeight);
2014-04-06 19:57:54 +04:00
MOSES_FNAME(SetSourcePhrase);
2014-04-08 20:19:26 +04:00
MOSES_FNAME(CountNonTerms);
2014-04-11 00:34:34 +04:00
MOSES_FNAME(ReferenceComparison);
2014-05-16 15:47:43 +04:00
MOSES_FNAME(RuleScope);
MOSES_FNAME(MaxSpanFreeNonTermSource);
2014-05-20 23:02:49 +04:00
MOSES_FNAME(NieceTerminal);
MOSES_FNAME(SparseHieroReorderingFeature);
2013-09-01 19:58:23 +04:00
MOSES_FNAME(SkeletonStatelessFF);
MOSES_FNAME(SkeletonStatefulFF);
2013-09-25 01:26:51 +04:00
MOSES_FNAME(SkeletonLM);
2013-09-25 19:57:01 +04:00
MOSES_FNAME(SkeletonPT);
#ifdef HAVE_CMPH
MOSES_FNAME(PhraseDictionaryCompact);
#endif
#ifdef PT_UG
MOSES_FNAME(Mmsapt);
#endif
2013-07-19 01:54:52 +04:00
#ifdef HAVE_SYNLM
MOSES_FNAME(SyntacticLanguageModel);
#endif
#ifdef LM_IRST
MOSES_FNAME2("IRSTLM", LanguageModelIRST);
#endif
#ifdef LM_SRI
MOSES_FNAME2("SRILM", LanguageModelSRI);
#endif
#ifdef LM_MAXENT_SRI
MOSES_FNAME2("MaxEntLM", LanguageModelMaxEntSRI);
#endif
2013-07-19 01:54:52 +04:00
#ifdef LM_RAND
MOSES_FNAME2("RANDLM", LanguageModelRandLM);
2013-07-19 01:54:52 +04:00
#endif
2013-10-29 03:21:02 +04:00
#ifdef LM_NEURAL
MOSES_FNAME2("NeuralLM", NeuralLMWrapper);
2013-10-29 02:11:37 +04:00
#endif
2013-11-05 18:37:56 +04:00
#ifdef LM_DALM
MOSES_FNAME2("DALM", LanguageModelDALM);
#endif
2013-10-29 02:11:37 +04:00
2013-07-19 01:54:52 +04:00
Add("KENLM", new KenFactory());
}
FeatureRegistry::~FeatureRegistry()
{
}
2013-07-19 01:54:52 +04:00
2013-07-19 16:56:02 +04:00
void FeatureRegistry::Add(const std::string &name, FeatureFactory *factory)
{
2013-07-19 01:54:52 +04:00
std::pair<std::string, boost::shared_ptr<FeatureFactory> > to_ins(name, boost::shared_ptr<FeatureFactory>(factory));
2013-11-23 00:27:46 +04:00
UTIL_THROW_IF2(!registry_.insert(to_ins).second, "Duplicate feature name " << name);
2013-07-19 01:54:52 +04:00
}
2013-07-19 16:56:02 +04:00
namespace
{
2013-07-19 01:54:52 +04:00
class UnknownFeatureException : public util::Exception {};
}
2013-07-19 16:56:02 +04:00
void FeatureRegistry::Construct(const std::string &name, const std::string &line)
{
2013-07-19 01:54:52 +04:00
Map::iterator i = registry_.find(name);
UTIL_THROW_IF(i == registry_.end(), UnknownFeatureException, "Feature name " << name << " is not registered.");
i->second->Create(line);
}
void FeatureRegistry::PrintFF() const
{
std::cerr << "Available feature functions:" << std::endl;
Map::const_iterator iter;
for (iter = registry_.begin(); iter != registry_.end(); ++iter) {
const string &ffName = iter->first;
std::cerr << ffName << " ";
}
std::cerr << std::endl;
}
2013-07-19 01:54:52 +04:00
} // namespace Moses