2014-05-20 00:54:08 +04:00
|
|
|
|
|
|
|
#include "moses/PP/Factory.h"
|
|
|
|
#include "util/exception.hh"
|
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
|
|
|
|
2014-06-11 23:02:31 +04:00
|
|
|
#include "moses/PP/CountsPhraseProperty.h"
|
2014-06-12 00:08:22 +04:00
|
|
|
#include "moses/PP/SourceLabelsPhraseProperty.h"
|
2016-01-11 23:14:28 +03:00
|
|
|
#include "moses/PP/TargetPreferencesPhraseProperty.h"
|
2014-05-20 00:54:08 +04:00
|
|
|
#include "moses/PP/TreeStructurePhraseProperty.h"
|
2014-06-04 19:52:57 +04:00
|
|
|
#include "moses/PP/SpanLengthPhraseProperty.h"
|
2014-07-11 19:26:48 +04:00
|
|
|
#include "moses/PP/NonTermContextProperty.h"
|
2014-08-08 00:02:51 +04:00
|
|
|
#include "moses/PP/OrientationPhraseProperty.h"
|
2016-02-12 20:46:57 +03:00
|
|
|
#include "moses/PP/TargetConstituentBoundariesLeftPhraseProperty.h"
|
|
|
|
#include "moses/PP/TargetConstituentBoundariesRightAdjacentPhraseProperty.h"
|
2014-05-20 00:54:08 +04:00
|
|
|
|
|
|
|
namespace Moses
|
|
|
|
{
|
|
|
|
|
|
|
|
class PhrasePropertyCreator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~PhrasePropertyCreator() {}
|
|
|
|
|
|
|
|
virtual boost::shared_ptr<PhraseProperty> CreateProperty(const std::string &value) = 0;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
template <class P> boost::shared_ptr<P> Create(P *property);
|
|
|
|
|
|
|
|
PhrasePropertyCreator() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class P> boost::shared_ptr<P> PhrasePropertyCreator::Create(P *property)
|
|
|
|
{
|
|
|
|
return boost::shared_ptr<P>(property);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
template <class P> class DefaultPhrasePropertyCreator : public PhrasePropertyCreator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
boost::shared_ptr<PhraseProperty> CreateProperty(const std::string &value) {
|
2014-06-13 19:37:13 +04:00
|
|
|
P* property = new P();
|
|
|
|
property->ProcessValue(value);
|
2014-05-20 00:54:08 +04:00
|
|
|
return Create(property);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
|
|
PhrasePropertyFactory::PhrasePropertyFactory()
|
|
|
|
{
|
2014-05-20 01:09:27 +04:00
|
|
|
// Property with same key as class
|
2014-05-20 00:54:08 +04:00
|
|
|
#define MOSES_PNAME(name) Add(#name, new DefaultPhrasePropertyCreator< name >());
|
|
|
|
// Properties with different key than class.
|
|
|
|
#define MOSES_PNAME2(name, type) Add(name, new DefaultPhrasePropertyCreator< type >());
|
|
|
|
|
2014-06-11 23:02:31 +04:00
|
|
|
MOSES_PNAME2("Counts", CountsPhraseProperty);
|
2014-06-12 00:08:22 +04:00
|
|
|
MOSES_PNAME2("SourceLabels", SourceLabelsPhraseProperty);
|
2016-02-12 20:46:57 +03:00
|
|
|
MOSES_PNAME2("TargetConstituentBoundariesLeft", TargetConstituentBoundariesLeftPhraseProperty);
|
|
|
|
MOSES_PNAME2("TargetConstituentBoundariesRightAdjacent", TargetConstituentBoundariesRightAdjacentPhraseProperty);
|
2016-01-11 23:14:28 +03:00
|
|
|
MOSES_PNAME2("TargetPreferences", TargetPreferencesPhraseProperty);
|
2014-05-20 00:54:08 +04:00
|
|
|
MOSES_PNAME2("Tree",TreeStructurePhraseProperty);
|
2014-06-04 19:52:57 +04:00
|
|
|
MOSES_PNAME2("SpanLength", SpanLengthPhraseProperty);
|
2014-07-11 19:26:48 +04:00
|
|
|
MOSES_PNAME2("NonTermContext", NonTermContextProperty);
|
2014-08-08 00:02:51 +04:00
|
|
|
MOSES_PNAME2("Orientation", OrientationPhraseProperty);
|
2014-05-20 00:54:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
PhrasePropertyFactory::~PhrasePropertyFactory()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhrasePropertyFactory::Add(const std::string &name, PhrasePropertyCreator *creator)
|
|
|
|
{
|
|
|
|
std::pair<std::string, boost::shared_ptr<PhrasePropertyCreator> > to_ins(name, boost::shared_ptr<PhrasePropertyCreator>(creator));
|
|
|
|
UTIL_THROW_IF2(!m_registry.insert(to_ins).second, "Phrase property registered twice: " << name);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
class UnknownPhrasePropertyException : public util::Exception {};
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::shared_ptr<PhraseProperty> PhrasePropertyFactory::ProduceProperty(const std::string &key, const std::string &value) const
|
|
|
|
{
|
|
|
|
Registry::const_iterator i = m_registry.find(key);
|
|
|
|
UTIL_THROW_IF(i == m_registry.end(), UnknownPhrasePropertyException, "Phrase property is not registered: " << key);
|
|
|
|
return i->second->CreateProperty(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhrasePropertyFactory::PrintPP() const
|
|
|
|
{
|
2014-06-08 11:44:59 +04:00
|
|
|
std::cerr << "Registered phrase properties:" << std::endl;
|
|
|
|
Registry::const_iterator iter;
|
|
|
|
for (iter = m_registry.begin(); iter != m_registry.end(); ++iter) {
|
|
|
|
const std::string &ppName = iter->first;
|
|
|
|
std::cerr << ppName << " ";
|
|
|
|
}
|
|
|
|
std::cerr << std::endl;
|
2014-05-20 00:54:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Moses
|
|
|
|
|