Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support double pinyin with Projection #20

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions scripts/lua/baidu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,30 @@ local function make_url(input, bg, ed)
'&result=hanzi&resultcoding=utf-8&ch_en=0&clientinfo=web&version=1'
end

local function translator(input, seg)
local url = make_url(input, 0, 5)
local function translator(input, seg, env)
local config = env.engine.schema.config
local config_list= config:get_list("translator/preedit_format")
local delimiter = config:get_string("speller/delimiter"):sub(1, 1) -- 获取首个字符为分隔符,常见为空格

local projection = Projection(config_list)
local p_str = projection:apply(input, true)

local url = make_url(p_str, 0, 5)
local reply = http.request(url)
local _, j = pcall(json.decode, reply)
if j.status == "T" and j.result and j.result[1] then
for i, v in ipairs(j.result[1]) do
local c = Candidate("simple", seg.start, seg.start + v[2], v[1], "(百度云拼音)")
c.quality = 2
if string.gsub(v[3].pinyin, "'", "") == string.sub(input, 1, v[2]) then
c.preedit = string.gsub(v[3].pinyin, "'", " ")
end
yield(c)
for ii, vv in ipairs(j.result) do
for i, v in ipairs(vv) do
local hanzi = v[1]
local pylen = v[2]
local py = v[3].pinyin
local c = Candidate("simple", seg.start, seg.start + pylen, hanzi, "(百度云拼音)")
c.quality = 2
c.preedit = string.gsub(py, "'", delimiter) -- 避免编码区显示为原始输入字符串
yield(c)
end
end
end
end
end

return translator
9 changes: 5 additions & 4 deletions scripts/lua/trigger.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
local function make(trig_key, trig_translator)
local flag = false

local last_script_text = ""
local function processor(key, env)
local kAccepted = 1
local kNoop = 2
local engine = env.engine
local context = engine.context

if key:repr() == trig_key then
if context:is_composing() then
flag = true
last_script_text = context:get_script_text()
last_script_text = string.gsub(last_script_text, " ", "'")
flag = true
context:refresh_non_confirmed_composition()
return kAccepted
end
Expand All @@ -21,7 +22,7 @@ local function make(trig_key, trig_translator)
local function translator(input, seg, env)
if flag then
flag = false
trig_translator(input, seg, env)
trig_translator(last_script_text, seg, env)
end
end

Expand Down