From dcea021cd7c6b1669a26d0808775c267972d595a Mon Sep 17 00:00:00 2001 From: Hieu Hoang Date: Fri, 2 Oct 2015 22:15:03 +0100 Subject: [PATCH] use util::StringStream --- moses/FF/GlobalLexicalModelUnlimited.cpp | 26 +++++++++++++++--------- moses/FF/PhraseBoundaryFeature.cpp | 4 +++- moses/FF/PhraseLengthFeature.cpp | 10 ++++++--- moses/FF/PhrasePairFeature.cpp | 24 +++++++++++++++------- moses/FF/RulePairUnlexicalizedSource.cpp | 5 +++-- util/string_stream.hh | 3 +++ 6 files changed, 49 insertions(+), 23 deletions(-) diff --git a/moses/FF/GlobalLexicalModelUnlimited.cpp b/moses/FF/GlobalLexicalModelUnlimited.cpp index 675af2b6b..fbbeeda47 100644 --- a/moses/FF/GlobalLexicalModelUnlimited.cpp +++ b/moses/FF/GlobalLexicalModelUnlimited.cpp @@ -5,6 +5,7 @@ #include "moses/Hypothesis.h" #include "moses/TranslationTask.h" #include "util/string_piece_hash.hh" +#include "util/string_stream.hh" using namespace std; @@ -131,12 +132,13 @@ void GlobalLexicalModelUnlimited::EvaluateWhenApplied(const Hypothesis& cur_hypo } if (m_biasFeature) { - stringstream feature; + string str; + util::StringStream feature(str); feature << "glm_"; feature << targetString; feature << "~"; feature << "**BIAS**"; - accumulator->SparsePlusEquals(feature.str(), 1); + accumulator->SparsePlusEquals(str, 1); } boost::unordered_set alreadyScored; @@ -165,13 +167,14 @@ void GlobalLexicalModelUnlimited::EvaluateWhenApplied(const Hypothesis& cur_hypo if (m_sourceContext) { if (sourceIndex == 0) { // add trigger feature for source - stringstream feature; + string str; + util::StringStream feature(str); feature << "glm_"; feature << targetString; feature << "~"; feature << ","; feature << sourceString; - accumulator->SparsePlusEquals(feature.str(), 1); + accumulator->SparsePlusEquals(str, 1); alreadyScored.insert(sourceHash); } @@ -183,14 +186,15 @@ void GlobalLexicalModelUnlimited::EvaluateWhenApplied(const Hypothesis& cur_hypo contextExists = FindStringPiece(m_vocabSource, contextString ) != m_vocabSource.end(); if (m_unrestricted || contextExists) { - stringstream feature; + string str; + util::StringStream feature(str); feature << "glm_"; feature << targetString; feature << "~"; feature << sourceString; feature << ","; feature << contextString; - accumulator->SparsePlusEquals(feature.str(), 1); + accumulator->SparsePlusEquals(str, 1); alreadyScored.insert(sourceHash); } } @@ -304,12 +308,13 @@ void GlobalLexicalModelUnlimited::EvaluateWhenApplied(const Hypothesis& cur_hypo } } } else { - stringstream feature; + string str; + util::StringStream feature(str); feature << "glm_"; feature << targetString; feature << "~"; feature << sourceString; - accumulator->SparsePlusEquals(feature.str(), 1); + accumulator->SparsePlusEquals(str, 1); alreadyScored.insert(sourceHash); } @@ -323,7 +328,8 @@ void GlobalLexicalModelUnlimited::AddFeature(ScoreComponentCollection* accumulat StringPiece sourceTrigger, StringPiece sourceWord, StringPiece targetTrigger, StringPiece targetWord) const { - stringstream feature; + string str; + util::StringStream feature(str); feature << "glm_"; feature << targetTrigger; feature << ","; @@ -332,7 +338,7 @@ void GlobalLexicalModelUnlimited::AddFeature(ScoreComponentCollection* accumulat feature << sourceTrigger; feature << ","; feature << sourceWord; - accumulator->SparsePlusEquals(feature.str(), 1); + accumulator->SparsePlusEquals(str, 1); } diff --git a/moses/FF/PhraseBoundaryFeature.cpp b/moses/FF/PhraseBoundaryFeature.cpp index 893db9a90..1a127d9e4 100644 --- a/moses/FF/PhraseBoundaryFeature.cpp +++ b/moses/FF/PhraseBoundaryFeature.cpp @@ -3,6 +3,7 @@ #include "moses/Hypothesis.h" #include "moses/TranslationOption.h" #include "moses/InputPath.h" +#include "util/string_stream.hh" using namespace std; @@ -61,7 +62,8 @@ void PhraseBoundaryFeature::AddFeatures( ScoreComponentCollection* scores) const { for (size_t i = 0; i < factors.size(); ++i) { - ostringstream name; + std::string str; + util::StringStream name(str); name << side << ":"; name << factors[i]; name << ":"; diff --git a/moses/FF/PhraseLengthFeature.cpp b/moses/FF/PhraseLengthFeature.cpp index 0eb0740b8..55cd2f847 100644 --- a/moses/FF/PhraseLengthFeature.cpp +++ b/moses/FF/PhraseLengthFeature.cpp @@ -3,6 +3,7 @@ #include "moses/Hypothesis.h" #include "moses/ScoreComponentCollection.h" #include "moses/TranslationOption.h" +#include "util/string_stream.hh" namespace Moses { @@ -25,13 +26,16 @@ void PhraseLengthFeature::EvaluateInIsolation(const Phrase &source size_t sourceLength = source.GetSize(); // create feature names - stringstream nameSource; + string nameSourceStr; + util::StringStream nameSource(nameSourceStr); nameSource << "s" << sourceLength; - stringstream nameTarget; + string nameTargetStr; + util::StringStream nameTarget(nameTargetStr); nameTarget << "t" << targetLength; - stringstream nameBoth; + string nameBothStr; + util::StringStream nameBoth(nameBothStr); nameBoth << sourceLength << "," << targetLength; // increase feature counts diff --git a/moses/FF/PhrasePairFeature.cpp b/moses/FF/PhrasePairFeature.cpp index 1e343877c..920317c00 100644 --- a/moses/FF/PhrasePairFeature.cpp +++ b/moses/FF/PhrasePairFeature.cpp @@ -7,6 +7,7 @@ #include "moses/TranslationOption.h" #include "moses/InputPath.h" #include "util/string_piece_hash.hh" +#include "util/string_stream.hh" #include "util/exception.hh" using namespace std; @@ -126,7 +127,9 @@ void PhrasePairFeature::EvaluateWithSourceContext(const InputType &input const bool use_topicid_prob = isnt.GetUseTopicIdAndProb(); // compute pair - ostringstream pair; + string pairStr; + util::StringStream pair(pairStr); + pair << ReplaceTilde( source.GetWord(0).GetFactor(m_sourceFactorId)->GetString() ); for (size_t i = 1; i < source.GetSize(); ++i) { const Factor* sourceFactor = source.GetWord(i).GetFactor(m_sourceFactorId); @@ -145,7 +148,9 @@ void PhrasePairFeature::EvaluateWithSourceContext(const InputType &input if(use_topicid) { // use topicid as trigger const long topicid = isnt.GetTopicId(); - stringstream feature; + string featureStr; + util::StringStream feature(featureStr); + feature << m_description << "_"; if (topicid == -1) feature << "unk"; @@ -159,13 +164,15 @@ void PhrasePairFeature::EvaluateWithSourceContext(const InputType &input // use topic probabilities const vector &topicid_prob = *(isnt.GetTopicIdAndProb()); if (atol(topicid_prob[0].c_str()) == -1) { - stringstream feature; + string featureStr; + util::StringStream feature(featureStr); feature << m_description << "_unk_"; feature << pair.str(); scoreBreakdown.SparsePlusEquals(feature.str(), 1); } else { for (size_t i=0; i+1 < topicid_prob.size(); i+=2) { - stringstream feature; + string featureStr; + util::StringStream feature(featureStr); feature << m_description << "_"; feature << topicid_prob[i]; feature << "_"; @@ -179,7 +186,8 @@ void PhrasePairFeature::EvaluateWithSourceContext(const InputType &input const long docid = isnt.GetDocumentId(); for (set::const_iterator p = m_vocabDomain[docid].begin(); p != m_vocabDomain[docid].end(); ++p) { string sourceTrigger = *p; - ostringstream namestr; + string str; + util::StringStream namestr(str); namestr << m_description << "_"; namestr << sourceTrigger; namestr << "_"; @@ -207,7 +215,8 @@ void PhrasePairFeature::EvaluateWithSourceContext(const InputType &input sourceTriggerExists = FindStringPiece(m_vocabSource, sourceTrigger ) != m_vocabSource.end(); if (m_unrestricted || sourceTriggerExists) { - ostringstream namestr; + string str; + util::StringStream namestr(str); namestr << m_description << "_"; namestr << sourceTrigger; namestr << "~"; @@ -237,7 +246,8 @@ void PhrasePairFeature::EvaluateInIsolation(const Phrase &source , ScoreComponentCollection &estimatedFutureScore) const { if (m_simple) { - ostringstream namestr; + string str; + util::StringStream namestr(str); namestr << m_description << "_"; namestr << ReplaceTilde( source.GetWord(0).GetFactor(m_sourceFactorId)->GetString() ); for (size_t i = 1; i < source.GetSize(); ++i) { diff --git a/moses/FF/RulePairUnlexicalizedSource.cpp b/moses/FF/RulePairUnlexicalizedSource.cpp index d65810af8..e82f1cd2a 100644 --- a/moses/FF/RulePairUnlexicalizedSource.cpp +++ b/moses/FF/RulePairUnlexicalizedSource.cpp @@ -4,7 +4,7 @@ #include "moses/ScoreComponentCollection.h" #include "moses/FactorCollection.h" #include - +#include "util/string_stream.hh" using namespace std; @@ -58,7 +58,8 @@ void RulePairUnlexicalizedSource::EvaluateInIsolation(const Phrase &source } } - ostringstream namestr; + string str; + util::StringStream namestr(str); for (size_t posT=0; posT { return *this; } + const std::string &str() const + { return out_; } + protected: friend class FakeOStream; char *Ensure(std::size_t amount) {