Skip to content

Commit

Permalink
Always learn word less than 4 pinyin.
Browse files Browse the repository at this point in the history
  • Loading branch information
wengxt committed Apr 29, 2024
1 parent 245f6f8 commit 21328ab
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 24 deletions.
54 changes: 30 additions & 24 deletions src/libime/pinyin/pinyincontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,41 +201,47 @@ class PinyinContextPrivate : public fcitx::QPtrHolder<PinyinContext> {
if (selected_.size() == 1 && selected_[0].size() == 1) {
return LearnWordResult::Ignored;
}
// Validate the learning word.
// All single || custom || length <= 4
bool hasCustom = false;
size_t totalPinyinLength = 0;
bool isAllSingleWord = true;
for (auto &s : selected_) {
isAllSingleWord =
isAllSingleWord &&
(s.empty() ||
(s.size() == 1 && (s[0].word_.word().empty() ||
s[0].encodedPinyin_.size() == 2)));
for (auto &item : s) {
if (item.word_.word().empty()) {
continue;
}
if (item.custom_) {
hasCustom = true;
break;
}
}
if (hasCustom) {
break;
// We can't learn non pinyin word.
if (item.encodedPinyin_.empty() ||
item.encodedPinyin_.size() % 2 != 0) {
return LearnWordResult::Ignored;
}
totalPinyinLength += item.encodedPinyin_.size() / 2;
}
}
if (!isAllSingleWord && !hasCustom && totalPinyinLength > 4) {
return LearnWordResult::Ignored;
}
for (auto &s : selected_) {
bool first = true;
for (auto &item : s) {
if (!item.word_.word().empty()) {
// We can't learn non pinyin word.
if (item.encodedPinyin_.empty()) {
return LearnWordResult::Ignored;
}
if (item.encodedPinyin_.size() != 2 && !hasCustom) {
return LearnWordResult::Ignored;
}
if (first) {
first = false;
ss += item.word_.word();
if (!pinyin.empty()) {
pinyin.push_back('\'');
}
pinyin += PinyinEncoder::decodeFullPinyin(
item.encodedPinyin_);
} else {
return LearnWordResult::Ignored;
}
if (item.word_.word().empty()) {
continue;
}
assert(!item.encodedPinyin_.empty());
assert(item.encodedPinyin_.size() % 2 == 0);
ss += item.word_.word();
if (!pinyin.empty()) {
pinyin.push_back('\'');
}
pinyin += PinyinEncoder::decodeFullPinyin(item.encodedPinyin_);
}
}

Expand Down
12 changes: 12 additions & 0 deletions test/testpinyinime_unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "libime/pinyin/shuangpinprofile.h"
#include "testdir.h"
#include <fcitx-utils/log.h>
#include <iterator>
#include <memory>

using namespace libime;
Expand Down Expand Up @@ -42,5 +43,16 @@ int main() {
<< c.candidatesToCursorSet();
FCITX_ASSERT(!c.candidatesToCursorSet().count("你好中国"));
FCITX_ASSERT(c.candidatesToCursorSet().count("你好"));
c.setCursor(0);
auto iter = std::find_if(
c.candidates().begin(), c.candidates().end(),
[](const auto &cand) { return cand.toString() == "你好中国"; });
FCITX_ASSERT(iter != c.candidates().end());
FCITX_ASSERT(!ime.dict()->lookupWord(PinyinDictionary::UserDict,
"ni'hao'zhong'guo", "你好中国"));
c.select(std::distance(c.candidates().begin(), iter));
c.learn();
FCITX_ASSERT(ime.dict()->lookupWord(PinyinDictionary::UserDict,
"ni'hao'zhong'guo", "你好中国"));
return 0;
}

0 comments on commit 21328ab

Please sign in to comment.