Skip to content

Commit 725231d

Browse files
優化候選詞列表和記憶
1 parent dcc9ae2 commit 725231d

18 files changed

Lines changed: 78 additions & 256 deletions

lua/dp_filter.lua

Lines changed: 30 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -6187,65 +6187,7 @@ local function get_caps_mask(env)
61876187
return caps
61886188
end
61896189

6190-
local function codes_to_handwriting(comment)
6191-
if not comment or comment == "" then return "" end
6192-
local result = comment:gsub("[%w]+", function(code)
6193-
local hw = SYLLABLE_MAP[code:lower()]
6194-
return hw or code
6195-
end)
6196-
result = result:gsub(" ", "--")
6197-
result = result:gsub(" ", "-")
6198-
return result
6199-
end
6200-
6201-
local function apply_user_separators(cand, env)
6202-
local ctx = env.engine.context
6203-
local input = ctx.input or ""
6204-
local comment = cand.comment or ""
6205-
if input == "" then return cand.text end
6206-
6207-
local caps = get_caps_mask(env)
6208-
6209-
local codes = {}
6210-
for code in comment:gmatch("[%w]+") do
6211-
table.insert(codes, code)
6212-
end
62136190

6214-
local res = ""
6215-
local i = 1
6216-
local code_idx = 1
6217-
while i <= #input do
6218-
local token = input:match("^[%w]+", i)
6219-
if token then
6220-
local hw = token
6221-
local full_code = codes[code_idx]
6222-
if full_code then
6223-
hw = SYLLABLE_MAP[full_code:lower()] or SYLLABLE_MAP[token:lower()] or token
6224-
code_idx = code_idx + 1
6225-
else
6226-
hw = SYLLABLE_MAP[token:lower()] or token
6227-
end
6228-
6229-
if caps:sub(i, i) == "U" then
6230-
hw = capitalize_first(hw)
6231-
end
6232-
6233-
res = res .. hw
6234-
i = i + #token
6235-
else
6236-
local sep = input:match("^[^%w]+", i)
6237-
if sep then
6238-
res = res .. sep
6239-
i = i + #sep
6240-
else
6241-
local char = input:sub(i, i)
6242-
res = res .. char
6243-
i = i + 1
6244-
end
6245-
end
6246-
end
6247-
return res
6248-
end
62496191

62506192
local function is_han_char(text)
62516193
if not text or #text == 0 then return false end
@@ -6256,77 +6198,45 @@ local function is_han_char(text)
62566198
end
62576199

62586200
local function filter(translation, env)
6201+
local ctx = env.engine.context
62596202
local caps = _caps_mask
62606203
local has_caps = caps:sub(1, 1) == "U"
6261-
local items = {}
62626204

62636205
for cand in translation:iter() do
6264-
local original_is_han = is_han_char(cand.text)
6265-
local need_reconstruct = (cand.type == "sentence" or cand.type == "user_phrase"
6266-
or cand.type == "dictionary")
6267-
6268-
local text = cand.text
6269-
local text_changed = false
6270-
if need_reconstruct then
6271-
text = apply_user_separators(cand, env)
6272-
text_changed = true
6273-
if has_caps then
6274-
text = capitalize_first(text)
6275-
end
6276-
else
6277-
if has_caps then
6278-
text = capitalize_first(cand.text)
6279-
text_changed = true
6280-
end
6281-
end
6282-
6283-
local display_comment = original_is_han
6284-
and codes_to_handwriting(cand.comment)
6285-
or ""
6286-
6287-
if text_changed then
6288-
table.insert(items, {
6289-
cand = cand,
6290-
type = cand.type,
6291-
start = cand.start,
6292-
_end = cand._end,
6293-
text = text,
6294-
comment = display_comment,
6295-
is_latin = text:match("^%a") ~= nil,
6296-
new_cand = true
6297-
})
6206+
if is_han_char(cand.text) then
6207+
local comment = cand.comment or ""
6208+
local hw = comment:gsub("[%w]+", function(code)
6209+
return SYLLABLE_MAP[code:lower()] or code
6210+
end)
6211+
hw = hw:gsub(" ", "--")
6212+
hw = hw:gsub(" ", "-")
6213+
cand.comment = hw
6214+
yield(cand)
62986215
else
6299-
cand.comment = display_comment
6300-
table.insert(items, {
6301-
cand = cand,
6302-
is_latin = cand.text:match("^%a") ~= nil,
6303-
new_cand = false
6304-
})
6305-
end
6306-
end
6307-
6308-
if has_caps then
6309-
for _, item in ipairs(items) do
6310-
if item.is_latin then
6311-
if item.new_cand then
6312-
yield(Candidate(item.type, item.start, item._end, item.text, item.comment))
6216+
local comment = cand.comment or ""
6217+
local hw_parts = {}
6218+
for code in comment:gmatch("[%w]+") do
6219+
local hw = SYLLABLE_MAP[code:lower()]
6220+
if hw then
6221+
table.insert(hw_parts, hw)
63136222
else
6314-
yield(item.cand)
6223+
hw_parts = {}
6224+
break
63156225
end
63166226
end
6317-
end
6318-
for _, item in ipairs(items) do
6319-
if not item.is_latin then
6320-
if item.new_cand then
6321-
yield(Candidate(item.type, item.start, item._end, item.text, item.comment))
6322-
else
6323-
yield(item.cand)
6324-
end
6227+
local new_text = cand.text
6228+
if #hw_parts > 0 then
6229+
new_text = table.concat(hw_parts, "-")
6230+
end
6231+
if has_caps then
6232+
new_text = capitalize_first(new_text)
6233+
end
6234+
if new_text ~= cand.text then
6235+
yield(ShadowCandidate(cand, cand.type, new_text, ""))
6236+
else
6237+
cand.comment = ""
6238+
yield(cand)
63256239
end
6326-
end
6327-
else
6328-
for _, item in ipairs(items) do
6329-
yield(item.cand)
63306240
end
63316241
end
63326242
end

lua/puj_filter.lua

Lines changed: 30 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -6187,65 +6187,7 @@ local function get_caps_mask(env)
61876187
return caps
61886188
end
61896189

6190-
local function codes_to_handwriting(comment)
6191-
if not comment or comment == "" then return "" end
6192-
local result = comment:gsub("[%w]+", function(code)
6193-
local hw = SYLLABLE_MAP[code:lower()]
6194-
return hw or code
6195-
end)
6196-
result = result:gsub(" ", "--")
6197-
result = result:gsub(" ", "-")
6198-
return result
6199-
end
6200-
6201-
local function apply_user_separators(cand, env)
6202-
local ctx = env.engine.context
6203-
local input = ctx.input or ""
6204-
local comment = cand.comment or ""
6205-
if input == "" then return cand.text end
6206-
6207-
local caps = get_caps_mask(env)
6208-
6209-
local codes = {}
6210-
for code in comment:gmatch("[%w]+") do
6211-
table.insert(codes, code)
6212-
end
62136190

6214-
local res = ""
6215-
local i = 1
6216-
local code_idx = 1
6217-
while i <= #input do
6218-
local token = input:match("^[%w]+", i)
6219-
if token then
6220-
local hw = token
6221-
local full_code = codes[code_idx]
6222-
if full_code then
6223-
hw = SYLLABLE_MAP[full_code:lower()] or SYLLABLE_MAP[token:lower()] or token
6224-
code_idx = code_idx + 1
6225-
else
6226-
hw = SYLLABLE_MAP[token:lower()] or token
6227-
end
6228-
6229-
if caps:sub(i, i) == "U" then
6230-
hw = capitalize_first(hw)
6231-
end
6232-
6233-
res = res .. hw
6234-
i = i + #token
6235-
else
6236-
local sep = input:match("^[^%w]+", i)
6237-
if sep then
6238-
res = res .. sep
6239-
i = i + #sep
6240-
else
6241-
local char = input:sub(i, i)
6242-
res = res .. char
6243-
i = i + 1
6244-
end
6245-
end
6246-
end
6247-
return res
6248-
end
62496191

62506192
local function is_han_char(text)
62516193
if not text or #text == 0 then return false end
@@ -6256,77 +6198,45 @@ local function is_han_char(text)
62566198
end
62576199

62586200
local function filter(translation, env)
6201+
local ctx = env.engine.context
62596202
local caps = _caps_mask
62606203
local has_caps = caps:sub(1, 1) == "U"
6261-
local items = {}
62626204

62636205
for cand in translation:iter() do
6264-
local original_is_han = is_han_char(cand.text)
6265-
local need_reconstruct = (cand.type == "sentence" or cand.type == "user_phrase"
6266-
or cand.type == "dictionary")
6267-
6268-
local text = cand.text
6269-
local text_changed = false
6270-
if need_reconstruct then
6271-
text = apply_user_separators(cand, env)
6272-
text_changed = true
6273-
if has_caps then
6274-
text = capitalize_first(text)
6275-
end
6276-
else
6277-
if has_caps then
6278-
text = capitalize_first(cand.text)
6279-
text_changed = true
6280-
end
6281-
end
6282-
6283-
local display_comment = original_is_han
6284-
and codes_to_handwriting(cand.comment)
6285-
or ""
6286-
6287-
if text_changed then
6288-
table.insert(items, {
6289-
cand = cand,
6290-
type = cand.type,
6291-
start = cand.start,
6292-
_end = cand._end,
6293-
text = text,
6294-
comment = display_comment,
6295-
is_latin = text:match("^%a") ~= nil,
6296-
new_cand = true
6297-
})
6206+
if is_han_char(cand.text) then
6207+
local comment = cand.comment or ""
6208+
local hw = comment:gsub("[%w]+", function(code)
6209+
return SYLLABLE_MAP[code:lower()] or code
6210+
end)
6211+
hw = hw:gsub(" ", "--")
6212+
hw = hw:gsub(" ", "-")
6213+
cand.comment = hw
6214+
yield(cand)
62986215
else
6299-
cand.comment = display_comment
6300-
table.insert(items, {
6301-
cand = cand,
6302-
is_latin = cand.text:match("^%a") ~= nil,
6303-
new_cand = false
6304-
})
6305-
end
6306-
end
6307-
6308-
if has_caps then
6309-
for _, item in ipairs(items) do
6310-
if item.is_latin then
6311-
if item.new_cand then
6312-
yield(Candidate(item.type, item.start, item._end, item.text, item.comment))
6216+
local comment = cand.comment or ""
6217+
local hw_parts = {}
6218+
for code in comment:gmatch("[%w]+") do
6219+
local hw = SYLLABLE_MAP[code:lower()]
6220+
if hw then
6221+
table.insert(hw_parts, hw)
63136222
else
6314-
yield(item.cand)
6223+
hw_parts = {}
6224+
break
63156225
end
63166226
end
6317-
end
6318-
for _, item in ipairs(items) do
6319-
if not item.is_latin then
6320-
if item.new_cand then
6321-
yield(Candidate(item.type, item.start, item._end, item.text, item.comment))
6322-
else
6323-
yield(item.cand)
6324-
end
6227+
local new_text = cand.text
6228+
if #hw_parts > 0 then
6229+
new_text = table.concat(hw_parts, "-")
6230+
end
6231+
if has_caps then
6232+
new_text = capitalize_first(new_text)
6233+
end
6234+
if new_text ~= cand.text then
6235+
yield(ShadowCandidate(cand, cand.type, new_text, ""))
6236+
else
6237+
cand.comment = ""
6238+
yield(cand)
63256239
end
6326-
end
6327-
else
6328-
for _, item in ipairs(items) do
6329-
yield(item.cand)
63306240
end
63316241
end
63326242
end

teochew.dict.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Generated from merged.csv - do not edit manually
33
---
44
name: teochew
5-
version: "20260426.203949"
5+
version: "20260427.211159"
66
sort: by_weight
77
...
88

teochew_chars.dict.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Generated from merged.csv - decomposed character table
33
---
44
name: teochew_chars
5-
version: "20260426.203949"
5+
version: "20260427.211159"
66
sort: by_weight
77
...
88

teochew_dp.dict.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Generated from merged.csv - do not edit manually
33
---
44
name: teochew_dp
5-
version: "20260426.203949"
5+
version: "20260427.211159"
66
sort: by_weight
77
import_tables:
88
- teochew_chars

teochew_dp.schema.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
schema:
55
schema_id: teochew_dp
66
name: 潮州話拼音
7-
version: "20260426.203949"
7+
version: "20260427.211159"
88
author:
99
- Hokkien Writing Project
1010
dependencies:
@@ -103,6 +103,7 @@ speller:
103103
- derive/x/X/
104104
- derive/y/Y/
105105
- derive/z/Z/
106+
- abbrev/^([a-z]).+$/$1/
106107

107108
menu:
108109
page_size: 6

0 commit comments

Comments
 (0)