Module: Keypad

Defined in:
lib/keypad.rb

Overview

The Keypad module determine all sequences of letters (not necessarily valid words) associated to a given number using according to standard phone keypad.

If a dictionary is available, filtering out invalid words is as trivial as

Keypad::words(number).select{|word| dictionary.includes? word}

Finally, the other way round functionality is also provided. The number method finds the sequences of digits associated with a given word according to standard phone keypad.

References

Similar systems and more information can be found at

Class Method Summary collapse

Class Method Details

.letters(digit) ⇒ Object

Returns the letters associated with a given numerical digit, in lower case.

Keypad::letters(9) #=> [“w”, “x”, “y”, “z”]



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/keypad.rb', line 95

def Keypad.letters(digit)
  case digit.to_s
    when '2' then %w(a b c)
    when '3' then %w(d e f)
    when '4' then %w(g h i)
    when '5' then %w(j k l)
    when '6' then %w(m n o)
    when '7' then %w(p q r s)
    when '8' then %w(t u v)
    when '9' then %w(w x y z)
    else []
  end
end

.letters_per_digit(number) ⇒ Object

Returns the letters associated with each given numerical digit.

Keypad::letters_per_digit(23) #=> [[“a”, “b”, “c”], [“d”, “e”, “f”]]



113
114
115
116
117
118
119
# File 'lib/keypad.rb', line 113

def Keypad.letters_per_digit(number)
  result = []
  number.to_s.split('').each do |digit|
    result << letters(digit)
  end
  result
end

.number(word) ⇒ Object

– TODO: Find a way of preventing RDoc from linking the words “number” below to the method with same name. ++ Returns the number (i.e., the sequences of digits) associated with the given word according to standard phone keypad.

Keypad::number(“cat”) #=> 228



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/keypad.rb', line 72

def Keypad.number(word)
  number = []
  word.downcase.split('').each do |letter|
    number << case letter
      when 'a'..'c' then 2
      when 'd'..'f' then 3
      when 'g'..'i' then 4
      when 'j'..'l' then 5
      when 'm'..'o' then 6
      when 'p'..'s' then 7
      when 't'..'v' then 8
      when 'w'..'z' then 9
      else '?'
    end
  end
  number.join
end

.words(number) ⇒ Object

– TODO: Find a way of preventing RDoc from linking the words “words” and “number” below to the methods with same name. ++ Returns all sequences of letters (not necessarily valid words in natural languages) associated with a given number using according to standard phone keypad. Remember to quote numbers beginning with zero, for octal base is otherwise assumed for zero-beginning numbers.

Keypad::words(228) #=> [“aat”, “aau”, “aav”, “abt”, “abu”, “abv”, “act”,

"acu", "acv", "bat", "bau", "bav", "bbt", "bbu",
"bbv", "bct", "bcu", "bcv", "cat", "cau", "cav",
"cbt", "cbu", "cbv", "cct", "ccu", "ccv"]


54
55
56
57
58
59
60
61
# File 'lib/keypad.rb', line 54

def Keypad.words(number)
  letter_seq = letters_per_digit(number)
  words = letter_seq.shift
  letter_seq.each do |seq|
    words = Cartesian::joined_product( words, seq )
  end
  words
end