-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[feature](inverted-index) Add Japanese (Kuromoji) morphological analyzer #64667
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nishant94
wants to merge
24
commits into
apache:master
Choose a base branch
from
nishant94:feat/kuromoji-japanese-analyzer
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
098f16c
[feature](inverted-index) Add Japanese (Kuromoji) morphological analyzer
nishant94 47c2041
Update Kuromoji analyzer files and formatting
nishant94 4cf1bc8
Implement search mode in Kuromoji analyzer for improved compound deco…
nishant94 2400e58
Enhance Kuromoji Viterbi segmenter to support extended mode for unkno…
nishant94 df64d0b
Enhance Japanese analyzer tests
nishant94 263f3e6
Add configuration for Kuromoji analyzer support
nishant94 2613c3e
fix indentation issues
nishant94 97d0d98
Refactor Kuromoji namespace to inverted_index
nishant94 3c296c9
Update README.md to include Apache License information for Kuromoji d…
nishant94 0bada61
Enhance Kuromoji analyzer with strict dictionary validation
nishant94 386b40f
Fix comment formatting
nishant94 7c31e87
Refactor Kuromoji Viterbi segmenter for improved efficiency
nishant94 c047644
Fix test case for Kuromoji dictionary
nishant94 55c2675
Update Kuromoji dictionary installation logic for unit-test builds
nishant94 d4cf17f
Enhance Kuromoji dictionary validation and error handling
nishant94 416bc10
Enhance inverted index parser mode handling
nishant94 94818f6
Add mecab-ipadic staging in build script
nishant94 95f158a
Refactor Japanese analyzer tests for clarity and consistency
nishant94 13114b1
Enhance Kuromoji Viterbi segmenter with penalty handling and caching
nishant94 66a0520
Refactor mecab-ipadic staging logic in build script
nishant94 1661eda
Update Kuromoji analyzer mode handling in inverted index
nishant94 c553ef2
Refactor Kuromoji dictionary building process for improved file handling
nishant94 9842cf7
Add first_codepoint function to KuromojiTokenizer for UTF-8 handling
nishant94 d67498f
Add offline generator for Kuromoji dictionary in CMake configuration
nishant94 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| <!-- | ||
| Licensed to the Apache Software Foundation (ASF) under one | ||
| or more contributor license agreements. See the NOTICE file | ||
| distributed with this work for additional information | ||
| regarding copyright ownership. The ASF licenses this file | ||
| to you under the Apache License, Version 2.0 (the | ||
| "License"); you may not use this file except in compliance | ||
| with the License. You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, | ||
| software distributed under the License is distributed on an | ||
| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| KIND, either express or implied. See the License for the | ||
| specific language governing permissions and limitations | ||
| under the License. | ||
| --> | ||
|
|
||
| # Kuromoji (Japanese) dictionary | ||
|
|
||
| This directory holds the compiled IPADIC dictionary consumed at runtime by the | ||
| `kuromoji` inverted-index analyzer (`KuromojiAnalyzer` → `KuromojiDictionary`): | ||
|
|
||
| - `system.bin` — surface→word Darts trie + word entries + feature blob | ||
| - `matrix.bin` — connection-cost matrix (1316×1316) | ||
| - `chardef.bin` — character-category map + per-category flags | ||
| - `unkdict.bin` — unknown-word entries per category | ||
|
|
||
| These `*.bin` files are **generated** (not committed; see `.gitignore`). The | ||
| runtime resolves them at `${inverted_index_dict_path}/kuromoji` | ||
| (default `${DORIS_HOME}/dict/kuromoji`); `be/CMakeLists.txt` installs this | ||
| directory into the BE package. | ||
|
|
||
| ## How it's (re)generated | ||
|
|
||
| Source: the UTF-8 IPADIC from <https://github.com/lindera/mecab-ipadic> | ||
| (tag `2.7.0-20250920`) — the original `mecab-ipadic-2.7.0-20070801` lexicon | ||
| converted to UTF-8 (license: NAIST-2003, see `dist/licenses/LICENSE-ipadic.txt`). | ||
|
|
||
| Automated, two steps: | ||
|
|
||
| ```bash | ||
| # 1. thirdparty fetches + stages the UTF-8 IPADIC source into | ||
| # ${DORIS_THIRDPARTY}/installed/share/mecab-ipadic-2.7.0-20250920 | ||
| sh thirdparty/build-thirdparty.sh mecab_ipadic | ||
|
|
||
| # 2. the CMake target builds the offline compiler and produces the *.bin here | ||
| ninja -C be/ut_build_RELEASE kuromoji_dict | ||
| ``` | ||
|
|
||
| CI/release should run `ninja kuromoji_dict` before packaging; the BE `install` | ||
| rule then ships this directory. Override the source dir with | ||
| `-DKUROMOJI_IPADIC_SRC=<path>` at CMake configure time. (The tool can also be | ||
| run directly: `kuromoji_build_dict <utf8_ipadic_src_dir> be/dict/kuromoji`.) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
be/src/storage/index/inverted/analyzer/kuromoji/KuromojiAnalyzer.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <memory> | ||
| #include <string> | ||
|
|
||
| #include "common/exception.h" | ||
| #include "common/logging.h" | ||
| #include "storage/index/inverted/analyzer/kuromoji/KuromojiTokenizer.h" | ||
| #include "storage/index/inverted/analyzer/kuromoji/dict/kuromoji_dictionary.h" | ||
|
|
||
| namespace doris::segment_v2 { | ||
|
|
||
| class KuromojiAnalyzer : public Analyzer { | ||
| public: | ||
| KuromojiAnalyzer() { | ||
| _lowercase = true; | ||
| _ownReader = false; | ||
| } | ||
| ~KuromojiAnalyzer() override = default; | ||
|
|
||
| bool isSDocOpt() override { return true; } | ||
|
|
||
| // Loads (once, process-wide) the IPADIC dictionary from `dictPath`. | ||
| void initDict(const std::string& dictPath) override { | ||
| dict_ = inverted_index::kuromoji::KuromojiDictionary::get_or_load(dictPath); | ||
| if (dict_ == nullptr) { | ||
| throw doris::Exception( | ||
| doris::ErrorCode::INVERTED_INDEX_ANALYZER_ERROR, | ||
| "kuromoji dictionary could not be loaded from {}; ensure system.bin, " | ||
| "matrix.bin, chardef.bin and unkdict.bin are present in the BE package", | ||
| dictPath); | ||
| } | ||
| } | ||
|
|
||
| void setMode(KuromojiMode mode) { mode_ = mode; } | ||
|
|
||
| TokenStream* tokenStream(const TCHAR* fieldName, lucene::util::Reader* reader) override { | ||
| auto* tokenizer = _CLNEW KuromojiTokenizer(mode_, _lowercase, _ownReader, dict_); | ||
| tokenizer->reset(reader); | ||
| return (TokenStream*)tokenizer; | ||
| } | ||
|
|
||
| TokenStream* reusableTokenStream(const TCHAR* fieldName, | ||
| lucene::util::Reader* reader) override { | ||
| if (tokenizer_ == nullptr) { | ||
| tokenizer_ = std::make_unique<KuromojiTokenizer>(mode_, _lowercase, _ownReader, dict_); | ||
| } | ||
| tokenizer_->reset(reader); | ||
| return (TokenStream*)tokenizer_.get(); | ||
| } | ||
|
|
||
| private: | ||
| const inverted_index::kuromoji::KuromojiDictionary* dict_ {nullptr}; | ||
| KuromojiMode mode_ {KuromojiMode::Search}; | ||
| std::unique_ptr<KuromojiTokenizer> tokenizer_; | ||
| }; | ||
|
|
||
| } // namespace doris::segment_v2 |
48 changes: 48 additions & 0 deletions
48
be/src/storage/index/inverted/analyzer/kuromoji/KuromojiMode.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <string> | ||
|
|
||
| #include "common/exception.h" | ||
|
|
||
| namespace doris::segment_v2 { | ||
|
|
||
| // Segmentation mode, mirroring Lucene's JapaneseTokenizer.Mode. Normal returns | ||
| // the minimum-cost segmentation. Search additionally decomposes long compounds | ||
| // into their shorter parts (via a length-based cost penalty) for better search | ||
| // recall. Extended applies the Search penalty and also splits unknown | ||
| // (out-of-vocabulary) words into per-character unigrams. | ||
| enum class KuromojiMode { Normal, Search, Extended }; | ||
|
|
||
| inline KuromojiMode kuromoji_mode_from_string(const std::string& mode) { | ||
| if (mode.empty() || mode == "search") { | ||
| return KuromojiMode::Search; | ||
| } | ||
| if (mode == "normal") { | ||
| return KuromojiMode::Normal; | ||
| } | ||
| if (mode == "extended") { | ||
| return KuromojiMode::Extended; | ||
| } | ||
| throw doris::Exception(doris::ErrorCode::INVERTED_INDEX_ANALYZER_ERROR, | ||
| "Invalid kuromoji parser_mode: '{}', must be search, normal or extended", | ||
| mode); | ||
| } | ||
|
|
||
| } // namespace doris::segment_v2 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.