Merge /home/hieu/workspace/github/mosesdecoder into hieu_opt_input

This commit is contained in:
Hieu Hoang 2013-07-19 13:56:45 +01:00
commit d7ab163b0e
2 changed files with 55 additions and 43 deletions

View File

@ -51,63 +51,70 @@
#include <vector>
namespace Moses {
namespace Moses
{
class FeatureFactory {
public:
virtual ~FeatureFactory() {}
class FeatureFactory
{
public:
virtual ~FeatureFactory() {}
virtual void Create(const std::string &line) = 0;
virtual void Create(const std::string &line) = 0;
protected:
template <class F> static void DefaultSetup(F *feature);
protected:
template <class F> static void DefaultSetup(F *feature);
FeatureFactory() {}
FeatureFactory() {}
};
template <class F> void FeatureFactory::DefaultSetup(F *feature) {
template <class F> void FeatureFactory::DefaultSetup(F *feature)
{
StaticData &static_data = StaticData::InstanceNonConst();
std::vector<float> &weights = static_data.GetParameter()->GetWeights(feature->GetScoreProducerDescription());
if (feature->IsTuneable() || weights.size()) {
// 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
// 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 {
} else {
std::vector<float> defaultWeights = feature->DefaultWeights();
static_data.SetWeights(feature, defaultWeights);
}
}
namespace {
namespace
{
template <class F> class DefaultFeatureFactory : public FeatureFactory {
public:
void Create(const std::string &line) {
DefaultSetup(new F(line));
}
template <class F> class DefaultFeatureFactory : public FeatureFactory
{
public:
void Create(const std::string &line) {
DefaultSetup(new F(line));
}
};
class KenFactory : public FeatureFactory {
public:
void Create(const std::string &line) {
DefaultSetup(ConstructKenLM(line));
}
class KenFactory : public FeatureFactory
{
public:
void Create(const std::string &line) {
DefaultSetup(ConstructKenLM(line));
}
};
#ifdef LM_RAND
class RandFactory : public FeatureFactory {
public:
void Create(const std::string &line) {
DefaultSetup(NewRandLM());
}
class RandFactory : public FeatureFactory
{
public:
void Create(const std::string &line) {
DefaultSetup(NewRandLM());
}
};
#endif
} // namespace
FeatureRegistry::FeatureRegistry() {
FeatureRegistry::FeatureRegistry()
{
// Feature with same name as class
#define MOSES_FNAME(name) Add(#name, new DefaultFeatureFactory< name >());
// Feature with different name than class.
@ -157,16 +164,19 @@ FeatureRegistry::FeatureRegistry() {
FeatureRegistry::~FeatureRegistry() {}
void FeatureRegistry::Add(const std::string &name, FeatureFactory *factory) {
void FeatureRegistry::Add(const std::string &name, FeatureFactory *factory)
{
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);
}
namespace {
namespace
{
class UnknownFeatureException : public util::Exception {};
}
void FeatureRegistry::Construct(const std::string &name, const std::string &line) {
void FeatureRegistry::Construct(const std::string &name, const std::string &line)
{
Map::iterator i = registry_.find(name);
UTIL_THROW_IF(i == registry_.end(), UnknownFeatureException, "Feature name " << name << " is not registered.");
i->second->Create(line);

View File

@ -5,24 +5,26 @@
#include <boost/shared_ptr.hpp>
#include <boost/unordered_map.hpp>
namespace Moses {
namespace Moses
{
class FeatureFactory;
class FeatureRegistry {
public:
FeatureRegistry();
class FeatureRegistry
{
public:
FeatureRegistry();
~FeatureRegistry();
~FeatureRegistry();
void Construct(const std::string &name, const std::string &line);
void Construct(const std::string &name, const std::string &line);
private:
void Add(const std::string &name, FeatureFactory *factory);
private:
void Add(const std::string &name, FeatureFactory *factory);
typedef boost::unordered_map<std::string, boost::shared_ptr<FeatureFactory> > Map;
typedef boost::unordered_map<std::string, boost::shared_ptr<FeatureFactory> > Map;
Map registry_;
Map registry_;
};
} // namespace Moses