bangou 1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile +5 -0
- data/LICENSE +22 -0
- data/Rakefile +6 -0
- data/lib/bangou.rb +93 -0
- data/readme.md +31 -0
- data/spec/bangou_spec.rb +108 -0
- metadata +70 -0
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Delisa Mason
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
data/lib/bangou.rb
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
class Bangou
|
4
|
+
class OutOfRangeException < Exception; end
|
5
|
+
|
6
|
+
def self.integer_to_japanese_numerals int
|
7
|
+
convert_number(int, :numeral)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.integer_to_japanese_text int
|
11
|
+
convert_number(int, :text)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.in_range? int
|
15
|
+
int >= 0 and int < 100000000
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
NUMERALS = {
|
21
|
+
0 => "", 1 => "一", 2 => "二",
|
22
|
+
3 => "三", 4 => "四", 5 => "五",
|
23
|
+
6 => "六", 7 => "七", 8 => "八",
|
24
|
+
9 => "九"
|
25
|
+
}
|
26
|
+
|
27
|
+
NUMERALS_TEXT = {
|
28
|
+
0 => "", 1 => "いち", 2 => "に",
|
29
|
+
3 => "さん", 4 => "よん", 5 => "ご",
|
30
|
+
6 => "ろく", 7 => "なな", 8 => "はち",
|
31
|
+
9 => "きゅう"
|
32
|
+
}
|
33
|
+
|
34
|
+
BASES = {
|
35
|
+
1 => "", 10 => "十", 100 => "百",
|
36
|
+
1000 => "千", 10000 => "万"
|
37
|
+
}
|
38
|
+
|
39
|
+
BASES_TEXT = {
|
40
|
+
1 => "", 10 => "じゅう", 100 => "ひゃく",
|
41
|
+
1000 => "せん", 10000 => "まん"
|
42
|
+
}
|
43
|
+
|
44
|
+
EXCEPTIONS_TEXT = {
|
45
|
+
300 => "さんびゃく", 600 => "ろっぴゃく",
|
46
|
+
800 => "はっぴゃく",
|
47
|
+
3000 => "さんぜん", 8000 => "はっせん"
|
48
|
+
}
|
49
|
+
|
50
|
+
EXCEPTIONS = {}
|
51
|
+
|
52
|
+
ZEROES = {
|
53
|
+
:text => "ぜろ",
|
54
|
+
:numeral => "0"
|
55
|
+
}
|
56
|
+
|
57
|
+
def self.exceptions num, format
|
58
|
+
format == :text ? EXCEPTIONS_TEXT[num] : EXCEPTIONS[num]
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.numerals num, format
|
62
|
+
format == :text ? NUMERALS_TEXT[num] : NUMERALS[num]
|
63
|
+
end
|
64
|
+
|
65
|
+
def self.bases num, format
|
66
|
+
format == :text ? BASES_TEXT[num] : BASES[num]
|
67
|
+
end
|
68
|
+
|
69
|
+
def self.convert_number int, format
|
70
|
+
raise OutOfRangeException.new("Bangou can only process numbers between 0 and 99,999,999") unless in_range?(int)
|
71
|
+
return ZEROES[format] if int == 0
|
72
|
+
digits = int.to_s.split(//).reverse
|
73
|
+
digits.each_with_index.map do |digit, power|
|
74
|
+
num = digit.to_i
|
75
|
+
base = 10 ** power
|
76
|
+
if value = exceptions(num * base, format)
|
77
|
+
value
|
78
|
+
else
|
79
|
+
if num > 0
|
80
|
+
if power > 4
|
81
|
+
text = exceptions(num * base/10000, format) || numerals(num, format) + bases(base/10000, format)
|
82
|
+
text += bases(10000, format) unless digits.size > 1 and (digits[0..power - 1] & ("1".."9").to_a).size > 0
|
83
|
+
else
|
84
|
+
text = numerals(num, format) + bases(base, format)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
text = text[1..-1] if text =~ /^(一).+/
|
88
|
+
text = text[2..-1] if text =~ /^(いち).+/
|
89
|
+
text
|
90
|
+
end
|
91
|
+
end.reverse.join
|
92
|
+
end
|
93
|
+
end
|
data/readme.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# 番号
|
2
|
+
|
3
|
+
A Ruby library for converting between positive integers and Sino-Japanese numbers or text.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
```
|
8
|
+
require 'bangou'
|
9
|
+
|
10
|
+
Bangou.integer_to_japanese_numerals(2305)
|
11
|
+
# => "二千三百五"
|
12
|
+
|
13
|
+
Bangou.integer_to_japanese_text(2305)
|
14
|
+
# => "にせんさんびゃくご"
|
15
|
+
```
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
`gem install bangou`
|
20
|
+
|
21
|
+
## Running Tests
|
22
|
+
|
23
|
+
番号 uses bacon for testing, runnable using `rake spec`.
|
24
|
+
|
25
|
+
## Contributing
|
26
|
+
|
27
|
+
Pull requests with tests accepted!
|
28
|
+
|
29
|
+
## Current Limitations
|
30
|
+
|
31
|
+
Only works on numbers between 0 and 99,999,999 (otherwise throwing an OutOfRangeException).
|
data/spec/bangou_spec.rb
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
$:.unshift(File.expand_path('../../lib',__FILE__))
|
3
|
+
|
4
|
+
require 'bangou.rb'
|
5
|
+
|
6
|
+
describe "Bangou" do
|
7
|
+
describe "changes integers into Japanese numerals" do
|
8
|
+
|
9
|
+
def compare integer, numeral
|
10
|
+
Bangou.integer_to_japanese_numerals(integer).should.equal(numeral)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "uses the correct bases for numbers" do
|
14
|
+
compare(1, "一")
|
15
|
+
compare(10, "十")
|
16
|
+
compare(100, "百")
|
17
|
+
compare(1000, "千")
|
18
|
+
compare(10000, "万")
|
19
|
+
end
|
20
|
+
|
21
|
+
it "can parse digits in different places in a number, ignoring zeroes" do
|
22
|
+
compare(7509, "七千五百九")
|
23
|
+
compare(80000, "八万")
|
24
|
+
compare(80001, "八万一")
|
25
|
+
compare(80020, "八万二十")
|
26
|
+
end
|
27
|
+
|
28
|
+
it "removes unneeded ones" do
|
29
|
+
compare(81000, "八万千")
|
30
|
+
compare(10100, "万百")
|
31
|
+
end
|
32
|
+
|
33
|
+
it "can handle bases above 10^4" do
|
34
|
+
compare(100000, "十万")
|
35
|
+
compare(1000000, "百万")
|
36
|
+
compare(50000000, "五千万")
|
37
|
+
compare(22000000, "二千二百万")
|
38
|
+
end
|
39
|
+
|
40
|
+
it "raises OutOfRangeException if given a number out of usable range" do
|
41
|
+
should.raise(Bangou::OutOfRangeException) {
|
42
|
+
Bangou.integer_to_japanese_numerals(100000000)
|
43
|
+
}
|
44
|
+
should.raise(Bangou::OutOfRangeException) {
|
45
|
+
Bangou.integer_to_japanese_numerals(-1)
|
46
|
+
}
|
47
|
+
should.not.raise(Bangou::OutOfRangeException) {
|
48
|
+
Bangou.integer_to_japanese_numerals(99999999)
|
49
|
+
}
|
50
|
+
should.not.raise(Bangou::OutOfRangeException) {
|
51
|
+
Bangou.integer_to_japanese_numerals(0)
|
52
|
+
}
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "changes integers into Japanese text" do
|
57
|
+
def compare integer, text
|
58
|
+
Bangou.integer_to_japanese_text(integer).should.equal(text)
|
59
|
+
end
|
60
|
+
|
61
|
+
it "ignores zeroes, unless the number is just zero" do
|
62
|
+
compare(7509, "ななせんごひゃくきゅう")
|
63
|
+
compare(0, "ぜろ")
|
64
|
+
compare(80000, "はちまん")
|
65
|
+
compare(80001, "はちまんいち")
|
66
|
+
compare(80020, "はちまんにじゅう")
|
67
|
+
end
|
68
|
+
|
69
|
+
it "raises OutOfRangeException if given a number out of usable range" do
|
70
|
+
should.raise(Bangou::OutOfRangeException) {
|
71
|
+
Bangou.integer_to_japanese_text(100000000)
|
72
|
+
}
|
73
|
+
should.not.raise(Bangou::OutOfRangeException) {
|
74
|
+
Bangou.integer_to_japanese_text(99999999)
|
75
|
+
}
|
76
|
+
end
|
77
|
+
|
78
|
+
it "uses the correct transformation for 3, 6, and 8" do
|
79
|
+
compare(3, "さん")
|
80
|
+
compare(6, "ろく")
|
81
|
+
compare(8, "はち")
|
82
|
+
|
83
|
+
compare(30, "さんじゅう")
|
84
|
+
compare(60, "ろくじゅう")
|
85
|
+
compare(80, "はちじゅう")
|
86
|
+
|
87
|
+
compare(300, "さんびゃく")
|
88
|
+
compare(600, "ろっぴゃく")
|
89
|
+
compare(800, "はっぴゃく")
|
90
|
+
|
91
|
+
compare(3000, "さんぜん")
|
92
|
+
compare(6000, "ろくせん")
|
93
|
+
compare(8000, "はっせん")
|
94
|
+
|
95
|
+
compare(30000, "さんまん")
|
96
|
+
compare(60000, "ろくまん")
|
97
|
+
compare(80000, "はちまん")
|
98
|
+
|
99
|
+
compare(300000, "さんじゅうまん")
|
100
|
+
compare(600000, "ろくじゅうまん")
|
101
|
+
compare(800000, "はちじゅうまん")
|
102
|
+
|
103
|
+
compare(3000000, "さんびゃくまん")
|
104
|
+
compare(6000000, "ろっぴゃくまん")
|
105
|
+
compare(8000000, "はっぴゃくまん")
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bangou
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '1.0'
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Delisa Mason
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-21 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bacon
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: Integer <=> Japanese number converter
|
31
|
+
email:
|
32
|
+
- iskanamagus@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files:
|
36
|
+
- readme.md
|
37
|
+
files:
|
38
|
+
- Gemfile
|
39
|
+
- Rakefile
|
40
|
+
- LICENSE
|
41
|
+
- readme.md
|
42
|
+
- lib/bangou.rb
|
43
|
+
- spec/bangou_spec.rb
|
44
|
+
homepage: http://kattrali.github.com/bangou
|
45
|
+
licenses: []
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
requirements: []
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 1.8.24
|
65
|
+
signing_key:
|
66
|
+
specification_version: 3
|
67
|
+
summary: A library for converting between positive integers and Sino-Japanese numbers
|
68
|
+
or text.
|
69
|
+
test_files:
|
70
|
+
- spec/bangou_spec.rb
|