丸数値①を(1)に変換する例


ドキュメント等で①などの丸数字があると migemo の力が活かせないので、さくっと変換してしまおうとして考えたコードです。

list = [
  {:type => "○", :size => "中", :base => 0,  :range => 0x24ea..0x24ea   },
  {:type => "○", :size => "中", :base => 1,  :range => 0x2460..0x2473   },
  {:type => "○", :size => "中", :base => 21, :range => 0x3251..0x325f   },
  {:type => "○", :size => "中", :base => 36, :range => 0x32b1..0x32bf   },
  {:type => "○", :size => "小", :base => 0,  :range => 0x1f10b..0x1f10b },
  {:type => "○", :size => "小", :base => 1,  :range => 0x2780..0x2789   },
  {:type => "●", :size => "中", :base => 0,  :range => 0x24ff..0x24ff   },
  {:type => "●", :size => "中", :base => 1,  :range => 0x2776..0x277f   },
  {:type => "●", :size => "中", :base => 11, :range => 0x24eb..0x24f4   },
  {:type => "●", :size => "中", :base => 0,  :range => 0x1f10c..0x1f10c },
  {:type => "●", :size => "小", :base => 1,  :range => 0x278a..0x2793   },
  {:type => "◎", :size => "中", :base => 1,  :range => 0x24f5..0x24fe   },
]

# テーブルが正しいか確認する用
if false
  list.each do |e|
    range = e[:base] .. e[:base] + e[:range].size.pred
    s = e[:range].to_a.pack("U*")
    p [e[:type], e[:size], range, s]
  end
end

# 置換用ハッシュを準備
replace_hash = list.inject({}) { |a, e|
  a.merge(e[:range].each.with_index(e[:base]).inject({}) { |a, (v, i)|
      a.merge([v].pack("U") => "(#{i})")
    })
}

replace = -> s { s.gsub(/[#{replace_hash.keys.join}]/o, replace_hash) }

# マッチしたところだけ変換できる
replace["項目①です"]           # => "項目(1)です"

# 全部変換してみる
list.each do |e|
  s = e[:range].to_a.pack("U*")
  p [s, replace[s]]
end
# >> ["⓪", "(0)"]
# >> ["①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳", "(1)(2)(3)(4)(5)(6)(7)(8)(9)(10)(11)(12)(13)(14)(15)(16)(17)(18)(19)(20)"]
# >> ["㉑㉒㉓㉔㉕㉖㉗㉘㉙㉚㉛㉜㉝㉞㉟", "(21)(22)(23)(24)(25)(26)(27)(28)(29)(30)(31)(32)(33)(34)(35)"]
# >> ["㊱㊲㊳㊴㊵㊶㊷㊸㊹㊺㊻㊼㊽㊾㊿", "(36)(37)(38)(39)(40)(41)(42)(43)(44)(45)(46)(47)(48)(49)(50)"]
# >> ["🄋", "(0)"]
# >> ["➀➁➂➃➄➅➆➇➈➉", "(1)(2)(3)(4)(5)(6)(7)(8)(9)(10)"]
# >> ["⓿", "(0)"]
# >> ["❶❷❸❹❺❻❼❽❾❿", "(1)(2)(3)(4)(5)(6)(7)(8)(9)(10)"]
# >> ["⓫⓬⓭⓮⓯⓰⓱⓲⓳⓴", "(11)(12)(13)(14)(15)(16)(17)(18)(19)(20)"]
# >> ["🄌", "(0)"]
# >> ["➊➋➌➍➎➏➐➑➒➓", "(1)(2)(3)(4)(5)(6)(7)(8)(9)(10)"]
# >> ["⓵⓶⓷⓸⓹⓺⓻⓼⓽⓾", "(1)(2)(3)(4)(5)(6)(7)(8)(9)(10)"]

参照