numbers_translator 0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6c579fa7cf3e04e2113acc7e47a25be16ab302012531a4ef8e683475fda2b371
4
+ data.tar.gz: df42abda0275a65602658b17ec33eec401c88b3193ba2a8dec7cbb827ba8ee0d
5
+ SHA512:
6
+ metadata.gz: 1a367b79eb52156d32d03dd6abf63878dfd9b9893bf584b96c73a70be777d703d06663859b1f08651921e468be1580964480adbde6ab0df8a0e10267a6b80e44
7
+ data.tar.gz: 19b4d507f54b7ce6f4063b8a44d421b31e0a183c1501decb44940d4ff4874e02ab8f88897dcb3c9baa97499a33bd3a1df3ab1e051d56ee0ca04e319c1ff8f28f
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ Gemfile.lock
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ .idea
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.0
7
+ before_install: gem install bundler -v 1.16.6
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at russellx@yandex.ru. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in numbers_translator.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Ruslan Karelin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # NumbersTranslator
2
+
3
+ Translating numbers into English
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'numbers_translator'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install numbers_translator
20
+
21
+ ## Usage
22
+
23
+ NumbersTranslator::Translator::make 1560
24
+
25
+ => "one thousand five hundred and sixty"
26
+
27
+ OR
28
+
29
+ NumbersTranslator::Translator::make 1560, 'usa'
30
+
31
+ => "one thousand five hundred sixty"
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "numbers_translator"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,28 @@
1
+ require "numbers_translator/version"
2
+ require "numbers_translator/validator"
3
+ require "numbers_translator/prepare_data"
4
+ require "numbers_translator/data_storage"
5
+ require "numbers_translator/engine"
6
+
7
+ module NumbersTranslator
8
+ class Translator
9
+ def self.make(number, lang = 'uk')
10
+
11
+ unless Validator.validate number
12
+ raise ArgumentError, 'Invalid characters are present or a number greater than 16 characters!'
13
+ end
14
+
15
+ engine = Engine.new(
16
+ PrepareData.set(number),
17
+ DataStorage.get_storage,
18
+ lang
19
+ )
20
+ engine.run
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def initialize
27
+ end
28
+ end
@@ -0,0 +1,81 @@
1
+ class NumbersTranslator::DataStorage
2
+ protected
3
+
4
+ @@storage = {
5
+ comparison: {
6
+ '0' => '',
7
+ '1' => {
8
+ '1' => 'one',
9
+ '2' => 'ten',
10
+ '3' => 'one hundred'
11
+ },
12
+ '2' => {
13
+ '1' => 'two',
14
+ '2' => 'twenty',
15
+ '3' => 'two hundred'
16
+ },
17
+ '3' => {
18
+ '1' => 'three',
19
+ '2' => 'thirty',
20
+ '3' => 'three hundred'
21
+ },
22
+ '4' => {
23
+ '1' => 'four',
24
+ '2' => 'forty',
25
+ '3' => 'four hundred'
26
+ },
27
+ '5' => {
28
+ '1' => 'five',
29
+ '2' => 'fifty',
30
+ '3' => 'five hundred'
31
+ },
32
+ '6' => {
33
+ '1' => 'six',
34
+ '2' => 'sixty',
35
+ '3' => 'six hundred'
36
+ },
37
+ '7' => {
38
+ '1' => 'seven',
39
+ '2' => 'seventy',
40
+ '3' => 'seven hundred'
41
+ },
42
+ '8' => {
43
+ '1' => 'eight',
44
+ '2' => 'eighty',
45
+ '3' => 'eight hundred'
46
+ },
47
+ '9' => {
48
+ '1' => 'nine',
49
+ '2' => 'ninety',
50
+ '3' => 'nine hundred'
51
+ },
52
+ '10' => 'ten',
53
+ '11' => 'eleven',
54
+ '12' => 'twelve',
55
+ '13' => 'thirteen',
56
+ '14' => 'fourteen',
57
+ '15' => 'fifteen',
58
+ '16' => 'sixteen',
59
+ '17' => 'seventeen',
60
+ '18' => 'eighteen',
61
+ '19' => 'nineteen',
62
+ },
63
+ breakdown: {
64
+ '6' => 'quadrillion',
65
+ '5' => 'trillion',
66
+ '4' => 'billion',
67
+ '3' => 'million',
68
+ '2' => 'thousand',
69
+ '1' => ''
70
+ }
71
+ }
72
+
73
+ def self.get_storage
74
+ @@storage
75
+ end
76
+
77
+ private
78
+
79
+ def initialize
80
+ end
81
+ end
@@ -0,0 +1,211 @@
1
+ class NumbersTranslator::Engine
2
+
3
+ FIRST_POSITION = 1
4
+
5
+ SECOND_POSITION = 2
6
+
7
+ FIRST_NUMBER = 0
8
+
9
+ SECOND_NUMBER = 1
10
+
11
+ THIRD_NUMBER = 2
12
+
13
+ def initialize(data, data_storage, lang)
14
+ @data = data.get_data
15
+ @data_storage = data_storage
16
+ @separator = (lang == 'uk') ? ' and ' : ' '
17
+ @lang = lang
18
+ @result_string = ''
19
+ end
20
+
21
+ def run
22
+
23
+ @data[:numeric_groups].each do |key|
24
+ data = ''
25
+
26
+ if key.length > 1
27
+
28
+ data = for_fist_element(key, data) if key.length == 3
29
+
30
+ if key[THIRD_NUMBER]
31
+ if_count_numbers_three(key, data)
32
+ else
33
+ if_count_numbers_not_three(key, data)
34
+ end
35
+ else
36
+ for_fist_element(key, data);
37
+ end
38
+
39
+ if key.split('').map {|e| e.to_i}.sum != 0
40
+ @result_string += ' ' + @data_storage[:breakdown][@data[:count_numeric_groups].to_s]
41
+ end
42
+
43
+ @data[:count_numeric_groups] -= 1
44
+
45
+ end
46
+ postprocessing
47
+ end
48
+
49
+ protected
50
+
51
+ def get_from_storage_and_concat(params)
52
+
53
+ if params[:for_search_in_the_storage].is_a? Array
54
+
55
+ data = @data_storage[:comparison][
56
+ params[:key][params[:for_search_in_the_storage][FIRST_NUMBER]] +
57
+ params[:key][params[:for_search_in_the_storage][SECOND_NUMBER]]
58
+ ]
59
+
60
+ params[:data] = data
61
+ concatenation_to_string(params)
62
+ else
63
+ data = @data_storage[:comparison][params[:key][params[:key_in_storage]]]
64
+
65
+ params[:data] = data
66
+ data = concatenation_to_string(params)
67
+ end
68
+
69
+ data
70
+ end
71
+
72
+ def concatenation_to_string(params)
73
+ str = ''
74
+
75
+ if params[:data].is_a? Hash
76
+
77
+ if params[:position]
78
+ str = (params[:position] == FIRST_POSITION) ?
79
+ "-#{params[:data][params[:position].to_s]}" :
80
+ "#{@separator}#{params[:data][params[:position].to_s]}"
81
+
82
+ str.gsub!('-', @separator) if params[:before_zero]
83
+ else
84
+ str = " #{params[:data][params[:key].length.to_s]}"
85
+ end
86
+
87
+ else
88
+ str = params[:not_and] ? params[:data] : @separator + params[:data] unless params[:data].empty?
89
+ end
90
+ @result_string += str
91
+ ''
92
+ end
93
+
94
+ def if_count_numbers_three(key, data)
95
+
96
+ if @data_storage[:comparison].has_key?(key[SECOND_NUMBER] +
97
+ key[THIRD_NUMBER]
98
+ )
99
+ data = get_from_storage_and_concat(
100
+ key: key,
101
+ data: data,
102
+ for_search_in_the_storage: [SECOND_NUMBER, THIRD_NUMBER]
103
+ )
104
+ end
105
+
106
+ if data.empty?
107
+
108
+ get_from_storage_and_concat(
109
+ key_in_storage: SECOND_NUMBER,
110
+ key: key,
111
+ data: data,
112
+ position: SECOND_POSITION
113
+ )
114
+
115
+ data = @data_storage[:comparison][key[THIRD_NUMBER]]
116
+
117
+ data = key[SECOND_NUMBER] != '0' ?
118
+
119
+ concatenation_to_string(
120
+ key: key,
121
+ data: data,
122
+ position: FIRST_POSITION
123
+ ) :
124
+
125
+ concatenation_to_string(
126
+ key: key,
127
+ data: data,
128
+ position: FIRST_POSITION,
129
+ before_zero: true
130
+ )
131
+ end
132
+
133
+ data
134
+ end
135
+
136
+ def if_count_numbers_not_three(key, data)
137
+
138
+ if @data_storage[:comparison].has_key?(
139
+ key[FIRST_NUMBER] +
140
+ key[SECOND_NUMBER]
141
+ )
142
+ data = get_from_storage_and_concat(
143
+ key: key,
144
+ data: data,
145
+ position: nil,
146
+ not_and: true,
147
+ for_search_in_the_storage: [FIRST_NUMBER, SECOND_NUMBER]
148
+ )
149
+ else
150
+ if key.length == 2
151
+
152
+ data = get_from_storage_and_concat(
153
+ key_in_storage: FIRST_NUMBER,
154
+ key: key,
155
+ data: data,
156
+ position: SECOND_POSITION,
157
+ not_and: true
158
+ )
159
+
160
+ data = get_from_storage_and_concat(
161
+ key_in_storage: SECOND_NUMBER,
162
+ key: key,
163
+ data: data,
164
+ position: FIRST_POSITION
165
+ )
166
+ else
167
+
168
+ data = get_from_storage_and_concat(
169
+ key_in_storage: SECOND_NUMBER,
170
+ key: key,
171
+ data: data
172
+ )
173
+ end
174
+ end
175
+
176
+ data
177
+ end
178
+
179
+ def for_fist_element(key, data)
180
+ get_from_storage_and_concat(
181
+ key_in_storage: FIRST_NUMBER,
182
+ key: key,
183
+ data: data
184
+ )
185
+ end
186
+
187
+ def postprocessing
188
+
189
+ if @result_string.start_with?(@separator) && @lang == 'uk'
190
+ @result_string = @result_string.slice(@separator.length..@result_string.length)
191
+ end
192
+
193
+ @result_string.strip
194
+ end
195
+
196
+ def get_params
197
+ {
198
+ key_in_storage: nil,
199
+ key: nil,
200
+ data: '',
201
+ position: nil,
202
+ not_and: nil,
203
+ before_zero: nil,
204
+ for_search_in_the_storage: nil
205
+ }
206
+ end
207
+
208
+ def merge_params(params)
209
+ get_params.merge(params)
210
+ end
211
+ end
@@ -0,0 +1,26 @@
1
+ class NumbersTranslator::PrepareData
2
+
3
+ def get_data
4
+ prepare
5
+ end
6
+
7
+ protected
8
+
9
+ def self.set(number)
10
+ self.new(number)
11
+ end
12
+
13
+ private
14
+
15
+ def initialize(number)
16
+ @number = number
17
+ @data = {}
18
+ end
19
+
20
+ def prepare
21
+ string_number = @number.to_s.gsub(/(?<=\d)(?=(?:\d{3})+\z)/, ' ')
22
+ @data.store(:numeric_groups, string_number.split)
23
+ @data.store(:count_numeric_groups, @data[:numeric_groups].size)
24
+ @data
25
+ end
26
+ end
@@ -0,0 +1,11 @@
1
+ class NumbersTranslator::Validator
2
+
3
+ def self.validate(number)
4
+ number = number.to_s
5
+ return (@@pattern.match(number) && number.length <= 16) || false
6
+ end
7
+
8
+ protected
9
+
10
+ @@pattern = /^[0-9]+$/
11
+ end
@@ -0,0 +1,3 @@
1
+ module NumbersTranslator
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,34 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "numbers_translator/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "numbers_translator"
8
+ spec.version = NumbersTranslator::VERSION
9
+ spec.authors = ["Ruslan Karelin"]
10
+ spec.email = ["ruslankarelin68@yandex.ru"]
11
+
12
+ spec.summary = %q{numbers translator}
13
+ spec.description = %q{Translating numbers into English}
14
+ spec.homepage = "https://github.com/RuslanKarelin/ruby-numbers-translator"
15
+ spec.license = "MIT"
16
+
17
+ if spec.respond_to?(:metadata)
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ else
20
+ raise "RubyGems 2.0 or newer is required to protect against " \
21
+ "public gem pushes."
22
+ end
23
+
24
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_development_dependency "bundler", "~> 1.16"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "rspec", "~> 3.0"
34
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: numbers_translator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ruslan Karelin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-04-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Translating numbers into English
56
+ email:
57
+ - ruslankarelin68@yandex.ru
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/numbers_translator.rb
73
+ - lib/numbers_translator/data_storage.rb
74
+ - lib/numbers_translator/engine.rb
75
+ - lib/numbers_translator/prepare_data.rb
76
+ - lib/numbers_translator/validator.rb
77
+ - lib/numbers_translator/version.rb
78
+ - numbers_translator.gemspec
79
+ homepage: https://github.com/RuslanKarelin/ruby-numbers-translator
80
+ licenses:
81
+ - MIT
82
+ metadata:
83
+ homepage_uri: https://github.com/RuslanKarelin/ruby-numbers-translator
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.7.10
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: numbers translator
104
+ test_files: []