From b0521c6d6eae8e23672d5918d1d8aecf829392c8 Mon Sep 17 00:00:00 2001 From: wyy Date: Wed, 30 Oct 2013 23:11:51 -0700 Subject: [PATCH] update cppjieba to adapter new limonp --- cppjieba/TransCode.hpp | 1 - cppjieba/structs.h | 25 ++-- demo/keywordext.cpp | 2 +- demo/segment.cpp | 2 +- demo/server.cpp | 2 +- husky/HttpReqInfo.hpp | 25 ++-- husky/ServerFrame.cpp | 2 +- limonp/ArgvContext.hpp | 18 ++- limonp/MysqlClient.hpp | 11 +- limonp/logger.hpp | 1 - limonp/map_functs.hpp | 147 ++++++++++----------- limonp/str_functs.hpp | 282 +++++++++++++++++++++++++++++++---------- limonp/typedefs.h | 21 --- limonp/vec_functs.hpp | 142 --------------------- 14 files changed, 340 insertions(+), 341 deletions(-) delete mode 100644 limonp/typedefs.h delete mode 100644 limonp/vec_functs.hpp diff --git a/cppjieba/TransCode.hpp b/cppjieba/TransCode.hpp index 46ede56..e49c84d 100644 --- a/cppjieba/TransCode.hpp +++ b/cppjieba/TransCode.hpp @@ -8,7 +8,6 @@ #include "globals.h" #include -#include namespace CppJieba { diff --git a/cppjieba/structs.h b/cppjieba/structs.h index 38fd5ec..88c5894 100644 --- a/cppjieba/structs.h +++ b/cppjieba/structs.h @@ -74,7 +74,7 @@ namespace CppJieba KeyWordInfo(const TrieNodeInfo& trieNodeInfo):TrieNodeInfo(trieNodeInfo) { } - inline string toString() const + string toString() const { string tmp; TransCode::encode(word, tmp); @@ -89,16 +89,23 @@ namespace CppJieba return *this; } }; - - inline string joinWordInfos(const vector& vec) + + inline ostream& operator << (ostream& os, const KeyWordInfo& info) { - vector tmp; - for(uint i = 0; i < vec.size(); i++) - { - tmp.push_back(vec[i].toString()); - } - return joinStr(tmp, ","); + string tmp; + TransCode::encode(info.word, tmp); + return os << "{words:" << tmp << ", weight:" << info.weight << ", idf:" << info.idf << "}"; } + + //inline string joinWordInfos(const vector& vec) + //{ + // vector tmp; + // for(uint i = 0; i < vec.size(); i++) + // { + // tmp.push_back(vec[i].toString()); + // } + // return joinStr(tmp, ","); + //} } #endif diff --git a/demo/keywordext.cpp b/demo/keywordext.cpp index bbdd781..b7713d2 100644 --- a/demo/keywordext.cpp +++ b/demo/keywordext.cpp @@ -23,7 +23,7 @@ void testKeyWordExt(const char * dictPath, const char * filePath) if(!line.empty()) { ext.extract(line, res, 20); - cout<cut(line, res); - cout<& mp) { diff --git a/husky/ServerFrame.cpp b/husky/ServerFrame.cpp index 9903fb7..5d83eb8 100644 --- a/husky/ServerFrame.cpp +++ b/husky/ServerFrame.cpp @@ -140,7 +140,7 @@ namespace Husky if(SOCKET_ERROR==nRetCode) { - LogError("error [%s]", strerror(errno)); + LogDebug("error [%s]", strerror(errno)); closesocket(hClientSock); continue; } diff --git a/limonp/ArgvContext.hpp b/limonp/ArgvContext.hpp index f4aeef2..8be15f1 100644 --- a/limonp/ArgvContext.hpp +++ b/limonp/ArgvContext.hpp @@ -10,7 +10,6 @@ #include #include "str_functs.hpp" #include "map_functs.hpp" -#include "vec_functs.hpp" namespace Limonp { @@ -43,12 +42,7 @@ namespace Limonp } ~ArgvContext(){}; public: - string toString() - { - stringstream ss; - ss<(_args)<(_mpss)<(_sset); - return ss.str(); - } + friend ostream& operator << (ostream& os, const ArgvContext& args); string operator [](uint i) { if(i < _args.size()) @@ -81,6 +75,16 @@ namespace Limonp set _sset; }; + + inline ostream& operator << (ostream& os, const ArgvContext& args) + { + return os< #include #include "logger.hpp" -#include "vec_functs.hpp" namespace Limonp { @@ -21,8 +20,9 @@ namespace Limonp const char * const USER; const char * const PASSWD; const char * const DB; + const char * const CHARSET; public: - MysqlClient(const char* host, uint port, const char* user, const char* passwd, const char* db): HOST(host), PORT(port), USER(user), PASSWD(passwd), DB(db){ _conn = NULL;}; + MysqlClient(const char* host, uint port, const char* user, const char* passwd, const char* db, const char* charset = "utf8"): HOST(host), PORT(port), USER(user), PASSWD(passwd), DB(db), CHARSET(charset){ _conn = NULL;}; ~MysqlClient(){dispose();}; public: bool init() @@ -42,10 +42,17 @@ namespace Limonp return false; } + if(mysql_set_character_set(_conn, CHARSET)) + { + LogError("mysql_set_character_set [%s] failed.", CHARSET); + return false; + } + //set reconenct char value = 1; mysql_options(_conn, MYSQL_OPT_RECONNECT, &value); + LogInfo("MysqlClient {host: %s, port:%d, database:%s, charset:%s}", HOST, PORT, DB, CHARSET); return true; } bool dispose() diff --git a/limonp/logger.hpp b/limonp/logger.hpp index 5ccfdec..ed1a3c2 100644 --- a/limonp/logger.hpp +++ b/limonp/logger.hpp @@ -13,7 +13,6 @@ #include #include "io_functs.hpp" #include "str_functs.hpp" -#include "typedefs.h" #define LogDebug(fmt, ...) Logger::LoggingF(LL_DEBUG, __FILE__, __LINE__, fmt, ## __VA_ARGS__) #define LogInfo(fmt, ...) Logger::LoggingF(LL_INFO, __FILE__, __LINE__, fmt, ## __VA_ARGS__) diff --git a/limonp/map_functs.hpp b/limonp/map_functs.hpp index 9479691..44d472c 100644 --- a/limonp/map_functs.hpp +++ b/limonp/map_functs.hpp @@ -11,91 +11,84 @@ #include #include #include -#include "typedefs.h" + +#include +#define HashMap std::tr1::unordered_map namespace Limonp { using namespace std; - template - string setToString(const set& st) - { - if(st.empty()) - { - return "{}"; - } - stringstream ss; - ss<<'{'; - typename set::const_iterator it = st.begin(); - ss<<*it; - it++; - while(it != st.end()) - { - ss<<", "<<*it; - it++; - } - ss<<'}'; - return ss.str(); - } + //template + // string setToString(const set& st) + // { + // if(st.empty()) + // { + // return "{}"; + // } + // stringstream ss; + // ss<<'{'; + // typename set::const_iterator it = st.begin(); + // ss<<*it; + // it++; + // while(it != st.end()) + // { + // ss<<", "<<*it; + // it++; + // } + // ss<<'}'; + // return ss.str(); + // } - template - string mapToString(const map& mp) - { - if(mp.empty()) - { - return "{}"; - } - stringstream ss; - ss<<'{'; - typename map::const_iterator it = mp.begin(); - ss<first<<": "<second; - it++; - while(it != mp.end()) - { - ss<<", "<first<<": "<second; - it++; - } - ss<<'}'; - return ss.str(); - } + //template + // string mapToString(const map& mp) + // { + // if(mp.empty()) + // { + // return "{}"; + // } + // stringstream ss; + // ss<<'{'; + // typename map::const_iterator it = mp.begin(); + // ss<first<<": "<second; + // it++; + // while(it != mp.end()) + // { + // ss<<", "<first<<": "<second; + // it++; + // } + // ss<<'}'; + // return ss.str(); + // } - template - string HashMapToString(const HashMap& mp) - { - if(mp.empty()) - { - return "{}"; - } - stringstream ss; - ss<<'{'; - typename HashMap::const_iterator it = mp.begin(); - ss<first<<": "<second; - it++; - while(it != mp.end()) - { - ss<<", "<first<<": "<second; - it++; - } - ss<<'}'; - return ss.str(); - } - template - string pairToString(const pair& p) - { - stringstream ss; - ss< - void printMap(const map& mp) - { - for(typename map::const_iterator it = mp.begin(); it != mp.end(); it++) - { - cout<first<<' '<second< + // string HashMapToString(const HashMap& mp) + // { + // if(mp.empty()) + // { + // return "{}"; + // } + // stringstream ss; + // ss<<'{'; + // typename HashMap::const_iterator it = mp.begin(); + // ss<first<<": "<second; + // it++; + // while(it != mp.end()) + // { + // ss<<", "<first<<": "<second; + // it++; + // } + // ss<<'}'; + // return ss.str(); + // } + //template + // string pairToString(const pair& p) + // { + // stringstream ss; + // ss< vT getMap(const map& mp, const kT & key, const vT & defaultVal) diff --git a/limonp/str_functs.hpp b/limonp/str_functs.hpp index 0116995..e3c87ab 100644 --- a/limonp/str_functs.hpp +++ b/limonp/str_functs.hpp @@ -15,9 +15,14 @@ #include #include #include -#include "typedefs.h" #include #include +#include +#include +#include + +#define print(x) cout<<(x)<& src, string& dest, const string& connectorStr) - { - if(src.empty()) - { - return false; - } - for(uint i = 0; i < src.size() - 1; i++) - { - dest += src[i]; - dest += connectorStr; - } - dest += src[src.size() - 1]; - return true; - } + //inline bool joinStr(const vector& src, string& dest, const string& connectorStr) + //{ + // if(src.empty()) + // { + // return false; + // } + // for(uint i = 0; i < src.size() - 1; i++) + // { + // dest += src[i]; + // dest += connectorStr; + // } + // dest += src[src.size() - 1]; + // return true; + //} - inline string joinStr(const vector& source, const string& connector) - { - string res; - joinStr(source, res, connector); - return res; - } + //inline string joinStr(const vector& source, const string& connector) + //{ + // string res; + // joinStr(source, res, connector); + // return res; + //} + + template + void join(T begin, T end, string& res, const string& connector) + { + if(begin == end) + { + return; + } + stringstream ss; + ss<<*begin; + begin++; + while(begin != end) + { + ss << connector << *begin; + begin ++; + } + res = ss.str(); + } + + template + string join(T begin, T end, const string& connector) + { + string res; + join(begin ,end, res, connector); + return res; + } + + template + ostream& operator << (ostream& os, const pair& pr) + { + os << pr.first << ":" << pr.second ; + return os; + } + + template + ostream& operator << (ostream& os, const vector& vec) + { + if(vec.empty()) + { + return os << "[]"; + } + os<<"[\""< + string& operator << (string& str, const T& obj) + { + stringstream ss; + ss << obj; // call ostream& operator << (ostream& os, + return str = ss.str(); + } + + template + ostream& operator << (ostream& os, const map& mp) + { + if(mp.empty()) + { + os<<"{}"; + return os; + } + os<<'{'; + typename map::const_iterator it = mp.begin(); + os<<*it; + it++; + while(it != mp.end()) + { + os<<", "<<*it; + it++; + } + os<<'}'; + return os; + } + + + //template + // string& operator << (string& str, const map& mp) + // { + // if(mp.empty()) + // { + // str = "{}"; + // return str; + // } + // stringstream ss; + // ss<<'{'; + // typename map::const_iterator it = mp.begin(); + // ss<<*it; + // it++; + // while(it != mp.end()) + // { + // ss<<", "<<*it; + // it++; + // } + // ss<<'}'; + // str = ss.str(); + // return str; + // } + + template + ostream& operator << (ostream& os, const HashMap& mp) + { + if(mp.empty()) + { + return os << "{}"; + } + os<<'{'; + typename map::const_iterator it = mp.begin(); + os<<*it; + it++; + while(it != mp.end()) + { + os<<", "<<*it++; + } + return os<<'}'; + } + + //template + // string& operator << (string& str, const set& st) + // { + // stringstream ss; + // ss << st; + // return str = ss.str(); + // } + + template + ostream& operator << (ostream& os, const set& st) + { + if(st.empty()) + { + os << "{}"; + return os; + } + os<<'{'; + typename set::const_iterator it = st.begin(); + os<<*it; + it++; + while(it != st.end()) + { + os<<", "<<*it; + it++; + } + os<<'}'; + return os; + } inline bool splitStr(const string& src, vector& res, const string& pattern) { @@ -104,26 +258,24 @@ namespace Limonp return true; } res.push_back(src.substr(start, end - start)); - if(end == src.size() - 1) - { - res.push_back(""); - break; - } + if(end == src.size() - 1) + { + res.push_back(""); + break; + } start = end + 1; } return true; } - inline string upperStr(const string& strIn) + inline string& upper(string& str) { - string str = strIn; transform(str.begin(), str.end(), str.begin(), (int (*)(int))toupper); return str; } - inline string lowerStr(const string& strIn) + inline string& lower(string& str) { - string str = strIn; transform(str.begin(), str.end(), str.begin(), (int (*)(int))tolower); return str; } @@ -183,40 +335,40 @@ namespace Limonp return str.find(ch) != string::npos; } - inline void extractWords(const string& sentence, vector& words) - { - bool flag = false; - uint lhs = 0, len = 0; - for(uint i = 0; i < sentence.size(); i++) - { - char x = sentence[i]; - if((0x0030 <= x && x<= 0x0039) || (0x0041 <= x && x <= 0x005a ) || (0x0061 <= x && x <= 0x007a)) - { - if(flag) - { - len ++; - } - else - { - lhs = i; - len = 1; - } - flag = true; - } - else - { - if(flag) - { - words.push_back(string(sentence, lhs, len)); - } - flag = false; - } - } - if(flag) - { - words.push_back(string(sentence, lhs, len)); - } - } + //inline void extractWords(const string& sentence, vector& words) + //{ + // bool flag = false; + // uint lhs = 0, len = 0; + // for(uint i = 0; i < sentence.size(); i++) + // { + // char x = sentence[i]; + // if((0x0030 <= x && x<= 0x0039) || (0x0041 <= x && x <= 0x005a ) || (0x0061 <= x && x <= 0x007a)) + // { + // if(flag) + // { + // len ++; + // } + // else + // { + // lhs = i; + // len = 1; + // } + // flag = true; + // } + // else + // { + // if(flag) + // { + // words.push_back(string(sentence, lhs, len)); + // } + // flag = false; + // } + // } + // if(flag) + // { + // words.push_back(string(sentence, lhs, len)); + // } + //} } diff --git a/limonp/typedefs.h b/limonp/typedefs.h deleted file mode 100644 index a8da002..0000000 --- a/limonp/typedefs.h +++ /dev/null @@ -1,21 +0,0 @@ -/************************************ - * file enc : utf8 - * author : wuyanyi09@gmail.com -************************************/ -#ifndef LIMONP_TYPEDEFS_H -#define LIMONP_TYPEDEFS_H - -#include -#include -#include -#include -#define HashMap std::tr1::unordered_map - -namespace Limonp -{ - typedef std::vector Unicode; - typedef std::vector::const_iterator UnicodeConstIterator; -} - - -#endif diff --git a/limonp/vec_functs.hpp b/limonp/vec_functs.hpp deleted file mode 100644 index ac18548..0000000 --- a/limonp/vec_functs.hpp +++ /dev/null @@ -1,142 +0,0 @@ -/************************************ - * file enc : ascii - * author : wuyanyi09@gmail.com -************************************/ -#ifndef LIMONP_VEC_FUNCTS_H -#define LIMONP_VEC_FUNCTS_H -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define FOR_VECTOR(vec, i) for(size_t i = 0; i < vec.size(); i++) - -#define PRINT_VECTOR(vec) FOR_VECTOR(vec, i)\ -{\ - cout< - bool vecToString(const vector& vec, string& res) - { - if(vec.empty()) - { - res = "[]"; - return false; - } - stringstream ss; - ss<<"[\""< - string vecToString(const vector& vec) - { - string res; - vecToString(vec, res); - return res; - } - - template - bool isInVec(const vector& vec, const T& item) - { - typename vector::const_iterator it = find(vec.begin(), vec.end(), item); - return it != vec.end(); - } - template - void splitVec(const vector& vecSrc, vector< pair > >& outVec, const vector& patterns) - { - vector tmp; - T pattern; - size_t patternSize = patterns.size(); - for(size_t i = 0; i < vecSrc.size(); i++) - { - size_t patternPos = patternSize; - for(size_t j = 0; j < patternSize; j++) - { - if(patterns[j] == vecSrc[i]) - { - patternPos = j; - break; - } - } - if(patternPos != patternSize) - { - if(!tmp.empty()) - { - outVec.push_back(make_pair >(pattern, tmp)); - tmp.clear(); - } - pattern = patterns[patternPos]; - } - else - { - tmp.push_back(vecSrc[i]); - } - } - if(!tmp.empty()) - { - outVec.push_back(make_pair >(pattern, tmp)); - } - } - - template - void splitVec(const vector& vecSrc, vector< vector >& outVec, const vector& patternVec) - { - vector tmp; - for(size_t i = 0; i < vecSrc.size(); i++) - { - bool flag = false; - for(size_t j = 0; j < patternVec.size(); j++) - { - if(patternVec[j] == vecSrc[i]) - { - flag = true; - break; - } - } - if(flag) - { - if(!tmp.empty()) - { - outVec.push_back(tmp); - tmp.clear(); - } - } - else - { - tmp.push_back(vecSrc[i]); - } - } - if(!tmp.empty()) - { - outVec.push_back(tmp); - } - } -} - -#endif