phone_number_to_words 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,62 @@
1
+ require 'phone_number_to_words/version'
2
+ ERROR_MSG = "Can't generate words: As length may be greater or lesser than 10 (or) phone number has 0 or 1 values".freeze
3
+ module PhoneNumberToWords
4
+ class Convert
5
+ attr_accessor :number_key_mapping, :dictionary
6
+ def initialize
7
+ @number_key_mapping = {
8
+ '2' => %w[a b c],
9
+ '3' => %w[d e f],
10
+ '4' => %w[g h i],
11
+ '5' => %w[j k l],
12
+ '6' => %w[m n o],
13
+ '7' => %w[p q r s],
14
+ '8' => %w[t u v],
15
+ '9' => %w[w x y z]
16
+ }
17
+ @dictionary = load_dictionary
18
+ end
19
+
20
+ def load_dictionary
21
+ words = {}
22
+ File.foreach('lib/dictionary.txt') do |word|
23
+ word = word.chop.to_s.downcase
24
+ if words[word.length]
25
+ words[word.length] << word
26
+ else
27
+ words[word.length] = [word]
28
+ end
29
+ end
30
+ words
31
+ end
32
+
33
+ def is_valid_no(phone_no)
34
+ (phone_no.to_s.chars - %w[1 0]).length == 10
35
+ end
36
+
37
+ def generate_words(phone_number)
38
+ return ERROR_MSG unless is_valid_no(phone_number)
39
+ character_arrays = phone_number.chars.map { |digit| number_key_mapping[digit] }
40
+ character_arrays_lng = character_arrays.length - 1
41
+ results = []
42
+ compared_hash = {}
43
+ for i in (2..character_arrays_lng - 2)
44
+ temp_split_array = []
45
+ temp_split_array << character_arrays[0..i]
46
+ next if temp_split_array.first.length < 3
47
+ temp_split_array << character_arrays[i + 1..character_arrays_lng]
48
+ next if temp_split_array.last.length < 3
49
+ temp_combinate_array = []
50
+ temp_combinate_array << temp_split_array.first.shift.product(*temp_split_array.first).map(&:join)
51
+ next if temp_combinate_array.first.nil?
52
+ temp_combinate_array << temp_split_array.last.shift.product(*temp_split_array.last).map(&:join)
53
+ next if temp_combinate_array.last.nil?
54
+ compared_hash[i] = [(temp_combinate_array.first & dictionary[i + 1]), (temp_combinate_array.last & dictionary[character_arrays_lng - i])]
55
+ compared_hash[i].first.product(compared_hash[i].last).each do |words|
56
+ results << words
57
+ end
58
+ end
59
+ results << (character_arrays.shift.product(*character_arrays).map(&:join) & dictionary[10]).join(', ')
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,3 @@
1
+ module PhoneNumberToWords
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "phone_number_to_words/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "phone_number_to_words"
8
+ spec.version = PhoneNumberToWords::VERSION
9
+ spec.authors = ["sivamanikandan"]
10
+ spec.email = ["siva.mca19@gmail.com"]
11
+
12
+ spec.summary = %q{ Convert phone number to words }
13
+ spec.description = %q{ Given a 10 digit phone number, you must return all possible words or combination of words from the provided dictionary, that can be mapped back as a whole to the number.}
14
+ spec.homepage = "https://github.com/sivamca19/phone_number_to_words"
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.16"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: phone_number_to_words
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - sivamanikandan
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-01-31 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: " Given a 10 digit phone number, you must return all possible words or
56
+ combination of words from the provided dictionary, that can be mapped back as a
57
+ whole to the number."
58
+ email:
59
+ - siva.mca19@gmail.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - ".travis.yml"
67
+ - CODE_OF_CONDUCT.md
68
+ - Gemfile
69
+ - Gemfile.lock
70
+ - LICENSE.txt
71
+ - README.md
72
+ - Rakefile
73
+ - bin/console
74
+ - bin/setup
75
+ - lib/dictionary.txt
76
+ - lib/phone_number_to_words.rb
77
+ - lib/phone_number_to_words/version.rb
78
+ - phone_number_to_words.gemspec
79
+ homepage: https://github.com/sivamca19/phone_number_to_words
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.7.6
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Convert phone number to words
103
+ test_files: []