|
| 1 | +/* |
| 2 | + * Copyright 2026-present Alibaba Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#include "paimon/global_index/lucene/jieba_analyzer.h" |
| 17 | + |
| 18 | +#include "paimon/global_index/lucene/lucene_utils.h" |
| 19 | + |
| 20 | +namespace paimon::lucene { |
| 21 | +JiebaTokenizerContext::JiebaTokenizerContext(const std::string& _tokenize_mode, bool _with_position, |
| 22 | + const std::shared_ptr<cppjieba::Jieba> _jieba, |
| 23 | + const std::shared_ptr<MemoryPool>& _pool, |
| 24 | + int32_t _buffer_size) |
| 25 | + : pool(_pool), |
| 26 | + tokenize_mode(_tokenize_mode), |
| 27 | + with_position(_with_position), |
| 28 | + buffer_size(_buffer_size), |
| 29 | + jieba(_jieba) {} |
| 30 | + |
| 31 | +JiebaTokenizer::JiebaTokenizer(const JiebaTokenizerContext& context, const Lucene::ReaderPtr& input) |
| 32 | + : Lucene::Tokenizer(input), context_(context) { |
| 33 | + term_att_ = addAttribute<Lucene::TermAttribute>(); |
| 34 | + pos_att_ = addAttribute<Lucene::PositionIncrementAttribute>(); |
| 35 | + buffer_ = static_cast<wchar_t*>( |
| 36 | + context_.pool->Malloc(context_.buffer_size * sizeof(wchar_t), /*alignment=*/8)); |
| 37 | +} |
| 38 | + |
| 39 | +JiebaTokenizer::~JiebaTokenizer() { |
| 40 | + if (buffer_) { |
| 41 | + context_.pool->Free(reinterpret_cast<void*>(buffer_), |
| 42 | + context_.buffer_size * sizeof(wchar_t), |
| 43 | + /*alignment=*/8); |
| 44 | + buffer_ = nullptr; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +bool JiebaTokenizer::incrementToken() { |
| 49 | + if (term_index_ >= normalized_terms_.size()) { |
| 50 | + return false; |
| 51 | + } |
| 52 | + |
| 53 | + const auto& term = normalized_terms_[term_index_++]; |
| 54 | + clearAttributes(); |
| 55 | + |
| 56 | + term_att_->setTermBuffer(LuceneUtils::StringToWstring(term)); |
| 57 | + |
| 58 | + if (context_.with_position) { |
| 59 | + pos_att_->setPositionIncrement(1); |
| 60 | + } else { |
| 61 | + pos_att_->setPositionIncrement(0); |
| 62 | + } |
| 63 | + return true; |
| 64 | +} |
| 65 | + |
| 66 | +void JiebaTokenizer::CutWithMode(const std::string& tokenize_mode, const cppjieba::Jieba* jieba, |
| 67 | + const std::string& str, std::vector<std::string>* terms_ptr) { |
| 68 | + auto& terms = *terms_ptr; |
| 69 | + if (tokenize_mode == "mp") { |
| 70 | + jieba->CutSmall(str, terms, /*max_word_len=*/JiebaTokenizerContext::kMaxWordLen); |
| 71 | + } else if (tokenize_mode == "hmm") { |
| 72 | + jieba->CutHMM(str, terms); |
| 73 | + } else if (tokenize_mode == "mix") { |
| 74 | + jieba->Cut(str, terms, /*hmm=*/true); |
| 75 | + } else if (tokenize_mode == "full") { |
| 76 | + jieba->CutAll(str, terms); |
| 77 | + } else if (tokenize_mode == "query") { |
| 78 | + jieba->CutForSearch(str, terms, /*hmm=*/true); |
| 79 | + } else { |
| 80 | + throw Lucene::IllegalArgumentException( |
| 81 | + L"only support mp/hmm/mix/full/query in jieba tokenizer"); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +void JiebaTokenizer::Normalize(const std::unordered_set<std::string>& stop_words, |
| 86 | + std::vector<std::string>* input_ptr, |
| 87 | + std::vector<std::string_view>* output_ptr) { |
| 88 | + auto& input = *input_ptr; |
| 89 | + auto& output = *output_ptr; |
| 90 | + output.clear(); |
| 91 | + output.reserve(input.size()); |
| 92 | + for (auto& term : input) { |
| 93 | + // remove stop words |
| 94 | + if (stop_words.find(term) != stop_words.end()) { |
| 95 | + continue; |
| 96 | + } |
| 97 | + |
| 98 | + // to lower case |
| 99 | + bool is_alphanumeric = true; |
| 100 | + for (const auto& c : term) { |
| 101 | + if (!std::isalnum(static_cast<unsigned char>(c))) { |
| 102 | + is_alphanumeric = false; |
| 103 | + break; |
| 104 | + } |
| 105 | + } |
| 106 | + if (is_alphanumeric && !term.empty()) { |
| 107 | + std::transform(term.begin(), term.end(), term.begin(), [](char ch) { |
| 108 | + return static_cast<char>(std::tolower(static_cast<unsigned char>(ch))); |
| 109 | + }); |
| 110 | + } |
| 111 | + output.emplace_back(term.data(), term.length()); |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +void JiebaTokenizer::reset() { |
| 116 | + Lucene::Tokenizer::reset(); |
| 117 | + InnerReset(); |
| 118 | +} |
| 119 | + |
| 120 | +void JiebaTokenizer::reset(const Lucene::ReaderPtr& input) { |
| 121 | + Lucene::Tokenizer::reset(input); |
| 122 | + InnerReset(); |
| 123 | +} |
| 124 | + |
| 125 | +void JiebaTokenizer::InnerReset() { |
| 126 | + terms_.clear(); |
| 127 | + normalized_terms_.clear(); |
| 128 | + term_index_ = 0; |
| 129 | + |
| 130 | + // read wchar from input |
| 131 | + Lucene::String wstr; |
| 132 | + wstr.reserve(context_.buffer_size); |
| 133 | + while (true) { |
| 134 | + int32_t length = input->read(buffer_, /*offset=*/0, context_.buffer_size); |
| 135 | + if (length <= 0) { |
| 136 | + break; |
| 137 | + } |
| 138 | + wstr.append(buffer_, length); |
| 139 | + } |
| 140 | + |
| 141 | + // jieba tokenize |
| 142 | + std::string doc_str = LuceneUtils::WstringToString(wstr); |
| 143 | + // TODO(xinyu.lxy): support porter2 stemmer |
| 144 | + CutWithMode(context_.tokenize_mode, context_.jieba.get(), doc_str, &terms_); |
| 145 | + Normalize(context_.jieba->extractor.GetStopWords(), &terms_, &normalized_terms_); |
| 146 | +} |
| 147 | + |
| 148 | +} // namespace paimon::lucene |
0 commit comments