Files
msime-windows/engine/core/input_session_candidates.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

130 lines
6.1 KiB
C++

#include "input_session.h"
#include "data_path.h"
#include "../common/helpcode_utils.h"
#include "../contracts/assets/assets.h"
#include "../user_dictionary/user_dictionary_journal.h"
#include "../local_modes/jianpin_query.h"
#include "../quanpin/quanpin_utils.h"
#include <algorithm>
#include <cctype>
namespace metasequoia
{
void InputSession::enable_fixed_positions()
{
fixed_positions_enabled_ = true;
update_mixed_candidates();
}
std::string InputSession::position_context(bool english) const
{
if (english)
{
std::string input = dedicated_english_mode_ ? dedicated_english_preedit_
: (local_input_mode_ == LocalInputMode::TemporaryEnglish
? local_preedit_.substr(1)
: engine_.get_request().raw_input_with_cases);
std::transform(input.begin(), input.end(), input.begin(),
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
return "english:" + input;
}
if (local_input_mode_ == LocalInputMode::SuperJianpin)
return local_modes::jianpin_ranking_context(local_preedit_.substr(1), scheme(), shuangpin_profile_);
if (wubi_candidates_are_native())
return engine_.get_request().raw_input;
std::string context = get_quanpin();
if (context.empty())
context = get_pinyin_segmentation();
if (engine_.get_request().raw_input.size() == 1)
return context;
auto plain = context;
plain.erase(std::remove(plain.begin(), plain.end(), '\''), plain.end());
const auto cuts = quanpin::cut_pinyin_by_mode(plain, "correction");
return cuts.empty() ? context : quanpin::join_segments(cuts.front());
}
void InputSession::apply_candidate_positions(std::vector<WordItem> &items)
{
if (!fixed_positions_enabled_ || items.empty())
return;
const auto journal = path_to_utf8(paths_.user(assets::user_journal));
if (local_input_mode_ == LocalInputMode::None && !dedicated_english_mode_ && scheme() != SchemeType::JapaneseRomaji)
user_dictionary::apply_fixed_positions(
journal, position_context(false), items, engine_.get_request().raw_input.size() == 1,
[this](const std::string &key, const std::string &word) { return engine_.find_candidate(key, word); },
has_active_helpcode());
else if (local_input_mode_ == LocalInputMode::SuperJianpin)
user_dictionary::apply_fixed_positions(journal, position_context(false), items, false);
if (std::any_of(items.begin(), items.end(),
[](const auto &item) { return item.source == CandidateSource::EnglishDictionary; }))
user_dictionary::apply_fixed_positions(journal, position_context(true), items, false, {}, true);
}
KeyResult InputSession::set_candidate_position(std::size_t index, int position)
{
if (position < 0 || position > 5 || index >= candidates().size())
return {};
const auto selected = candidates()[index];
const bool english = selected.source == CandidateSource::EnglishDictionary;
if (!english &&
((selected.source != CandidateSource::Database && selected.source != CandidateSource::UserDatabase) ||
scheme() == SchemeType::JapaneseRomaji))
return {};
const auto context = position_context(english);
const bool wubi = wubi_candidates_are_native() && local_input_mode_ != LocalInputMode::SuperJianpin;
const auto key = english || wubi
? selected.pinyin
: (selected.canonical_pinyin.empty() ? selected.pinyin : selected.canonical_pinyin);
if (context.empty() || key.empty())
return {};
const auto journal = path_to_utf8(paths_.user(assets::user_journal));
const bool ok = position == 0 ? user_dictionary::clear_fixed_position(journal, context, key, selected.word)
: user_dictionary::set_fixed_position(journal, context, key, selected.word, position);
if (!ok)
return {true, std::nullopt, "Unable to persist candidate position."};
reset_cache();
if (dedicated_english_mode_)
update_dedicated_english_candidates();
else if (local_input_mode_ != LocalInputMode::None)
return {true, std::nullopt, update_local_candidates()};
else
recompute_candidates();
return {true, std::nullopt, std::nullopt};
}
KeyResult InputSession::remove_candidate(std::size_t index)
{
if (index >= candidates().size())
return {};
const auto selected = candidates()[index];
const bool english = selected.source == CandidateSource::EnglishDictionary;
if (!english &&
((selected.source != CandidateSource::Database && selected.source != CandidateSource::UserDatabase) ||
scheme() == SchemeType::JapaneseRomaji || HelpcodeUtils::count_utf8_chars(selected.word) <= 1))
return {};
const bool wubi = wubi_candidates_are_native() && local_input_mode_ != LocalInputMode::SuperJianpin;
const auto kind = english
? user_dictionary::DictionaryKind::English
: (wubi ? user_dictionary::DictionaryKind::Wubi : user_dictionary::DictionaryKind::Pinyin);
// A displayed pinyin candidate already carries its exact dictionary key. Re-segmenting it
// (or expanding shuangpin again) can delete a different pronunciation of the same word.
const auto &key = english || wubi ? selected.pinyin : selected.canonical_pinyin;
if (key.empty())
return {};
if (!user_dictionary::delete_dictionary_candidate(
path_to_utf8(paths_.dictionary(english ? assets::english_dictionary : assets::main_dictionary)),
path_to_utf8(paths_.user(assets::user_journal)), kind, key, selected.word))
return {true, std::nullopt, "Unable to persist candidate removal."};
reset_cache();
if (dedicated_english_mode_)
update_dedicated_english_candidates();
else if (local_input_mode_ != LocalInputMode::None)
return {true, std::nullopt, update_local_candidates()};
else
recompute_candidates();
return {true, std::nullopt, std::nullopt};
}
} // namespace metasequoia