在一个数据处理项目中,我需要检测中文中的拆分词(中文中的词不包含空格)。有没有办法使用原生 c++ 功能或 boost.locale 库来检测中文字符?
问问题
519 次
2 回答
3
一般来说,如果你想在 C++ 中完全支持 Unicode,那么ICU几乎没有办法。Boost 提供了对其功能的一些访问(通过 Boost.Locale 和 Boost.Regex),但它需要在 ICU 支持的情况下编译 Boost。因此,与其确保编译目标平台的 Boost,不如直接使用 ICU API。
如果您正在寻找单词边界,icu::BreakIterator(更具体地说,icu::BreakIterator::createWordInstance)是起点。然后,您通过setText传递要迭代的文本,并通过next等移动迭代器。(是的,ICU 这种方式有点不习惯,因为它起源于 Java 领域)。
或者,如果您不想使用完整的 C++ API,则可以使用ublock_getCode告诉您相关代码点的UBlockCode。
于 2020-06-23T09:52:23.097 回答
1
这是我仅使用 boost 和标准库的尝试:
#include <iostream>
#include <boost/regex/pending/unicode_iterator.hpp>
#include <functional>
#include <algorithm>
using Iter = boost::u8_to_u32_iterator<std::string::const_iterator>;
template <::boost::uint32_t a, ::boost::uint32_t b>
class UnicodeRange
{
static_assert(a <= b, "Proper range");
public:
constexpr bool operator()(::boost::uint32_t x) const noexcept
{
return x >= a && x <= b;
}
};
using UnifiedIdeographs = UnicodeRange<0x4E00, 0x9FFF>;
using UnifiedIdeographsA = UnicodeRange<0x3400, 0x4DBF>;
using UnifiedIdeographsB = UnicodeRange<0x20000, 0x2A6DF>;
using UnifiedIdeographsC = UnicodeRange<0x2A700, 0x2B73F>;
using UnifiedIdeographsD = UnicodeRange<0x2B740, 0x2B81F>;
using UnifiedIdeographsE = UnicodeRange<0x2B820, 0x2CEAF>;
using CompatibilityIdeographs = UnicodeRange<0xF900, 0xFAFF>;
using CompatibilityIdeographsSupplement = UnicodeRange<0x2F800, 0x2FA1F>;
constexpr bool isChineese(::boost::uint32_t x) noexcept
{
return UnifiedIdeographs{}(x)
|| UnifiedIdeographsA{}(x) || UnifiedIdeographsB{}(x) || UnifiedIdeographsC{}(x)
|| UnifiedIdeographsD{}(x) || UnifiedIdeographsE{}(x)
|| CompatibilityIdeographs{}(x) || CompatibilityIdeographsSupplement{}(x);
}
int main()
{
std::string s;
while (std::getline(std::cin, s))
{
auto start = std::find_if(Iter{s.cbegin()}, Iter{s.cend()}, isChineese);
auto stop = std::find_if_not(start, Iter{s.cend()}, isChineese);
std::cout << std::string{start.base(), stop.base()} << '\n';
}
return 0;
}
https://wandbox.org/permlink/FtxKa8D2LtR3ko9t
也许您应该能够将这种方法改进为功能齐全的东西。我不知道如何通过测试正确覆盖这一点,并且不确定哪些字符应包含在此检查中。
于 2020-06-23T16:06:04.297 回答