Files
msime-windows/engine/schemes/wubi_scheme.cpp
T
fanlusky a7779d2b05 refactor(engine): 将 engine 从 submodule 内嵌为仓内一等组件
`vendor/MetasequoiaImeEngine` submodule 改为 `engine/` 目录,导入的是
product-lock.json 原本钉住的提交 c810d201,逐文件核对过 blob,因此这是一次
搬迁而不是引擎升级。引擎自己的嵌套 submodule(googlepinyinime-rev、utfcpp、
miniaudio)按各自 gitlink 一并展开,来源与提交见 engine/UPSTREAM.md。

Windows 专门化裁掉了不属于本产品的部分:whisper.cpp 与本地推理 provider、
macOS 示例宿主、整个 dictionary/ 建库流水线、上游 CI 与 release-please 配置。
词库仍按 product-lock.json 在构建时取发布产物。

引擎不再进 product-lock.json:没有 gitlink 就没有可比对的东西,锁里写一个上游
commit 等于宣称这棵树仍等于上游,而第一个 Windows 专门化改动落地时这句话就假了。
verify-contracts / verify-checkout 随之移除,verify-published 保留。

CI 与工具链:引擎源码并入根 scripts/format.sh(删掉引擎自己的那份,避免两套排除
列表分叉);引擎 voice.yml 里的 silero_vad.onnx 摘要校验移到本仓 lock job;
engine-bump-triage 工作流与其测试删除;gitsubmodule 的 dependabot 频率由 daily
改为 monthly。

README、README.en、AGENTS.md、CONTRIBUTING、docs/product-release.md 及各子目录
文档同步说明新的位置与边界。
2026-09-12 03:40:48 +08:00

121 lines
3.0 KiB
C++

#include "wubi_scheme.h"
#include <algorithm>
#include <cctype>
namespace
{
bool is_wubi_letter(char lower, bool mixed_pinyin_allowed)
{
return (lower >= 'a' && lower <= 'y') || (mixed_pinyin_allowed && lower == 'z');
}
bool is_wubi_vk(ImeKeyCode vk, bool mixed_pinyin_allowed)
{
return vk >= 'A' && vk <= 'Z' && is_wubi_letter(static_cast<char>(vk + ('a' - 'A')), mixed_pinyin_allowed);
}
std::string normalize_wubi_code(const std::string &input, size_t max_length, bool mixed_pinyin_allowed)
{
std::string normalized;
normalized.reserve((std::min)(input.size(), max_length));
for (const unsigned char ch : input)
{
const char lower = static_cast<char>(std::tolower(ch));
if (!is_wubi_letter(lower, mixed_pinyin_allowed))
{
continue;
}
normalized.push_back(lower);
if (normalized.size() == max_length)
{
break;
}
}
return normalized;
}
} // namespace
void WubiScheme::reset()
{
raw_input_.clear();
key_strokes_.clear();
extended_length_allowed_ = false;
}
void WubiScheme::handle_key(ImeKeyCode vk, ImeModifierMask modifiers_down, ImeCharacter wch)
{
if (vk == ImeKey::Backspace)
{
if (!raw_input_.empty())
{
raw_input_.pop_back();
}
if (!key_strokes_.empty())
{
key_strokes_.pop_back();
}
return;
}
if (vk == ImeKey::Escape || vk == ImeKey::Return)
{
reset();
return;
}
if (!is_wubi_vk(vk, mixed_pinyin_allowed_) || raw_input_.size() >= max_code_length())
{
return;
}
key_strokes_.push_back(KeyStroke{vk, modifiers_down, wch});
raw_input_.push_back(static_cast<char>(vk + ('a' - 'A')));
}
void WubiScheme::set_raw_input(const std::string &raw_input, const std::string &raw_input_with_cases)
{
raw_input_ = normalize_wubi_code(raw_input_with_cases.empty() ? raw_input : raw_input_with_cases, max_code_length(),
mixed_pinyin_allowed_);
key_strokes_.clear();
}
void WubiScheme::set_extended_length_allowed(bool allowed)
{
extended_length_allowed_ = allowed;
}
void WubiScheme::set_mixed_pinyin_allowed(bool allowed)
{
mixed_pinyin_allowed_ = allowed;
}
size_t WubiScheme::max_code_length() const
{
return extended_length_allowed_ ? kMaxMixedCodeLength : kMaxCodeLength;
}
QueryRequest WubiScheme::build_request() const
{
QueryRequest request;
request.scheme = type();
request.raw_input = raw_input_;
request.raw_input_with_cases = raw_input_;
request.normalized_input = raw_input_;
request.raw_segmentation = raw_input_;
request.normalized_segmentation = raw_input_;
request.segmentation = raw_input_;
request.key_strokes = key_strokes_;
request.valid = !raw_input_.empty();
return request;
}
std::string WubiScheme::get_preedit() const
{
return raw_input_;
}
SchemeType WubiScheme::type() const
{
return SchemeType::Wubi;
}