@@ -6187,65 +6187,7 @@ local function get_caps_mask(env)
61876187 return caps
61886188end
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
62506192local 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)
62566198end
62576199
62586200local 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
63326242end
0 commit comments