Guest User

Untitled

a guest
Feb 20th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. require 'rubygems'
  2. require 'activesupport'
  3. $KCODE = 'u'
  4.  
  5. class String
  6. # Converts the Greek Unicode characters contained in the string
  7. # to latin ones (aka greeklish) and returns self.
  8. # For unobstructive conversion call the non-bang method 'greeklish'
  9. #
  10. # example:
  11. # puts 'αβγδεζηθικλμνξοπρστυφχψω άέήίϊΐόύ ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ ABCDEFGHIJKLMNOPQRSTUVXYZ'.greeklish
  12. # returns:
  13. # avgdezhthiklmnksoprstyfxpsw aehiiioy AVGDEZHTHIKLMNKSOPRSTYFXPSW ABCDEFGHIJKLMNOPQRSTUVXYZ
  14. def greeklish!
  15. mapping_table = {
  16. 'ά' => 'a', 'α' => 'a', 'Α' => 'A', 'Ά' => 'A',
  17. 'β' => 'v', 'Β' => 'V',
  18. 'γ' => 'g', 'Γ' => 'G',
  19. 'δ' => 'd', 'Δ' => 'D',
  20. 'έ' => 'e', 'ε' => 'e', 'Ε' => 'E', 'Έ' => 'E',
  21. 'ζ' => 'z', 'Ζ' => 'Z',
  22. 'ή' => 'h', 'η' => 'h', 'Η' => 'H', 'Ή' => 'H',
  23. 'θ' => 'th', 'Θ' => 'TH',
  24. 'ί' => 'i', 'ϊ' => 'i', 'ΐ' => 'i', 'ι' => 'i', 'Ι' => 'I', 'Ί' => 'I',
  25. 'κ' => 'k', 'Κ' => 'K',
  26. 'λ' => 'l', 'Λ' => 'L',
  27. 'μ' => 'm', 'Μ' => 'M',
  28. 'ν' => 'n', 'Ν' => 'N',
  29. 'ξ' => 'ks', 'Ξ' => 'KS',
  30. 'ό' => 'o', 'ο' => 'o', 'Ό' => 'O','Ο' => 'O',
  31. 'π' => 'p', 'Π' => 'P',
  32. 'ρ' => 'r', 'Ρ' => 'R',
  33. 'σ' => 's', 'Σ' => 'S', 'ς' => 's',
  34. 'τ' => 't', 'Τ' => 'T',
  35. 'ύ' => 'y', 'υ' => 'y', 'Ύ' => 'Y','Υ' => 'Y',
  36. 'φ' => 'f', 'Φ' => 'F',
  37. 'χ' => 'x', 'Χ' => 'X',
  38. 'ψ' => 'ps', 'Ψ' => 'PS',
  39. 'ώ' => 'w', 'ω' => 'w', 'Ώ' => 'W','Ω' => 'W'
  40. }
  41.  
  42. for i in 0...self.chars.length
  43. char = self.chars[i..i]
  44. self.chars[i..i] = mapping_table[char.to_s] ? mapping_table[char.to_s] : char
  45. end
  46.  
  47. self
  48. end
  49.  
  50. # Returns a new string which is converted from Greek Unicode characters
  51. # to latin ones (aka greeklish)
  52. def greeklish
  53. self.dup.greeklish!
  54. end
  55. end
Add Comment
Please, Sign In to add comment