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/CompactPT/PhraseDictionaryCompact.h"
|
|
|
|
#include "moses/TranslationModel/PhraseDictionaryMultiModel.h"
|
|
|
|
#include "moses/TranslationModel/PhraseDictionaryMultiModelCounts.h"
|
|
|
|
#include "moses/TranslationModel/RuleTable/PhraseDictionaryALSuffixArray.h"
|
|
|
|
#include "moses/TranslationModel/PhraseDictionaryDynSuffixArray.h"
|
|
|
|
|
|
|
|
#include "moses/LexicalReordering.h"
|
|
|
|
|
|
|
|
#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"
|
|
|
|
#include "moses/FF/WordPenaltyProducer.h"
|
|
|
|
#include "moses/FF/InputFeature.h"
|
|
|
|
#include "moses/FF/PhrasePenalty.h"
|
|
|
|
#include "moses/FF/OSM-Feature/OpSequenceModel.h"
|
|
|
|
|
|
|
|
#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_RAND
|
|
|
|
#include "moses/LM/Rand.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_SYNLM
|
|
|
|
#include "moses/SyntacticLanguageModel.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#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-07-19 14:35:50 +04:00
|
|
|
std::vector<float> &weights = static_data.GetParameter()->GetWeights(feature->GetScoreProducerDescription());
|
|
|
|
|
|
|
|
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
|
2013-07-19 14:35:50 +04:00
|
|
|
static_data.SetWeights(feature, weights);
|
2013-07-19 16:56:02 +04:00
|
|
|
} else {
|
2013-07-19 14:35:50 +04:00
|
|
|
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(PhraseDictionaryCompact);
|
|
|
|
MOSES_FNAME(PhraseDictionaryMultiModel);
|
|
|
|
MOSES_FNAME(PhraseDictionaryMultiModelCounts);
|
|
|
|
MOSES_FNAME(PhraseDictionaryALSuffixArray);
|
|
|
|
MOSES_FNAME(PhraseDictionaryDynSuffixArray);
|
|
|
|
MOSES_FNAME(OpSequenceModel);
|
|
|
|
MOSES_FNAME(PhrasePenalty);
|
2013-07-19 16:24:05 +04:00
|
|
|
MOSES_FNAME2("UnknownWordPenalty", UnknownWordPenaltyProducer);
|
|
|
|
|
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_RAND
|
2013-07-20 03:19:04 +04:00
|
|
|
MOSES_FNAME2("RANDLM", LanguageModelRandLM);
|
2013-07-19 01:54:52 +04:00
|
|
|
#endif
|
|
|
|
Add("KENLM", new KenFactory());
|
|
|
|
}
|
|
|
|
|
|
|
|
FeatureRegistry::~FeatureRegistry() {}
|
|
|
|
|
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));
|
|
|
|
UTIL_THROW_IF(!registry_.insert(to_ins).second, util::Exception, "Duplicate feature name " << name);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Moses
|