-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Korea morphological analyzer through mecab (#1860)
### What problem does this PR solve? Use libmecab embedded within ijma, and the dictionary is generated according to the instructions of https://bitbucket.org/eunjeon/mecab-ko-dic Issue link:#1228 ### Type of change - [x] New Feature (non-breaking change which adds functionality)
- Loading branch information
Showing
10 changed files
with
285 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
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) 2024 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; | ||
|
||
#pragma clang diagnostic push | ||
#pragma clang diagnostic ignored "-Wunused-variable" | ||
#pragma clang diagnostic ignored "-Wunused-but-set-variable" | ||
#include <mecab_wrapper.h> | ||
#pragma clang diagnostic pop | ||
|
||
#include <cstring> | ||
#include <filesystem> | ||
#include <iostream> | ||
import stl; | ||
import term; | ||
import analyzer; | ||
import common_analyzer; | ||
import logger; | ||
import status; | ||
import ijma; | ||
|
||
module korea_analyzer; | ||
|
||
namespace fs = std::filesystem; | ||
|
||
namespace infinity { | ||
static const String KNOWLEDGE_PATH = "mecab/ko-dic"; | ||
|
||
KoreaAnalyzer::KoreaAnalyzer(const String &base_path) { | ||
cjk_ = true; | ||
own_mecab_ = true; | ||
fs::path root(base_path); | ||
fs::path knowledge_path(root / KNOWLEDGE_PATH); | ||
knowledge_path_ = "-d " + knowledge_path.string(); | ||
} | ||
|
||
KoreaAnalyzer::KoreaAnalyzer(const KoreaAnalyzer &other) { | ||
cjk_ = true; | ||
knowledge_path_ = other.knowledge_path_; | ||
own_mecab_ = false; | ||
SetCaseSensitive(false); | ||
} | ||
|
||
KoreaAnalyzer::~KoreaAnalyzer() { | ||
if (own_mecab_) | ||
delete mecab_; | ||
} | ||
|
||
Status KoreaAnalyzer::Load() { | ||
try { | ||
mecab_ = new jma::MeCab(knowledge_path_); | ||
} catch (std::logic_error) { | ||
return Status::InvalidAnalyzerFile("Failed to load Korea analyzer"); | ||
} | ||
|
||
SetCaseSensitive(false); | ||
|
||
return Status::OK(); | ||
} | ||
|
||
} // 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,80 @@ | ||
// Copyright(C) 2024 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 korea_analyzer; | ||
|
||
import stl; | ||
import ijma; | ||
import term; | ||
import common_analyzer; | ||
import status; | ||
|
||
namespace infinity { | ||
|
||
export class KoreaAnalyzer : public CommonLanguageAnalyzer { | ||
public: | ||
KoreaAnalyzer(const String &path); | ||
|
||
KoreaAnalyzer(const KoreaAnalyzer &other); | ||
|
||
~KoreaAnalyzer(); | ||
|
||
Status Load(); | ||
|
||
protected: | ||
void Parse(const String &input) override { | ||
mecab_->SetSentence(input); | ||
local_offset_ = -1; | ||
|
||
ResetToken(); | ||
} | ||
|
||
bool NextToken() override { | ||
while (DoNext()) { | ||
mecab_->GetToken(token_str_); | ||
token_ = token_str_.c_str(); | ||
len_ = token_str_.size(); | ||
offset_ = local_offset_; | ||
is_index_ = true; | ||
return true; | ||
} | ||
ResetToken(); | ||
return false; | ||
} | ||
|
||
bool IsAlpha() override { return mecab_->IsAlpha(); } | ||
|
||
bool IsSpecialChar() override { return false; } | ||
|
||
private: | ||
bool DoNext() { | ||
while (!mecab_->IsEnd()) { | ||
mecab_->Next(); | ||
++local_offset_; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
String knowledge_path_; | ||
|
||
jma::MeCab *mecab_{nullptr}; | ||
|
||
bool own_mecab_; | ||
|
||
String token_str_; | ||
}; | ||
} // namespace infinity |
File renamed without changes.
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,38 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
#include "mecab.h" | ||
|
||
namespace jma { | ||
|
||
class MeCab { | ||
public: | ||
MeCab(const std::string &option); | ||
|
||
~MeCab(); | ||
|
||
bool Parse(std::vector<std::string> &out, const char *str, size_t str_len = 0); | ||
|
||
void SetSentence(const std::string &str); | ||
|
||
bool IsAlpha() const; | ||
|
||
bool IsSpecial() const; | ||
|
||
bool IsEnd() const; | ||
|
||
void Next(); | ||
|
||
void GetToken(std::string &out); | ||
|
||
std::string GetFeature(); | ||
|
||
private: | ||
::MeCab::Tagger *tagger_{nullptr}; | ||
const ::MeCab::Node *node_{nullptr}; | ||
char buf_[1024]; | ||
}; | ||
|
||
} // namespace jma |
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,67 @@ | ||
#include "mecab_wrapper.h" | ||
#include "mecab.h" | ||
|
||
namespace jma { | ||
|
||
MeCab::MeCab(const std::string &option) : tagger_(::MeCab::createTagger(option.c_str())) {} | ||
|
||
MeCab::~MeCab() { delete tagger_; } | ||
|
||
bool MeCab::Parse(std::vector<std::string> &out, const char *str, size_t str_len) { | ||
if (str_len == 0) { | ||
str_len = strlen(str); | ||
} | ||
const char *p = tagger_->parse(str, str_len); | ||
if (p == 0) | ||
return false; | ||
while (*p) { | ||
if (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t') { | ||
p++; | ||
continue; | ||
} | ||
const char *q = strchr(p, ' '); | ||
if (q == 0) { | ||
out.push_back(p); | ||
break; | ||
} | ||
out.push_back(std::string(p, q)); | ||
p = q + 1; | ||
} | ||
return true; | ||
} | ||
|
||
void MeCab::SetSentence(const std::string &str) { node_ = tagger_->parseToNode(str.c_str(), str.size()); } | ||
|
||
bool MeCab::IsAlpha() const { | ||
const char *p = node_->feature; | ||
if (node_->length < 2) | ||
return false; | ||
return p[0] == 'S' && p[1] == 'L'; | ||
} | ||
|
||
bool MeCab::IsSpecial() const { | ||
const char *p = node_->feature; | ||
return p[0] == 'S' && p[1] == 'C'; | ||
} | ||
|
||
bool MeCab::IsEnd() const { | ||
if (node_ == nullptr) | ||
return true; | ||
return node_->stat == MECAB_EOS_NODE; | ||
} | ||
|
||
void MeCab::Next() { | ||
// assert(node_); | ||
node_ = node_->next; | ||
} | ||
|
||
void MeCab::GetToken(std::string &out) { | ||
out = node_->surface; | ||
strcpy(buf_, node_->surface); | ||
buf_[node_->length] = '\0'; | ||
out.assign(buf_); | ||
} | ||
|
||
std::string MeCab::GetFeature() { return node_->feature; } | ||
|
||
} // namespace jma |