Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions yuta_randym/phonetic_map.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# encoding: UTF-8
#
puts $1
require 'nkf'
class PhoneticMap

def data
@data ||= build_data
end

def index_of(string)
data.each.with_index do |phonetic_equivelant, index|
return index if phonetic_equivelant.include?(string)
end
nil
end

def build_data
n = []
n << %w(まる ま れい れ オウ ゼロ ゼ)
n << %w(ひとつ ひと ひ いち い ワン)
n << %w(ふたつ ふた ふ に ツ)
n << %w(みつ み さん さ スリー)
n << %w(よん よ よつ し フォー)
n << %w(いつつ いつ ご こ ファイブ ファイヴ)
n << %w(むつ む ろく ろ シックス)
n << %w(ななつ なな な しち セブン セヴン)
n << %w(やつ や はち は ば エート)
n << %w(ここのつ こ きゅう く ナイン)
n.map do |phonetics|
phonetics.map { |item| item = NKF.nkf("-w -h2", item) }
end
end

def transliterate(kana)
tmp = ''
output = []
kana.each_char do |char|
tmp += char
puts tmp
if index = index_of(tmp)
output << index
tmp =''
else
tmp += char
end
end
output.join('')
end
end

if ARGV.size == 1
_map = PhoneticMap.new
puts _map.transliterate(ARGV.first)
end

33 changes: 33 additions & 0 deletions yuta_randym/test_phonetic_map.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# encoding: UTF-8
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/"

require 'phonetic_map'

require 'test/unit'
class TestPhoneticMap < Test::Unit::TestCase

def setup
@data = PhoneticMap.new
end

def test_initialize
assert(@data.data.is_a?(Array))
end
def test_index_of_yo
assert_equal(0, @data.index_of('マ'))
end

def test_not_found
assert_equal(nil,@data.index_of('foo'))
end

def test_multiple_char
assert_equal(7, @data.index_of('ナナツ'))
end

def test_multiple_char
assert_equal(6, @data.index_of('ロ'))
end

end