class Ja::SoundIndex

Constants

CONSONANT
LARGE
SMALL
SPECIAL
VOWEL

Public Class Methods

new(mode) click to toggle source
# File lib/ja/sound_index.rb, line 21
def initialize(mode)
  @mode = mode
end

Public Instance Methods

consonant(text, with=[]) click to toggle source
# File lib/ja/sound_index.rb, line 41
def consonant(text, with=[])
  if !only_katakana?(text)
    raise ArgumentError, 'Error: first param must consist only of katakana.'
  end
  text = text.gsub(/([#{LARGE.join}])/u) {
    i = LARGE.index($1) - LARGE.index($1) % 5
    @mode == :ascii ? CONSONANT[i / 5] : LARGE[i]
  }
  text = text.gsub(/[#{SMALL.join}]/u, '')
  special(text, with)
end
only_katakana?(text) click to toggle source
# File lib/ja/sound_index.rb, line 25
def only_katakana?(text)
  text.match /^[#{LARGE.join}#{SMALL.join}#{SPECIAL.keys.join}]+$/u
end
vowel(text, with=[]) click to toggle source
# File lib/ja/sound_index.rb, line 29
def vowel(text, with=[])
  if !only_katakana?(text)
    raise ArgumentError, 'Error: first param must consist only of katakana.'
  end
  text = text.gsub(/([#{LARGE.join}])([#{SMALL.join}])*/u) {
    map, snd = $2 ? [SMALL, $2] : [LARGE, $1]
    arr = @mode == :ascii ? VOWEL : LARGE
    arr[map.index(snd) % 5]
  }
  special(text, with)
end

Private Instance Methods

special(text, with) click to toggle source
# File lib/ja/sound_index.rb, line 54
def special(text, with)
  if !(without = SPECIAL.keys - with).empty?
    text = text.gsub(/[#{without.join}]/u, '')
  end
  if @mode == :ascii
    SPECIAL.each do |k, v|
      text = text.gsub(/#{k}/u) { v }
    end
  end
  text
end