2021-05-22 09:18:35 +08:00
|
|
|
|
#pragma once
|
2020-12-31 21:14:13 +08:00
|
|
|
|
|
|
|
|
|
#include "limonp/Logging.hpp"
|
2021-05-22 09:18:35 +08:00
|
|
|
|
#include <unordered_set>
|
|
|
|
|
#include "Unicode.hpp"
|
2020-12-31 21:14:13 +08:00
|
|
|
|
|
|
|
|
|
namespace cppjieba {
|
|
|
|
|
|
|
|
|
|
class PreFilter {
|
2021-04-26 15:06:47 +08:00
|
|
|
|
public:
|
2021-05-22 09:18:35 +08:00
|
|
|
|
PreFilter(const std::unordered_set<Rune>& symbols,
|
2021-04-26 15:06:47 +08:00
|
|
|
|
const string& sentence)
|
|
|
|
|
: symbols_(symbols) {
|
2021-05-22 09:18:35 +08:00
|
|
|
|
if (!DecodeRunesInString(sentence, sentence_)) {
|
|
|
|
|
XLOG(ERROR) << "decode failed. "<<sentence;
|
2021-04-26 15:06:47 +08:00
|
|
|
|
}
|
2021-05-22 09:18:35 +08:00
|
|
|
|
|
2021-04-26 15:06:47 +08:00
|
|
|
|
cursor_ = sentence_.begin();
|
|
|
|
|
}
|
|
|
|
|
~PreFilter() {
|
|
|
|
|
}
|
|
|
|
|
bool HasNext() const {
|
|
|
|
|
return cursor_ != sentence_.end();
|
2020-12-31 21:14:13 +08:00
|
|
|
|
}
|
2021-06-07 15:37:06 +08:00
|
|
|
|
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;
|
|
|
|
|
|
2021-06-07 15:37:06 +08:00
|
|
|
|
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个字
|
2021-06-23 15:50:19 +08:00
|
|
|
|
wordRange.right = cursor_;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2021-06-07 15:37:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wordRange.right = sentence_.end();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-22 09:18:35 +08:00
|
|
|
|
WordRange Next() {
|
|
|
|
|
WordRange range(cursor_, cursor_);
|
|
|
|
|
|
|
|
|
|
while (cursor_ != sentence_.end()) {
|
2021-05-28 15:55:26 +08:00
|
|
|
|
//if (IsIn(symbols_, cursor_->rune)) {
|
|
|
|
|
if (cursor_->rune == 0x20) {
|
2021-05-22 09:18:35 +08:00
|
|
|
|
if (range.left == cursor_) {
|
2021-04-26 15:06:47 +08:00
|
|
|
|
cursor_ ++;
|
|
|
|
|
}
|
2021-05-22 09:18:35 +08:00
|
|
|
|
|
|
|
|
|
range.right = cursor_;
|
2021-04-26 15:06:47 +08:00
|
|
|
|
return range;
|
|
|
|
|
}
|
2021-05-22 09:18:35 +08:00
|
|
|
|
|
2021-04-26 15:06:47 +08:00
|
|
|
|
cursor_ ++;
|
2020-12-31 21:14:13 +08:00
|
|
|
|
}
|
2021-05-22 09:18:35 +08:00
|
|
|
|
|
|
|
|
|
range.right = sentence_.end();
|
2020-12-31 21:14:13 +08:00
|
|
|
|
return range;
|
|
|
|
|
}
|
2021-04-26 15:06:47 +08:00
|
|
|
|
private:
|
|
|
|
|
RuneStrArray::const_iterator cursor_;
|
|
|
|
|
RuneStrArray sentence_;
|
2021-05-22 09:18:35 +08:00
|
|
|
|
const std::unordered_set<Rune>& symbols_;
|
2020-12-31 21:14:13 +08:00
|
|
|
|
}; // class PreFilter
|
|
|
|
|
|
|
|
|
|
} // namespace cppjieba
|
|
|
|
|
|