Class: D2W::PhoneToWord

Inherits:
Object
  • Object
show all
Defined in:
lib/d2w/phone_to_word.rb

Overview

require ‘thor’

Instance Method Summary collapse

Instance Method Details

#digit2word(phone_no) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/d2w/phone_to_word.rb', line 4

def digit2word(phone_no)

   # path_of_dictionary = "/Users/babaloo/Downloads/dictionary.txt"
   
   if phone_no.nil? || phone_no.length != 10 || phone_no.split('').select{|a|(a.to_i == 0 || a.to_i == 1)}.length > 0
    return []
   end
  dw_map =  {  
              "2" => ["a", "b", "c"],
              "3" => ["d", "e", "f"],
              "4" => ["g", "h", "i"],
              "5" => ["j", "k", "l"],
              "6" => ["m", "n", "o"],
              "7" => ["p", "q", "r", "s"],
              "8" => ["t", "u", "v"],
              "9" => ["w", "x", "y", "z"]
            }

  #this is path of given dictionary in my local machine
  dict = D2W::Dictionary.new.dictionary
  
  #fetching dictionay in my local dictionary array with leght 3,5,7,4,6,10
  dict = dict.map(&:downcase)
  dictionary = []
  dict.each do |word|
    dictionary <<  word if [3,4,5,6,7,10].include?(word.length)
  end
  # get all letters for numbers in form of array
  
  keys = phone_no.split('').map{|digit| dw_map[digit]}.flatten

  possible_words = []

  # filter words which is possible for given phone number

  dictionary.each do |word|
    if (word.split("") - keys).empty?
      possible_words << word
    end
  end
  exact_words = []
  # filer exact words for phone number from possible words
  possible_words.each do |word|
    exact_words << word if D2W::WordInNumber.new.word_in_number?(phone_no, word, dw_map)
  end

  p possible_pair = D2W::FilterWordLengthWise.new.filter_word_length_wise(exact_words, phone_no, dw_map)
end