Skip to content

Commit

Permalink
Implement builder
Browse files Browse the repository at this point in the history
  • Loading branch information
labocho committed Aug 24, 2012
1 parent f80e713 commit 7e9ed3d
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 3 deletions.
1 change: 1 addition & 0 deletions kansuji.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ Gem::Specification.new do |gem|
gem.version = Kansuji::VERSION
gem.add_development_dependency "rspec", "~> 2.11.0"
gem.add_development_dependency "guard-rspec"
gem.add_development_dependency "ruby-debug19"
end
7 changes: 7 additions & 0 deletions lib/kansuji.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
# Reference: http://ja.wikipedia.org/w/index.php?title=%E6%BC%A2%E6%95%B0%E5%AD%97&oldid=43573289
module Kansuji
class ParseError < StandardError; end
autoload :Builder, "kansuji/builder"
autoload :Parser, "kansuji/parser"

POWER_OF_MAN = %w( 𥝱 恒河沙 阿僧祇 那由他 不可思議 無量大数)

module_function
# Convert various type of Kansuji to Fixnum
# Kansuji.to_i("一二五〇〇〇〇〇〇") #=> 125000000
Expand All @@ -25,4 +28,8 @@ def to_i(kansuji)
def normalize(kansuji)
Parser.normalize(kansuji)
end

def to_kansuji(i, type = :traditional)
Builder.to_kansuji(i, type)
end
end
96 changes: 96 additions & 0 deletions lib/kansuji/builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# encoding: UTF-8
module Kansuji
module Builder
INTEGER_TO_POWER_OF_MAN =
Kansuji::POWER_OF_MAN.each_with_index.map{|char, i|
[char, 10000 ** (i + 1)]
}.reverse
INTEGER_TO_POWER_OF_TEN = [
["千", 1000],
["百", 100],
["十", 10]
]

module_function

def to_kansuji(i, type = :traditional)
case type
when :replace
replace_to_kansuji(i)
when :mixed
to_mixed_kansuji(i)
when :mixed_arabic
to_mixed_arabic_kansuji(i)
when :traditional
to_traditional_kansuji(i)
else
raise "Unknown Kansuji notation type #{type.inspect}, only supported (:replace|:mixed|:traditional)"
end
end

def replace_to_kansuji(i)
i.to_s.tr("0123456789", "〇一二三四五六七八九")
end

def to_mixed_kansuji(number)
return "〇" if number == 0
kansuji = ""
INTEGER_TO_POWER_OF_MAN.each do |char, i|
four = number / i
number = number % i
next if four == 0
kansuji << replace_to_kansuji(four).rjust(4, "〇") + char
end
kansuji << replace_to_kansuji(number).rjust(4, "〇") if number > 0 # 1-9999
kansuji.gsub(/^〇+/, "")
end

def to_mixed_arabic_kansuji(number)
return "0" if number == 0
kansuji = ""
INTEGER_TO_POWER_OF_MAN.each do |char, i|
four = number / i
number = number % i
next if four == 0
kansuji << four.to_s.rjust(4, "0") + char
end
kansuji << number.to_s.rjust(4, "0") if number > 0 # 1-9999
kansuji.gsub(/^0+/, "")
end

def to_traditional_kansuji(number)
return "〇" if number == 0
kansuji = ""
INTEGER_TO_POWER_OF_MAN.each do |char, i|
four = number / i
number = number % i
next if four == 0
kansuji << to_traditional_four_kansuji(four) + char
end
kansuji << to_traditional_four_kansuji(number) if number > 0 # 1-9999
kansuji
end

# to_traditional_four_kansuji(1234) #=> 千二百三十四
# to_traditional_four_kansuji(0) # => 〇
def to_traditional_four_kansuji(number)
return "〇" if number == 0
kansuji = ""
INTEGER_TO_POWER_OF_TEN.each do |char, i|
k = number / i
number = number % i

case k
when 0
# ignore
when 1
kansuji << char
else
kansuji << (replace_to_kansuji(k) + char)
end
end
kansuji << replace_to_kansuji(number) if number > 0
kansuji
end
end
end
3 changes: 1 addition & 2 deletions lib/kansuji/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ module Parser
"千" => 1000
}

POWER_OF_MAN = %w( 𥝱 恒河沙 阿僧祇 那由他 不可思議 無量大数)
POWER_OF_MAN_TO_INTEGER =
POWER_OF_MAN.each.with_index.inject({}){|table, (char, i)|
Kansuji::POWER_OF_MAN.each.with_index.inject({}){|table, (char, i)|
table[char] = 10000 ** (i + 1)
table
}
Expand Down
72 changes: 71 additions & 1 deletion spec/kansuji_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# encoding: UTF-8
require "spec_helper"
require "ruby-debug"

describe Kansuji do
describe ".to_i" do
Expand Down Expand Up @@ -50,7 +51,7 @@
should == 1_0000_2345
end
it "should parse ommission of some orders" do
@src = "一千二十万三百四"
@src = "千二十万三百四"
should == 1020_0304
end
it "shoud ignore leading spaces" do
Expand All @@ -68,6 +69,75 @@
end
end

describe ".to_arabic" do
it "should convert kansuji to arabic" do
pending
@src = "10000 USDは七万八千六百九十円五十八銭"
Kansuji.to_arabic(@src).should == "10000 USDは78690円58銭"
end
it "should convert kansuji to arabic" do
pending
@src = "10000 USDは七万八千六百九十円五十八銭"
Kansuji.to_arabic(@src, :mixed).should == "10000 USDは7万8690円58銭"
end
end

describe ".to_kansuji" do
context "type: replace" do
subject { Kansuji.to_kansuji(@src, :replace) }
it "should return '一〇二三四五六七八九' for 1023456789" do
@src = 10_2345_6789
should == "一〇二三四五六七八九"
end
end
context "type: mixed" do
subject { Kansuji.to_kansuji(@src, :mixed) }
it "should build '一〇億二三四五万六七八九'" do
@src = 10_2345_6789
should == "一〇億二三四五万六七八九"
end
it "should ommit four orders" do
@src = 1_0000_2345
should == "一億二三四五"
end
end
context "type: mixed_arabic" do
subject { Kansuji.to_kansuji(@src, :mixed_arabic) }
it "should build '10億2345万6789" do
@src = 10_2345_6789
should == "10億2345万6789"
end
it "should build max order" do
@src = 1_2345_6789_0123_4567_8901_2345_6789_0123_4567_8901_2345_6789_0123_4567_8901_2345_6789
should == "1無量大数2345不可思議6789那由他0123阿僧祇4567恒河沙8901極2345載6789正0123澗4567溝8901穣2345𥝱6789垓0123京4567兆8901億2345万6789"
end
end
context "type: traditional" do
subject { Kansuji.to_kansuji(@src, :traditional) }
it "should build '十億二千三百四十五万六千七百八十九'" do
@src = 10_2345_6789
should == '十億二千三百四十五万六千七百八十九'
end
it "should build 千, 百, 十 without 一" do
@src = 1111_1111
should == "千百十一万千百十一"
end
it "should build 一千" do
pending
@src = 1111_1111
should == "一千百十一万一千百十一"
end
it "should omit four orders" do
@src = 1_0000_2345
should == "一億二千三百四十五"
end
it "should omit some orders" do
@src = 1020_0304
should == "千二十万三百四"
end
end
end

describe ".normalize" do
it %{should return "二十一万" for "廿壱萬"} do
Kansuji.normalize("廿壱萬").should == "二十一万"
Expand Down

0 comments on commit 7e9ed3d

Please sign in to comment.