00001
00002 #include "moses/PP/Factory.h"
00003 #include "util/exception.hh"
00004 #include <iostream>
00005 #include <vector>
00006
00007 #include "moses/PP/CountsPhraseProperty.h"
00008 #include "moses/PP/SourceLabelsPhraseProperty.h"
00009 #include "moses/PP/TargetPreferencesPhraseProperty.h"
00010 #include "moses/PP/TreeStructurePhraseProperty.h"
00011 #include "moses/PP/SpanLengthPhraseProperty.h"
00012 #include "moses/PP/NonTermContextProperty.h"
00013 #include "moses/PP/OrientationPhraseProperty.h"
00014 #include "moses/PP/TargetConstituentBoundariesLeftPhraseProperty.h"
00015 #include "moses/PP/TargetConstituentBoundariesRightAdjacentPhraseProperty.h"
00016
00017 namespace Moses
00018 {
00019
00020 class PhrasePropertyCreator
00021 {
00022 public:
00023 virtual ~PhrasePropertyCreator() {}
00024
00025 virtual boost::shared_ptr<PhraseProperty> CreateProperty(const std::string &value) = 0;
00026
00027 protected:
00028 template <class P> boost::shared_ptr<P> Create(P *property);
00029
00030 PhrasePropertyCreator() {}
00031 };
00032
00033 template <class P> boost::shared_ptr<P> PhrasePropertyCreator::Create(P *property)
00034 {
00035 return boost::shared_ptr<P>(property);
00036 }
00037
00038 namespace
00039 {
00040
00041 template <class P> class DefaultPhrasePropertyCreator : public PhrasePropertyCreator
00042 {
00043 public:
00044 boost::shared_ptr<PhraseProperty> CreateProperty(const std::string &value) {
00045 P* property = new P();
00046 property->ProcessValue(value);
00047 return Create(property);
00048 }
00049 };
00050
00051 }
00052
00053
00054 PhrasePropertyFactory::PhrasePropertyFactory()
00055 {
00056
00057 #define MOSES_PNAME(name) Add(#name, new DefaultPhrasePropertyCreator< name >());
00058
00059 #define MOSES_PNAME2(name, type) Add(name, new DefaultPhrasePropertyCreator< type >());
00060
00061 MOSES_PNAME2("Counts", CountsPhraseProperty);
00062 MOSES_PNAME2("SourceLabels", SourceLabelsPhraseProperty);
00063 MOSES_PNAME2("TargetConstituentBoundariesLeft", TargetConstituentBoundariesLeftPhraseProperty);
00064 MOSES_PNAME2("TargetConstituentBoundariesRightAdjacent", TargetConstituentBoundariesRightAdjacentPhraseProperty);
00065 MOSES_PNAME2("TargetPreferences", TargetPreferencesPhraseProperty);
00066 MOSES_PNAME2("Tree",TreeStructurePhraseProperty);
00067 MOSES_PNAME2("SpanLength", SpanLengthPhraseProperty);
00068 MOSES_PNAME2("NonTermContext", NonTermContextProperty);
00069 MOSES_PNAME2("Orientation", OrientationPhraseProperty);
00070 }
00071
00072 PhrasePropertyFactory::~PhrasePropertyFactory()
00073 {
00074 }
00075
00076 void PhrasePropertyFactory::Add(const std::string &name, PhrasePropertyCreator *creator)
00077 {
00078 std::pair<std::string, boost::shared_ptr<PhrasePropertyCreator> > to_ins(name, boost::shared_ptr<PhrasePropertyCreator>(creator));
00079 UTIL_THROW_IF2(!m_registry.insert(to_ins).second, "Phrase property registered twice: " << name);
00080 }
00081
00082 namespace
00083 {
00084 class UnknownPhrasePropertyException : public util::Exception {};
00085 }
00086
00087 boost::shared_ptr<PhraseProperty> PhrasePropertyFactory::ProduceProperty(const std::string &key, const std::string &value) const
00088 {
00089 Registry::const_iterator i = m_registry.find(key);
00090 UTIL_THROW_IF(i == m_registry.end(), UnknownPhrasePropertyException, "Phrase property is not registered: " << key);
00091 return i->second->CreateProperty(value);
00092 }
00093
00094 void PhrasePropertyFactory::PrintPP() const
00095 {
00096 std::cerr << "Registered phrase properties:" << std::endl;
00097 Registry::const_iterator iter;
00098 for (iter = m_registry.begin(); iter != m_registry.end(); ++iter) {
00099 const std::string &ppName = iter->first;
00100 std::cerr << ppName << " ";
00101 }
00102 std::cerr << std::endl;
00103 }
00104
00105 }
00106