cppjieba icon indicating copy to clipboard operation
cppjieba copied to clipboard

出现按字分词bug

Open yourbikun opened this issue 1 year ago • 5 comments

能帮我解决下吗 执行结果为 他来到了网易杭研大厦 他/来/到/了/网/易/杭/研/大/厦

我的代码为: #include "Jieba.hpp"

using namespace std; const char* const DICT_PATH = "C:\Users\ins\Desktop\DLP\3rdparty\dict/jieba.dict.utf8"; const char* const HMM_PATH = "C:\Users\ins\Desktop\DLP\3rdparty\dict/hmm_model.utf8"; const char* const USER_DICT_PATH = "C:\Users\ins\Desktop\DLP\3rdparty\dict/user.dict.utf8"; const char* const IDF_PATH = "C:\Users\ins\Desktop\DLP\3rdparty\dict/idf.utf8"; const char* const STOP_WORD_PATH = "C:\Users\ins\Desktop\DLP\3rdparty\dict/stop_words.utf8";

int main(int argc, char** argv) { cppjieba::Jieba jieba(DICT_PATH, HMM_PATH, USER_DICT_PATH, IDF_PATH, STOP_WORD_PATH); string text = "他来到了网易杭研大厦";

std::vector<std::string> words;
jieba.Cut(text, words, false);  // 这里直接获取分词结果到vector
cout << text << endl;
cout << limonp::Join(words.begin(), words.end(), "/") << endl;

return 0;

}

yourbikun avatar Dec 17 '24 07:12 yourbikun

运行在windows上

yourbikun avatar Dec 17 '24 07:12 yourbikun

https://github.com/yanyiwu/cppjieba-demo/blob/main/demo.cpp 你试试这个代码是否跑得起来

yanyiwu avatar Feb 15 '25 07:02 yanyiwu

我一开始在window上也是按字分词,包括运行demo也是按字分词。后来发现要把传入cut函数中的text转为UTF-8格式之后就正常分词了。 我是在Qt中写的代码,一开始写std::string text = "他来到了网易杭研大厦"; jieba.Cut(text, words, false); 这个时候是按字分词的。后面代码改成了QString text = QStringLiteral("他来到了网易杭研大厦"); jieba.Cut(text.toUtf8().data(), words, false); 此时可以正常分词。

HuaiForOne avatar Feb 19 '25 03:02 HuaiForOne

我一开始在window上也是按字分词,包括运行demo也是按字分词。后来发现要把传入cut函数中的text转为UTF-8格式之后就正常分词了。 我是在Qt中写的代码,一开始写std::string text = "他来到了网易杭研大厦"; jieba.Cut(text, words, false); 这个时候是按字分词的。后面代码改成了QString text = QStringLiteral("他来到了网易杭研大厦"); jieba.Cut(text.toUtf8().data(), words, false); 此时可以正常分词。

是的 我已经跑起来了 必须转为utf8格式才能正常运行 在终端运行前先chcp 65001

yourbikun avatar Feb 19 '25 08:02 yourbikun

确实,入参必须转化为utf-8才行

CassidyMojohand avatar Mar 10 '25 11:03 CassidyMojohand