diff --git a/moses-chart-cmd/IOWrapper.cpp b/moses-chart-cmd/IOWrapper.cpp index b9cf2420c..db12dcfd6 100644 --- a/moses-chart-cmd/IOWrapper.cpp +++ b/moses-chart-cmd/IOWrapper.cpp @@ -48,6 +48,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "moses/FF/StatefulFeatureFunction.h" #include "moses/FF/StatelessFeatureFunction.h" #include "moses/FF/TreeStructureFeature.h" +#include "moses/PP/TreeStructurePhraseProperty.h" #include "util/exception.hh" using namespace std; @@ -410,17 +411,15 @@ void IOWrapper::OutputTreeFragmentsTranslationOptions(std::ostream &out, Applica if (hypo != NULL) { OutputTranslationOption(out, applicationContext, hypo, sentence, translationId); - const std::string key = "Tree"; - std::string value; - bool hasProperty; const TargetPhrase &currTarPhr = hypo->GetCurrTargetPhrase(); - currTarPhr.GetProperty(key, value, hasProperty); + boost::shared_ptr property; out << " ||| "; - if (hasProperty) - out << " " << value; - else + if (currTarPhr.GetProperty("Tree", property)) { + out << " " << property->GetValueString(); + } else { out << " " << "noTreeInfo"; + } out << std::endl; } @@ -439,17 +438,15 @@ void IOWrapper::OutputTreeFragmentsTranslationOptions(std::ostream &out, Applica if (applied != NULL) { OutputTranslationOption(out, applicationContext, applied, sentence, translationId); - const std::string key = "Tree"; - std::string value; - bool hasProperty; const TargetPhrase &currTarPhr = *static_cast(applied->GetNote().vp); - currTarPhr.GetProperty(key, value, hasProperty); + boost::shared_ptr property; out << " ||| "; - if (hasProperty) - out << " " << value; - else + if (currTarPhr.GetProperty("Tree", property)) { + out << " " << property->GetValueString(); + } else { out << " " << "noTreeInfo"; + } out << std::endl; } diff --git a/moses/FF/TreeStructureFeature.cpp b/moses/FF/TreeStructureFeature.cpp index aa879fe0e..25490a470 100644 --- a/moses/FF/TreeStructureFeature.cpp +++ b/moses/FF/TreeStructureFeature.cpp @@ -6,6 +6,7 @@ #include "moses/TargetPhrase.h" #include #include +#include "moses/PP/TreeStructurePhraseProperty.h" using namespace std; @@ -270,10 +271,9 @@ FFState* TreeStructureFeature::EvaluateChart(const ChartHypothesis& cur_hypo , int featureID /* used to index the state in the previous hypotheses */ , ScoreComponentCollection* accumulator) const { - std::string tree; - bool found = 0; - cur_hypo.GetCurrTargetPhrase().GetProperty("Tree", tree, found); - if (found) { + boost::shared_ptr property; + if (cur_hypo.GetCurrTargetPhrase().GetProperty("Tree", property)) { + const std::string &tree = property->GetValueString(); TreePointer mytree (new InternalTree(tree)); if (m_labelset) { diff --git a/moses/Jamfile b/moses/Jamfile index 74fb9894a..cc65f56ea 100644 --- a/moses/Jamfile +++ b/moses/Jamfile @@ -65,6 +65,7 @@ lib moses : FF/*.cpp FF/OSM-Feature/*.cpp FF/LexicalReordering/*.cpp + PP/*.cpp : #exceptions ThreadPool.cpp SyntacticLanguageModel.cpp 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 +#include + +#include "moses/PP/TreeStructurePhraseProperty.h" + +namespace Moses +{ + +class PhrasePropertyCreator +{ +public: + virtual ~PhrasePropertyCreator() {} + + virtual boost::shared_ptr CreateProperty(const std::string &value) = 0; + +protected: + template boost::shared_ptr

Create(P *property); + + PhrasePropertyCreator() {} +}; + +template boost::shared_ptr

PhrasePropertyCreator::Create(P *property) +{ + return boost::shared_ptr

(property); +} + +namespace +{ + +template class DefaultPhrasePropertyCreator : public PhrasePropertyCreator +{ +public: + boost::shared_ptr 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 > to_ins(name, boost::shared_ptr(creator)); + UTIL_THROW_IF2(!m_registry.insert(to_ins).second, "Phrase property registered twice: " << name); +} + +namespace +{ +class UnknownPhrasePropertyException : public util::Exception {}; +} + +boost::shared_ptr 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 + +#include +#include + +namespace Moses +{ + +class PhrasePropertyCreator; + +class PhrasePropertyFactory +{ +public: + PhrasePropertyFactory(); + + ~PhrasePropertyFactory(); + + boost::shared_ptr 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 > 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 +#include + +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 + +namespace Moses +{ + +class TreeStructurePhraseProperty : public PhraseProperty +{ +public: + TreeStructurePhraseProperty(const std::string &value) : PhraseProperty(value) {}; + +}; + +} // namespace Moses + diff --git a/moses/StaticData.h b/moses/StaticData.h index 1f106a13d..882ac912e 100644 --- a/moses/StaticData.h +++ b/moses/StaticData.h @@ -42,6 +42,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA #include "SentenceStats.h" #include "ScoreComponentCollection.h" #include "moses/FF/Factory.h" +#include "moses/PP/Factory.h" namespace Moses { @@ -200,6 +201,7 @@ protected: bool m_adjacentOnly; FeatureRegistry m_registry; + PhrasePropertyFactory m_phrasePropertyFactory; StaticData(); @@ -734,6 +736,9 @@ public: const FeatureRegistry &GetFeatureRegistry() const { return m_registry; } + const PhrasePropertyFactory &GetPhrasePropertyFactory() const + { return m_phrasePropertyFactory; } + /** check whether we should be using the old code to support binary phrase-table. ** eventually, we'll stop support the binary phrase-table and delete this legacy code **/ diff --git a/moses/TargetPhrase.cpp b/moses/TargetPhrase.cpp index cd1cc391b..8562abb42 100644 --- a/moses/TargetPhrase.cpp +++ b/moses/TargetPhrase.cpp @@ -156,7 +156,6 @@ void TargetPhrase::Evaluate(const InputType &input, const InputPath &inputPath) void TargetPhrase::SetXMLScore(float score) { - const StaticData &staticData = StaticData::Instance(); const FeatureFunction* prod = PhraseDictionary::GetColl()[0]; size_t numScores = prod->GetNumScoreComponents(); vector scoreVector(numScores,score/numScores); @@ -240,16 +239,22 @@ void TargetPhrase::SetProperties(const StringPiece &str) } } -void TargetPhrase::GetProperty(const std::string &key, std::string &value, bool &found) const +void TargetPhrase::SetProperty(const std::string &key, const std::string &value) { - std::map::const_iterator iter; + const StaticData &staticData = StaticData::Instance(); + const PhrasePropertyFactory& phrasePropertyFactory = staticData.GetPhrasePropertyFactory(); + m_properties[key] = phrasePropertyFactory.ProduceProperty(key,value); +} + +bool TargetPhrase::GetProperty(const std::string &key, boost::shared_ptr &value) const +{ + std::map >::const_iterator iter; iter = m_properties.find(key); - if (iter == m_properties.end()) { - found = false; - } else { - found = true; + if (iter != m_properties.end()) { value = iter->second; + return true; } + return false; } void TargetPhrase::SetRuleSource(const Phrase &ruleSource) const diff --git a/moses/TargetPhrase.h b/moses/TargetPhrase.h index 7b0cfbc2a..1f2fa96dd 100644 --- a/moses/TargetPhrase.h +++ b/moses/TargetPhrase.h @@ -28,9 +28,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA #include "Phrase.h" #include "ScoreComponentCollection.h" #include "AlignmentInfo.h" - +#include "moses/PP/PhraseProperty.h" #include "util/string_piece.hh" +#include + #ifdef HAVE_PROTOBUF #include "rule.pb.h" #endif @@ -55,7 +57,8 @@ private: const Word *m_lhsTarget; mutable Phrase *m_ruleSource; // to be set by the feature function that needs it. - std::map m_properties; + std::map > m_properties; + public: TargetPhrase(); TargetPhrase(const TargetPhrase ©); @@ -133,10 +136,8 @@ public: void SetRuleSource(const Phrase &ruleSource) const; void SetProperties(const StringPiece &str); - void SetProperty(const std::string &key, const std::string &value) { - m_properties[key] = value; - } - void GetProperty(const std::string &key, std::string &value, bool &found) const; + void SetProperty(const std::string &key, const std::string &value); + bool GetProperty(const std::string &key, boost::shared_ptr &value) const; void Merge(const TargetPhrase ©, const std::vector& factorVec);