feat: 引入 SuperFilter (超级滤镜) —— 高自定义、高性能的流水线候选词处理引擎#1148
feat: 引入 SuperFilter (超级滤镜) —— 高自定义、高性能的流水线候选词处理引擎#1148amzxyz wants to merge 2 commits intorime:masterfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR introduces a new built-in Rime gear filter, SuperFilter (超级滤镜), aiming to provide a high-performance, highly configurable, native C++ pipeline for candidate post-processing (replace/append/comment/abbrev), backed by a LevelDB cache and configurable rule sets.
Changes:
- Add
SuperFilter/SuperFilterTranslationimplementing streaming candidate processing plus abbrev injection logic. - Add rule parsing + LevelDB build/rebuild with file-signature-based invalidation and a process-level DB cache.
- Register
super_filteras a gears module component.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 7 comments.
| File | Description |
|---|---|
src/rime/gear/super_filter.h |
Declares SuperFilter rule model, translation wrapper, and filter component interface. |
src/rime/gear/super_filter.cc |
Implements config parsing, streaming translation pipeline, abbrev injection, and LevelDB signature/rebuild logic. |
src/rime/gear/gears_module.cc |
Registers super_filter component in the gears module. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
80fa10a to
c068390
Compare
|
一個 Filter 包羅萬象,不如考慮拆成功能單一的 Filter 哪? 如果不求通用性,只爲實現現有需求開發,可以做成 librime 插件吧。 |
本来是想适配系统的流程,可以数据的统一管理变成了问题,流程的耦合变成了问题,这个实现恰恰强在拉出一段分支出来做了很多组合,其次数据库的应用也怕不支持,放在这里肯定要考虑通用型,需大家讨论一下,我讲一个事,内置的字符集 单字优先啥的根本没办法耦合业务逻辑,在我看来通用性极差,所以我怕真正的小小组件并不能带来通用,反而平掉了耦合、文件分离这些优质的特性,当然了还需深入思考。 |
需要请教userdb合不合适,分离命名空间会不会造成多线访问数据库问题 |
🔽 :userdb版本留存```// librime/src/rime/gear/super_filter.cc namespace rime { // Process-level global cache for the LevelDb instance. static SuperDbCache& GetGlobalDbCache() { static std::vector<size_t> GetUtf8Offsets(const std::string& text) { static std::vectorstd::string Split(const std::string& str, const std::string& delim) { SuperFilterTranslation::SuperFilterTranslation( } void SuperFilterTranslation::UpdateExhausted() { an SuperFilterTranslation::Peek() { } bool SuperFilterTranslation::Next() { } // Forward Maximum Matching algorithm for segmenting and replacing long phrases. } void SuperFilterTranslation::GenerateAbbrevCandidates(const std::string& input_code, size_t start, size_t end) { } struct CandData { void SuperFilterTranslation::ProcessNextInner() { } SuperFilter::SuperFilter(const Ticket& ticket) : Filter(ticket) { SuperFilter::~SuperFilter() { void SuperFilter::LoadConfig(Config* config) { } // Generates a stringent signature combining prefixes, file paths, and system attributes } static std::mutex g_db_cache_mutex; void SuperFilter::InitializeDb() { } // Data structure for in-memory sorting before writing to LevelDb void SuperFilter::RebuildDb() { } an SuperFilter::Apply( } } // namespace rime // librime/src/rime/gear/super_filter.h #include namespace rime { // Representation of a single filter rule configured in YAML. }; // Wrapper for candidates that require forced injection at specific positions or quality thresholds. // The core translation class implementing lazy evaluation and stream processing. private: }; // Filter component responsible for parsing configurations and managing the global LevelDb connection. private: }; } // namespace rime #endif // RIME_SUPER_FILTER_H_ |
|
@lotem 完成的新的结构,按要求打散了,与rime使用逻辑一致,并使用简单的bin编译方式进行了打包,不再使用userdb |
💡 背景与初衷 (Motivation)
在长期的 Rime 输入法定制与使用经验中,用户对于候选词的“后处理”需求极为庞大(如:Emoji 附加、中英混输提示、简繁/方言转换、特定简码强插等)。
基于此需求,本项目抽象并实现了
SuperFilter—— 一个纯 C++ 原生的高自定义超级滤镜引擎。它通过底层的极速架构与灵活的配置逻辑,提供轻量级、高性能的文本过滤与二次处理能力,让用户在自定义数据与后期维护上变得前所未有的简单,成为 Rime 滤镜作业环境中一个大能的存在。功能已经在万象拼音方案测试许久,性能表现优异,希望未来能成为 librime 生态中的一大利器:
https://github.com/amzxyz/rime_wanxiang/blob/wanxiang/lua/wanxiang/super_replacer.lua
⚙️ 核心运行结构 (Architecture & Pipeline)
SuperFilter采用了极客级别的原生编译与单文件内存映射(Mmap)引擎,构建了极其高效的数据流转体系:engine/filters中依次挂载不同的独立命名空间,将数据流转的任务完美融入 Rime 原生引擎。配置彻底扁平化,告别深层嵌套,数据流转严丝合缝。k\tv1|v2|v3(支持同 k 多行自动合并)。在底层,引擎会在后台瞬间将其编译为紧凑的单体二进制.bin文件,实现数据的极致瘦身与规整排列。option开关,引擎在敲击按键时实时查询状态,瞬间接入或无缝剥离处理分支。🧰 功能模块与模式解析 (Modules & Modes)
引擎内置了四大核心
mode,以及丰富的子模式组合,足以覆盖绝大多数文本处理需求:1.
mode: replace(替换模式)sentence: true:开启正向最大匹配(FMM)分词。不仅能替换单字,还能对未切分的超长句子进行精准的局部替换。2.
mode: append(附加模式)3.
mode: comment(注释模式)comment_mode:none: 清空注释。append: 继承原生拼音注释。text: 彻底替换原有注释,结合comment_format(如〔%s〕)实现极佳的 UI 排版(例如apple -> 苹果〔apple〕)。4.
mode: abbrev(简码/强插模式)类型,位置/分数,生效数量):order: "index,2,1":绝对排位。无视原生词频,强行将匹配到的第 1 个简码霸占第 2 候选位。order: "quality,110,6":动态分数跳水。赋予前 6 个简码 110 的权重,顺应自然排序体系。quality=0,自动沉底,既不干扰正常打字,又能在无匹配词时提供兜底保障。t9_mode: true。在此模式下,九宫格输入的字母会被智能转换为数字编码存入底库(如lss\t老实说变为577\t老实说==lss),并通过切割完美回显preedit,彻底解决九键简码的维护与注入回显痛点。以及全局tag控制,为不同tag打造专用数据(如反查数据库的注释修饰) 产物cand_type参数自定义为后续处理提供方便(针对特定候选的放行)等等,打开了方便之门。
🛡️ 稳定性保障与问题规避 (Safeguards)
为了达到生产级标准,底层 C++ 代码进行了极度严苛的防御性编程与架构设计:
.bin二进制文件映射至操作系统的虚拟内存。配合极限指针偏移与纯内存二分查找,在只读打字场景下提供光速查询体验,且绝对不会造成堆内存(Heap)膨胀与泄漏。build/目录。结构清爽,完美契合 Rime 的静态编译缓存区架构设计,有效避免数据同步(Sync)时的文件冲突。.bin文件的毫秒级热重载,绝不波及其他流水线。内置的多实例缓存机制完美抵御了频繁切换窗口时的上下文重建开销。🌟 总结
SuperFilter真正强大之处在于其外部配置的高级抽象与底层的极致单文件内存映射架构。它赋予了输入法极高的可玩性:通过数据分类细化、模式分类解耦、以及利用命名空间进行宏观排列,将文本的定制能力提升到了一个全新的高度。具体配置思路与感知,可参考后续参数说明及万象项目
lua/data中的实测数据。📝 配置示例 (Configuration Example)
🚀 未来的改进方向 (Future Roadmap)
随着本工具的强化,未来我们计划在性能与易用性上做进一步的抽象与突破:
schema.yaml彻底减负,也可能是一个JSON预设文件。