diff --git a/moses-chart-cmd/IOWrapper.cpp b/moses-chart-cmd/IOWrapper.cpp index bd019cd99..db12dcfd6 100644 --- a/moses-chart-cmd/IOWrapper.cpp +++ b/moses-chart-cmd/IOWrapper.cpp @@ -412,9 +412,10 @@ void IOWrapper::OutputTreeFragmentsTranslationOptions(std::ostream &out, Applica OutputTranslationOption(out, applicationContext, hypo, sentence, translationId); const TargetPhrase &currTarPhr = hypo->GetCurrTargetPhrase(); + boost::shared_ptr property; out << " ||| "; - if (const PhraseProperty *property = currTarPhr.GetProperty("Tree")) { + if (currTarPhr.GetProperty("Tree", property)) { out << " " << property->GetValueString(); } else { out << " " << "noTreeInfo"; @@ -438,9 +439,10 @@ void IOWrapper::OutputTreeFragmentsTranslationOptions(std::ostream &out, Applica OutputTranslationOption(out, applicationContext, applied, sentence, translationId); const TargetPhrase &currTarPhr = *static_cast(applied->GetNote().vp); + boost::shared_ptr property; out << " ||| "; - if (const PhraseProperty *property = currTarPhr.GetProperty("Tree")) { + if (currTarPhr.GetProperty("Tree", property)) { out << " " << property->GetValueString(); } else { out << " " << "noTreeInfo"; diff --git a/moses/FF/TreeStructureFeature.cpp b/moses/FF/TreeStructureFeature.cpp index 10578982a..b14b888b0 100644 --- a/moses/FF/TreeStructureFeature.cpp +++ b/moses/FF/TreeStructureFeature.cpp @@ -271,9 +271,10 @@ FFState* TreeStructureFeature::EvaluateChart(const ChartHypothesis& cur_hypo , int featureID /* used to index the state in the previous hypotheses */ , ScoreComponentCollection* accumulator) const { - if (const PhraseProperty *property = cur_hypo.GetCurrTargetPhrase().GetProperty("Tree")) { - const std::string &tree = property->GetValueString(); - TreePointer mytree (new InternalTree(tree)); + 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) { AddNTLabels(mytree); diff --git a/moses/PP/CountsPhraseProperty.cpp b/moses/PP/CountsPhraseProperty.cpp index f7af18d5b..def07e5da 100644 --- a/moses/PP/CountsPhraseProperty.cpp +++ b/moses/PP/CountsPhraseProperty.cpp @@ -1,14 +1,13 @@ #include "moses/PP/CountsPhraseProperty.h" #include #include -#include "util/exception.hh" namespace Moses { -void CountsPhraseProperty::ProcessValue() +void CountsPhraseProperty::ProcessValue(const std::string &value) { - std::istringstream tokenizer(m_value); + std::istringstream tokenizer(value); if (! (tokenizer >> m_targetMarginal)) { // first token: countE UTIL_THROW2("CountsPhraseProperty: Not able to read target marginal. Flawed property?"); @@ -24,7 +23,6 @@ void CountsPhraseProperty::ProcessValue() UTIL_THROW2("CountsPhraseProperty: Not able to read joint count. Flawed property?"); } assert( m_jointCount > 0 ); - }; } // namespace Moses diff --git a/moses/PP/CountsPhraseProperty.h b/moses/PP/CountsPhraseProperty.h index f633381b6..19382fe29 100644 --- a/moses/PP/CountsPhraseProperty.h +++ b/moses/PP/CountsPhraseProperty.h @@ -2,6 +2,7 @@ #pragma once #include "moses/PP/PhraseProperty.h" +#include "util/exception.hh" #include #include @@ -28,9 +29,9 @@ class CountsPhraseProperty : public PhraseProperty { public: - CountsPhraseProperty(const std::string &value) : PhraseProperty(value) {}; + CountsPhraseProperty() {}; - virtual void ProcessValue(); + virtual void ProcessValue(const std::string &value); size_t GetSourceMarginal() const { return m_sourceMarginal; @@ -44,6 +45,11 @@ public: return m_jointCount; } + virtual const std::string *GetValueString() const { + UTIL_THROW2("CountsPhraseProperty: value string not available in this phrase property"); + return NULL; + }; + protected: float m_sourceMarginal, m_targetMarginal, m_jointCount; diff --git a/moses/PP/Factory.cpp b/moses/PP/Factory.cpp index 0cc891a82..45bdafe36 100644 --- a/moses/PP/Factory.cpp +++ b/moses/PP/Factory.cpp @@ -36,8 +36,8 @@ template class DefaultPhrasePropertyCreator : public PhrasePropertyCre { public: boost::shared_ptr CreateProperty(const std::string &value) { - P* property = new P(value); - property->ProcessValue(); + P* property = new P(); + property->ProcessValue(value); return Create(property); } }; @@ -54,7 +54,7 @@ PhrasePropertyFactory::PhrasePropertyFactory() MOSES_PNAME2("Counts", CountsPhraseProperty); MOSES_PNAME2("SourceLabels", SourceLabelsPhraseProperty); - MOSES_PNAME2("Tree",TreeStructurePhraseProperty); + MOSES_PNAME2("Tree", TreeStructurePhraseProperty); } diff --git a/moses/PP/PhraseProperty.h b/moses/PP/PhraseProperty.h index a4353e634..36de46033 100644 --- a/moses/PP/PhraseProperty.h +++ b/moses/PP/PhraseProperty.h @@ -11,15 +11,15 @@ namespace Moses class PhraseProperty { public: - PhraseProperty(const std::string &value) : m_value(value) {}; + PhraseProperty() {}; - virtual void ProcessValue() {}; + virtual void ProcessValue(const std::string &value) { m_value = new std::string(value); }; - const std::string &GetValueString() const { return m_value; }; + virtual const std::string *GetValueString() const { return m_value; }; protected: - const std::string m_value; + std::string *m_value; }; diff --git a/moses/PP/SourceLabelsPhraseProperty.cpp b/moses/PP/SourceLabelsPhraseProperty.cpp index 6f18480c5..bca5c9a30 100644 --- a/moses/PP/SourceLabelsPhraseProperty.cpp +++ b/moses/PP/SourceLabelsPhraseProperty.cpp @@ -7,14 +7,13 @@ #include #include #include -#include "util/exception.hh" namespace Moses { -void SourceLabelsPhraseProperty::ProcessValue() +void SourceLabelsPhraseProperty::ProcessValue(const std::string &value) { - std::istringstream tokenizer(m_value); + std::istringstream tokenizer(value); if (! (tokenizer >> m_nNTs)) { // first token: number of non-terminals (incl. left-hand side) UTIL_THROW2("SourceLabelsPhraseProperty: Not able to read number of non-terminals. Flawed property?"); diff --git a/moses/PP/SourceLabelsPhraseProperty.h b/moses/PP/SourceLabelsPhraseProperty.h index 049d88f17..39b43ad3e 100644 --- a/moses/PP/SourceLabelsPhraseProperty.h +++ b/moses/PP/SourceLabelsPhraseProperty.h @@ -2,6 +2,7 @@ #pragma once #include "moses/PP/PhraseProperty.h" +#include "util/exception.hh" #include #include @@ -43,9 +44,9 @@ private: class SourceLabelsPhraseProperty : public PhraseProperty { public: - SourceLabelsPhraseProperty(const std::string &value) : PhraseProperty(value) {}; + SourceLabelsPhraseProperty() {}; - virtual void ProcessValue(); + virtual void ProcessValue(const std::string &value); size_t GetNumberOfNonTerminals() const { return m_nNTs; @@ -59,6 +60,11 @@ public: return m_sourceLabelItems; }; + virtual const std::string *GetValueString() const { + UTIL_THROW2("SourceLabelsPhraseProperty: value string not available in this phrase property"); + return NULL; + }; + protected: size_t m_nNTs; diff --git a/moses/PP/TreeStructurePhraseProperty.h b/moses/PP/TreeStructurePhraseProperty.h index f9acc38dd..45124973f 100644 --- a/moses/PP/TreeStructurePhraseProperty.h +++ b/moses/PP/TreeStructurePhraseProperty.h @@ -10,7 +10,7 @@ namespace Moses class TreeStructurePhraseProperty : public PhraseProperty { public: - TreeStructurePhraseProperty(const std::string &value) : PhraseProperty(value) {}; + TreeStructurePhraseProperty() {}; }; diff --git a/moses/TargetPhrase.cpp b/moses/TargetPhrase.cpp index ede16293c..141d3f387 100644 --- a/moses/TargetPhrase.cpp +++ b/moses/TargetPhrase.cpp @@ -248,15 +248,15 @@ void TargetPhrase::SetProperty(const std::string &key, const std::string &value) m_properties[key] = phrasePropertyFactory.ProduceProperty(key,value); } -const PhraseProperty *TargetPhrase::GetProperty(const std::string &key) const +bool TargetPhrase::GetProperty(const std::string &key, boost::shared_ptr &pp) const { std::map >::const_iterator iter; iter = m_properties.find(key); if (iter != m_properties.end()) { - const boost::shared_ptr &value = iter->second; - return value.get(); + pp = iter->second; + return true; } - return NULL; + return false; } void TargetPhrase::SetRuleSource(const Phrase &ruleSource) const diff --git a/moses/TargetPhrase.h b/moses/TargetPhrase.h index 5056d95c4..e87bf9c5c 100644 --- a/moses/TargetPhrase.h +++ b/moses/TargetPhrase.h @@ -137,7 +137,7 @@ public: void SetProperties(const StringPiece &str); void SetProperty(const std::string &key, const std::string &value); - const PhraseProperty *GetProperty(const std::string &key) const; + bool GetProperty(const std::string &key, boost::shared_ptr &pp) const; void Merge(const TargetPhrase ©, const std::vector& factorVec);