-
Notifications
You must be signed in to change notification settings - Fork 318
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support traditional Chinese for full text search (#1378)
### What problem does this PR solve? Using opencc for converting from tradtional Chinese to simplified Chinese (Just introducing older version of OpenCC for much less dependencies) Issue:#1376 ### Type of change - [x] New Feature (non-breaking change which adds functionality)
- Loading branch information
Showing
33 changed files
with
3,068 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
|
@@ -25,6 +25,7 @@ target_link_libraries( | |
atomic.a | ||
thrift.a | ||
jma | ||
opencc | ||
) | ||
|
||
if(ENABLE_JEMALLOC) | ||
|
This file contains 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 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 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 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 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,73 @@ | ||
// Copyright(C) 2023 InfiniFlow, Inc. All rights reserved. | ||
// | ||
// Licensed 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 | ||
// | ||
// https://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. | ||
|
||
module; | ||
|
||
#include <cstring> | ||
#include <filesystem> | ||
|
||
#include <openccxx.h> | ||
|
||
import stl; | ||
import logger; | ||
import third_party; | ||
import chinese_analyzer; | ||
|
||
module traditional_chinese_analyzer; | ||
|
||
namespace fs = std::filesystem; | ||
|
||
namespace infinity { | ||
static const String OPENCC_PATH = "opencc"; | ||
|
||
namespace fs = std::filesystem; | ||
|
||
TraditionalChineseAnalyzer::TraditionalChineseAnalyzer(const String &path) : ChineseAnalyzer(path) {} | ||
|
||
TraditionalChineseAnalyzer::TraditionalChineseAnalyzer(const TraditionalChineseAnalyzer &other) : ChineseAnalyzer(other) { own_opencc_ = false; } | ||
|
||
TraditionalChineseAnalyzer::~TraditionalChineseAnalyzer() { | ||
if (own_opencc_) { | ||
delete opencc_; | ||
} | ||
} | ||
|
||
Status TraditionalChineseAnalyzer::Load() { | ||
try { | ||
ChineseAnalyzer::Load(); | ||
} catch (const std::exception &e) { | ||
return Status::InvalidAnalyzerFile("Failed to load jieba analyzer"); | ||
} | ||
fs::path root(dict_path_); | ||
fs::path opencc_path(root / OPENCC_PATH); | ||
|
||
if (!fs::exists(opencc_path)) { | ||
return Status::InvalidAnalyzerFile(opencc_path); | ||
} | ||
try { | ||
opencc_ = new ::OpenCC(opencc_path.string()); | ||
} catch (const std::exception &e) { | ||
return Status::InvalidAnalyzerFile("Failed to load OpenCC"); | ||
} | ||
own_opencc_ = true; | ||
return Status::OK(); | ||
} | ||
|
||
void TraditionalChineseAnalyzer::Parse(const String &input) { | ||
String out; | ||
opencc_->convert(input, out); | ||
ChineseAnalyzer::Parse(out); | ||
} | ||
|
||
} // namespace infinity |
This file contains 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,44 @@ | ||
// Copyright(C) 2023 InfiniFlow, Inc. All rights reserved. | ||
// | ||
// Licensed 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 | ||
// | ||
// https://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. | ||
|
||
module; | ||
|
||
export module traditional_chinese_analyzer; | ||
|
||
import stl; | ||
import status; | ||
import chinese_analyzer; | ||
|
||
class OpenCC; | ||
|
||
namespace infinity { | ||
|
||
export class TraditionalChineseAnalyzer : public ChineseAnalyzer { | ||
public: | ||
TraditionalChineseAnalyzer(const String &path); | ||
|
||
TraditionalChineseAnalyzer(const TraditionalChineseAnalyzer &other); | ||
|
||
~TraditionalChineseAnalyzer(); | ||
|
||
Status Load(); | ||
|
||
protected: | ||
void Parse(const String &input) override; | ||
|
||
private: | ||
OpenCC *opencc_{nullptr}; | ||
bool own_opencc_{false}; | ||
}; | ||
} // namespace infinity |
This file contains 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 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,12 @@ | ||
project(OpenCC CXX C) | ||
|
||
add_definitions(-march=native) | ||
|
||
# library target | ||
FILE(GLOB_RECURSE OPENCC_SRC | ||
"${CMAKE_CURRENT_SOURCE_DIR}/*.cpp" | ||
"${CMAKE_CURRENT_SOURCE_DIR}/*.c" | ||
) | ||
add_library(opencc STATIC | ||
${OPENCC_SRC}) | ||
set_target_properties(opencc PROPERTIES POSITION_INDEPENDENT_CODE TRUE) |
Oops, something went wrong.