cppjieba/test/demo.cpp
yanyiwu 3528b6296a 修改 cjserver 服务,可以通过http参数使用不同切词算法进行切词。
修改 make install 的安装目录,统一安装到同一个目录 /usr/local/cppjieba
2015-06-05 21:59:16 +08:00

77 lines
2.6 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "../src/Application.hpp"
using namespace CppJieba;
void LoadSentences(const string& filepath, vector<string>& sentences) {
ifstream ifile(filepath.c_str());
if(!ifile.is_open()) {
LogFatal("open %s failed.", filepath.c_str());
}
string line;
while(getline(ifile, line)) {
if(!line.empty()) {
sentences.push_back(line);
}
}
}
int main(int argc, char** argv) {
CppJieba::Application app("../dict/jieba.dict.utf8",
"../dict/hmm_model.utf8",
"../dict/user.dict.utf8",
"../dict/idf.utf8",
"../dict/stop_words.utf8");
vector<string> words;
string result;
string s;
vector<string> sentences;
LoadSentences("../test/testdata/testlines.utf8", sentences);
cout << "\n\e[32m" << "[demo] METHOD_MP" << "\e[0m\n"; // colorful
for (size_t i = 0; i < sentences.size(); i++) {
app.cut(sentences[i], words, METHOD_MP);
cout << join(words.begin(), words.end(), "/") << endl;
}
cout << "\n\e[32m" << "[demo] METHOD_HMM" << "\e[0m\n"; // colorful
for (size_t i = 0; i < sentences.size(); i++) {
app.cut(sentences[i], words, METHOD_HMM);
cout << join(words.begin(), words.end(), "/") << endl;
}
cout << "\n\e[32m" << "[demo] METHOD_MIX" << "\e[0m\n"; // colorful
for (size_t i = 0; i < sentences.size(); i++) {
app.cut(sentences[i], words, METHOD_MIX);
cout << join(words.begin(), words.end(), "/") << endl;
}
cout << "\n\e[32m" << "[demo] METHOD_FULL" << "\e[0m\n"; // colorful
for (size_t i = 0; i < sentences.size(); i++) {
app.cut(sentences[i], words, METHOD_FULL);
cout << join(words.begin(), words.end(), "/") << endl;
}
cout << "\n\e[32m" << "[demo] METHOD_QUERY" << "\e[0m\n"; // colorful
for (size_t i = 0; i < sentences.size(); i++) {
app.cut(sentences[i], words, METHOD_QUERY);
cout << join(words.begin(), words.end(), "/") << endl;
}
cout << "\n\e[32m" << "[demo] TAGGING" << "\e[0m\n"; // colorful
vector<pair<string, string> > tagres;
s = "我是蓝翔技工拖拉机学院手扶拖拉机专业的。不用多久我就会升职加薪当上总经理出任CEO迎娶白富美走上人生巅峰。";
app.tag(s, tagres);
cout << s << endl;
cout << tagres << endl;;
cout << "\n\e[32m" << "[demo] KEYWORD" << "\e[0m\n"; // colorful
vector<pair<string, double> > keywordres;
s = "我是拖拉机学院手扶拖拉机专业的。不用多久我就会升职加薪当上CEO走上人生巅峰。";
app.extract(s, keywordres, 5);
cout << s << endl;
cout << keywordres << endl;
return EXIT_SUCCESS;
}