贵所的ICTCLAS号称是目前最好的中文分词系统。今天使用了一下,记录下心得。
我的需求主要是分词,无需词性标注,而这一点可能恰恰是ICTCLAS的亮点。
主要流程:
ICTCLAS_Init(cfg_path)
ICTCLAS_ParagraphProcess(input, input_len, buffer, ENCODING, 0)
buffer是缓冲区,建议为6 * input_len
0是我不需要词性标注
对这个类包装了一下,方便今后使用。
PS:ICTCLAS确实够慢的,我这里对一个16KB的文档分词,QPS居然只有85……当然了,人家ICTCLAS的标称速度是500KB/s,我这里85*16=1.3MB,已经大于标称速度了……
以后有时间我会关注一下SCWS,开发者也是蛮活跃的。
ICTClas.h
#ifndef ICTCLAS_H
#define ICTCLAS_H
#include <string>
#include <exception>
using std::string;
using std::exception;
//ICTClas Exception
class ICTClasException: public exception
{
public:
//Construct Msg
ICTClasException(const string& msg)
:errmsg(msg)
{
}
//Destruct
~ICTClasException() throw()
{
}
//Return Error Msg
const char* what() const throw()
{
return errmsg.c_str();
}
private:
//Error Msg
string errmsg;
};
//ICTClas Interface
class ICTClas
{
public:
//Init ICTClas
ICTClas(const string& path = "./ictclas");
//Exit ICTClas
~ICTClas();
//Process
bool Process(const string& input, string* output);
};
#endif
ICTClas.cpp
#include "ICTCLAS50.h"
#include "ICTClas.h"
ICTClas::ICTClas(const string& path)
{
if(!ICTCLAS_Init(path.c_str()))
{
throw ICTClasException("ICTCLAS_Init() Fail.");
}
}
ICTClas::~ICTClas()
{
if(!ICTCLAS_Exit())
{
//Do nothing
throw ICTClasException("ICTCLAS_Exit() Fail.");
}
}
bool ICTClas::Process(const string& input, string* output)
{
size_t len, ret_len;
char* buf = NULL;
//Process
len = input.size();
buf = new char[len*6];
ret_len = ICTCLAS_ParagraphProcess(input.c_str(), len, buf, CODE_TYPE_UTF8, 0);
//Assign && delete buffer
output->assign(buf, ret_len);
delete [] buf;
}