Class: GreeklishIso843::GreekText

Inherits:
Object
  • Object
show all
Defined in:
lib/greeklish_iso843/greek_text.rb

Defined Under Namespace

Classes: Error, UnhandledCaseError

Constant Summary collapse

GREEK_UPPER =
'ΑΆΒΓΔΕΈΖΗΉΘΙΊΪΚΛΜΝΞΟΌΠΡΣΤΥΎΫΦΧΨΩΏ'.freeze
GREEK_LOWER =
'αάβγδεέζηήθιίϊΐκλμνξοόπρσςτυύϋΰφχψωώ'.freeze
GREEK_VOWELS =
'αάεέηήιίϊΐοόυύϋΰωώ'.freeze
PAIRS_FOR_V_OR_F =
%w[αυ αύ ευ εύ ηυ ηύ].freeze
GREEK_LETTERS_AFTER_V =
"#{GREEK_VOWELS}βγδζλμνρ".freeze
GREEK_LETTERS_AFTER_F =
'θκξπσςτφχψ'.freeze
REPLACEMENTS =
{
  'αι' => 'ai',
  'αί' => 'ai',
  'οι' => 'oi',
  'οί' => 'oi',
  'ου' => 'ou',
  'ού' => 'ou',
  'ει' => 'ei',
  'εί' => 'ei',
  'ντ' => 'nt',
  'τσ' => 'ts',
  'τζ' => 'tz',
  'γγ' => 'ng',
  'γκ' => 'gk',
  'γχ' => 'nch',
  'γξ' => 'nx',
  'αυ' => nil,
  'αύ' => nil,
  'ευ' => nil,
  'εύ' => nil,
  'ηυ' => nil,
  'ηύ' => nil,
  'μπ' => nil,
  'α' => 'a',
  'ά' => 'a',
  'β' => 'v',
  'γ' => 'g',
  'δ' => 'd',
  'ε' => 'e',
  'έ' => 'e',
  'ζ' => 'z',
  'η' => 'i',
  'ή' => 'i',
  'θ' => 'th',
  'ι' => 'i',
  'ί' => 'i',
  'ϊ' => 'i',
  'ΐ' => 'i',
  'κ' => 'k',
  'λ' => 'l',
  'μ' => 'm',
  'ν' => 'n',
  'ξ' => 'x',
  'ο' => 'o',
  'ό' => 'o',
  'π' => 'p',
  'ρ' => 'r',
  'σ' => 's',
  'ς' => 's',
  'τ' => 't',
  'υ' => 'y',
  'ύ' => 'y',
  'ϋ' => 'y',
  'ΰ' => 'y',
  'φ' => 'f',
  'χ' => 'ch',
  'ψ' => 'ps',
  'ω' => 'o',
  'ώ' => 'o'
}.freeze
REPLACEMENT_KEYS_REGEXP =
/#{REPLACEMENTS.keys.join('|')}/i.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ GreekText

Returns a new instance of GreekText.



87
88
89
# File 'lib/greeklish_iso843/greek_text.rb', line 87

def initialize(text)
  @text = text
end

Instance Attribute Details

#textObject (readonly)

Returns the value of attribute text.



81
82
83
# File 'lib/greeklish_iso843/greek_text.rb', line 81

def text
  @text
end

Class Method Details

.to_greeklish(text) ⇒ Object



83
84
85
# File 'lib/greeklish_iso843/greek_text.rb', line 83

def self.to_greeklish(text)
  new(text).to_greeklish
end

Instance Method Details

#to_greeklishObject



91
92
93
94
95
96
97
98
99
100
# File 'lib/greeklish_iso843/greek_text.rb', line 91

def to_greeklish
  text.gsub(REPLACEMENT_KEYS_REGEXP) do |match|
    greeklish = REPLACEMENTS[match.downcase] ||
      convert_pair(match, Regexp.last_match)

    next match if greeklish.nil? # Unhandled case. Return as-is.

    fix_case(greeklish, match, Regexp.last_match)
  end
end