Module: Tobopomo
- Defined in:
- lib/tobopomo.rb,
lib/tobopomo/data.rb,
lib/tobopomo/keymap.rb,
lib/tobopomo/version.rb
Constant Summary collapse
- TSI =
JSON.load(open(data_path))
- LAYOUT =
{ "31": "\n", "33": "!", "34": "\"", "35": "#", "36": "$", "37": "%", "38": "&", "39": "'", "40": "(", "41": ")", "42": "*", "43": "+", "44": "ㄝ", "45": "ㄦ", "46": "ㄡ", "47": "ㄥ", "48": "ㄢ", "49": "ㄅ", "50": "ㄉ", "51": "ˇ", "52": "ˋ", "53": "ㄓ", "54": "ˊ", "55": "˙", "56": "ㄚ", "57": "ㄞ", "58": ":", "59": "ㄤ", "60": "<", "61": "=", "62": ">", "63": "?", "64": "@", "91": "[", "92": "\\", "93": "]", "94": "^", "95": "_", "96": "`", "97": "ㄇ", "98": "ㄖ", "99": "ㄏ", "100": "ㄎ", "101": "ㄍ", "102": "ㄑ", "103": "ㄕ", "104": "ㄘ", "105": "ㄛ", "106": "ㄨ", "107": "ㄜ", "108": "ㄠ", "109": "ㄩ", "110": "ㄙ", "111": "ㄟ", "112": "ㄣ", "113": "ㄆ", "114": "ㄐ", "115": "ㄋ", "116": "ㄔ", "117": "ㄧ", "118": "ㄒ", "119": "ㄊ", "120": "ㄌ", "121": "ㄗ", "122": "ㄈ", "123": "{", "124": "|", "125": "}", "126": "~" , "32": " " }
- VERSION =
"1.2.0"
Instance Method Summary collapse
Instance Method Details
#normalise(string) ⇒ Object
6 7 8 |
# File 'lib/tobopomo.rb', line 6 def normalise(string) string.gsub(/\s+/, "") end |
#tobopomo(input) ⇒ Object
9 10 11 12 13 |
# File 'lib/tobopomo.rb', line 9 def tobopomo(input) input = input.chars.map{|x| Tobopomo::LAYOUT[:"#{x.ord.to_s}"]}.join("") string = input.split(/(?<=[" "|"ˇ"|"ˋ"|"ˊ"|"˙"])/) return string.map{|x| normalise(x)} end |
#tokanji(input, limit = 5) ⇒ Object
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 |
# File 'lib/tobopomo.rb', line 14 def tokanji(input, limit=5) if input.is_a? Array input = input.map{|x| normalise(x)} output = [] last = 0 input.each_with_index do | x , index| string = input[last..index].join("") if Tobopomo::TSI[string] && index < (input.length - 1) next elsif Tobopomo::TSI[string] && index == (input.length - 1) output << Tobopomo::TSI[string][0...limit] elsif Tobopomo::TSI[string].nil? &&index == (input.length - 1) string = input[last...index].join("") output << Tobopomo::TSI[string][0...limit] output << Tobopomo::TSI[input[index]][0...limit] else string = input[last...index].join("") output << Tobopomo::TSI[string][0...limit] last = index end end return output elsif input.is_a? String normalise(input) return Tobopomo::TSI[input][0...limit] end end |