From 0720de0bee526e8e9b311bb91d0a3a1efa8c1438 Mon Sep 17 00:00:00 2001 From: graehl Date: Tue, 20 Jul 2010 22:44:40 +0000 Subject: beautiful (?) low-level tokenization to fix valgrind problem with previous TD string->Sentence git-svn-id: https://ws10smt.googlecode.com/svn/trunk@345 ec762483-ff6d-05da-a07a-a48fb63a330f --- decoder/tdict.cc | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) (limited to 'decoder/tdict.cc') diff --git a/decoder/tdict.cc b/decoder/tdict.cc index 93f7b0eb..d7fc7eb7 100644 --- a/decoder/tdict.cc +++ b/decoder/tdict.cc @@ -1,7 +1,9 @@ +#include #include "Ngram.h" #include "dict.h" #include "tdict.h" #include "Vocab.h" +#include "stringlib.h" using namespace std; @@ -16,6 +18,10 @@ WordID TD::Convert(const std::string& s) { return dict_.addWord((VocabString)s.c_str()); } +WordID TD::Convert(char const* s) { + return dict_.addWord((VocabString)s); +} + const char* TD::Convert(const WordID& w) { return dict_.getWord((VocabIndex)w); } @@ -31,25 +37,27 @@ void TD::GetWordIDs(const std::vector& strings, std::vector } std::string TD::GetString(const std::vector& str) { - string res; - for (vector::const_iterator i = str.begin(); i != str.end(); ++i) - res += (i == str.begin() ? empty : space) + TD::Convert(*i); - return res; + ostringstream o; + for (int i=0;i* ids) { - string s = sent; - int last = 0; - ids->clear(); - for (int i=0; i < s.size(); ++i) - if (s[i] == 32 || s[i] == '\t') { - s[i]=0; - if (last != i) { - ids->push_back(Convert(&s[last])); - } - last = i + 1; - } - if (last != s.size()) - ids->push_back(Convert(&s[last])); +namespace { +struct add_wordids { + typedef std::vector Ws; + Ws *ids; + explicit add_wordids(Ws *i) : ids(i) { } + void operator()(char const* s) { + ids->push_back(TD::Convert(s)); + } +}; + } +void TD::ConvertSentence(std::string const& s, std::vector* ids) { + ids->clear(); + VisitTokens(s,add_wordids(ids)); +} -- cgit v1.2.3