update cppjieba to adapter new limonp

This commit is contained in:
wyy 2013-10-30 23:11:51 -07:00
parent 3089e9bf95
commit b0521c6d6e
14 changed files with 340 additions and 341 deletions

View File

@ -8,7 +8,6 @@
#include "globals.h" #include "globals.h"
#include <str_functs.hpp> #include <str_functs.hpp>
#include <vec_functs.hpp>
namespace CppJieba namespace CppJieba
{ {

View File

@ -74,7 +74,7 @@ namespace CppJieba
KeyWordInfo(const TrieNodeInfo& trieNodeInfo):TrieNodeInfo(trieNodeInfo) KeyWordInfo(const TrieNodeInfo& trieNodeInfo):TrieNodeInfo(trieNodeInfo)
{ {
} }
inline string toString() const string toString() const
{ {
string tmp; string tmp;
TransCode::encode(word, tmp); TransCode::encode(word, tmp);
@ -89,16 +89,23 @@ namespace CppJieba
return *this; return *this;
} }
}; };
inline string joinWordInfos(const vector<KeyWordInfo>& vec) inline ostream& operator << (ostream& os, const KeyWordInfo& info)
{ {
vector<string> tmp; string tmp;
for(uint i = 0; i < vec.size(); i++) TransCode::encode(info.word, tmp);
{ return os << "{words:" << tmp << ", weight:" << info.weight << ", idf:" << info.idf << "}";
tmp.push_back(vec[i].toString());
}
return joinStr(tmp, ",");
} }
//inline string joinWordInfos(const vector<KeyWordInfo>& vec)
//{
// vector<string> tmp;
// for(uint i = 0; i < vec.size(); i++)
// {
// tmp.push_back(vec[i].toString());
// }
// return joinStr(tmp, ",");
//}
} }
#endif #endif

View File

@ -23,7 +23,7 @@ void testKeyWordExt(const char * dictPath, const char * filePath)
if(!line.empty()) if(!line.empty())
{ {
ext.extract(line, res, 20); ext.extract(line, res, 20);
cout<<line<<'\n'<<joinWordInfos(res)<<endl; cout<<line<<'\n'<<res<<endl;
} }
} }

View File

@ -44,7 +44,7 @@ void cut(const ISegment * seg, const char * const filePath)
{ {
res.clear(); res.clear();
seg->cut(line, res); seg->cut(line, res);
cout<<line<<"\n"<<joinStr(res,"/")<<endl; cout<<line<<"\n"<<join(res.begin(), res.end(),"/")<<endl;
} }
} }
} }

View File

@ -31,7 +31,7 @@ class ServerDemo: public IRequestHandler
httpReq.GET("key", tmp); httpReq.GET("key", tmp);
URLDecode(tmp, sentence); URLDecode(tmp, sentence);
_segment.cut(sentence, words); _segment.cut(sentence, words);
vecToString(words, strSnd); strSnd << words;
return true; return true;
} }
private: private:

View File

@ -130,7 +130,8 @@ namespace Husky
LogFatal("headerStr illegal."); LogFatal("headerStr illegal.");
return false; return false;
} }
_headerMap[upperStr(k)] = v; upper(k);
_headerMap[k] = v;
lpos = rpos + 1; lpos = rpos + 1;
} }
//message header end //message header end
@ -173,17 +174,17 @@ namespace Husky
} }
public: public:
//string toString() const;// function for debug because of heavy time consuming //string toString() const;// function for debug because of heavy time consuming
string toString() const //string toString() const
{ //{
string res("{"); // string res("{");
res += HashMapToString(_headerMap); // res += HashMapToString(_headerMap);
res += ","; // res += ",";
res += HashMapToString(_methodGetMap); // res += HashMapToString(_methodGetMap);
res += ","; // res += ",";
res += HashMapToString(_methodPostMap); // res += HashMapToString(_methodPostMap);
res += "}"; // res += "}";
return res; // return res;
} //}
private: private:
bool _parseUrl(const string& url, HashMap<string, string>& mp) bool _parseUrl(const string& url, HashMap<string, string>& mp)
{ {

View File

@ -140,7 +140,7 @@ namespace Husky
if(SOCKET_ERROR==nRetCode) if(SOCKET_ERROR==nRetCode)
{ {
LogError("error [%s]", strerror(errno)); LogDebug("error [%s]", strerror(errno));
closesocket(hClientSock); closesocket(hClientSock);
continue; continue;
} }

View File

@ -10,7 +10,6 @@
#include <sstream> #include <sstream>
#include "str_functs.hpp" #include "str_functs.hpp"
#include "map_functs.hpp" #include "map_functs.hpp"
#include "vec_functs.hpp"
namespace Limonp namespace Limonp
{ {
@ -43,12 +42,7 @@ namespace Limonp
} }
~ArgvContext(){}; ~ArgvContext(){};
public: public:
string toString() friend ostream& operator << (ostream& os, const ArgvContext& args);
{
stringstream ss;
ss<<vecToString<string>(_args)<<mapToString<string, string>(_mpss)<<setToString<string>(_sset);
return ss.str();
}
string operator [](uint i) string operator [](uint i)
{ {
if(i < _args.size()) if(i < _args.size())
@ -81,6 +75,16 @@ namespace Limonp
set<string> _sset; set<string> _sset;
}; };
inline ostream& operator << (ostream& os, const ArgvContext& args)
{
return os<<args._args<<args._mpss<<args._sset;
}
//string toString()
//{
// stringstream ss;
// return ss.str();
//}
} }
#endif #endif

View File

@ -6,7 +6,6 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include "logger.hpp" #include "logger.hpp"
#include "vec_functs.hpp"
namespace Limonp namespace Limonp
{ {
@ -21,8 +20,9 @@ namespace Limonp
const char * const USER; const char * const USER;
const char * const PASSWD; const char * const PASSWD;
const char * const DB; const char * const DB;
const char * const CHARSET;
public: 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();}; ~MysqlClient(){dispose();};
public: public:
bool init() bool init()
@ -42,10 +42,17 @@ namespace Limonp
return false; return false;
} }
if(mysql_set_character_set(_conn, CHARSET))
{
LogError("mysql_set_character_set [%s] failed.", CHARSET);
return false;
}
//set reconenct //set reconenct
char value = 1; char value = 1;
mysql_options(_conn, MYSQL_OPT_RECONNECT, &value); mysql_options(_conn, MYSQL_OPT_RECONNECT, &value);
LogInfo("MysqlClient {host: %s, port:%d, database:%s, charset:%s}", HOST, PORT, DB, CHARSET);
return true; return true;
} }
bool dispose() bool dispose()

View File

@ -13,7 +13,6 @@
#include <stdarg.h> #include <stdarg.h>
#include "io_functs.hpp" #include "io_functs.hpp"
#include "str_functs.hpp" #include "str_functs.hpp"
#include "typedefs.h"
#define LogDebug(fmt, ...) Logger::LoggingF(LL_DEBUG, __FILE__, __LINE__, fmt, ## __VA_ARGS__) #define LogDebug(fmt, ...) Logger::LoggingF(LL_DEBUG, __FILE__, __LINE__, fmt, ## __VA_ARGS__)
#define LogInfo(fmt, ...) Logger::LoggingF(LL_INFO, __FILE__, __LINE__, fmt, ## __VA_ARGS__) #define LogInfo(fmt, ...) Logger::LoggingF(LL_INFO, __FILE__, __LINE__, fmt, ## __VA_ARGS__)

View File

@ -11,91 +11,84 @@
#include <set> #include <set>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include "typedefs.h"
#include <tr1/unordered_map>
#define HashMap std::tr1::unordered_map
namespace Limonp namespace Limonp
{ {
using namespace std; using namespace std;
template <typename T> //template <typename T>
string setToString(const set<T>& st) // string setToString(const set<T>& st)
{ // {
if(st.empty()) // if(st.empty())
{ // {
return "{}"; // return "{}";
} // }
stringstream ss; // stringstream ss;
ss<<'{'; // ss<<'{';
typename set<T>::const_iterator it = st.begin(); // typename set<T>::const_iterator it = st.begin();
ss<<*it; // ss<<*it;
it++; // it++;
while(it != st.end()) // while(it != st.end())
{ // {
ss<<", "<<*it; // ss<<", "<<*it;
it++; // it++;
} // }
ss<<'}'; // ss<<'}';
return ss.str(); // return ss.str();
} // }
template<typename T1, typename T2> //template<typename T1, typename T2>
string mapToString(const map<T1, T2>& mp) // string mapToString(const map<T1, T2>& mp)
{ // {
if(mp.empty()) // if(mp.empty())
{ // {
return "{}"; // return "{}";
} // }
stringstream ss; // stringstream ss;
ss<<'{'; // ss<<'{';
typename map<T1, T2>::const_iterator it = mp.begin(); // typename map<T1, T2>::const_iterator it = mp.begin();
ss<<it->first<<": "<<it->second; // ss<<it->first<<": "<<it->second;
it++; // it++;
while(it != mp.end()) // while(it != mp.end())
{ // {
ss<<", "<<it->first<<": "<<it->second; // ss<<", "<<it->first<<": "<<it->second;
it++; // it++;
} // }
ss<<'}'; // ss<<'}';
return ss.str(); // return ss.str();
} // }
template<typename T1, typename T2> //template<typename T1, typename T2>
string HashMapToString(const HashMap<T1, T2>& mp) // string HashMapToString(const HashMap<T1, T2>& mp)
{ // {
if(mp.empty()) // if(mp.empty())
{ // {
return "{}"; // return "{}";
} // }
stringstream ss; // stringstream ss;
ss<<'{'; // ss<<'{';
typename HashMap<T1, T2>::const_iterator it = mp.begin(); // typename HashMap<T1, T2>::const_iterator it = mp.begin();
ss<<it->first<<": "<<it->second; // ss<<it->first<<": "<<it->second;
it++; // it++;
while(it != mp.end()) // while(it != mp.end())
{ // {
ss<<", "<<it->first<<": "<<it->second; // ss<<", "<<it->first<<": "<<it->second;
it++; // it++;
} // }
ss<<'}'; // ss<<'}';
return ss.str(); // return ss.str();
} // }
template<typename T1, typename T2> //template<typename T1, typename T2>
string pairToString(const pair<T1, T2>& p) // string pairToString(const pair<T1, T2>& p)
{ // {
stringstream ss; // stringstream ss;
ss<<p.first<<":"<<p.second; // ss<<p.first<<":"<<p.second;
return ss.str(); // return ss.str();
} // }
template<class kT, class vT>
void printMap(const map<kT, vT>& mp)
{
for(typename map<kT, vT>::const_iterator it = mp.begin(); it != mp.end(); it++)
{
cout<<it->first<<' '<<it->second<<endl;
}
}
template<class kT, class vT> template<class kT, class vT>
vT getMap(const map<kT, vT>& mp, const kT & key, const vT & defaultVal) vT getMap(const map<kT, vT>& mp, const kT & key, const vT & defaultVal)

View File

@ -15,9 +15,14 @@
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#include <memory.h> #include <memory.h>
#include "typedefs.h"
#include <functional> #include <functional>
#include <locale> #include <locale>
#include <sstream>
#include <sys/types.h>
#include <map_functs.hpp>
#define print(x) cout<<(x)<<endl
namespace Limonp namespace Limonp
{ {
using namespace std; using namespace std;
@ -42,7 +47,7 @@ namespace Limonp
} }
return str; return str;
} }
inline void string_format(string& res, const char* fmt, ...) inline void string_format(string& res, const char* fmt, ...)
{ {
int size = 256; int size = 256;
@ -63,27 +68,176 @@ namespace Limonp
} }
} }
inline bool joinStr(const vector<string>& src, string& dest, const string& connectorStr) //inline bool joinStr(const vector<string>& src, string& dest, const string& connectorStr)
{ //{
if(src.empty()) // if(src.empty())
{ // {
return false; // return false;
} // }
for(uint i = 0; i < src.size() - 1; i++) // for(uint i = 0; i < src.size() - 1; i++)
{ // {
dest += src[i]; // dest += src[i];
dest += connectorStr; // dest += connectorStr;
} // }
dest += src[src.size() - 1]; // dest += src[src.size() - 1];
return true; // return true;
} //}
inline string joinStr(const vector<string>& source, const string& connector) //inline string joinStr(const vector<string>& source, const string& connector)
{ //{
string res; // string res;
joinStr(source, res, connector); // joinStr(source, res, connector);
return res; // return res;
} //}
template<class T>
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<class T>
string join(T begin, T end, const string& connector)
{
string res;
join(begin ,end, res, connector);
return res;
}
template<class T1, class T2>
ostream& operator << (ostream& os, const pair<T1, T2>& pr)
{
os << pr.first << ":" << pr.second ;
return os;
}
template<class T>
ostream& operator << (ostream& os, const vector<T>& vec)
{
if(vec.empty())
{
return os << "[]";
}
os<<"[\""<<vec[0];
for(uint i = 1; i < vec.size(); i++)
{
os<<"\", \""<<vec[i];
}
os<<"\"]";
return os;
}
template<class T>
string& operator << (string& str, const T& obj)
{
stringstream ss;
ss << obj; // call ostream& operator << (ostream& os,
return str = ss.str();
}
template<class T1, class T2>
ostream& operator << (ostream& os, const map<T1, T2>& mp)
{
if(mp.empty())
{
os<<"{}";
return os;
}
os<<'{';
typename map<T1, T2>::const_iterator it = mp.begin();
os<<*it;
it++;
while(it != mp.end())
{
os<<", "<<*it;
it++;
}
os<<'}';
return os;
}
//template<class T1, class T2>
// string& operator << (string& str, const map<T1, T2>& mp)
// {
// if(mp.empty())
// {
// str = "{}";
// return str;
// }
// stringstream ss;
// ss<<'{';
// typename map<T1, T2>::const_iterator it = mp.begin();
// ss<<*it;
// it++;
// while(it != mp.end())
// {
// ss<<", "<<*it;
// it++;
// }
// ss<<'}';
// str = ss.str();
// return str;
// }
template<class T1, class T2>
ostream& operator << (ostream& os, const HashMap<T1, T2>& mp)
{
if(mp.empty())
{
return os << "{}";
}
os<<'{';
typename map<T1, T2>::const_iterator it = mp.begin();
os<<*it;
it++;
while(it != mp.end())
{
os<<", "<<*it++;
}
return os<<'}';
}
//template<class T>
// string& operator << (string& str, const set<T>& st)
// {
// stringstream ss;
// ss << st;
// return str = ss.str();
// }
template<class T>
ostream& operator << (ostream& os, const set<T>& st)
{
if(st.empty())
{
os << "{}";
return os;
}
os<<'{';
typename set<T>::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<string>& res, const string& pattern) inline bool splitStr(const string& src, vector<string>& res, const string& pattern)
{ {
@ -104,26 +258,24 @@ namespace Limonp
return true; return true;
} }
res.push_back(src.substr(start, end - start)); res.push_back(src.substr(start, end - start));
if(end == src.size() - 1) if(end == src.size() - 1)
{ {
res.push_back(""); res.push_back("");
break; break;
} }
start = end + 1; start = end + 1;
} }
return true; 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); transform(str.begin(), str.end(), str.begin(), (int (*)(int))toupper);
return str; 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); transform(str.begin(), str.end(), str.begin(), (int (*)(int))tolower);
return str; return str;
} }
@ -183,40 +335,40 @@ namespace Limonp
return str.find(ch) != string::npos; return str.find(ch) != string::npos;
} }
inline void extractWords(const string& sentence, vector<string>& words) //inline void extractWords(const string& sentence, vector<string>& words)
{ //{
bool flag = false; // bool flag = false;
uint lhs = 0, len = 0; // uint lhs = 0, len = 0;
for(uint i = 0; i < sentence.size(); i++) // for(uint i = 0; i < sentence.size(); i++)
{ // {
char x = sentence[i]; // char x = sentence[i];
if((0x0030 <= x && x<= 0x0039) || (0x0041 <= x && x <= 0x005a ) || (0x0061 <= x && x <= 0x007a)) // if((0x0030 <= x && x<= 0x0039) || (0x0041 <= x && x <= 0x005a ) || (0x0061 <= x && x <= 0x007a))
{ // {
if(flag) // if(flag)
{ // {
len ++; // len ++;
} // }
else // else
{ // {
lhs = i; // lhs = i;
len = 1; // len = 1;
} // }
flag = true; // flag = true;
} // }
else // else
{ // {
if(flag) // if(flag)
{ // {
words.push_back(string(sentence, lhs, len)); // words.push_back(string(sentence, lhs, len));
} // }
flag = false; // flag = false;
} // }
} // }
if(flag) // if(flag)
{ // {
words.push_back(string(sentence, lhs, len)); // words.push_back(string(sentence, lhs, len));
} // }
} //}
} }

View File

@ -1,21 +0,0 @@
/************************************
* file enc : utf8
* author : wuyanyi09@gmail.com
************************************/
#ifndef LIMONP_TYPEDEFS_H
#define LIMONP_TYPEDEFS_H
#include <stdint.h>
#include <vector>
#include <sys/types.h>
#include <tr1/unordered_map>
#define HashMap std::tr1::unordered_map
namespace Limonp
{
typedef std::vector<uint16_t> Unicode;
typedef std::vector<uint16_t>::const_iterator UnicodeConstIterator;
}
#endif

View File

@ -1,142 +0,0 @@
/************************************
* file enc : ascii
* author : wuyanyi09@gmail.com
************************************/
#ifndef LIMONP_VEC_FUNCTS_H
#define LIMONP_VEC_FUNCTS_H
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <string>
#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <algorithm>
#include <sstream>
#define FOR_VECTOR(vec, i) for(size_t i = 0; i < vec.size(); i++)
#define PRINT_VECTOR(vec) FOR_VECTOR(vec, i)\
{\
cout<<vec[i]<<endl;\
}
#define PRINT_MATRIX(mat) FOR_VECTOR(mat, i) \
{\
FOR_VECTOR(mat[i], j)\
{\
cout<<"["<<i<<","<<j<<"]:"<<mat[i][j]<<endl;\
}\
}
namespace Limonp
{
using namespace std;
template <typename T>
bool vecToString(const vector<T>& vec, string& res)
{
if(vec.empty())
{
res = "[]";
return false;
}
stringstream ss;
ss<<"[\""<<vec[0];
for(uint i = 1; i < vec.size(); i++)
{
ss<<"\", \""<<vec[i];
}
ss<<"\"]";
res = ss.str();
return true;
}
template <typename T>
string vecToString(const vector<T>& vec)
{
string res;
vecToString(vec, res);
return res;
}
template<typename T>
bool isInVec(const vector<T>& vec, const T& item)
{
typename vector<T>::const_iterator it = find(vec.begin(), vec.end(), item);
return it != vec.end();
}
template<typename T>
void splitVec(const vector<T>& vecSrc, vector< pair<T, vector<T> > >& outVec, const vector<T>& patterns)
{
vector<T> 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<T, vector<T> >(pattern, tmp));
tmp.clear();
}
pattern = patterns[patternPos];
}
else
{
tmp.push_back(vecSrc[i]);
}
}
if(!tmp.empty())
{
outVec.push_back(make_pair<T, vector<T> >(pattern, tmp));
}
}
template<typename T>
void splitVec(const vector<T>& vecSrc, vector< vector<T> >& outVec, const vector<T>& patternVec)
{
vector<T> 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