在完整音节后敲反引号,接着的字母筛这个音节上的字:第一码大小写均可, 紧跟的大写字母是第二码,之后可以接着打,一个输入里可以有多段。开关 helpcode.shuangpin_mid_sentence_helpcode 默认关闭。 约束在整句解码时生效,而不是解完再筛: - Google 解码器在 add_char_qwerty 里送 extend_mtrx_nd 之前按约束过滤词条, 锁住的字带着上下文重新整句解码;约束变化时整段重解,不污染增量搜索。 - kenlm 词格建图时按约束筛边,受约束的跨度不截断地查词库;神经重排只在 词格 n-best 里挑,自动满足约束。 - 普通词条、云和 AI 候选按同一规则收尾校验;满足不了就不出整句。 反引号段在请求里换成手动分隔符,原串留给宿主回读与预编辑显示;选词推进 时剩余音节上的约束随原串保留。输入形状规则放在 contracts/mid_sentence_helpcode.h, TSF、Server、引擎共用,新增 Worker opcode MidSentenceHelpcodeChanged = 28。 googlepinyinime-rev 已与上游解绑,AGENTS 与 UPSTREAM.md 同步说明。
37 lines
1.4 KiB
C++
37 lines
1.4 KiB
C++
#pragma once
|
|
#include <cstddef>
|
|
#include <filesystem>
|
|
#include <functional>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace metasequoia
|
|
{
|
|
// 句中辅助码对 Google 解码的约束:拼音串里从 pinyin_offset 起的那个音节,只能解成
|
|
// accept 认可的汉字(UTF-8 单字)。约束在解码器内部建格子时生效,不是解完再筛。
|
|
struct DecoderCharConstraint
|
|
{
|
|
std::size_t pinyin_offset = 0;
|
|
std::function<bool(const std::string &hanzi)> accept;
|
|
};
|
|
|
|
// Google Pinyin exposes process-wide singletons. This adapter owns their lifecycle and
|
|
// executes a complete, reset-before-search operation under one lock. No caller can retain
|
|
// candidate pointers or incremental search state between requests.
|
|
class PinyinDecoder
|
|
{
|
|
public:
|
|
PinyinDecoder(std::filesystem::path model, std::filesystem::path user_dictionary);
|
|
~PinyinDecoder();
|
|
PinyinDecoder(const PinyinDecoder &) = delete;
|
|
PinyinDecoder &operator=(const PinyinDecoder &) = delete;
|
|
// 约束为空就是普通整句解码。解码器是全进程共享的,每次调用都会重设约束,
|
|
// 上一个调用方留下的约束不会漏到这一次。
|
|
std::string sentence(const std::string &pinyin, const std::vector<DecoderCharConstraint> &constraints = {}) const;
|
|
|
|
private:
|
|
std::filesystem::path model_;
|
|
std::filesystem::path user_dictionary_;
|
|
};
|
|
} // namespace metasequoia
|