|
-- 为交替输出中英情况加空格 |
|
-- 为中英混输词条(cn_en.dict.yaml)自动空格 |
|
-- 示例:`VIP中P` → `VIP 中 P` |
|
|
|
-- local puts = require("tools/debugtool") |
|
|
|
local use_fluency_editor=true |
|
-- Since we may need space to split between words, so use "Shift+Return" |
|
local fluency_commit_key="Shift+Return" |
|
local LOG=true |
|
|
|
-- here must assign to one local variable https://stackoverflow.com/a/63461673/21294350 |
|
local cn_en_space_common_module = require "cn_en_space_common_module" |
|
|
|
local function init(env) |
|
log.warning("init auto_append_space_processor") |
|
|
|
env.property_notifier=env.engine.context.property_update_notifier:connect(function(ctx, name) |
|
cn_en_space_common_module.sync_prev_cands(env,ctx) |
|
cn_en_space_common_module.check_unique_true_property(env) |
|
end) |
|
|
|
cn_en_space_common_module.reset_cand_property(env) |
|
end |
|
|
|
local function fini(env) |
|
env.property_notifier:disconnect() |
|
log.warning("end processor") |
|
end |
|
|
|
local function auto_append_space_processor(key, env) |
|
local engine = env.engine |
|
local context = engine.context |
|
local input_code = context.input |
|
local pos = context.caret_pos |
|
local composition = context.composition |
|
local dict_mem = Memory(env.engine, env.engine.schema) |
|
|
|
local cand_select_kyes = { |
|
["semicolon"] = 1, |
|
-- ["apostrophe"] = 2, |
|
["1"] = 0, |
|
["2"] = 1, |
|
["3"] = 2, |
|
["4"] = 3, |
|
["5"] = 4, |
|
["6"] = 5, |
|
["7"] = 6, |
|
["8"] = 7, |
|
["9"] = 8 |
|
-- ["10"] = 9 |
|
} |
|
|
|
local spec_keys = { |
|
-- ['equal'] = true, |
|
-- ['apostrophe'] = true, |
|
['grave'] = true, |
|
['minus'] = true, |
|
-- ['slash'] = true, |
|
['Shift+at'] = true, |
|
['Shift+plus'] = true, |
|
['Shift+dollar'] = true, |
|
['Shift+quotedbl'] = true, |
|
['Shift+asterisk'] = true, |
|
['Shift+underscore'] = true, |
|
['Shift+parenleft'] = true, |
|
['Shift+parenright'] = true, |
|
-- ['Return'] = true, |
|
['Control+Return'] = true, |
|
['Alt+Return'] = true |
|
} |
|
|
|
local punct_keys = { |
|
['Shift+exclam'] = true, |
|
['Shift+question'] = true, |
|
['comma'] = true, |
|
['period'] = true |
|
-- ['semicolon'] = true, -- may confilct with the self-defined keybinding |
|
} |
|
|
|
local whether_preedit |
|
|
|
local cfg = engine.schema.config |
|
local procs_list=cfg:get_list("engine/processors") |
|
|
|
if procs_list:get_value_at(procs_list.size-1):get_string() == "express_editor" then |
|
use_fluency_editor=false |
|
end |
|
if use_fluency_editor then |
|
cand_select_kyes["Return"]="x" |
|
-- spec_keys['Return']=false |
|
whether_preedit= key:repr() == fluency_commit_key |
|
else |
|
cand_select_kyes["space"]="x" |
|
whether_preedit= key:repr() == "Return" |
|
end |
|
|
|
local prev_cand_is_nullv = env.prev_cand_is_nullv |
|
local prev_cand_is_hanziv = env.prev_cand_is_hanziv |
|
local prev_cand_is_awordv = env.prev_cand_is_awordv |
|
local prev_cand_is_preeditv = env.prev_cand_is_preeditv |
|
local prev_cand_is_punctv = env.prev_cand_is_punctv |
|
|
|
if (#input_code >= 1 and composition:empty()) then |
|
log.error("composition:empty() when having the input_code") |
|
end |
|
|
|
if (#input_code == 0) and (spec_keys[key:repr()]) then |
|
cn_en_space_common_module.reset_cand_property(env) |
|
context:set_property('prev_cand_is_null', '1') |
|
end |
|
|
|
if (#input_code == 0) and (punct_keys[key:repr()]) then |
|
cn_en_space_common_module.reset_cand_property(env) |
|
context:set_property('prev_cand_is_punct', '1') |
|
end |
|
|
|
if (#input_code >= 1) and (whether_preedit) then |
|
if LOG then |
|
log.warning("get preedit key " .. key:repr() .. " property " .. prev_cand_is_nullv .. "; prev_cand_is_hanzi-" .. prev_cand_is_hanziv .. ";" .. prev_cand_is_awordv .. "; preeditv-" .. prev_cand_is_preeditv .. ";prev_cand_is_punct-" .. prev_cand_is_punctv .. " ") |
|
end |
|
local cand_text = input_code |
|
if (prev_cand_is_nullv ~= '1') and |
|
((prev_cand_is_hanziv == '1') or (prev_cand_is_awordv == '1') or prev_cand_is_preeditv == '1') then |
|
cand_text = " " .. input_code |
|
engine:commit_text(cand_text) |
|
else |
|
engine:commit_text(cand_text) |
|
end |
|
cn_en_space_common_module.reset_cand_property(env) |
|
context:set_property('prev_cand_is_preedit', "1") |
|
context:clear() |
|
return 1 -- kAccepted |
|
end |
|
|
|
return 2 -- kNoop |
|
end |
|
|
|
local function add_spaces(s,env) |
|
local modified |
|
-- 在中文字符后和英文字符前插入空格 |
|
modified = s:gsub("([\228-\233][\128-\191]-)([%w%p])", "%1 %2") |
|
-- 在英文字符后和中文字符前插入空格 |
|
modified = modified:gsub("([%w%p])([\228-\233][\128-\191]-)", "%1 %2") |
|
return modified |
|
end |
|
|
|
-- 是否同时包含中文和英文数字 |
|
local function is_mixed_cn_en_num(s) |
|
return s:find("([\228-\233][\128-\191]-)") and s:find("[%a]") |
|
end |
|
|
|
local function cn_en_spacer(input, env) |
|
for cand in input:iter() do |
|
if is_mixed_cn_en_num(cand.text) then |
|
cand = cand:to_shadow_candidate(cand.type, add_spaces(cand.text,env), cand.comment) |
|
end |
|
yield(cand) |
|
end |
|
end |
|
|
|
return {processor = {init=init, func=auto_append_space_processor, fini=fini}, filter = cn_en_spacer} |