polish-number 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +3 -0
- data/lib/polish_number.rb +54 -144
- metadata +9 -4
data/README.rdoc
CHANGED
data/lib/polish_number.rb
CHANGED
@@ -1,170 +1,80 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
|
3
3
|
module PolishNumber
|
4
|
-
|
5
|
-
|
6
|
-
1 => 'jeden',
|
7
|
-
2 => 'dwa',
|
8
|
-
3 => 'trzy',
|
9
|
-
4 => 'cztery',
|
10
|
-
5 => 'pięć',
|
11
|
-
7 => 'siedem',
|
12
|
-
6 => 'sześć',
|
13
|
-
8 => 'osiem',
|
14
|
-
9 => 'dziewięć',
|
15
|
-
10 => 'dziesięć',
|
16
|
-
11 => 'jedenaście',
|
17
|
-
12 => 'dwanaście',
|
18
|
-
13 => 'trzynaście',
|
19
|
-
14 => 'czternaście',
|
20
|
-
15 => 'piętnaście',
|
21
|
-
16 => 'szesnaście',
|
22
|
-
17 => 'siedemnaście',
|
23
|
-
18 => 'osiemnaście',
|
24
|
-
19 => 'dziewiętnaście',
|
25
|
-
20 => 'dwadzieścia',
|
26
|
-
30 => 'trzydzieści',
|
27
|
-
40 => 'czterdzieści',
|
28
|
-
50 => 'pięćdziesiąt',
|
29
|
-
60 => 'sześćdziesiąt',
|
30
|
-
70 => 'siedemdziesiąt',
|
31
|
-
80 => 'osiemdziesiąt',
|
32
|
-
90 => 'dziewięćdziesiąt',
|
33
|
-
100 => 'sto',
|
34
|
-
200 => 'dwieście',
|
35
|
-
300 => 'trzysta',
|
36
|
-
400 => 'czterysta',
|
37
|
-
500 => 'pięćset',
|
38
|
-
600 => 'sześćset',
|
39
|
-
700 => 'siedemset',
|
40
|
-
800 => 'osiemset',
|
41
|
-
900 => 'dziewięćset',
|
42
|
-
1000 => 'tysiąc'
|
43
|
-
}.freeze
|
44
|
-
|
45
|
-
def self.rehash(hash)
|
46
|
-
result = Hash.new(hash['5'])
|
47
|
-
result[0] = hash['5']
|
48
|
-
result[1] = hash['1']
|
49
|
-
result[2] = result[3] = result[4] = hash['2_3_4']
|
50
|
-
end
|
51
|
-
|
52
|
-
THOUSANDS = rehash({
|
53
|
-
'1' => 'tysiąc',
|
54
|
-
'2_3_4' => 'tysiące', # 22..24, 32..34
|
55
|
-
'5' => 'tysięcy', # 0, 5..21, 25..31
|
56
|
-
})
|
57
|
-
|
58
|
-
def self.translate_0_19(number)
|
59
|
-
TRANSLATIONS[number]
|
60
|
-
end
|
61
|
-
|
62
|
-
def self.translate_20_99(number)
|
63
|
-
return TRANSLATIONS[number] if TRANSLATIONS.has_key? number
|
64
|
-
|
65
|
-
first, second = number.to_s.split(//).map { |c| c.to_i }
|
66
|
-
"#{TRANSLATIONS[first*10]} #{TRANSLATIONS[second]}"
|
67
|
-
end
|
4
|
+
HUNDREDS = ['', 'sto ', 'dwieście ', 'trzysta ', 'czterysta ', 'pięćset ', 'sześćset ',
|
5
|
+
'siedemset ', 'osiemset ', 'dziewięćset ']
|
68
6
|
|
69
|
-
|
70
|
-
|
71
|
-
end
|
7
|
+
TENS = ['', 'dziesięć ', 'dwadzieścia ', 'trzydzieści ', 'czterdzieści ', 'pięćdziesiąt ',
|
8
|
+
'sześćdziesiąt ', 'siedemdziesiąt ', 'osiemdziesiąt ', 'dziewięćdziesiąt ']
|
72
9
|
|
73
|
-
|
74
|
-
|
10
|
+
TEENS = ['', 'jedenaście ', 'dwanaście ', 'trzynaście ', 'czternaście ', 'piętnaście ',
|
11
|
+
'szesnaście ', 'siedemnaście ', 'osiemnaście ', 'dziewiętnaście ']
|
75
12
|
|
76
|
-
|
77
|
-
|
78
|
-
translations << TRANSLATIONS[hundreds*100]
|
13
|
+
UNITIES = ['', 'jeden ', 'dwa ', 'trzy ', 'cztery ', 'pięć ', 'sześć ', 'siedem ', 'osiem ',
|
14
|
+
'dziewięć ']
|
79
15
|
|
80
|
-
|
81
|
-
if tens_units > 0
|
82
|
-
translations << translate_0_99(tens_units)
|
83
|
-
end
|
16
|
+
ZERO = 'zero'
|
84
17
|
|
85
|
-
|
86
|
-
end
|
18
|
+
THOUSANDS = ['', 'tysiąc ', 'tysiące ', 'tysięcy ']
|
87
19
|
|
88
|
-
|
89
|
-
|
90
|
-
end
|
20
|
+
AND = 'i '
|
21
|
+
DECIMALS = ['', 'dziesiąta ', 'dziesiąte ', 'dziesiątych ']
|
91
22
|
|
92
|
-
def self.
|
93
|
-
|
94
|
-
|
95
|
-
translations = []
|
23
|
+
def self.translate(number)
|
24
|
+
number = number.to_f
|
96
25
|
|
97
|
-
|
98
|
-
|
99
|
-
translations << TRANSLATIONS[1000]
|
100
|
-
else
|
101
|
-
translations << "#{translate_100_999(number)} #{pluralize(thousands, THOUSANDS)}"
|
26
|
+
unless (0..999999.99).include? number
|
27
|
+
raise ArgumentError, 'number should be in 0..999999.99 range'
|
102
28
|
end
|
103
29
|
|
104
|
-
number
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
return translate_100_999(number)
|
120
|
-
when 1000..9999
|
121
|
-
return translate_1000_9999(number)
|
30
|
+
return ZERO.dup if number == 0.0
|
31
|
+
|
32
|
+
formatted_number = sprintf("%09.2f", number)
|
33
|
+
digits = formatted_number.chars.map { |char| char.to_i }
|
34
|
+
|
35
|
+
result = ''
|
36
|
+
result << process_0_999(digits[0..2])
|
37
|
+
result << pluralize(digits[1..2], THOUSANDS)
|
38
|
+
result << process_0_999(digits[3..5])
|
39
|
+
# skip the 6th digit - it was a decimal dot
|
40
|
+
fraction = process_0_99(digits[7..8])
|
41
|
+
unless fraction == ''
|
42
|
+
result << AND
|
43
|
+
result << fraction
|
44
|
+
result << pluralize(digits[7..8], DECIMALS)
|
122
45
|
end
|
46
|
+
result.strip!
|
47
|
+
result
|
123
48
|
end
|
124
49
|
|
125
|
-
|
126
|
-
unless (0..999) === number
|
127
|
-
raise "#{number} not supported"
|
128
|
-
end
|
129
|
-
|
130
|
-
# handle simple numbers
|
131
|
-
if TRANSLATIONS.has_key? number
|
132
|
-
return TRANSLATIONS[number]
|
133
|
-
end
|
50
|
+
private
|
134
51
|
|
135
|
-
|
52
|
+
def self.process_0_999(digits)
|
53
|
+
result = ''
|
54
|
+
result << HUNDREDS[digits[0]]
|
55
|
+
result << process_0_99(digits[1..2])
|
56
|
+
result
|
57
|
+
end
|
136
58
|
|
137
|
-
|
138
|
-
if
|
139
|
-
|
59
|
+
def self.process_0_99(digits)
|
60
|
+
if digits[0] == 1 && digits[1] != 0
|
61
|
+
TEENS[digits[1]]
|
140
62
|
else
|
141
|
-
|
63
|
+
TENS[digits[0]] + UNITIES[digits[1]]
|
142
64
|
end
|
65
|
+
end
|
143
66
|
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
number /= 100
|
152
|
-
position = 2
|
67
|
+
def self.pluralize(digits, words)
|
68
|
+
if digits[0] == 0 && digits[1] == 0
|
69
|
+
thousand_id = 0
|
70
|
+
elsif digits[1] == 0 && digits[2] == 1
|
71
|
+
thousand_id = 1
|
72
|
+
elsif digits[1] != 1 && (2..4).include?(digits[2])
|
73
|
+
thousand_id = 2
|
153
74
|
else
|
154
|
-
|
75
|
+
thousand_id = 3
|
155
76
|
end
|
156
|
-
|
157
|
-
while number != 0
|
158
|
-
digit = number%10
|
159
|
-
if digit > 0
|
160
|
-
power = 10**position
|
161
|
-
translation_digits.unshift digit*power
|
162
|
-
end
|
163
|
-
number /= 10
|
164
|
-
position += 1
|
165
|
-
end
|
166
|
-
|
167
|
-
translation_digits
|
77
|
+
words[thousand_id]
|
168
78
|
end
|
169
79
|
end
|
170
80
|
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: polish-number
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
4
5
|
prerelease: false
|
5
6
|
segments:
|
6
7
|
- 0
|
7
8
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Wojciech Piekutowski
|
@@ -14,7 +15,7 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date: 2010-
|
18
|
+
date: 2010-06-10 00:00:00 +02:00
|
18
19
|
default_executable:
|
19
20
|
dependencies: []
|
20
21
|
|
@@ -47,23 +48,27 @@ rdoc_options:
|
|
47
48
|
require_paths:
|
48
49
|
- lib
|
49
50
|
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
50
52
|
requirements:
|
51
53
|
- - ">="
|
52
54
|
- !ruby/object:Gem::Version
|
55
|
+
hash: 3
|
53
56
|
segments:
|
54
57
|
- 0
|
55
58
|
version: "0"
|
56
59
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
57
61
|
requirements:
|
58
62
|
- - ">="
|
59
63
|
- !ruby/object:Gem::Version
|
64
|
+
hash: 3
|
60
65
|
segments:
|
61
66
|
- 0
|
62
67
|
version: "0"
|
63
68
|
requirements: []
|
64
69
|
|
65
70
|
rubyforge_project: polish-number
|
66
|
-
rubygems_version: 1.3.
|
71
|
+
rubygems_version: 1.3.7
|
67
72
|
signing_key:
|
68
73
|
specification_version: 3
|
69
74
|
summary: Translates numbers to Polish words
|