-
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
29
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
29 commits
Select commit
Hold shift + click to select a range
d9aaa96
[feature](inverted-index) Add Japanese (Kuromoji) morphological analyzer
nishant94 2df195a
Update Kuromoji analyzer files and formatting
nishant94 0b74053
Implement search mode in Kuromoji analyzer for improved compound deco…
nishant94 9896f79
Enhance Kuromoji Viterbi segmenter to support extended mode for unkno…
nishant94 fc47fec
Enhance Japanese analyzer tests
nishant94 96cc23f
Add configuration for Kuromoji analyzer support
nishant94 b9fe555
fix indentation issues
nishant94 efe1735
Refactor Kuromoji namespace to inverted_index
nishant94 05e4c1a
Update README.md to include Apache License information for Kuromoji d…
nishant94 dd52f0c
Enhance Kuromoji analyzer with strict dictionary validation
nishant94 b88124a
Fix comment formatting
nishant94 8343333
Refactor Kuromoji Viterbi segmenter for improved efficiency
nishant94 ee09cb6
Fix test case for Kuromoji dictionary
nishant94 5a66ca8
Update Kuromoji dictionary installation logic for unit-test builds
nishant94 53f977f
Enhance Kuromoji dictionary validation and error handling
nishant94 3272f35
Enhance inverted index parser mode handling
nishant94 f1066bf
Add mecab-ipadic staging in build script
nishant94 7eee6a0
Refactor Japanese analyzer tests for clarity and consistency
nishant94 9592435
Enhance Kuromoji Viterbi segmenter with penalty handling and caching
nishant94 62954bf
Refactor mecab-ipadic staging logic in build script
nishant94 e595449
Update Kuromoji analyzer mode handling in inverted index
nishant94 9f191e9
Refactor Kuromoji dictionary building process for improved file handling
nishant94 08ade62
Add first_codepoint function to KuromojiTokenizer for UTF-8 handling
nishant94 0dfed53
Add offline generator for Kuromoji dictionary in CMake configuration
nishant94 1ac28cc
Fix BE UT (MacOS) workflow
nishant94 786ac88
Enhance Kuromoji dictionary loading and testing process
nishant94 aeee37c
Refactor Japanese analyzer tests to use inline assertions
nishant94 e1ecbf4
Enhance Kuromoji dictionary build process with atomic file handling
nishant94 562b9f4
Refactor UTF-8 decoding in Kuromoji Viterbi and Tokenizer for improve…
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,60 @@ | ||
| <!-- | ||
| 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`). | ||
|
|
||
| A normal BE build (`sh build.sh`) generates these `*.bin` automatically: the | ||
| `kuromoji_dict` target is part of `ALL` and the `install` rule then ships this | ||
| directory. The target is defined only for real (`MAKE_TEST=OFF`) builds, not for | ||
| the unit-test tree. | ||
|
|
||
| To regenerate manually: | ||
|
|
||
| ```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. run the target in a real (non-test) build tree, e.g. the one sh build.sh | ||
| # creates under be/build_<BUILD_TYPE> (build_Release by default) | ||
| ninja -C be/build_Release kuromoji_dict | ||
| ``` | ||
|
|
||
| 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.
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.