PageRenderTime 59ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/tr.rb

http://webinars.googlecode.com/
Ruby | 57 lines | 51 code | 6 blank | 0 comment | 3 complexity | ad89397a982abf96c9cc920fca63bbc2 MD5 | raw file
  1. $KCODE = 'u'
  2. TABLE_LOWER = {
  3. "?"=>"i","?"=>"g","?"=>"yo","?"=>"#","?"=>"e",
  4. "?"=>"yi","?"=>"a","?"=>"b",
  5. "?"=>"v","?"=>"g","?"=>"d","?"=>"e","?"=>"zh",
  6. "?"=>"z","?"=>"i","?"=>"y","?"=>"k","?"=>"l",
  7. "?"=>"m","?"=>"n","?"=>"o","?"=>"p","?"=>"r",
  8. "?"=>"s","?"=>"t","?"=>"u","?"=>"f","?"=>"h",
  9. "?"=>"ts","?"=>"ch","?"=>"sh","?"=>"sch","?"=>"'",
  10. "?"=>"yi","?"=>"","?"=>"e","?"=>"yu","?"=>"ya"
  11. }.sort do | one, two|
  12. two[1].size <=> one[1].size
  13. end
  14. TABLE_UPPER = {
  15. "?"=>"G","?"=>"YO","?"=>"E","?"=>"YI","?"=>"I",
  16. "?"=>"A","?"=>"B","?"=>"V","?"=>"G",
  17. "?"=>"D","?"=>"E","?"=>"ZH","?"=>"Z","?"=>"I",
  18. "?"=>"Y","?"=>"K","?"=>"L","?"=>"M","?"=>"N",
  19. "?"=>"O","?"=>"P","?"=>"R","?"=>"S","?"=>"T",
  20. "?"=>"U","?"=>"F","?"=>"H","?"=>"TS","?"=>"CH",
  21. "?"=>"SH","?"=>"SCH","?"=>"'","?"=>"YI","?"=>"",
  22. "?"=>"E","?"=>"YU","?"=>"YA",
  23. }.sort do | one, two|
  24. two[1].size <=> one[1].size
  25. end
  26. TABLE = TABLE_UPPER + TABLE_LOWER
  27. def tr(str)
  28. chars = str.split(//)
  29. lowers = TABLE_LOWER.map{|e| e[0] }
  30. uppers = TABLE_UPPER.map{|e| e[0] }
  31. hashtable = {}
  32. TABLE.each do | item |
  33. next unless item[0] && item[1]
  34. hashtable[item[0]] = item[1]
  35. end
  36. result = ''
  37. chars.each_with_index do | char, index |
  38. if uppers.include?(char) && lowers.include?(chars[index+1])
  39. ch = hashtable[char].downcase.capitalize
  40. result << ch
  41. elsif uppers.include?(char)
  42. result << hashtable[char]
  43. elsif lowers.include?(char)
  44. result << hashtable[char]
  45. else
  46. result << char
  47. end
  48. end
  49. return result
  50. end