mirror of
https://github.com/yanyiwu/cppjieba.git
synced 2025-07-18 00:00:12 +08:00
modify test/segment_demo.cpp
This commit is contained in:
parent
f8487fd9cf
commit
57ef504d9b
@ -1,6 +1,6 @@
|
||||
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR})
|
||||
|
||||
ADD_EXECUTABLE(segment.demo segment.cpp)
|
||||
ADD_EXECUTABLE(segment.demo segment_demo.cpp)
|
||||
ADD_EXECUTABLE(keyword.demo keyword_demo.cpp)
|
||||
ADD_EXECUTABLE(tagging.demo tagging_demo.cpp)
|
||||
ADD_EXECUTABLE(load_test load_test.cpp)
|
||||
|
@ -1,60 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include "../src/MPSegment.hpp"
|
||||
#include "../src/HMMSegment.hpp"
|
||||
#include "../src/MixSegment.hpp"
|
||||
|
||||
using namespace CppJieba;
|
||||
|
||||
void cut(const ISegment * seg, const char * const filePath)
|
||||
{
|
||||
ifstream ifile(filePath);
|
||||
vector<string> res;
|
||||
string line;
|
||||
while(getline(ifile, line))
|
||||
{
|
||||
if(!line.empty())
|
||||
{
|
||||
res.clear();
|
||||
seg->cut(line, res);
|
||||
cout<<join(res.begin(), res.end(),"/")<<endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const char * const TEST_FILE = "../test/testdata/testlines.utf8";
|
||||
const char * const JIEBA_DICT_FILE = "../dict/jieba.dict.utf8";
|
||||
const char * const HMM_DICT_FILE = "../dict/hmm_model.utf8";
|
||||
|
||||
int main(int argc, char ** argv)
|
||||
{
|
||||
//demo
|
||||
{
|
||||
MPSegment seg(JIEBA_DICT_FILE);
|
||||
if(!seg)
|
||||
{
|
||||
cout<<"seg init failed."<<endl;
|
||||
return false;
|
||||
}
|
||||
cut(&seg, TEST_FILE);
|
||||
}
|
||||
{
|
||||
HMMSegment seg(HMM_DICT_FILE);
|
||||
if(!seg)
|
||||
{
|
||||
cout<<"seg init failed."<<endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
cut(&seg, TEST_FILE);
|
||||
}
|
||||
{
|
||||
MixSegment seg(JIEBA_DICT_FILE, HMM_DICT_FILE);
|
||||
if(!seg)
|
||||
{
|
||||
cout<<"seg init failed."<<endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
cut(&seg, TEST_FILE);
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
59
test/segment_demo.cpp
Normal file
59
test/segment_demo.cpp
Normal file
@ -0,0 +1,59 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
#define LOGGER_LEVEL LL_WARN
|
||||
|
||||
#include "../src/MPSegment.hpp"
|
||||
#include "../src/HMMSegment.hpp"
|
||||
#include "../src/MixSegment.hpp"
|
||||
|
||||
using namespace CppJieba;
|
||||
|
||||
const char * const TEST_FILE = "../test/testdata/testlines.utf8";
|
||||
const char * const JIEBA_DICT_FILE = "../dict/jieba.dict.utf8";
|
||||
const char * const HMM_DICT_FILE = "../dict/hmm_model.utf8";
|
||||
const char * const USER_DICT_FILE = "../test/testdata/userdict.utf8";
|
||||
|
||||
void cut(const ISegment& seg, const char * const filePath)
|
||||
{
|
||||
ifstream ifile(filePath);
|
||||
vector<string> words;
|
||||
string line;
|
||||
string res;
|
||||
while(getline(ifile, line))
|
||||
{
|
||||
if(!line.empty())
|
||||
{
|
||||
words.clear();
|
||||
seg.cut(line, words);
|
||||
join(words.begin(), words.end(), res, "/");
|
||||
cout<< res <<endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char ** argv)
|
||||
{
|
||||
{
|
||||
printf("\e[32m%s\e[0m\n", "[demo] MPSegment"); // colorful
|
||||
MPSegment seg(JIEBA_DICT_FILE);
|
||||
cut(seg, TEST_FILE);
|
||||
}
|
||||
{
|
||||
printf("\e[32m%s\e[0m\n", "[demo] HMMSegment"); // colorful
|
||||
HMMSegment seg(HMM_DICT_FILE);
|
||||
cut(seg, TEST_FILE);
|
||||
}
|
||||
{
|
||||
printf("\e[32m%s\e[0m\n", "[demo] MixSegment"); // colorful
|
||||
MixSegment seg(JIEBA_DICT_FILE, HMM_DICT_FILE);
|
||||
cut(seg, TEST_FILE);
|
||||
}
|
||||
{
|
||||
printf("\e[32m%s\e[0m\n", "[demo] MixSegment with UserDict"); // colorful
|
||||
MixSegment seg(JIEBA_DICT_FILE, HMM_DICT_FILE, USER_DICT_FILE);
|
||||
cut(seg, TEST_FILE);
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
8
test/testdata/testlines.utf8
vendored
8
test/testdata/testlines.utf8
vendored
@ -1,11 +1,7 @@
|
||||
我来到北京清华大学
|
||||
他来到了网易杭研大厦
|
||||
杭研
|
||||
小明硕士毕业于中国科学院计算所,后在日本京都大学深造
|
||||
我来自北京邮电大学。。。 学号 091111xx。。。
|
||||
来这里看看别人正在搜索什么吧
|
||||
我来到南京市长江大桥
|
||||
请在一米线外等候
|
||||
人事处女干事
|
||||
去医院做B超,叫号123
|
||||
B超 T恤
|
||||
去医院做B超,编号123
|
||||
令狐冲是云计算行业的专家
|
||||
|
Loading…
x
Reference in New Issue
Block a user