Skip to content

Commit

Permalink
fix&feat: 修复人民币转换前导词、适配「以词定字」
Browse files Browse the repository at this point in the history
* fix: 大写人民币转换限定前导字母

- 限定前导字母进行人民币大写转换

* feat(以词定字): 适配以词定字

- 适配以词定字

参考自: https://github.com/BlindingDark/rime-lua-select-character
  • Loading branch information
YummyCocoa authored Jan 23, 2024
1 parent 1d54a6e commit 92deddc
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lua/number_translator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,11 @@ local function number_translatorFunc(num)
end

-- 触发模式为任意大写字母(除了 U,U 用在 Unicode 了)开头,可在 recognizer/patterns 中自定义
local function number_translator(input, seg)
local function number_translator(input, seg, env)
-- 获取 recognizer/patterns/number 的第 2 个字符作为触发前缀
env.number_keyword = env.number_keyword or env.engine.schema.config:get_string('recognizer/patterns/rmb'):sub(2, 2)
local str, num, numberPart
if string.match(input, "^([A-TV-Z]+%d+)(%.?)(%d*)$") ~= nil then
if env.number_keyword ~= '' and input:sub(1, 1) == env.number_keyword then
str = string.gsub(input, "^(%a+)", "")
numberPart = number_translatorFunc(str)
if #numberPart > 0 then
Expand Down
79 changes: 79 additions & 0 deletions lua/select_character.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
-- 依次定字
-- 来源:
-- https://github.com/BlindingDark/rime-lua-select-character/blob/master/lua/select_character.lua
-- http://lua-users.org/lists/lua-l/2014-04/msg00590.html
local function utf8_sub(s, i, j)
i = i or 1
j = j or -1

if i < 1 or j < 1 then
local n = utf8.len(s)
if not n then return nil end
if i < 0 then i = n + 1 + i end
if j < 0 then j = n + 1 + j end
if i < 0 then i = 1 elseif i > n then i = n end
if j < 0 then j = 1 elseif j > n then j = n end
end

if j < i then return "" end

i = utf8.offset(s, i)
j = utf8.offset(s, j + 1)

if i and j then
return s:sub(i, j - 1)
elseif i then
return s:sub(i)
else
return ""
end
end

local function first_character(s)
return utf8_sub(s, 1, 1)
end

local function last_character(s)
return utf8_sub(s, -1, -1)
end

local function get_commit_text(context, fun)
local candidate_text = context:get_selected_candidate().text
local selected_character = fun(candidate_text)

context:clear_previous_segment()
local commit_text = context:get_commit_text()

context:clear()

return commit_text .. selected_character
end

local function select_character(key, env)
local engine = env.engine
local context = engine.context
local config = engine.schema.config

-- 选择开头字
local first_key = config:get_string('key_binder/select_first_character') or 'bracketleft'
-- 选择末尾字
local last_key = config:get_string('key_binder/select_last_character') or 'bracketright'

local commit_text = context:get_commit_text()

if (key:repr() == first_key and commit_text ~= "") then
engine:commit_text(get_commit_text(context, first_character))

return 1 -- kAccepted
end

if (key:repr() == last_key and commit_text ~= "") then
engine:commit_text(get_commit_text(context, last_character))

return 1 -- kAccepted
end

return 2 -- kNoop
end

return select_character
2 changes: 2 additions & 0 deletions rime.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ reduce_english_filter = require("reduce_english_filter")
corrector = require("corrector")
-- 中国农历
Chinese_lunar_calendar = require("chineseLunarCalendar")
-- 以词定字(默认为「[」和「]」;在 key_binder 下配置快捷键
select_character = require("select_character")
3 changes: 2 additions & 1 deletion rime_mint.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ switches:

engine:
processors:
- lua_processor@select_character # 以词定字
- ascii_composer # ※ 處理西文模式及中西文切換
- recognizer # ※ 與 matcher 搭配,處理符合特定規則的輸入碼,如網址、反查等
- key_binder # ※ 在特定條件下將按鍵綁定到其他按鍵,如重定義逗號、句號爲候選翻頁鍵
Expand Down Expand Up @@ -290,5 +291,5 @@ recognizer:
wubi98_mint: "uw[a-z]*'?$"
stroke: "ui[a-z]*'?$"
chaizi: "uu[a-z]*'?$"
number: "^R[0-9]+[.]?[0-9]*" # 响应 lua_translator@number_translator 脚本将自动获取第 2 个字符作为触发前缀
rmb: "^R[0-9]+[.]?[0-9]*" # 响应 lua_translator@number_translator 脚本将自动获取第 2 个字符作为触发前缀
gregorian_to_lunar: "^N[0-9]{1,8}" # 响应 lua_translator@Chinese_lunar_calendar 公历转农历,输入 N20240115 得到「二〇二四年腊月初五」,脚本将自动获取第 2 个字符作为触发前缀

0 comments on commit 92deddc

Please sign in to comment.