Skip to content

Instantly share code, notes, and snippets.

@randym
Created October 21, 2012 05:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save randym/3926079 to your computer and use it in GitHub Desktop.
Save randym/3926079 to your computer and use it in GitHub Desktop.
crap code for finding index of kanna phonetics
# encoding: UTF-8
#
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(ここのつ こ きゅう く ナイン)
@data = n.map do |phonetics|
phonetics.map { |item| item = NKF.nkf("-w -h2", item) }
end
end
end
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment