Module: Kirillica::GOST_2000

Defined in:
lib/kirillica/gost_2000.rb

Constant Summary collapse

TABLE =
{
  'а' => 'a',
  'б' => 'b',
  'в' => 'v',
  'г' => 'g',
  'д' => 'd',
  'е' => 'e',
  'ё' => 'yo',
  'ж' => 'zh',
  'з' => 'z',
  'и' => ['i', 'i\''],
  'й' => 'j',
  'к' => 'k',
  'л' => 'l',
  'м' => 'm',
  'н' => 'n',
  'о' => 'o',
  'п' => 'p',
  'р' => 'r',
  'с' => 's',
  'т' => 't',
  'у' => 'u',
  'ф' => 'f',
  'х' => 'x',
  'ц' => ['cz', 'c'],
  'ч' => 'ch',
  'ш' => 'sh',
  'щ' => 'shh',
  'ъ' => '``',
  'ы' => 'y\'',
  'ь' => '`',
  'э' => 'e`',
  'ю' => 'yu',
  'я' => 'ya',
}
WINDOW =
3

Class Method Summary collapse

Class Method Details

.revert!(phrase) ⇒ Object

invert transliteration



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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 = '') ⇒ Object

transliteration



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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