Last active
January 30, 2023 12:00
-
-
Save Wintus/dcaa71e62db87666e85a609ef6456d06 to your computer and use it in GitHub Desktop.
やまとことばの数詞
This file contains 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
ds = { | |
1 => '', | |
2 => "ふた", | |
3 => "み", | |
4 => "よ", | |
5 => "い", | |
6 => "む", | |
7 => "なな", | |
8 => "や", | |
9 => "ここの", | |
} | |
extra = { | |
1 => "ひと", | |
10 => "とを", | |
20 => "はた", | |
100 => "もも", | |
} | |
b10 = { | |
10 => "そ", | |
100 => "ほ", | |
1000 => "ち", | |
} | |
# monoidal operation pair: (integer:mul, string:concat) | |
map = | |
ds.merge( | |
*b10.map { |base, s| ds.to_h { [_1 * base, _2 + s] } }, | |
extra, | |
) | |
ymap = map.merge(ds).to_h { [_1 * 1_0000, _2 + 'よろづ'] } | |
KAZU_MAP = map.merge(ymap).sort.to_h | |
INV_KAZU_MAP = KAZU_MAP.invert | |
SEP = 'あまり' | |
def factorize(n) | |
n.digits.each_with_index | |
end | |
def to_yamato(n, sep: SEP) | |
raise RangeError.new("#{n} must be positive") unless n > 0 | |
get = -> (d) { | |
KAZU_MAP.fetch(d) { | |
raise RangeError.new("#{n} is out of range") | |
} | |
} | |
factorize(n) | |
.reject { |d, _| d.zero? } | |
.reverse | |
.map { _1 * 10**_2 }.map(&get) | |
.join(sep) | |
end | |
def from_yamato(n, sep: SEP) | |
n.split(sep).map(&INV_KAZU_MAP).sum | |
end |
This file contains 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
require 'minitest/autorun' | |
require_relative 'yamato-kazu.rb' | |
describe "やまとことばのかず" do | |
describe "yamato" do | |
describe "mappings" do | |
map = { | |
1=>"ひと", | |
2=>"ふた", | |
3=>"み", | |
4=>"よ", | |
5=>"い", | |
6=>"む", | |
7=>"なな", | |
8=>"や", | |
9=>"ここの", | |
10=>"とを", | |
20=>"はた", | |
30=>"みそ", | |
40=>"よそ", | |
50=>"いそ", | |
60=>"むそ", | |
70=>"ななそ", | |
80=>"やそ", | |
90=>"ここのそ", | |
100=>"もも", | |
200=>"ふたほ", | |
300=>"みほ", | |
400=>"よほ", | |
500=>"いほ", | |
600=>"むほ", | |
700=>"ななほ", | |
800=>"やほ", | |
900=>"ここのほ", | |
1000=>"ち", | |
2000=>"ふたち", | |
3000=>"みち", | |
4000=>"よち", | |
5000=>"いち", | |
6000=>"むち", | |
7000=>"ななち", | |
8000=>"やち", | |
9000=>"ここのち", | |
10000=>"よろづ", | |
20000=>"ふたよろづ", | |
30000=>"みよろづ", | |
40000=>"よよろづ", | |
50000=>"いよろづ", | |
60000=>"むよろづ", | |
70000=>"ななよろづ", | |
80000=>"やよろづ", | |
90000=>"ここのよろづ", | |
100000=>"とをよろづ", | |
200000=>"はたよろづ", | |
300000=>"みそよろづ", | |
400000=>"よそよろづ", | |
500000=>"いそよろづ", | |
600000=>"むそよろづ", | |
700000=>"ななそよろづ", | |
800000=>"やそよろづ", | |
900000=>"ここのそよろづ", | |
1000000=>"ももよろづ", | |
2000000=>"ふたほよろづ", | |
3000000=>"みほよろづ", | |
4000000=>"よほよろづ", | |
5000000=>"いほよろづ", | |
6000000=>"むほよろづ", | |
7000000=>"ななほよろづ", | |
8000000=>"やほよろづ", | |
9000000=>"ここのほよろづ", | |
10000000=>"ちよろづ", | |
20000000=>"ふたちよろづ", | |
30000000=>"みちよろづ", | |
40000000=>"よちよろづ", | |
50000000=>"いちよろづ", | |
60000000=>"むちよろづ", | |
70000000=>"ななちよろづ", | |
80000000=>"やちよろづ", | |
90000000=>"ここのちよろづ" | |
} | |
map.each do |n, s| | |
it "match" do | |
assert_equal s, KAZU_MAP[n] | |
end | |
end | |
end | |
describe "conversions" do | |
map = ( | |
[ | |
1, | |
12, | |
123, | |
1234, | |
12345, | |
123456, | |
9_9999, | |
19_9999, | |
119_9999, | |
1010_0203, | |
1111_1111, | |
1119_9999, | |
].zip %w[ | |
ひと | |
とをあまりふた | |
ももあまりはたあまりみ | |
ちあまりふたほあまりみそあまりよ | |
よろづあまりふたちあまりみほあまりよそあまりい | |
とをよろづあまりふたよろづあまりみちあまりよほあまりいそあまりむ | |
ここのよろづあまりここのちあまりここのほあまりここのそあまりここの | |
とをよろづあまりここのよろづあまりここのちあまりここのほあまりここのそあまりここの | |
ももよろづあまりとをよろづあまりここのよろづあまりここのちあまりここのほあまりここのそあまりここの | |
ちよろづあまりとをよろづあまりふたほあまりみ | |
ちよろづあまりももよろづあまりとをよろづあまりよろづあまりちあまりももあまりとをあまりひと | |
ちよろづあまりももよろづあまりとをよろづあまりここのよろづあまりここのちあまりここのほあまりここのそあまりここの | |
] | |
).to_h | |
map.each do |n, s| | |
it "#{n} => #{s}" do | |
assert_equal s, to_yamato(n) | |
end | |
end | |
end | |
describe "out of range error" do | |
[ | |
0, | |
1_0000_0000, | |
].each do |n| | |
it "#{n}" do | |
_ { to_yamato(n) }.must_raise | |
end | |
end | |
end | |
describe "others" do | |
it "2023 => ふたちあまりはたあまりみ" do # とせ | |
assert_equal 'ふたちあまりはたあまりみ', to_yamato(2023) | |
end | |
end | |
end | |
describe "inverse" do | |
describe "mapping" do | |
map = { | |
"ひと"=>1, | |
"ふた"=>2, | |
"み"=>3, | |
"よ"=>4, | |
"い"=>5, | |
"む"=>6, | |
"なな"=>7, | |
"や"=>8, | |
"ここの"=>9, | |
"とを"=>10, | |
"はた"=>20, | |
"みそ"=>30, | |
"よそ"=>40, | |
"いそ"=>50, | |
"むそ"=>60, | |
"ななそ"=>70, | |
"やそ"=>80, | |
"ここのそ"=>90, | |
"もも"=>100, | |
"ふたほ"=>200, | |
"みほ"=>300, | |
"よほ"=>400, | |
"いほ"=>500, | |
"むほ"=>600, | |
"ななほ"=>700, | |
"やほ"=>800, | |
"ここのほ"=>900, | |
"ち"=>1000, | |
"ふたち"=>2000, | |
"みち"=>3000, | |
"よち"=>4000, | |
"いち"=>5000, | |
"むち"=>6000, | |
"ななち"=>7000, | |
"やち"=>8000, | |
"ここのち"=>9000, | |
"よろづ"=>10000, | |
"ふたよろづ"=>20000, | |
"みよろづ"=>30000, | |
"よよろづ"=>40000, | |
"いよろづ"=>50000, | |
"むよろづ"=>60000, | |
"ななよろづ"=>70000, | |
"やよろづ"=>80000, | |
"ここのよろづ"=>90000, | |
"とをよろづ"=>100000, | |
"はたよろづ"=>200000, | |
"みそよろづ"=>300000, | |
"よそよろづ"=>400000, | |
"いそよろづ"=>500000, | |
"むそよろづ"=>600000, | |
"ななそよろづ"=>700000, | |
"やそよろづ"=>800000, | |
"ここのそよろづ"=>900000, | |
"ももよろづ"=>1000000, | |
"ふたほよろづ"=>2000000, | |
"みほよろづ"=>3000000, | |
"よほよろづ"=>4000000, | |
"いほよろづ"=>5000000, | |
"むほよろづ"=>6000000, | |
"ななほよろづ"=>7000000, | |
"やほよろづ"=>8000000, | |
"ここのほよろづ"=>9000000, | |
"ちよろづ"=>10000000, | |
"ふたちよろづ"=>20000000, | |
"みちよろづ"=>30000000, | |
"よちよろづ"=>40000000, | |
"いちよろづ"=>50000000, | |
"むちよろづ"=>60000000, | |
"ななちよろづ"=>70000000, | |
"やちよろづ"=>80000000, | |
"ここのちよろづ"=>90000000, | |
} | |
map.each do |s, n| | |
it "match" do | |
assert_equal n, INV_KAZU_MAP[s] | |
end | |
end | |
end | |
describe "conversions" do | |
map = ( | |
%w[ | |
ひと | |
とをあまりふた | |
ももあまりはたあまりみ | |
ちあまりふたほあまりみそあまりよ | |
よろづあまりふたちあまりみほあまりよそあまりい | |
とをよろづあまりふたよろづあまりみちあまりよほあまりいそあまりむ | |
ここのよろづあまりここのちあまりここのほあまりここのそあまりここの | |
とをよろづあまりここのよろづあまりここのちあまりここのほあまりここのそあまりここの | |
ももよろづあまりとをよろづあまりここのよろづあまりここのちあまりここのほあまりここのそあまりここの | |
ちよろづあまりとをよろづあまりふたほあまりみ | |
ちよろづあまりももよろづあまりとをよろづあまりよろづあまりちあまりももあまりとをあまりひと | |
ちよろづあまりももよろづあまりとをよろづあまりここのよろづあまりここのちあまりここのほあまりここのそあまりここの | |
].zip [ | |
1, | |
12, | |
123, | |
1234, | |
12345, | |
123456, | |
9_9999, | |
19_9999, | |
119_9999, | |
1010_0203, | |
1111_1111, | |
1119_9999, | |
] | |
).to_h | |
map.each do |s, n| | |
it "#{s} => #{n}" do | |
assert_equal n, from_yamato(s) | |
end | |
end | |
end | |
end | |
describe "prop" do | |
[ | |
1, | |
12, | |
123, | |
1234, | |
12345, | |
123456, | |
9_9999, | |
19_9999, | |
119_9999, | |
1010_0203, | |
1111_1111, | |
1119_9999, | |
].each do |n| | |
it "id of #{n}" do | |
assert_equal n, from_yamato(to_yamato(n)) | |
end | |
end | |
%w[ | |
ひと | |
とをあまりふた | |
ももあまりはたあまりみ | |
ちあまりふたほあまりみそあまりよ | |
よろづあまりふたちあまりみほあまりよそあまりい | |
とをよろづあまりふたよろづあまりみちあまりよほあまりいそあまりむ | |
ここのよろづあまりここのちあまりここのほあまりここのそあまりここの | |
とをよろづあまりここのよろづあまりここのちあまりここのほあまりここのそあまりここの | |
ももよろづあまりとをよろづあまりここのよろづあまりここのちあまりここのほあまりここのそあまりここの | |
ちよろづあまりとをよろづあまりふたほあまりみ | |
ちよろづあまりももよろづあまりとをよろづあまりよろづあまりちあまりももあまりとをあまりひと | |
ちよろづあまりももよろづあまりとをよろづあまりここのよろづあまりここのちあまりここのほあまりここのそあまりここの | |
].each do |n| | |
it "id of #{n}" do | |
assert_equal n, to_yamato(from_yamato(n)) | |
end | |
end | |
end | |
describe "quick check" do | |
10.times do | |
n = rand(1...1_0000_0000) | |
it "id of #{n}" do | |
assert_equal n, from_yamato(to_yamato(n)) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ref
TODO