* fix(engine,server): 全拼多字纠错收窄为仅 QWERTY 邻键插入 多字(插入)纠错只保留「误插 QWERTY 邻键」:生成器 insertion_variants 不再生成重复相邻键(双击),生成式空间不再生成任意字母插入(远键插入)。autocorrect_table.h 随生成器重新生成。 * fix(engine): 全拼纠错开关关闭时真正不再改写切分 纠错开关关掉后真正不纠错:把手误拼写别名层从常开改为跟掩码走(ü 归一 jv->ju / nue->nve 仍常开);两个早于掩码可知的切分入口 QuanpinScheme::build_request 与 QuanpinDictionary::get_or_compute_segments 显式带上掩码,切分缓存键随之隔离。 * fix(engine,server): 纠错开关关闭时辅助码、写回与排序上下文也按字面切分 关掉全拼纠错后,只有方案的 build_request 和查询自身切分不再跑手误别名层, 其余几条路径仍用默认(带别名)的切分,和查询落在不同的键上: - 辅助码路径自己切底串作为显式切分交给词典,haoyonga 仍被切成 hao'yong'ga; - 调频、造词、删词、建 SQL、校验去掉分隔符后重切,开关关掉时选中 hao'yong'a 的词会去更新 hao'yong'ga,调频静默失败; - 排序 / 固定位置的上下文 key(引擎与 server 两处)仍按别名切,laing 与 liang 的固定位置互相串。 改动: - cut_pinyin_by_mode 的 include_correction_aliases 改为必填,每个调用点显式决定; 双拼词典、设置页词库校验和离线工具传 true,保持原行为。 - 新增 quanpin::correction_aliases_enabled,方案、词典、辅助码与排序 key 共用 (只看换位、邻键两位),修正只带删除 / 插入位的掩码在方案与词典两层判断不一致。 - 词典按最近一次 query 的掩码记录别名开关,写回路径统一走 cut_for_write。 - 会话改为在换方案、改开关时 bind_quanpin_scheme 一次,不再每次 refresh 下发。 - 方案只存 bool;jv -> ju 放回纠错表,去掉合并表。 - 在 CI 实际执行的 test_input_session 中补回归用例(写回、辅助码、固定位置、 方案掩码),每条都确认回退修复后会失败。 --------- Co-authored-by: fanlusky <1730976608@qq.com>
162 lines
4.3 KiB
C++
162 lines
4.3 KiB
C++
#include "quanpin_scheme.h"
|
|
#include "../common/helpcode_utils.h"
|
|
#include "../quanpin/quanpin_query.h"
|
|
#include "../quanpin/quanpin_utils.h"
|
|
#include "../shuangpin/shuangpin_query.h"
|
|
#include <cctype>
|
|
|
|
namespace
|
|
{
|
|
bool is_alpha_vk(ImeKeyCode vk)
|
|
{
|
|
return vk >= 'A' && vk <= 'Z';
|
|
}
|
|
} // namespace
|
|
|
|
void QuanpinScheme::reset()
|
|
{
|
|
raw_input_.clear();
|
|
key_strokes_.clear();
|
|
}
|
|
|
|
void QuanpinScheme::set_raw_input(const std::string &raw_input, const std::string &raw_input_with_cases)
|
|
{
|
|
raw_input_ = raw_input_with_cases.empty() ? raw_input : raw_input_with_cases;
|
|
key_strokes_.clear();
|
|
}
|
|
|
|
void QuanpinScheme::set_autocorrect_types(unsigned autocorrect_types)
|
|
{
|
|
include_correction_aliases_ = quanpin::correction_aliases_enabled(autocorrect_types);
|
|
}
|
|
|
|
void QuanpinScheme::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 (vk == ImeKey::Apostrophe)
|
|
{
|
|
if (raw_input_.empty() || raw_input_.back() != '\'')
|
|
{
|
|
key_strokes_.push_back(KeyStroke{vk, modifiers_down, wch});
|
|
raw_input_.push_back('\'');
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (!is_alpha_vk(vk))
|
|
{
|
|
return;
|
|
}
|
|
|
|
key_strokes_.push_back(KeyStroke{vk, modifiers_down, wch});
|
|
if (wch >= L'A' && wch <= L'Z')
|
|
{
|
|
raw_input_.push_back(static_cast<char>(wch));
|
|
}
|
|
else if (wch >= L'a' && wch <= L'z')
|
|
{
|
|
raw_input_.push_back(static_cast<char>(wch));
|
|
}
|
|
else
|
|
{
|
|
raw_input_.push_back(static_cast<char>(vk + ('a' - 'A')));
|
|
}
|
|
}
|
|
|
|
QueryRequest QuanpinScheme::build_request() const
|
|
{
|
|
QueryRequest request;
|
|
request.scheme = type();
|
|
request.raw_input_with_cases = raw_input_;
|
|
request.raw_input.reserve(raw_input_.size());
|
|
for (const char ch : raw_input_)
|
|
{
|
|
request.raw_input.push_back(ch == '\'' ? ch : static_cast<char>(std::tolower(static_cast<unsigned char>(ch))));
|
|
}
|
|
request.key_strokes = key_strokes_;
|
|
|
|
const size_t helpcode_length =
|
|
quanpin::detect_active_helpcode_length(request.raw_input, request.raw_input_with_cases);
|
|
std::string normalized_source = quanpin::strip_active_helpcodes(request.raw_input, request.raw_input_with_cases);
|
|
|
|
request.normalized_input.reserve(normalized_source.size());
|
|
for (const char ch : normalized_source)
|
|
{
|
|
if (ch != '\'')
|
|
{
|
|
request.normalized_input.push_back(ch);
|
|
}
|
|
}
|
|
if (!request.normalized_input.empty())
|
|
{
|
|
try
|
|
{
|
|
const auto segments =
|
|
quanpin::cut_pinyin_by_mode(normalized_source, "correction", include_correction_aliases_);
|
|
if (!segments.empty())
|
|
{
|
|
request.normalized_segmentation = quanpin::join_segments(segments.front());
|
|
}
|
|
}
|
|
catch (...)
|
|
{
|
|
}
|
|
}
|
|
|
|
if (request.normalized_segmentation.empty())
|
|
{
|
|
request.normalized_segmentation = normalized_source;
|
|
}
|
|
|
|
std::string cased_source = request.raw_input_with_cases;
|
|
if (helpcode_length > 0 && cased_source.size() >= helpcode_length)
|
|
{
|
|
cased_source = cased_source.substr(0, cased_source.size() - helpcode_length);
|
|
}
|
|
|
|
request.raw_segmentation = shuangpin::apply_segmentation_cases(request.normalized_segmentation, cased_source);
|
|
if (!cased_source.empty() && cased_source.back() == '\'' &&
|
|
(request.raw_segmentation.empty() || request.raw_segmentation.back() != '\''))
|
|
{
|
|
request.raw_segmentation.push_back('\'');
|
|
}
|
|
if (helpcode_length > 0)
|
|
{
|
|
request.raw_segmentation += "'";
|
|
request.raw_segmentation +=
|
|
request.raw_input_with_cases.substr(request.raw_input_with_cases.size() - helpcode_length, helpcode_length);
|
|
}
|
|
request.segmentation = request.normalized_segmentation;
|
|
|
|
request.valid = !request.normalized_input.empty();
|
|
return request;
|
|
}
|
|
|
|
std::string QuanpinScheme::get_preedit() const
|
|
{
|
|
return raw_input_;
|
|
}
|
|
|
|
SchemeType QuanpinScheme::type() const
|
|
{
|
|
return SchemeType::Quanpin;
|
|
}
|