Skip to content
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

follow opencc conversion chain #688

Merged
merged 3 commits into from
Aug 12, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 37 additions & 10 deletions src/rime/gear/simplifier.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,46 @@ class Opencc {
}

bool ConvertWord(const string& text, vector<string>* forms) {
if (dict_ == nullptr)
return false;
opencc::Optional<const opencc::DictEntry*> item = dict_->Match(text);
if (item.IsNull()) {
// Match not found
if (converter_ == nullptr) {
return false;
} else {
const opencc::DictEntry* entry = item.Get();
for (auto&& value : entry->Values()) {
forms->push_back(std::move(value));
}
const list<opencc::ConversionPtr> conversions =
converter_->GetConversionChain()->GetConversions();
vector<string> original_words{text};
bool matched = false;
for (auto conversion : conversions) {
opencc::DictPtr dict = conversion->GetDict();
if (dict == nullptr) {
return false;
}
return forms->size() > 0;
set<string> word_set;
vector<string> converted_words;
for (const auto& original_word : original_words) {
opencc::Optional<const opencc::DictEntry*> item =
dict->Match(original_word);
if (item.IsNull()) {
// Current dictionary doesn't convert the word. We need to keep it for
// other dicts in the chain. e.g. s2t.json expands 里 to 里 and 裏,
// then t2tw.json passes 里 as-is and converts 裏 to 裡.
converted_words.push_back(original_word);
eagleoflqj marked this conversation as resolved.
Show resolved Hide resolved
continue;
}
matched = true;
const opencc::DictEntry* entry = item.Get();
for (const auto& converted_word : entry->Values()) {
if (word_set.insert(converted_word).second) {
converted_words.push_back(converted_word);
}
}
}
original_words.swap(converted_words);
}
if (!matched) {
// No dictionary contains the word
return false;
}
*forms = std::move(original_words);
return forms->size() > 0;
}

bool RandomConvertText(const string& text, string* simplified) {
Expand Down