Keypad 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/keypad.rb +121 -0
  2. data/tests/tc_keypad.rb +27 -0
  3. metadata +55 -0
@@ -0,0 +1,121 @@
1
+ #
2
+ # The Keypad module provides methods to determine
3
+ # all sequences of letters associated to a given number
4
+ # according to standard phone keypad. The "inverse" function,
5
+ # i.e., find the digits corresponding to a certain word,
6
+ # is also provided.
7
+ #
8
+ # == Author
9
+ # Adriano Brito Mitre <adriano.mitre@gmail.com>
10
+ #
11
+ # == Ackowledgements
12
+ # I was driven to coding this after reading the beggining of
13
+ # a post entitled "Google Splash" from Ernest Turro's blog.
14
+ # One can read the post
15
+ # here[http://ernest.turro.cat/blog/2006/08/25/google-splash]
16
+
17
+
18
+ # The Keypad module determine all sequences of letters (not necessarily
19
+ # valid words) associated to a given number using according to
20
+ # standard phone keypad.
21
+ #
22
+ # If a dictionary is available, filtering out invalid words is
23
+ # as trivial as
24
+ #
25
+ # Keypad::words(_number_).select{|word| _dictionary_.includes? word}
26
+ #
27
+ # Finally, the other way round functionality is also provided.
28
+ # The _number_ method finds the sequences of digits associated
29
+ # with a given word according to standard phone keypad.
30
+ #
31
+ # == References
32
+ # Similar systems and more information can be found at
33
+ # * PhoneSpell, http://www.phonespell.org
34
+ # * DialABC, http://www.dialabc.com
35
+
36
+ require 'cartesian'
37
+
38
+ module Keypad
39
+
40
+ #--
41
+ # TODO: Find a way of preventing RDoc from linking the words
42
+ # "words" and "number" below to the methods with same name.
43
+ #++
44
+ # Returns all sequences of letters (not necessarily valid words
45
+ # in natural languages) associated with a given number using
46
+ # according to standard phone keypad.
47
+ # Remember to quote numbers beginning with zero,
48
+ # for octal base is otherwise assumed for zero-beginning numbers.
49
+ #
50
+ # Keypad::words(228) #=> ["aat", "aau", "aav", "abt", "abu", "abv", "act",
51
+ # "acu", "acv", "bat", "bau", "bav", "bbt", "bbu",
52
+ # "bbv", "bct", "bcu", "bcv", "cat", "cau", "cav",
53
+ # "cbt", "cbu", "cbv", "cct", "ccu", "ccv"]
54
+ #
55
+ def Keypad.words(number)
56
+ letter_seq = letters_per_digit(number)
57
+ words = letter_seq.shift
58
+ letter_seq.each do |seq|
59
+ words = Cartesian::joined_product( words, seq )
60
+ end
61
+ words
62
+ end
63
+
64
+ #--
65
+ # TODO: Find a way of preventing RDoc from linking the words
66
+ # "number" below to the method with same name.
67
+ #++
68
+ # Returns the number (i.e., the sequences of digits) associated
69
+ # with the given word according to standard phone keypad.
70
+ #
71
+ # Keypad::number("cat") #=> 228
72
+ #
73
+ def Keypad.number(word)
74
+ number = []
75
+ word.downcase.split('').each do |letter|
76
+ number << case letter
77
+ when 'a'..'c' then 2
78
+ when 'd'..'f' then 3
79
+ when 'g'..'i' then 4
80
+ when 'j'..'l' then 5
81
+ when 'm'..'o' then 6
82
+ when 'p'..'s' then 7
83
+ when 't'..'v' then 8
84
+ when 'w'..'z' then 9
85
+ else '?'
86
+ end
87
+ end
88
+ number.join
89
+ end
90
+
91
+ # Returns the letters associated with a given numerical digit,
92
+ # in lower case.
93
+ #
94
+ # Keypad::letters(9) #=> ["w", "x", "y", "z"]
95
+ #
96
+ def Keypad.letters(digit)
97
+ case digit.to_s
98
+ when '2' then %w(a b c)
99
+ when '3' then %w(d e f)
100
+ when '4' then %w(g h i)
101
+ when '5' then %w(j k l)
102
+ when '6' then %w(m n o)
103
+ when '7' then %w(p q r s)
104
+ when '8' then %w(t u v)
105
+ when '9' then %w(w x y z)
106
+ else []
107
+ end
108
+ end
109
+
110
+ # Returns the letters associated with each given numerical digit.
111
+ #
112
+ # Keypad::letters_per_digit(23) #=> [["a", "b", "c"], ["d", "e", "f"]]
113
+ #
114
+ def Keypad.letters_per_digit(number)
115
+ result = []
116
+ number.to_s.split('').each do |digit|
117
+ result << letters(digit)
118
+ end
119
+ result
120
+ end
121
+ end
@@ -0,0 +1,27 @@
1
+ require 'test/unit'
2
+
3
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
4
+ require 'keypad'
5
+
6
+ class TestKeypad < Test::Unit::TestCase
7
+ def test_words
8
+ words228 = ["aat", "aau", "aav", "abt", "abu", "abv", "act",
9
+ "acu", "acv", "bat", "bau", "bav", "bbt", "bbu",
10
+ "bbv", "bct", "bcu", "bcv", "cat", "cau", "cav",
11
+ "cbt", "cbu", "cbv", "cct", "ccu", "ccv"]
12
+ assert(Keypad::words(228) == words228)
13
+ end
14
+
15
+ def test_number
16
+ assert(Keypad::number("cat") == "228")
17
+ end
18
+
19
+ def test_letters
20
+ assert(Keypad::letters(9) == ["w", "x", "y", "z"])
21
+ end
22
+
23
+ def test_letters_per_digit
24
+ assert(Keypad::letters_per_digit(23) == \
25
+ [["a", "b", "c"], ["d", "e", "f"]])
26
+ end
27
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: Keypad
5
+ version: !ruby/object:Gem::Version
6
+ version: 1.0.0
7
+ date: 2006-11-24 00:00:00 -03:00
8
+ summary: The Keypad module provides methods to determine all sequences of letters associated to a given number according to standard phone keypad. The "inverse" function, i.e., find the digits corresponding to a certain word, is also provided.
9
+ require_paths:
10
+ - lib
11
+ email: adriano.mitre@gmail.com
12
+ homepage: http://rubyforge.org/projects/keypad/
13
+ rubyforge_project:
14
+ description:
15
+ autorequire: keypad.rb
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Adriano Brito Mitre
31
+ files:
32
+ - tests/tc_keypad.rb
33
+ - lib/keypad.rb
34
+ test_files:
35
+ - tests/tc_keypad.rb
36
+ rdoc_options: []
37
+
38
+ extra_rdoc_files: []
39
+
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ requirements: []
45
+
46
+ dependencies:
47
+ - !ruby/object:Gem::Dependency
48
+ name: Cartesian
49
+ version_requirement:
50
+ version_requirements: !ruby/object:Gem::Version::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 0.1.0
55
+ version: