module Kirillica::GOST_2000

Constants

TABLE
WINDOW

Public Class Methods

revert!(phrase) click to toggle source

invert transliteration

# File lib/kirillica/gost_2000.rb, line 60
def self.revert!(phrase)
  reverted_phrase = ''
  chars = phrase.scan /\w/

  while chars.any?
    window = WINDOW

    while window > 0
      raise 'cannot revert phrase' if window.zero?

      char_group = chars.take(window).join

      inverted_hash = {}
      TABLE.invert.each { |key, value| key.is_a?(Array) ? key.each { |kk| inverted_hash[kk] = value } : inverted_hash[key] = value }

      if inverted_hash[char_group]
        reverted_phrase << inverted_hash[char_group]
        chars = chars.drop(window)
        break
      end

      window -= 1
    end
  end

  reverted_phrase
end
translit(phrase='') click to toggle source

transliteration

# File lib/kirillica/gost_2000.rb, line 42
def self.translit(phrase='')
  return '' if phrase.empty?
  translitted_phrase = ''

  phrase.each_char do |char|
    translitted_char = TABLE[char]
    if translitted_char.nil?
      translitted_phrase << char
    else
      c = translitted_char.is_a?(Array) ? translitted_char.first : translitted_char
      translitted_phrase << c
    end
  end

  translitted_phrase
end