class BetaCode

Public Class Methods

beta_code_to_greek(beta_code, custom_map: {}) click to toggle source

This method is absurd. But having the logic all in one method makes it easier to work with than having it spread across multiple methods. It also makes it easier to compare the logic to the Beta Code libraries in other languages. rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize

# File lib/beta_code.rb, line 15
def self.beta_code_to_greek(beta_code, custom_map: {})
  map = beta_code_to_unicode_map.merge(stringify_keys(custom_map))
  beta_code_characters = beta_code.chars
  greek_characters = []
  start = 0
  max_beta_code_character_length = map.keys.map(&:length).max

  while start <= beta_code_characters.length
    current_character = beta_code_characters[start]
    new_start = start + 1
    max_length = [beta_code_characters.length, start + max_beta_code_character_length].min

    last = new_start
    while last <= max_length
      slice = beta_code_characters[start...last].join

      if map[slice]
        current_character = map[slice]
        new_start = last
      end

      last += 1
    end

    greek_characters << current_character
    start = new_start
  end

  sigma_to_end_of_word_sigma(greek_characters.join)
end
greek_to_beta_code(greek, custom_map: {}) click to toggle source
# File lib/beta_code.rb, line 4
def self.greek_to_beta_code(greek, custom_map: {})
  map = unicode_to_beta_code_map.merge(stringify_keys(custom_map))

  greek.unicode_normalize.chars.map { |c| map[c] || c }.join
end

Private Class Methods

beta_code_to_unicode_map() click to toggle source
# File lib/beta_code.rb, line 52
def self.beta_code_to_unicode_map
  @beta_code_to_unicode_map ||= JSON.parse(read_vendor_file('beta-code-json/beta_code_to_unicode.json'))
end
read_vendor_file(file) click to toggle source
# File lib/beta_code.rb, line 60
def self.read_vendor_file(file)
  File.read(File.expand_path("../../vendor/#{file}", __FILE__))
end
sigma_to_end_of_word_sigma(string) click to toggle source

rubocop:enable Metrics/MethodLength rubocop:enable Metrics/AbcSize

# File lib/beta_code.rb, line 48
def self.sigma_to_end_of_word_sigma(string)
  string.gsub(/σ(?=[,.:;·\s]|$)/, 'ς')
end
stringify_keys(hash) click to toggle source
# File lib/beta_code.rb, line 64
def self.stringify_keys(hash)
  {}.tap do |stringified_hash|
    hash.each { |k, v| stringified_hash[k.to_s] = v }
  end
end
unicode_to_beta_code_map() click to toggle source
# File lib/beta_code.rb, line 56
def self.unicode_to_beta_code_map
  @unicode_to_beta_code_map ||= JSON.parse(read_vendor_file('beta-code-json/unicode_to_beta_code.json'))
end