使用boost :: locale / ICU边界分析与中文

时间:2015-03-13 17:25:37

标签: c++ boost icu chinese-locale boost-locale

使用the boost::locale documentation中的示例代码,我无法获得以下内容以正确标记中文文本:

using namespace boost::locale::boundary;
boost::locale::generator gen;
std::string text="中華人民共和國";
ssegment_index map(word,text.begin(),text.end(),gen("zh_CN.UTF-8")); 
for(ssegment_index::iterator it=map.begin(),e=map.end();it!=e;++it)
    std::cout <<"\""<< * it << "\", ";
std::cout << std::endl;

这会将中華人民共和國分成七个不同的字符中/华/人/民/共/和/国,而不是按照预期的中华/人民/共和国。编译Boost的documentation of ICU声称中文应该开箱即用,并使用基于字典的标记生成器正确地分割短语。使用示例日语测试短语&#34;生きるか死ぬか,それが问题だ。&#34;在上面的代码中使用&#34; ja_JP.UTF-8&#34; locale 工作,但这种标记化不依赖于字典,只依赖于汉字/假名边界。

我已经按照建议的here直接在ICU中尝试了相同的代码,但结果是一样的。

UnicodeString text = "中華人民共和國";
UErrorCode status = U_ZERO_ERROR;
BreakIterator* bi = BreakIterator::createWordInstance(Locale::getChinese(), status);
bi->setText(text);
int32_t p = bi->first();
while (p != BreakIterator::DONE) {
    printf("Boundary at position %d\n", p);
    p = bi->next();
}
delete bi;

知道我做错了吗?

1 个答案:

答案 0 :(得分:1)

您最有可能使用5.0之前的ICU版本,这是第一个支持基于字典的中文分词的版本。

另外,请注意,默认情况下,boost使用ICU作为本地后端,因此会产生镜像结果。