Class: Dictionary

Inherits:
Object
  • Object
show all
Defined in:
lib/spell_check/dictionary.rb

Instance Method Summary collapse

Constructor Details

#initializeDictionary

Instantiates dictionary with words from the words.txt file. This gem has been built specifically for this word list, so the file name is hard coded.



6
7
8
9
10
11
12
# File 'lib/spell_check/dictionary.rb', line 6

def initialize
  @words_set = Set.new
  words_file = File.expand_path(File.dirname(__FILE__) + '/words.txt')
  File.readlines(words_file).each do |line|
    @words_set.add(line.to_s.strip)
  end
end

Instance Method Details

#find_reg_ex_matches(reg_ex) ⇒ Set

Scans the word set (down cased) for any values that match the regular expression parameter

Parameters:

  • reg_ex (RegExp)

Returns:

  • (Set)


34
35
36
37
38
39
40
# File 'lib/spell_check/dictionary.rb', line 34

def find_reg_ex_matches( reg_ex )
  match_set = Set.new
  @words_set.to_a.each do |word|
    match_set.add word if word.downcase.match(reg_ex)
  end
  return match_set
end

#find_word(word) ⇒ String

Checks if this dictionary contains the word, either in lowercase or capitalized (first character) form. Returns the matched word, or nil if no match was found.

Parameters:

  • word (String)

Returns:

  • (String)


18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/spell_check/dictionary.rb', line 18

def find_word( word )
  word_str = word.to_s
  found_word = nil
  if @words_set.include? word_str
    found_word = word_str
  elsif @words_set.include? word_str.capitalize
    found_word = word_str.capitalize
  elsif @words_set.include? word_str.downcase
    found_word = word_str.downcase
  end
  return found_word
end