/tr.rb
Ruby | 57 lines | 51 code | 6 blank | 0 comment | 3 complexity | ad89397a982abf96c9cc920fca63bbc2 MD5 | raw file
- $KCODE = 'u'
- TABLE_LOWER = {
- "?"=>"i","?"=>"g","?"=>"yo","?"=>"#","?"=>"e",
- "?"=>"yi","?"=>"a","?"=>"b",
- "?"=>"v","?"=>"g","?"=>"d","?"=>"e","?"=>"zh",
- "?"=>"z","?"=>"i","?"=>"y","?"=>"k","?"=>"l",
- "?"=>"m","?"=>"n","?"=>"o","?"=>"p","?"=>"r",
- "?"=>"s","?"=>"t","?"=>"u","?"=>"f","?"=>"h",
- "?"=>"ts","?"=>"ch","?"=>"sh","?"=>"sch","?"=>"'",
- "?"=>"yi","?"=>"","?"=>"e","?"=>"yu","?"=>"ya"
- }.sort do | one, two|
- two[1].size <=> one[1].size
- end
-
- TABLE_UPPER = {
- "?"=>"G","?"=>"YO","?"=>"E","?"=>"YI","?"=>"I",
- "?"=>"A","?"=>"B","?"=>"V","?"=>"G",
- "?"=>"D","?"=>"E","?"=>"ZH","?"=>"Z","?"=>"I",
- "?"=>"Y","?"=>"K","?"=>"L","?"=>"M","?"=>"N",
- "?"=>"O","?"=>"P","?"=>"R","?"=>"S","?"=>"T",
- "?"=>"U","?"=>"F","?"=>"H","?"=>"TS","?"=>"CH",
- "?"=>"SH","?"=>"SCH","?"=>"'","?"=>"YI","?"=>"",
- "?"=>"E","?"=>"YU","?"=>"YA",
- }.sort do | one, two|
- two[1].size <=> one[1].size
- end
- TABLE = TABLE_UPPER + TABLE_LOWER
-
- def tr(str)
- chars = str.split(//)
-
- lowers = TABLE_LOWER.map{|e| e[0] }
- uppers = TABLE_UPPER.map{|e| e[0] }
-
- hashtable = {}
- TABLE.each do | item |
- next unless item[0] && item[1]
- hashtable[item[0]] = item[1]
- end
-
- result = ''
- chars.each_with_index do | char, index |
- if uppers.include?(char) && lowers.include?(chars[index+1])
- ch = hashtable[char].downcase.capitalize
- result << ch
- elsif uppers.include?(char)
- result << hashtable[char]
- elsif lowers.include?(char)
- result << hashtable[char]
- else
- result << char
- end
- end
- return result
- end