number_words_en 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +28 -0
- data/Rakefile +1 -0
- data/lib/number_words_en.rb +61 -0
- data/lib/number_words_en/version.rb +3 -0
- data/lib/words.yml +33 -0
- data/number_words_en.gemspec +28 -0
- data/spec/english_spec.rb +66 -0
- data/spec/spec_helper.rb +17 -0
- metadata +140 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 conanite
|
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/README.md
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# NumberWordsEn
|
2
|
+
|
3
|
+
Provides :en locale for number_words gem. See https://github.com/conanite/number_words
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
gem 'number_words_en'
|
8
|
+
bundle
|
9
|
+
|
10
|
+
## Usage
|
11
|
+
|
12
|
+
require 'number_words/core_ext'
|
13
|
+
|
14
|
+
I18n.locale = :en
|
15
|
+
123.to_words # => "one hundred and twenty three"
|
16
|
+
|
17
|
+
If you don't want to monkey-patch Integer, you can do this instead:
|
18
|
+
|
19
|
+
I18n.locale = :en
|
20
|
+
NumberWords.int_to_words 123 # => "one hundred and twenty three"
|
21
|
+
|
22
|
+
## Contributing
|
23
|
+
|
24
|
+
1. Fork it ( http://github.com/conanite/number_words_en/fork )
|
25
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
26
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
27
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
28
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require "number_words"
|
2
|
+
require "number_words_en/version"
|
3
|
+
require "yaml"
|
4
|
+
|
5
|
+
module NumberWordsEn
|
6
|
+
WORDS = YAML::load File.read File.expand_path("../words.yml", __FILE__)
|
7
|
+
|
8
|
+
class English < NumberWords::Base
|
9
|
+
THOUSANDS = ["", "thousand", "million", "billion"]
|
10
|
+
|
11
|
+
def int_to_words int, options={ }
|
12
|
+
case int
|
13
|
+
when 0; "zero"
|
14
|
+
else
|
15
|
+
join_thousands THOUSANDS, split_by_thousands(int)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def hundreds_to_words int
|
20
|
+
h = int / 100
|
21
|
+
t = int % 100
|
22
|
+
if t == 0
|
23
|
+
case h;
|
24
|
+
when 0; "zero";
|
25
|
+
when 1; "one hundred";
|
26
|
+
else "#{tens_to_words h} hundred"
|
27
|
+
end
|
28
|
+
else
|
29
|
+
h == 0 ? "#{tens_to_words t}" : "#{tens_to_words h} hundred and #{tens_to_words t}"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def tens_to_words i
|
34
|
+
if i < 20
|
35
|
+
WORDS["numbers"]["initial"][i]
|
36
|
+
else
|
37
|
+
tens = WORDS["numbers"]["tens"][i / 10]
|
38
|
+
"#{tens} #{tens_to_words(i % 10)}"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def join_thousands names, amounts
|
43
|
+
working = amounts.zip(names)
|
44
|
+
working.delete_if { |amount, name| amount == 0 }
|
45
|
+
|
46
|
+
working = working.map { |amount, name|
|
47
|
+
[hundreds_to_words(amount), name]
|
48
|
+
}
|
49
|
+
|
50
|
+
if amounts[0] > 0 && amounts[0] < 100 && amounts.size > 1
|
51
|
+
huns = working.shift
|
52
|
+
working.unshift "and"
|
53
|
+
working.unshift huns
|
54
|
+
end
|
55
|
+
|
56
|
+
working.reverse.flatten.join(' ')
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
NumberWords.add_handler :en, English.new
|
61
|
+
end
|
data/lib/words.yml
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
numbers:
|
2
|
+
tens:
|
3
|
+
- ""
|
4
|
+
- ""
|
5
|
+
- twenty
|
6
|
+
- thirty
|
7
|
+
- forty
|
8
|
+
- fifty
|
9
|
+
- sixty
|
10
|
+
- seventy
|
11
|
+
- eighty
|
12
|
+
- ninety
|
13
|
+
initial:
|
14
|
+
- ""
|
15
|
+
- one
|
16
|
+
- two
|
17
|
+
- three
|
18
|
+
- four
|
19
|
+
- five
|
20
|
+
- six
|
21
|
+
- seven
|
22
|
+
- eight
|
23
|
+
- nine
|
24
|
+
- ten
|
25
|
+
- eleven
|
26
|
+
- twelve
|
27
|
+
- thirteen
|
28
|
+
- fourteen
|
29
|
+
- fifteen
|
30
|
+
- sixteen
|
31
|
+
- seventeen
|
32
|
+
- eighteen
|
33
|
+
- nineteen
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- coding: utf-8; mode: ruby -*-
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
|
6
|
+
require 'number_words_en/version'
|
7
|
+
|
8
|
+
Gem::Specification.new do |gem|
|
9
|
+
gem.name = "number_words_en"
|
10
|
+
gem.version = NumberWordsEn::VERSION
|
11
|
+
gem.authors = ["Conan Dalton"]
|
12
|
+
gem.license = 'MIT'
|
13
|
+
gem.email = ["conan@conandalton.net"]
|
14
|
+
gem.description = %q{number_words handler for english}
|
15
|
+
gem.summary = %q{number_words handler for english}
|
16
|
+
gem.homepage = "https://github.com/conanite/number_words_en"
|
17
|
+
|
18
|
+
gem.add_dependency 'number_words'
|
19
|
+
gem.add_development_dependency 'rspec', '~> 2.9'
|
20
|
+
gem.add_development_dependency 'rspec_numbering_formatter'
|
21
|
+
gem.add_development_dependency "bundler", "~> 1.5"
|
22
|
+
gem.add_development_dependency "rake"
|
23
|
+
|
24
|
+
gem.files = `git ls-files`.split($/)
|
25
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
26
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
27
|
+
gem.require_paths = ["lib"]
|
28
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
require 'i18n'
|
5
|
+
require 'number_words_en'
|
6
|
+
require 'number_words/core_ext'
|
7
|
+
|
8
|
+
|
9
|
+
describe NumberWordsEn do
|
10
|
+
def expect_words n, txt
|
11
|
+
expect(n.to_words).to eq txt
|
12
|
+
end
|
13
|
+
|
14
|
+
before { I18n.locale = :en }
|
15
|
+
|
16
|
+
it "should translate numbers into english" do
|
17
|
+
expect_words 0, "zero"
|
18
|
+
expect_words 1, "one"
|
19
|
+
expect_words 2, "two"
|
20
|
+
expect_words 10, "ten"
|
21
|
+
expect_words 12, "twelve"
|
22
|
+
expect_words 18, "eighteen"
|
23
|
+
expect_words 20, "twenty"
|
24
|
+
expect_words 21, "twenty one"
|
25
|
+
expect_words 22, "twenty two"
|
26
|
+
expect_words 31, "thirty one"
|
27
|
+
expect_words 33, "thirty three"
|
28
|
+
expect_words 44, "forty four"
|
29
|
+
expect_words 55, "fifty five"
|
30
|
+
expect_words 66, "sixty six"
|
31
|
+
expect_words 70, "seventy"
|
32
|
+
expect_words 71, "seventy one"
|
33
|
+
expect_words 72, "seventy two"
|
34
|
+
expect_words 80, "eighty"
|
35
|
+
expect_words 81, "eighty one"
|
36
|
+
expect_words 90, "ninety"
|
37
|
+
expect_words 99, "ninety nine"
|
38
|
+
expect_words 100, "one hundred"
|
39
|
+
expect_words 101, "one hundred and one"
|
40
|
+
expect_words 110, "one hundred and ten"
|
41
|
+
expect_words 111, "one hundred and eleven"
|
42
|
+
expect_words 119, "one hundred and nineteen"
|
43
|
+
expect_words 120, "one hundred and twenty"
|
44
|
+
expect_words 185, "one hundred and eighty five"
|
45
|
+
expect_words 300, "three hundred"
|
46
|
+
expect_words 301, "three hundred and one"
|
47
|
+
expect_words 999, "nine hundred and ninety nine"
|
48
|
+
expect_words 1000, "one thousand"
|
49
|
+
expect_words 1001, "one thousand and one"
|
50
|
+
expect_words 1002, "one thousand and two"
|
51
|
+
expect_words 1095, "one thousand and ninety five"
|
52
|
+
expect_words 1102, "one thousand one hundred and two"
|
53
|
+
expect_words 1199, "one thousand one hundred and ninety nine"
|
54
|
+
expect_words 1234, "one thousand two hundred and thirty four"
|
55
|
+
expect_words 2001, "two thousand and one"
|
56
|
+
expect_words 12001, "twelve thousand and one"
|
57
|
+
expect_words 12345, "twelve thousand three hundred and forty five"
|
58
|
+
expect_words 12500, "twelve thousand five hundred"
|
59
|
+
expect_words 123456, "one hundred and twenty three thousand four hundred and fifty six"
|
60
|
+
expect_words 1234567, "one million two hundred and thirty four thousand five hundred and sixty seven"
|
61
|
+
expect_words 9999999, "nine million nine hundred and ninety nine thousand nine hundred and ninety nine"
|
62
|
+
expect_words 12345678, "twelve million three hundred and forty five thousand six hundred and seventy eight"
|
63
|
+
expect_words 999999999, "nine hundred and ninety nine million nine hundred and ninety nine thousand nine hundred and ninety nine"
|
64
|
+
expect_words 999999999999, "nine hundred and ninety nine billion nine hundred and ninety nine million nine hundred and ninety nine thousand nine hundred and ninety nine"
|
65
|
+
end
|
66
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
|
+
config.run_all_when_everything_filtered = true
|
10
|
+
config.filter_run :focus
|
11
|
+
|
12
|
+
# Run specs in random order to surface order dependencies. If you find an
|
13
|
+
# order dependency and want to debug it, you can fix the order by providing
|
14
|
+
# the seed, which is printed after each run.
|
15
|
+
# --seed 1234
|
16
|
+
config.order = 'random'
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: number_words_en
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Conan Dalton
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-03-07 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: number_words
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
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
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '2.9'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '2.9'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec_numbering_formatter
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: bundler
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '1.5'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '1.5'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rake
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
description: number_words handler for english
|
95
|
+
email:
|
96
|
+
- conan@conandalton.net
|
97
|
+
executables: []
|
98
|
+
extensions: []
|
99
|
+
extra_rdoc_files: []
|
100
|
+
files:
|
101
|
+
- .gitignore
|
102
|
+
- .rspec
|
103
|
+
- Gemfile
|
104
|
+
- LICENSE.txt
|
105
|
+
- README.md
|
106
|
+
- Rakefile
|
107
|
+
- lib/number_words_en.rb
|
108
|
+
- lib/number_words_en/version.rb
|
109
|
+
- lib/words.yml
|
110
|
+
- number_words_en.gemspec
|
111
|
+
- spec/english_spec.rb
|
112
|
+
- spec/spec_helper.rb
|
113
|
+
homepage: https://github.com/conanite/number_words_en
|
114
|
+
licenses:
|
115
|
+
- MIT
|
116
|
+
post_install_message:
|
117
|
+
rdoc_options: []
|
118
|
+
require_paths:
|
119
|
+
- lib
|
120
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ! '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
|
+
none: false
|
128
|
+
requirements:
|
129
|
+
- - ! '>='
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
requirements: []
|
133
|
+
rubyforge_project:
|
134
|
+
rubygems_version: 1.8.24
|
135
|
+
signing_key:
|
136
|
+
specification_version: 3
|
137
|
+
summary: number_words handler for english
|
138
|
+
test_files:
|
139
|
+
- spec/english_spec.rb
|
140
|
+
- spec/spec_helper.rb
|