diff options
author | Matthias Huck <huck@i6.informatik.rwth-aachen.de> | 2014-05-20 00:54:08 +0400 |
---|---|---|
committer | Matthias Huck <huck@i6.informatik.rwth-aachen.de> | 2014-05-20 00:54:08 +0400 |
commit | 174047823861cb1a203ec25a9f43735b5296df39 (patch) | |
tree | cc02d0b95e6bc4eceb54c894934246f0da5a8797 /moses/PP | |
parent | aac51cec89cff2635cfef97687361383211b5a62 (diff) |
Framework for additional phrase properties in decoding.
Derive your property class from PhraseProperty. Do any expensive string
processing of the property value in there, not in the feature
implementation, and provide methods to access the information in
appropriate data formats. The property value string will thus have to
be processed only once (on loading) rather than each time the respective
phrase is applied and your feature needs to access the property value.
Diffstat (limited to 'moses/PP')
-rw-r--r-- | moses/PP/Factory.cpp | 91 | ||||
-rw-r--r-- | moses/PP/Factory.h | 33 | ||||
-rw-r--r-- | moses/PP/PhraseProperty.h | 27 | ||||
-rw-r--r-- | moses/PP/TreeStructurePhraseProperty.h | 18 |
4 files changed, 169 insertions, 0 deletions
diff --git a/moses/PP/Factory.cpp b/moses/PP/Factory.cpp new file mode 100644 index 000000000..ce34f3a5c --- /dev/null +++ b/moses/PP/Factory.cpp @@ -0,0 +1,91 @@ + +#include "moses/PP/Factory.h" +#include "util/exception.hh" +#include <iostream> +#include <vector> + +#include "moses/PP/TreeStructurePhraseProperty.h" + +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) { + P* property = new P(value); + property->ProcessValue(); + return Create(property); + } +}; + +} // namespace + + +PhrasePropertyFactory::PhrasePropertyFactory() +{ +// Feature with same key as class +#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 >()); + + MOSES_PNAME2("Tree",TreeStructurePhraseProperty); + +} + +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 +{ + 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; +} + +} // namespace Moses + diff --git a/moses/PP/Factory.h b/moses/PP/Factory.h new file mode 100644 index 000000000..226059961 --- /dev/null +++ b/moses/PP/Factory.h @@ -0,0 +1,33 @@ +#pragma once + +#include "moses/PP/PhraseProperty.h" +#include <string> + +#include <boost/shared_ptr.hpp> +#include <boost/unordered_map.hpp> + +namespace Moses +{ + +class PhrasePropertyCreator; + +class PhrasePropertyFactory +{ +public: + PhrasePropertyFactory(); + + ~PhrasePropertyFactory(); + + boost::shared_ptr<PhraseProperty> ProduceProperty(const std::string &key, const std::string &value) const; + void PrintPP() const; + +private: + void Add(const std::string &name, PhrasePropertyCreator *creator); + + typedef boost::unordered_map<std::string, boost::shared_ptr<PhrasePropertyCreator> > Registry; + + Registry m_registry; +}; + +} // namespace Moses + diff --git a/moses/PP/PhraseProperty.h b/moses/PP/PhraseProperty.h new file mode 100644 index 000000000..b977787b2 --- /dev/null +++ b/moses/PP/PhraseProperty.h @@ -0,0 +1,27 @@ +#pragma once + +#include <string> +#include <iostream> + +namespace Moses +{ + +/** base class for all phrase properties. + */ +class PhraseProperty +{ +public: + PhraseProperty(const std::string &value) : m_value(value) {}; + + virtual void ProcessValue() {}; + + const std::string &GetValueString() { return m_value; }; + +protected: + + const std::string m_value; + +}; + +} // namespace Moses + diff --git a/moses/PP/TreeStructurePhraseProperty.h b/moses/PP/TreeStructurePhraseProperty.h new file mode 100644 index 000000000..f9acc38dd --- /dev/null +++ b/moses/PP/TreeStructurePhraseProperty.h @@ -0,0 +1,18 @@ + +#pragma once + +#include "moses/PP/PhraseProperty.h" +#include <string> + +namespace Moses +{ + +class TreeStructurePhraseProperty : public PhraseProperty +{ +public: + TreeStructurePhraseProperty(const std::string &value) : PhraseProperty(value) {}; + +}; + +} // namespace Moses + |