use util::StringStream

This commit is contained in:
Hieu Hoang 2015-10-02 22:15:03 +01:00
parent fbaeccb772
commit dcea021cd7
6 changed files with 49 additions and 23 deletions

View File

@ -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<uint64_t> alreadyScored;
@ -165,13 +167,14 @@ void GlobalLexicalModelUnlimited::EvaluateWhenApplied(const Hypothesis& cur_hypo
if (m_sourceContext) {
if (sourceIndex == 0) {
// add <s> trigger feature for source
stringstream feature;
string str;
util::StringStream feature(str);
feature << "glm_";
feature << targetString;
feature << "~";
feature << "<s>,";
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);
}

View File

@ -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 << ":";

View File

@ -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

View File

@ -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<string> &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<string>::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) {

View File

@ -4,7 +4,7 @@
#include "moses/ScoreComponentCollection.h"
#include "moses/FactorCollection.h"
#include <sstream>
#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<targetPhrase.GetSize(); ++posT) {
const Word &wordT = targetPhrase.GetWord(posT);

View File

@ -21,6 +21,9 @@ class StringStream : public FakeOStream<StringStream> {
return *this;
}
const std::string &str() const
{ return out_; }
protected:
friend class FakeOStream<StringStream>;
char *Ensure(std::size_t amount) {