Created
October 21, 2012 05:56
-
-
Save randym/3926079 to your computer and use it in GitHub Desktop.
crap code for finding index of kanna phonetics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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