Class: Ja::SoundIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/ja/sound_index.rb

Constant Summary collapse

VOWEL =
%w[A I U E O]
CONSONANT =
%w[A K S T N H M Y R W G Z D B P]
LARGE =
[
  '', '', '', '', '', '', '', '', '', '', 
  '', '', '', '', '', '', '', '', '', '', 
  '', '', '', '', '', '', '', '', '', '', 
  '', '', '', '', '', '',  nil, '',  nil, '', 
  '', '', '', '', '', '', '', '', '', '', 
  '', '', '', '', '', '', '', '', '', '', 
  '', '', '', '', '', '', '', '', '', '', 
  '', '', '', '', '',
]
SMALL =
[
  '',  '', '', '', '', '',   nil, '',  nil, '',
  ''
]
SPECIAL =
{'' => '.', '' => '-', '' => 'N'}

Instance Method Summary collapse

Constructor Details

#initialize(mode) ⇒ SoundIndex

Returns a new instance of SoundIndex.



22
23
24
# File 'lib/ja/sound_index.rb', line 22

def initialize(mode)
  @mode = mode
end

Instance Method Details

#consonant(text, with = []) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ja/sound_index.rb', line 42

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) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/ja/sound_index.rb', line 26

def only_katakana?(text)
  text.match /^[#{LARGE.join}#{SMALL.join}#{SPECIAL.keys.join}]+$/u
end

#vowel(text, with = []) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ja/sound_index.rb', line 30

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