-
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
28
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
28 commits
Select commit
Hold shift + click to select a range
1c3d6d8
[feature](inverted-index) Add Japanese (Kuromoji) morphological analyzer
nishant94 268c86d
Update Kuromoji analyzer files and formatting
nishant94 2aa5b0d
Implement search mode in Kuromoji analyzer for improved compound deco…
nishant94 7b1cb76
Enhance Kuromoji Viterbi segmenter to support extended mode for unkno…
nishant94 f15be17
Enhance Japanese analyzer tests
nishant94 fddc92f
Add configuration for Kuromoji analyzer support
nishant94 78d1293
fix indentation issues
nishant94 2a777f7
Refactor Kuromoji namespace to inverted_index
nishant94 36a10e9
Update README.md to include Apache License information for Kuromoji d…
nishant94 17f0ef1
Enhance Kuromoji analyzer with strict dictionary validation
nishant94 65bfa26
Fix comment formatting
nishant94 e778764
Refactor Kuromoji Viterbi segmenter for improved efficiency
nishant94 060b662
Fix test case for Kuromoji dictionary
nishant94 00265c3
Update Kuromoji dictionary installation logic for unit-test builds
nishant94 58d766e
Enhance Kuromoji dictionary validation and error handling
nishant94 bf0e0a9
Enhance inverted index parser mode handling
nishant94 f7dd828
Add mecab-ipadic staging in build script
nishant94 6abdb7e
Refactor Japanese analyzer tests for clarity and consistency
nishant94 22bff35
Enhance Kuromoji Viterbi segmenter with penalty handling and caching
nishant94 fc2d00e
Refactor mecab-ipadic staging logic in build script
nishant94 e5bc484
Update Kuromoji analyzer mode handling in inverted index
nishant94 b43199e
Refactor Kuromoji dictionary building process for improved file handling
nishant94 9951a3b
Add first_codepoint function to KuromojiTokenizer for UTF-8 handling
nishant94 2b86871
Add offline generator for Kuromoji dictionary in CMake configuration
nishant94 b330e3b
Fix BE UT (MacOS) workflow
nishant94 f4bf514
Enhance Kuromoji dictionary loading and testing process
nishant94 0bf9cc3
Refactor Japanese analyzer tests to use inline assertions
nishant94 5bda577
Enhance Kuromoji dictionary build process with atomic file handling
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Major] Keep generated dictionary outputs scoped to the current build tree. KUROMOJI_IPADIC_SRC is a per-tree cache variable and the README documents overriding it, but KUROMOJI_DICT_OUT is this shared source-tree directory and the install rule copies it. If tree B successfully generates from a custom source, its newer files can make tree A's default-source edge look up to date, so A silently packages B's dictionary; concurrent trees also race the same temporary paths. Generate and install from a build/config-specific directory with a provenance/completion stamp, and cover two build trees configured with different sources.