Skip to content

Pass CorrectionProfile to ShuangpinProfile ctor #204

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

Merged
merged 3 commits into from
Dec 22, 2024
Merged
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
100 changes: 53 additions & 47 deletions im/pinyin/pinyin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,53 @@ void PinyinEngine::populateConfig() {
ime_->setPreeditMode(*config_.showActualPinyinInPreedit
? libime::PinyinPreeditMode::Pinyin
: libime::PinyinPreeditMode::RawText);

// Setup fuzzy flags.
libime::PinyinFuzzyFlags flags;
const auto &fuzzyConfig = *config_.fuzzyConfig;
#define SET_FUZZY_FLAG(VAR, ENUM) \
if (*fuzzyConfig.VAR) { \
flags |= libime::PinyinFuzzyFlag::ENUM; \
}
SET_FUZZY_FLAG(ue, VE_UE)
SET_FUZZY_FLAG(commonTypo, CommonTypo)
SET_FUZZY_FLAG(commonTypo, AdvancedTypo)
SET_FUZZY_FLAG(inner, Inner)
SET_FUZZY_FLAG(innerShort, InnerShort)
SET_FUZZY_FLAG(partialFinal, PartialFinal)
SET_FUZZY_FLAG(partialSp, PartialSp)
SET_FUZZY_FLAG(v, V_U)
SET_FUZZY_FLAG(an, AN_ANG)
SET_FUZZY_FLAG(en, EN_ENG)
SET_FUZZY_FLAG(ian, IAN_IANG)
SET_FUZZY_FLAG(in, IN_ING)
SET_FUZZY_FLAG(ou, U_OU)
SET_FUZZY_FLAG(uan, UAN_UANG)
SET_FUZZY_FLAG(c, C_CH)
SET_FUZZY_FLAG(f, F_H)
SET_FUZZY_FLAG(l, L_N)
SET_FUZZY_FLAG(s, S_SH)
SET_FUZZY_FLAG(z, Z_ZH)

{
std::shared_ptr<libime::PinyinCorrectionProfile> correctionProfile;
switch (*fuzzyConfig.correction) {
case CorrectionLayout::None:
break;
case CorrectionLayout::Qwerty:
correctionProfile =
std::make_shared<libime::PinyinCorrectionProfile>(
libime::BuiltinPinyinCorrectionProfile::Qwerty);
break;
}
ime_->setCorrectionProfile(std::move(correctionProfile));
}

if (ime_->correctionProfile()) {
flags |= libime::PinyinFuzzyFlag::Correction;
}
ime_->setFuzzyFlags(flags);

if (*config_.shuangpinProfile == ShuangpinProfileEnum::Custom) {
auto file = StandardPath::global().open(StandardPath::Type::PkgConfig,
"pinyin/sp.dat", O_RDONLY);
Expand All @@ -994,7 +1041,8 @@ void PinyinEngine::populateConfig() {
never_close_handle);
std::istream in(&buffer);
ime_->setShuangpinProfile(
std::make_shared<libime::ShuangpinProfile>(in));
std::make_shared<libime::ShuangpinProfile>(
in, ime_->correctionProfile().get()));
} catch (const std::exception &e) {
PINYIN_ERROR() << e.what();
}
Expand All @@ -1019,58 +1067,16 @@ void PinyinEngine::populateConfig() {
profile = libime::ShuangpinBuiltinProfile::Ziranma;
break;
}
ime_->setShuangpinProfile(
std::make_shared<libime::ShuangpinProfile>(profile));
ime_->setShuangpinProfile(std::make_shared<libime::ShuangpinProfile>(
profile, ime_->correctionProfile().get()));
}

// Always set a profile to avoid crash.
if (!ime_->shuangpinProfile()) {
ime_->setShuangpinProfile(std::make_shared<libime::ShuangpinProfile>(
libime::ShuangpinBuiltinProfile::Ziranma));
}

libime::PinyinFuzzyFlags flags;
const auto &fuzzyConfig = *config_.fuzzyConfig;
#define SET_FUZZY_FLAG(VAR, ENUM) \
if (*fuzzyConfig.VAR) { \
flags |= libime::PinyinFuzzyFlag::ENUM; \
libime::ShuangpinBuiltinProfile::Ziranma,
ime_->correctionProfile().get()));
}
SET_FUZZY_FLAG(ue, VE_UE)
SET_FUZZY_FLAG(commonTypo, CommonTypo)
SET_FUZZY_FLAG(commonTypo, AdvancedTypo)
SET_FUZZY_FLAG(inner, Inner)
SET_FUZZY_FLAG(innerShort, InnerShort)
SET_FUZZY_FLAG(partialFinal, PartialFinal)
SET_FUZZY_FLAG(partialSp, PartialSp)
SET_FUZZY_FLAG(v, V_U)
SET_FUZZY_FLAG(an, AN_ANG)
SET_FUZZY_FLAG(en, EN_ENG)
SET_FUZZY_FLAG(ian, IAN_IANG)
SET_FUZZY_FLAG(in, IN_ING)
SET_FUZZY_FLAG(ou, U_OU)
SET_FUZZY_FLAG(uan, UAN_UANG)
SET_FUZZY_FLAG(c, C_CH)
SET_FUZZY_FLAG(f, F_H)
SET_FUZZY_FLAG(l, L_N)
SET_FUZZY_FLAG(s, S_SH)
SET_FUZZY_FLAG(z, Z_ZH)

std::shared_ptr<libime::PinyinCorrectionProfile> correctionProfile;
switch (*fuzzyConfig.correction) {
case CorrectionLayout::None:
break;
case CorrectionLayout::Qwerty:
correctionProfile = std::make_shared<libime::PinyinCorrectionProfile>(
libime::BuiltinPinyinCorrectionProfile::Qwerty);
break;
}

if (correctionProfile) {
flags |= libime::PinyinFuzzyFlag::Correction;
ime_->setCorrectionProfile(std::move(correctionProfile));
}

ime_->setFuzzyFlags(flags);

quickphraseTriggerDict_.clear();
for (std::string_view prefix : *config_.quickphraseTrigger) {
Expand Down
Loading