banner
虫子游戈

虫子游戈

一个写故事的人类
mastodon
email

How to add specific traditional Chinese characters to the Rime input method candidates

I previously successfully implemented support for mixed input of simplified and traditional Chinese in Rime input method, but in practice, I found it only added trouble, as it disrupted the original familiar order, greatly increasing the time and effort cost of input. Thus, the input method reverted to a pure simplified version.

However, recently, due to work, I needed to input the specific traditional character "昇," which the simplified version of Rime cannot input directly—even adding "昇" to the custom word list does not work.

After researching for two nights, I finally found a solution with the help of Claude.

In fact, Claude provided several solutions, but after testing, only this one currently works.

This solution is based on a Lua script.

The first step is to create a new Lua script in User Folder/Lua, for example, namex.lua. The script content is as follows:

-- Special character handling
local special_chars = {
  ["升"] = "昇",
  ["龙"] = "龍"
  -- More special single character pairs can be added
}

-- Special phrase handling
local special_words = {
  ["升腾"] = "昇腾"
  -- More special phrase pairs can be added
}

local function filter(input, env)
  -- Record already processed special characters
  local handled = {}
  
  -- First pass: output all candidates normally and mark special characters/phrases
  for cand in input:iter() do
    yield(cand)
    
    -- Check single characters
    if special_chars[cand.text] then
      handled[cand.text] = true
      
      -- If a special character is found, immediately add the corresponding traditional character
      if not handled[special_chars[cand.text]] then
        local fan_text = special_chars[cand.text]
        local new_cand = Candidate("char", cand.start, cand._end, fan_text, "")
        new_cand.quality = cand.quality - 1  -- Slightly reduce weight
        yield(new_cand)
        handled[fan_text] = true
      end
    end
    
    -- Check phrases
    if special_words[cand.text] then
      handled[cand.text] = true
      
      -- If a special phrase is found, immediately add the corresponding traditional phrase
      if not handled[special_words[cand.text]] then
        local fan_text = special_words[cand.text]
        local new_cand = Candidate("word", cand.start, cand._end, fan_text, "")
        new_cand.quality = cand.quality - 1  -- Slightly reduce weight
        yield(new_cand)
        handled[fan_text] = true
      end
    end
  end
end

return filter

If you want to add more traditional characters or phrases, you can supplement them in the corresponding positions above.

Alternatively, you can use this simpler script:

-- Special character handling
local special_pairs = {
  ["升"] = "昇"
  -- More special character pairs can be added
}

local function filter(input, env)
  -- Record already processed special characters
  local handled = {}
  
  -- First pass: output all candidates normally and mark special characters
  for cand in input:iter() do
    yield(cand)
    
    -- Check if there are special character pairs
    if special_pairs[cand.text] then
      handled[cand.text] = true
    elseif special_pairs[cand.text] then
      handled[cand.text] = true
    end
    
    -- If a special character is found, immediately add the corresponding traditional/simplified character
    if special_pairs[cand.text] and not handled[special_pairs[cand.text]] then
      local fan_text = special_pairs[cand.text]
      local comment = "Traditional"
      local new_cand = Candidate("char", cand.start, cand._end, fan_text, comment)
      new_cand.quality = cand.quality - 1  -- Slightly reduce weight
      yield(new_cand)
      handled[fan_text] = true
    end
  end
end

return filter

The second step is to add a line in the rime.lua file:

namex_filter = require("namex")

The third step is to add in your your_input_scheme.custom.yaml file:

engine/+:
  filters/+:
    - lua_filter@namex_filter

Done. Just redeploy.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.