在 rime中州韵小狼毫 inputShow lua Translator 一文中,我们通过 inputShow.lua 定制了 inputShow_translator,这使得我们的输入方案可以将用户输入的字符透传到候选列表中来。如下👇:
 
 👆上图中我们在候选列表中看到了 inputShow_translator 透传过来的字符 Stra 和 a,但是这两个透传过来的字符在候选列表中的位置是比较靠后的。
本文中, 我们通过定义一个 inputShow_Filter 的 filter,用于调整候选词的顺序,以使 inputShow_translator 透传过来的字符处于合理的位置。
Filter
 
在 rime中州韵 help lua Translator 一文中有提到 Translator 和 Filter 的工作顺序。
 
 以上文所示流程,Filer 工作于 Translator 之后,也就是说 Translator 所抛出的候选词列表,是 Filter 工作的输入项。Filter 是很强大的处理器,其功能包括但不限于:
- 增删选项
- 调整次序
- 修饰选项(comment)
inputShow_Filter.lua
 
我们在 inputShow_Filter.lua 文档中定义我们的 Filter,脚本如下👇:
-- spaceAppending.lua
-- Copyright (C) 2023 yaoyuan.dou <douyaoyuan@126.com>
--[[
	这个脚本,作为filter来用,需要结合inputShow的在translator阶段的处理信息进行工作
--]]
local function _inputShow(input, env)
	local cands = {}
	local candsSelflg=false
	local candsInput = {}
	local inputInfo = {str='',len=0,flg=false,candsCntLimitForLen={15,25}}
	local candsCnt = 0
	
	for cand in input:iter() do
		if cand.comment == 'inputShowStr' then
			--这个选项表明了前端的输入编码信息
			inputInfo.str = string.sub(cand.text,4)
			inputInfo.len = string.len(inputInfo.str)
		elseif cand.comment == 'inputShow' then
			--这是一个转换后需要展示的选项
			inputInfo.flg = true
			
			local thisCand = {}
			thisCand.start = cand.start
			thisCand._end = cand._end
			thisCand.text = cand.text
			thisCand.comment = cand.comment
			
			table.insert(candsInput,cand)
			candsCnt = candsCnt + 1
		else
			--这是一个没有转换的选项
			table.insert(cands,cand)
			
			if string.find(cand.comment,'☯') then
				--标记选项中存在自造词
				candsSelflg = true
			end
			
			candsCnt = candsCnt + 1
		end
		
		if inputInfo.flg and 0~=inputInfo.len then
			--如果已经捕获取了inputShow选项
			--根据输入的编码的长度,判断候选项数量是否已经够用
			if nil~=inputInfo.candsCntLimitForLen[inputInfo.len] then
				if candsCnt >= inputInfo.candsCntLimitForLen[inputInfo.len] then
					break
				end
			end
		end
	end
	
	if 0==inputInfo.len then
		inputInfo.flg = false
	end
	
	local candsHasBeenYield=0
	
	--下面开始抛出候选项
	--第一步,如果存在自造词,则先抛出自造词
	local candsFor2nd = {}
	for idx=1,#cands do
		if inputInfo.flg then
			if string.find(cands[idx].comment,'☯') then
				yield(cands[idx])
				candsHasBeenYield = candsHasBeenYield + 1
			else
				table.insert(candsFor2nd,cands[idx])
			end
		else
			--如果不需要处理 inputShow,则不做处理,进行转存
			table.insert(candsFor2nd,cands[idx])
		end
	end
	
	--第二步,把编码完全项抛出,即没有comment(此处指的是编码提示的comment内容)的选项,以供优先选用
	local candsFor4th = {}
	for idx=1,#candsFor2nd do
		if inputInfo.flg then
			if candsFor2nd[idx].comment == '' then
				yield(candsFor2nd[idx])
				candsHasBeenYield = candsHasBeenYield + 1
			else
				table.insert(candsFor4th,candsFor2nd[idx])
			end
		else
			--如果不需要处理 inputShow,则不做处理,进行转存
			table.insert(candsFor4th,candsFor2nd[idx])
		end
	end
	
	--第三步,如果有的话,抛出inputShow的选项
	for idx=1,#candsInput do
		local thisC = candsInput[idx]
		--此处的comment是 inputShow,为了不为后续造成干扰,此处需要清除comment内容
		thisC:get_genuine().comment = ''
		yield(thisC)
		
		candsHasBeenYield = candsHasBeenYield + 1
	end
	
	--第四步,如果还有其它选项,则抛出其它选项
	for idx=1,#candsFor4th do
		if nil==inputInfo.candsCntLimitForLen[inputInfo.len] then
			yield(candsFor4th[idx])
		elseif candsHasBeenYield<inputInfo.candsCntLimitForLen[inputInfo.len] then
			yield(candsFor4th[idx])
		else
			break
		end
		
		candsHasBeenYield = candsHasBeenYield + 1
	end
end
local function inputShow(input, env)
	--获取debug选项开关状态
	--local debugSwitchSts = env.engine.context:get_option("debug")
	
	_inputShow(input,env)
end
return inputShow
👆以上的脚本中,我们首先依次读取候选项,根据既定的规则对所读取的候选项进行取舍处理。然后再根据既定的规则次序抛出候选项,从而达到对候选项的 增删,调整,修饰。
inputShow_Filter 文档应该位于 用户文件夹 下 lua 内,如下👇:
 
rime.lua
 
在 rime中州韵 help lua Translator 一文中,我们完成 translator 方法的定义后,我们需要在 rime.lua 内把 translator 方法转换为 Translator 接口,以便 rime中州韵小狼毫输入法引擎可以引用 Translator 接口。
同样的,我们在 inputShow_Filter.lua 中完成 Filter 方法的定义后,也需要在 rime.lua 内进行 Filter 方法向 Filter 接口的转换/映射。👇如下,我们在 rime.lua 中进行如下配置:
help_translator = require("help")
inputShow_translator = require("inputShow")
inputShow_Filter = require("inputShow_Filter")
👆以上的配置中,我们把 inputShow_Filter 映射成了 inputShow_Filter
wubi_pinyin.custom.yaml
 
类比对 translator 的引用,我们同样需要在输入方案中配置引用 Filter,此处以 五笔・拼音 输入方案为例,我们在 wubi_pinyin.custom.yaml 做👇如下的配置:
# encoding:utf-8
patch:
  engine/translators/+:                         #增加以下translator
    - lua_translator@inputShow_translator
    - table_translator@custom_phrase            # 指定使用 custom_phrase 进行输入字符的翻译
    - lua_translator@help_translator
  engine/filters:								# 设置以下filter
    - simplifier
    - lua_filter@inputShow_Filter				# 这个过滤器用于在特定场景下,增加候选项
    - uniquifier								# 过滤重复候选项,依赖 simplifier
  custom_phrase:  # 设置用户字/词典
    dictionary: ""
    user_dict: Custom_phrase  # 指向 Custom_phrase.txt 文档
    db_class: stabledb
    enable_completion: false
    enable_sentence: true
    initial_quality: 1
  punctuator:  # 设置标点符号集
    import_preset: symbols
  
  # 设置以下 translator 相关的开关
  translator/enable_sentence: true          #是否整句连打
  translator/enable_user_dict: true         #开启用户词典
  translator/enable_encoder: true           #是否自动造词
  translator/encode_commit_history: false 	#对已上屏的内容整合成词条,看需求
  translator/max_phrase_length: 4          	#自动造词的最长字数
  
  translator/enable_completion: true  		#编码逐渐提示开关;编码提示
👆以上配置中,请注意观察 engine/filters 节点下的内容,我们在这个节点内增加了 inputShow_Filter。
效果欣赏
当我们完成以上配置后,重新部署 rime中州韵小狼毫,则我们即可以观察到我们所配置的 inputShow_Filter 的效果,如下👇:
 
 👆上图中,我们可以观察到,透传的字符 a, b,ip 都排到了较前的位置,同时这些字符中的 comment 内容(例如 inputShow, inputShowStr)被移除。这些处理结果,为下一步更丰富强大功能提供了基础。
inputShow_Filter.lua/rime.lua/wubi_pinyin.custom.yaml 文档
 
👆以上所述 inputShow_Filter.lua,rime.lua,wubi_pinyin.custom.yaml 文档,你也可以在 inputShow.zip 下载取胜。
小结
以上就是今天分享的在 rime 中州韵小狼毫输入中配置 inputShow_Filter 的方法。并以此为样例演示了如何定义一个基础的 Filter,并在 rime.lua 中完成接口映射,然后在输入方案文档 wubi_pinyin.custom.yaml 中引用该 inputShow_Filter,从而实现对候选词的调序和修饰。

![[Unity]实时阴影技术方案总结](https://img-blog.csdnimg.cn/direct/7708bb572f7e4a43845a1b636394a17b.png)

















