* 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>
161 lines
6.6 KiB
C++
161 lines
6.6 KiB
C++
#include "engine.h"
|
|
#include "../common/helpcode_utils.h"
|
|
#include "quanpin_query.h"
|
|
#include "quanpin_utils.h"
|
|
|
|
namespace
|
|
{ // The request carries one bool per correction type; the dictionary layer gates
|
|
// the whole feature with a single mask, so bridge the two here. Design D2: any
|
|
// correction switch on also enables the deletion bit -- the user's intent to
|
|
// have typos fixed covers dropped letters -- while both off stays 0. The same
|
|
// linkage carries the insertion bit (design D4 of the insertion task).
|
|
unsigned autocorrect_types_from_request(const QueryRequest &request)
|
|
{
|
|
const unsigned legacy =
|
|
(request.enable_quanpin_autocorrect_transposition ? quanpin::kAutocorrectTransposition : 0u) |
|
|
(request.enable_quanpin_autocorrect_neighbor ? quanpin::kAutocorrectNeighbor : 0u);
|
|
return legacy == 0 ? 0u : legacy | quanpin::kAutocorrectDeletion | quanpin::kAutocorrectInsertion;
|
|
}
|
|
} // namespace
|
|
|
|
QuanpinEngine::QuanpinEngine(metasequoia::RuntimePaths paths)
|
|
: dictionary_({}, paths),
|
|
helpcodes_(HelpcodeUtils::load_helpcode_keymap(paths.resources, HelpcodeUtils::selected_helpcode_schema()))
|
|
{
|
|
}
|
|
|
|
QuanpinEngine::~QuanpinEngine() = default;
|
|
|
|
std::optional<WordItem> QuanpinEngine::find_candidate(const std::string &key, const std::string &value)
|
|
{
|
|
return dictionary_.find_candidate(key, value);
|
|
}
|
|
|
|
std::vector<WordItem> QuanpinEngine::query(const QueryRequest &request)
|
|
{
|
|
if (!request.valid)
|
|
{
|
|
return {};
|
|
}
|
|
|
|
// 整句联想开关随请求下发到词典层,四条整句来源据此各自门控(见 query_series)。
|
|
dictionary_.set_sentence_association(request.sentence_association);
|
|
// 前文同理随请求下发;换了前文,按旧前文重排出来的缓存顺序作废。
|
|
dictionary_.set_rescoring_context(request.rescoring_context);
|
|
|
|
const size_t helpcode_length =
|
|
request.enable_quanpin_helpcode
|
|
? quanpin::detect_active_helpcode_length(request.raw_input, request.raw_input_with_cases)
|
|
: 0;
|
|
// 辅助码路径自己切出底串的显式切分交给词典,词典不会再按掩码重切,所以这里要和方案的
|
|
// build_request 走同一个开关,否则关掉纠错后带辅助码的输入仍被别名层改写。
|
|
const bool correction_aliases = quanpin::correction_aliases_enabled(autocorrect_types_from_request(request));
|
|
|
|
if (helpcode_length == 2)
|
|
{
|
|
const std::string base_raw_input =
|
|
quanpin::strip_active_helpcodes(request.raw_input, request.raw_input_with_cases);
|
|
const auto cuts = quanpin::cut_pinyin_by_mode(base_raw_input, "correction", correction_aliases);
|
|
const std::string base_segmentation = quanpin::join_segments(cuts.front());
|
|
const std::string help_codes = request.raw_input.substr(request.raw_input.size() - 2, 2);
|
|
const auto base_candidates = dictionary_.query(base_raw_input, base_segmentation,
|
|
autocorrect_types_from_request(request), request.fuzzy_pinyin);
|
|
return HelpcodeUtils::filter_candidates_with_double_helpcodes(base_candidates, help_codes, helpcodes_.get());
|
|
}
|
|
|
|
if (helpcode_length == 1)
|
|
{
|
|
const std::string base_raw_input =
|
|
quanpin::strip_active_helpcodes(request.raw_input, request.raw_input_with_cases);
|
|
const auto cuts = quanpin::cut_pinyin_by_mode(base_raw_input, "correction", correction_aliases);
|
|
const std::string base_segmentation = quanpin::join_segments(cuts.front());
|
|
const std::string help_code = request.raw_input.substr(request.raw_input.size() - 1, 1);
|
|
const auto base_candidates = dictionary_.query(base_raw_input, base_segmentation,
|
|
autocorrect_types_from_request(request), request.fuzzy_pinyin);
|
|
return HelpcodeUtils::reorder_candidates_with_single_helpcode(base_candidates, help_code, helpcodes_.get());
|
|
}
|
|
|
|
return dictionary_.query(request.raw_input, request.segmentation, autocorrect_types_from_request(request),
|
|
request.fuzzy_pinyin);
|
|
}
|
|
|
|
bool QuanpinEngine::expand_initial_candidates(const QueryRequest &request, std::vector<WordItem> &candidates)
|
|
{
|
|
if (request.scheme != SchemeType::Quanpin ||
|
|
(request.enable_quanpin_helpcode &&
|
|
quanpin::detect_active_helpcode_length(request.raw_input, request.raw_input_with_cases) > 0))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
const auto segments = quanpin::split_segments(request.segmentation);
|
|
if (segments.empty() || segments.front().size() != 1)
|
|
{
|
|
return false;
|
|
}
|
|
return dictionary_.expand_initial_candidates(segments.front(), candidates);
|
|
}
|
|
|
|
int QuanpinEngine::handleVkCode(ImeKeyCode vk, ImeModifierMask modifiers_down, ImeCharacter wch)
|
|
{
|
|
return dictionary_.handleVkCode(vk, modifiers_down, wch);
|
|
}
|
|
|
|
int QuanpinEngine::create_word(std::string pinyin, std::string word)
|
|
{
|
|
return dictionary_.create_word(std::move(pinyin), std::move(word));
|
|
}
|
|
|
|
int QuanpinEngine::create_word_from_canonical_pinyin(std::string pinyin, std::string word)
|
|
{
|
|
return dictionary_.create_word_from_canonical_pinyin(std::move(pinyin), std::move(word));
|
|
}
|
|
|
|
int QuanpinEngine::update_weight_by_word(std::string word)
|
|
{
|
|
return dictionary_.update_weight_by_word(std::move(word));
|
|
}
|
|
|
|
int QuanpinEngine::update_weight_by_pinyin_and_word(std::string pinyin, std::string word)
|
|
{
|
|
return dictionary_.update_weight_by_pinyin_and_word(std::move(pinyin), std::move(word));
|
|
}
|
|
|
|
int QuanpinEngine::delete_by_pinyin_and_word(std::string pinyin, std::string word)
|
|
{
|
|
return dictionary_.delete_by_pinyin_and_word(std::move(pinyin), std::move(word));
|
|
}
|
|
|
|
int QuanpinEngine::insert_word_to_series_cache(const std::string &pinyin, const std::string &word,
|
|
CandidateSource source)
|
|
{
|
|
return dictionary_.insert_word_to_series_cache(pinyin, word, source);
|
|
}
|
|
|
|
int QuanpinEngine::insert_word_to_series_cache(const QueryRequest &request, const std::string &word,
|
|
CandidateSource source)
|
|
{
|
|
return dictionary_.insert_word_to_series_cache(request.raw_input, request.segmentation,
|
|
autocorrect_types_from_request(request), word, source);
|
|
}
|
|
|
|
std::string QuanpinEngine::search_sentence_from_ime_engine(const std::string &user_pinyin)
|
|
{
|
|
return dictionary_.search_sentence_from_ime_engine(user_pinyin);
|
|
}
|
|
|
|
void QuanpinEngine::reset_state()
|
|
{
|
|
dictionary_.reset_state();
|
|
}
|
|
|
|
void QuanpinEngine::reset_cache()
|
|
{
|
|
dictionary_.reset_cache();
|
|
}
|
|
|
|
void QuanpinEngine::reset_sentence_cache()
|
|
{
|
|
dictionary_.reset_sentence_cache();
|
|
}
|