ukui-search/libchinese-segmentation/cppjieba/PreFilter.hpp

127 lines
3.1 KiB
C++
Raw Normal View History

#pragma once
#include "limonp/Logging.hpp"
#include <unordered_set>
#include "Unicode.hpp"
namespace cppjieba {
class PreFilter {
2021-04-26 15:06:47 +08:00
public:
PreFilter(const std::unordered_set<Rune>& symbols,
2021-04-26 15:06:47 +08:00
const string& sentence)
: symbols_(symbols) {
if (!DecodeRunesInString(sentence, sentence_)) {
XLOG(ERROR) << "decode failed. "<<sentence;
2021-04-26 15:06:47 +08:00
}
2021-04-26 15:06:47 +08:00
cursor_ = sentence_.begin();
}
~PreFilter() {
}
bool HasNext() const {
return cursor_ != sentence_.end();
}
bool Next(WordRange& wordRange) {
if (cursor_ == sentence_.end()) {
return false;
}
wordRange.left = cursor_;
while (cursor_->rune == 0x20 && cursor_ != sentence_.end()) {
cursor_++;
}
if (cursor_ == sentence_.end()) {
wordRange.right = cursor_;
return true;
}
while (++cursor_ != sentence_.end()) {
if (cursor_->rune == 0x20) {
wordRange.right = cursor_;
return true;
}
}
wordRange.right = sentence_.end();
return true;
}
bool Next(WordRange& wordRange, bool& isNull) {
isNull = false;
if (cursor_ == sentence_.end()) {
return false;
}
wordRange.left = cursor_;
if (cursor_->rune == 0x20) {
while (cursor_ != sentence_.end()) {
if (cursor_->rune != 0x20) {
if (wordRange.left == cursor_) {
cursor_ ++;
}
wordRange.right = cursor_;
isNull = true;
return true;
}
cursor_ ++;
}
}
2021-07-07 11:37:00 +08:00
int max_num = 0;
uint32_t utf8_num = cursor_->len;
while (cursor_ != sentence_.end()) {
if (cursor_->rune == 0x20) {
if (wordRange.left == cursor_) {
cursor_ ++;
}
wordRange.right = cursor_;
return true;
}
cursor_ ++;
2021-07-07 11:37:00 +08:00
max_num++;
if (max_num >= 1024 or cursor_->len != utf8_num) { //todo 防止一次性传入过多字节暂定限制为1024个字
wordRange.right = cursor_;
return true;
}
}
wordRange.right = sentence_.end();
return true;
}
WordRange Next() {
WordRange range(cursor_, cursor_);
while (cursor_ != sentence_.end()) {
//if (IsIn(symbols_, cursor_->rune)) {
if (cursor_->rune == 0x20) {
if (range.left == cursor_) {
2021-04-26 15:06:47 +08:00
cursor_ ++;
}
range.right = cursor_;
2021-04-26 15:06:47 +08:00
return range;
}
2021-04-26 15:06:47 +08:00
cursor_ ++;
}
range.right = sentence_.end();
return range;
}
2021-04-26 15:06:47 +08:00
private:
RuneStrArray::const_iterator cursor_;
RuneStrArray sentence_;
const std::unordered_set<Rune>& symbols_;
}; // class PreFilter
} // namespace cppjieba