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