PageRenderTime 530ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/plugins/salty_slugs/lib/salty_slugs/transliteration.rb

http://kiev-apartment.googlecode.com/
Ruby | 60 lines | 42 code | 7 blank | 11 comment | 2 complexity | d77484e54e0e8601fde821a24d3a0245 MD5 | raw file
  1. # encoding: utf-8
  2. module SaltySlugs
  3. # Russian transliteration
  4. #
  5. # ?????????????? ??? ???? ???????? ????????
  6. module Transliteration
  7. extend self
  8. # Transliteration heavily based on rutils gem by Julian "julik" Tarkhanov and Co.
  9. # <http://rutils.rubyforge.org/>
  10. # Cleaned up and optimized.
  11. LOWER = {
  12. "?"=>"i","?"=>"g","?"=>"yo","?"=>"#","?"=>"e",
  13. "?"=>"yi","?"=>"a","?"=>"b",
  14. "?"=>"v","?"=>"g","?"=>"d","?"=>"e","?"=>"zh",
  15. "?"=>"z","?"=>"i","?"=>"y","?"=>"k","?"=>"l",
  16. "?"=>"m","?"=>"n","?"=>"o","?"=>"p","?"=>"r",
  17. "?"=>"s","?"=>"t","?"=>"u","?"=>"f","?"=>"h",
  18. "?"=>"ts","?"=>"ch","?"=>"sh","?"=>"sch","?"=>"'",
  19. "?"=>"y","?"=>"","?"=>"e","?"=>"yu","?"=>"ya"
  20. }.freeze
  21. UPPER = {
  22. "?"=>"G","?"=>"YO","?"=>"E","?"=>"YI","?"=>"I",
  23. "?"=>"A","?"=>"B","?"=>"V","?"=>"G",
  24. "?"=>"D","?"=>"E","?"=>"ZH","?"=>"Z","?"=>"I",
  25. "?"=>"Y","?"=>"K","?"=>"L","?"=>"M","?"=>"N",
  26. "?"=>"O","?"=>"P","?"=>"R","?"=>"S","?"=>"T",
  27. "?"=>"U","?"=>"F","?"=>"H","?"=>"TS","?"=>"CH",
  28. "?"=>"SH","?"=>"SCH","?"=>"'","?"=>"Y","?"=>"",
  29. "?"=>"E","?"=>"YU","?"=>"YA",
  30. }.freeze
  31. # Transliterate a string with russian characters
  32. #
  33. # ?????????? ??????, ? ??????? ??? ????? ???????? ???????? ???????? ?? ??????? ?? ???????? ????????
  34. def transliterate(str)
  35. chars = str.split(//)
  36. result = ""
  37. chars.each_with_index do |char, index|
  38. if UPPER.has_key?(char) && LOWER.has_key?(chars[index+1])
  39. # combined case
  40. result << UPPER[char].downcase.capitalize
  41. elsif UPPER.has_key?(char)
  42. result << UPPER[char]
  43. elsif LOWER.has_key?(char)
  44. result << LOWER[char]
  45. else
  46. result << char
  47. end
  48. end
  49. return result
  50. end
  51. end
  52. end