Module: Boggler::Dictionary

Extended by:
Dictionary
Included in:
Dictionary
Defined in:
lib/boggler/dictionary.rb

Instance Method Summary collapse

Instance Method Details

#wordsObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/boggler/dictionary.rb', line 5

def words
  return @words if defined?(@words)

  content = nil

  Benchmarking.measure('dictionary') do
    path = File.expand_path('../../../vendor/dictionary.txt', __FILE__)
    content = File.read(path)
    content = content.split("\n")
    @words = words_to_hash(content)
  end

  @words
end

#words_to_hash(words) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/boggler/dictionary.rb', line 20

def words_to_hash(words)
  hash = {}

  words.each do |word|
    word = word.split('')

    key = word[0..2].join
    letters = word[3..-1]

    merge_word hash, key, word_to_hash(letters)
  end

  hash
end