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

Improve ordering and spell checking in pinyin for upper case input #193

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
39 changes: 27 additions & 12 deletions im/pinyin/pinyin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,18 @@ void PinyinEngine::updatePredict(InputContext *inputContext) {
inputContext->updateUserInterface(UserInterfaceComponent::InputPanel);
}

int englishNess(const std::string &input, bool sp) {
std::tuple<bool, int> englishNess(const std::string &input, bool sp) {
const auto pys = stringutils::split(input, " ");
constexpr int fullWeight = -2;
constexpr int shortWeight = 3;
constexpr int invalidWeight = 6;
constexpr int defaultWeight = shortWeight;
int weight = 0;
if (std::any_of(input.begin(), input.end(), charutils::isupper)) {
return {true,
std::max<size_t>(1, (invalidWeight * pys.size() + 7) / 10)};
}

for (const auto &py : pys) {
if (sp) {
if (py.size() == 2) {
Expand All @@ -207,7 +212,7 @@ int englishNess(const std::string &input, bool sp) {
} else {
auto firstChr = py[0];
if (firstChr == '\'') {
return 0;
return {false, 0};
}
if (firstChr == 'i' || firstChr == 'u' || firstChr == 'v') {
weight += invalidWeight;
Expand All @@ -223,9 +228,9 @@ int englishNess(const std::string &input, bool sp) {
}

if (weight < 0) {
return 0;
return {false, 0};
}
return (weight + 7) / 10;
return {false, (weight + 7) / 10};
}

bool isStroke(const std::string &input) {
Expand Down Expand Up @@ -488,7 +493,8 @@ void PinyinEngine::updateUI(InputContext *inputContext) {
if (*config_.spellEnabled && spell() &&
parsedPyCursor >= selectedSentence.size() &&
selectedLength <= context.cursor()) {
int engNess = englishNess(parsedPy, context.useShuangpin());
auto [hasUpper, engNess] =
englishNess(parsedPy, context.useShuangpin());
if (engNess) {
parsedPyCursor -= selectedSentence.length();
parsedPy = parsedPy.substr(
Expand All @@ -498,14 +504,23 @@ void PinyinEngine::updateUI(InputContext *inputContext) {
: std::string::npos);
auto results = spell()->call<ISpell::hintWithProvider>(
"en", SpellProvider::Custom, pyBeforeCursor, engNess);
std::string bestSentence;
if (!candidates.empty()) {
bestSentence = candidates[0].toString();

// Our hint doesn't work well with mixed case, so, always put a
// word as is.
if (hasUpper && !pyBeforeCursor.empty()) {
if (std::find(results.begin(), results.end(),
pyBeforeCursor) == results.end()) {
if (!charutils::isupper(pyBeforeCursor[0])) {
results.insert(results.begin(), pyBeforeCursor);
} else {
results.push_back(pyBeforeCursor);
}
}
}
int position = 1;
for (auto &result : results) {
if (customCandidateSet.count(result) ||
context.candidatesToCursorSet().count(result)) {

int position = hasUpper ? 0 : 1;
for (const auto &result : results) {
if (customCandidateSet.count(result)) {
continue;
}
extraCandidates.push_back(
Expand Down
33 changes: 32 additions & 1 deletion test/testpinyin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,36 @@ void testSelectByChar(Instance *instance) {
});
}

void testUppercase(Instance *instance) {
instance->eventDispatcher().schedule([instance]() {
auto *testfrontend = instance->addonManager().addon("testfrontend");
auto uuid =
testfrontend->call<ITestFrontend::createInputContext>("testapp");

testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("Control+space"),
false);

testfrontend->call<ITestFrontend::pushCommitExpectation>("Apple");

testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("A"), false);
testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("p"), false);
testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("p"), false);
testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("l"), false);
testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("e"), false);
testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("space"), false);

testfrontend->call<ITestFrontend::pushCommitExpectation>("iPhone");

testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("i"), false);
testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("P"), false);
testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("h"), false);
testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("o"), false);
testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("n"), false);
testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("e"), false);
testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("space"), false);
});
}

void testForget(Instance *instance) {
instance->eventDispatcher().schedule([instance]() {
auto *testfrontend = instance->addonManager().addon("testfrontend");
Expand Down Expand Up @@ -372,13 +402,14 @@ int main() {
char arg0[] = "testpinyin";
char arg1[] = "--disable=all";
char arg2[] = "--enable=testim,testfrontend,pinyin,punctuation,"
"pinyinhelper";
"pinyinhelper,spell";
char *argv[] = {arg0, arg1, arg2};
fcitx::Log::setLogRule("default=5,pinyin=5");
Instance instance(FCITX_ARRAY_SIZE(argv), argv);
instance.addonManager().registerDefaultLoader(nullptr);
testBasic(&instance);
testSelectByChar(&instance);
testUppercase(&instance);
testForget(&instance);
testPin(&instance);
testPunctuation(&instance);
Expand Down
Loading