删除一些无必要的错误检查

This commit is contained in:
yanyiwu 2015-05-21 16:04:41 +08:00
parent 954100dc3d
commit aed1c8f4a6
7 changed files with 16 additions and 58 deletions

View File

@ -47,12 +47,6 @@ class FullSegment: public SegmentBase {
using SegmentBase::cut; using SegmentBase::cut;
bool cut(Unicode::const_iterator begin, Unicode::const_iterator end, vector<Unicode>& res) const { bool cut(Unicode::const_iterator begin, Unicode::const_iterator end, vector<Unicode>& res) const {
assert(dictTrie_);
if (begin >= end) {
LogError("begin >= end");
return false;
}
//resut of searching in trie tree //resut of searching in trie tree
DagType tRes; DagType tRes;
@ -64,6 +58,7 @@ class FullSegment: public SegmentBase {
//tmp variables //tmp variables
int wordLen = 0; int wordLen = 0;
assert(dictTrie_);
for (Unicode::const_iterator uItr = begin; uItr != end; uItr++) { for (Unicode::const_iterator uItr = begin; uItr != end; uItr++) {
//find word start from uItr //find word start from uItr
if (dictTrie_->find(uItr, end, tRes, 0)) { if (dictTrie_->find(uItr, end, tRes, 0)) {
@ -93,12 +88,6 @@ class FullSegment: public SegmentBase {
} }
bool cut(Unicode::const_iterator begin, Unicode::const_iterator end, vector<string>& res) const { bool cut(Unicode::const_iterator begin, Unicode::const_iterator end, vector<string>& res) const {
assert(dictTrie_);
if (begin >= end) {
LogError("begin >= end");
return false;
}
vector<Unicode> uRes; vector<Unicode> uRes;
if (!cut(begin, end, uRes)) { if (!cut(begin, end, uRes)) {
LogError("get unicode cut result error."); LogError("get unicode cut result error.");
@ -107,11 +96,8 @@ class FullSegment: public SegmentBase {
string tmp; string tmp;
for (vector<Unicode>::const_iterator uItr = uRes.begin(); uItr != uRes.end(); uItr++) { for (vector<Unicode>::const_iterator uItr = uRes.begin(); uItr != uRes.end(); uItr++) {
if (TransCode::encode(*uItr, tmp)) { TransCode::encode(*uItr, tmp);
res.push_back(tmp); res.push_back(tmp);
} else {
LogError("encode failed.");
}
} }
return true; return true;

View File

@ -90,9 +90,7 @@ class HMMSegment: public SegmentBase {
size_t offset = res.size(); size_t offset = res.size();
res.resize(res.size() + words.size()); res.resize(res.size() + words.size());
for(size_t i = 0; i < words.size(); i++) { for(size_t i = 0; i < words.size(); i++) {
if(!TransCode::encode(words[i], res[offset + i])) { TransCode::encode(words[i], res[offset + i]);
LogError("encode failed.");
}
} }
return true; return true;
} }

View File

@ -197,10 +197,7 @@ bool utf8ToUnicode(const string& str, Uint16Container& vec) {
} }
template <class Uint16ContainerConIter> template <class Uint16ContainerConIter>
bool unicodeToUtf8(Uint16ContainerConIter begin, Uint16ContainerConIter end, string& res) { void unicodeToUtf8(Uint16ContainerConIter begin, Uint16ContainerConIter end, string& res) {
if(begin >= end) {
return false;
}
res.clear(); res.clear();
uint16_t ui; uint16_t ui;
while(begin != end) { while(begin != end) {
@ -217,7 +214,6 @@ bool unicodeToUtf8(Uint16ContainerConIter begin, Uint16ContainerConIter end, str
} }
begin ++; begin ++;
} }
return true;
} }
@ -225,7 +221,7 @@ template <class Uint16Container>
bool gbkTrans(const char* const str, size_t len, Uint16Container& vec) { bool gbkTrans(const char* const str, size_t len, Uint16Container& vec) {
vec.clear(); vec.clear();
if(!str) { if(!str) {
return false; return true;
} }
size_t i = 0; size_t i = 0;
while(i < len) { while(i < len) {
@ -251,10 +247,7 @@ bool gbkTrans(const string& str, Uint16Container& vec) {
} }
template <class Uint16ContainerConIter> template <class Uint16ContainerConIter>
bool gbkTrans(Uint16ContainerConIter begin, Uint16ContainerConIter end, string& res) { void gbkTrans(Uint16ContainerConIter begin, Uint16ContainerConIter end, string& res) {
if(begin >= end) {
return false;
}
res.clear(); res.clear();
//pair<char, char> pa; //pair<char, char> pa;
char first, second; char first, second;
@ -270,7 +263,6 @@ bool gbkTrans(Uint16ContainerConIter begin, Uint16ContainerConIter end, string&
} }
begin++; begin++;
} }
return true;
} }
/* /*

View File

@ -42,10 +42,7 @@ class MPSegment: public SegmentBase {
size_t offset = res.size(); size_t offset = res.size();
res.resize(res.size() + words.size()); res.resize(res.size() + words.size());
for(size_t i = 0; i < words.size(); i++) { for(size_t i = 0; i < words.size(); i++) {
if(!TransCode::encode(words[i], res[i + offset])) { TransCode::encode(words[i], res[i + offset]);
LogError("encode failed.");
res[i + offset].clear();
}
} }
return true; return true;
} }

View File

@ -83,9 +83,7 @@ class MixSegment: public SegmentBase {
size_t offset = res.size(); size_t offset = res.size();
res.resize(res.size() + uRes.size()); res.resize(res.size() + uRes.size());
for(size_t i = 0; i < uRes.size(); i ++, offset++) { for(size_t i = 0; i < uRes.size(); i ++, offset++) {
if(!TransCode::encode(uRes[i], res[offset])) { TransCode::encode(uRes[i], res[offset]);
LogError("encode failed.");
}
} }
return true; return true;
} }

View File

@ -29,11 +29,6 @@ class QuerySegment: public SegmentBase {
} }
using SegmentBase::cut; using SegmentBase::cut;
bool cut(Unicode::const_iterator begin, Unicode::const_iterator end, vector<Unicode>& res) const { bool cut(Unicode::const_iterator begin, Unicode::const_iterator end, vector<Unicode>& res) const {
if (begin >= end) {
LogError("begin >= end");
return false;
}
//use mix cut first //use mix cut first
vector<Unicode> mixRes; vector<Unicode> mixRes;
if (!mixSeg_.cut(begin, end, mixRes)) { if (!mixSeg_.cut(begin, end, mixRes)) {
@ -64,11 +59,6 @@ class QuerySegment: public SegmentBase {
bool cut(Unicode::const_iterator begin, Unicode::const_iterator end, vector<string>& res) const { bool cut(Unicode::const_iterator begin, Unicode::const_iterator end, vector<string>& res) const {
if (begin >= end) {
LogError("begin >= end");
return false;
}
vector<Unicode> uRes; vector<Unicode> uRes;
if (!cut(begin, end, uRes)) { if (!cut(begin, end, uRes)) {
LogError("get unicode cut result error."); LogError("get unicode cut result error.");
@ -77,11 +67,8 @@ class QuerySegment: public SegmentBase {
string tmp; string tmp;
for (vector<Unicode>::const_iterator uItr = uRes.begin(); uItr != uRes.end(); uItr++) { for (vector<Unicode>::const_iterator uItr = uRes.begin(); uItr != uRes.end(); uItr++) {
if (TransCode::encode(*uItr, tmp)) { TransCode::encode(*uItr, tmp);
res.push_back(tmp); res.push_back(tmp);
} else {
LogError("encode failed.");
}
} }
return true; return true;

View File

@ -23,16 +23,16 @@ inline bool decode(const string& str, Unicode& res) {
#endif #endif
} }
inline bool encode(Unicode::const_iterator begin, Unicode::const_iterator end, string& res) { inline void encode(Unicode::const_iterator begin, Unicode::const_iterator end, string& res) {
#ifdef CPPJIEBA_GBK #ifdef CPPJIEBA_GBK
return gbkTrans(begin, end, res); gbkTrans(begin, end, res);
#else #else
return unicodeToUtf8(begin, end, res); unicodeToUtf8(begin, end, res);
#endif #endif
} }
inline bool encode(const Unicode& uni, string& res) { inline void encode(const Unicode& uni, string& res) {
return encode(uni.begin(), uni.end(), res); encode(uni.begin(), uni.end(), res);
} }
// compiler is expected to optimized this function to avoid return value copy // compiler is expected to optimized this function to avoid return value copy