cppjieba/test/demo.cpp

73 lines
2.4 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/");
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;
}