class GreeklishIso843::GreekText

Constants

GREEK_LETTERS_AFTER_F
GREEK_LETTERS_AFTER_V
GREEK_LOWER
GREEK_UPPER
GREEK_VOWELS
PAIRS_FOR_V_OR_F
REPLACEMENTS
REPLACEMENT_KEYS_REGEXP

Attributes

text[R]

Public Class Methods

new(text) click to toggle source
# File lib/greeklish_iso843/greek_text.rb, line 87
def initialize(text)
  @text = text
end
to_greeklish(text) click to toggle source
# File lib/greeklish_iso843/greek_text.rb, line 83
def self.to_greeklish(text)
  new(text).to_greeklish
end

Public Instance Methods

to_greeklish() click to toggle source
# 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

Private Instance Methods

convert_mp_or_b(prev_char, next_char) click to toggle source
# File lib/greeklish_iso843/greek_text.rb, line 167
        def convert_mp_or_b(prev_char, next_char)
  if prev_char && lowercase?(prev_char) && # *μπ
      next_char && lowercase?(next_char) # and μπ*
    'mp'
  else # μπ* or *μπ
    'b'
  end
end
convert_pair(match, match_data) click to toggle source
# File lib/greeklish_iso843/greek_text.rb, line 102
        def convert_pair(match, match_data)
  return 'ts' if match.casecmp?('τς')

  next_char = match_data.post_match[0]&.downcase

  if match.casecmp?('μπ')
    prev_char = match_data.pre_match[-1]&.downcase
    return convert_mp_or_b(prev_char, next_char)
  end

  if PAIRS_FOR_V_OR_F.any? { |pair| match.casecmp?(pair) }
    return convert_pair_for_v_or_f(match, next_char)
  end

  match # Unhandled case. Return as-is.
end
convert_pair_for_v_or_f(match, next_char) click to toggle source
# File lib/greeklish_iso843/greek_text.rb, line 176
        def convert_pair_for_v_or_f(match, next_char)
  v_or_f = convert_v_or_f(next_char)

  if v_or_f.nil?
    raise UnhandledCaseError # Should never happen
  end

  REPLACEMENTS[match[0].downcase] + v_or_f
end
convert_v_or_f(next_char) click to toggle source
# File lib/greeklish_iso843/greek_text.rb, line 194
        def convert_v_or_f(next_char)
  if next_char.nil? ||
      GREEK_LETTERS_AFTER_F[next_char] ||
      next_char !~ REPLACEMENT_KEYS_REGEXP
    return 'f'
  end

  'v' if GREEK_LETTERS_AFTER_V[next_char]
end
fix_case(greeklish, match, match_data) click to toggle source
# File lib/greeklish_iso843/greek_text.rb, line 119
        def fix_case(greeklish, match, match_data)
  if !uppercase?(match[0])
    return greeklish
  end

  if match.size == 1
    return fix_case_single_letter_match(greeklish, match_data)
  end

  if match.size == 2
    return fix_case_two_letter_match(greeklish, match)
  end

  raise UnhandledCaseError
end
fix_case_single_letter_match(greeklish, match_data) click to toggle source
# File lib/greeklish_iso843/greek_text.rb, line 135
        def fix_case_single_letter_match(greeklish, match_data)
  if greeklish.size == 1
    return greeklish.upcase
  end

  if greeklish.size == 2 # match is one of Θ, Χ, Ψ
    return fix_case_th_ch_ps(greeklish, match_data)
  end

  raise UnhandledCaseError
end
fix_case_th_ch_ps(greeklish, match_data) click to toggle source
# File lib/greeklish_iso843/greek_text.rb, line 155
        def fix_case_th_ch_ps(greeklish, match_data)
  next_char = match_data.post_match[0]

  if next_char.nil? ||
      next_char !~ REPLACEMENT_KEYS_REGEXP ||
      !uppercase?(next_char)
    return greeklish[0].upcase + greeklish[1].to_s
  end

  greeklish.upcase
end
fix_case_two_letter_match(greeklish, match) click to toggle source
# File lib/greeklish_iso843/greek_text.rb, line 147
        def fix_case_two_letter_match(greeklish, match)
  if uppercase?(match[1])
    return greeklish.upcase
  end

  greeklish[0].upcase + greeklish[1].to_s
end
lowercase?(char) click to toggle source
# File lib/greeklish_iso843/greek_text.rb, line 190
        def lowercase?(char)
  GREEK_LOWER[char]
end
uppercase?(char) click to toggle source
# File lib/greeklish_iso843/greek_text.rb, line 186
        def uppercase?(char)
  GREEK_UPPER[char]
end