Class: SpellingBee

Inherits:
Object
  • Object
show all
Defined in:
lib/spellingbee.rb,
lib/spellingbee/version.rb

Constant Summary collapse

DEFAULT_DICT =
File.join(File.dirname(__FILE__), '..', 'dict', 'default.txt')
VERSION =
'0.0.2'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ SpellingBee

Returns a new instance of SpellingBee.



7
8
9
10
11
12
# File 'lib/spellingbee.rb', line 7

def initialize(opts = {})
  options = { :source_text => DEFAULT_DICT }.merge opts
  @dict_frequency = Hash.new(1)
  @dict_words = File.new(options[:source_text]).read.downcase.scan(/[a-z]+/)
  @dict_words.each { |word| @dict_frequency[word] += 1 }
end

Instance Attribute Details

#dict_frequencyObject

Returns the value of attribute dict_frequency.



3
4
5
# File 'lib/spellingbee.rb', line 3

def dict_frequency
  @dict_frequency
end

#dict_wordsObject

Returns the value of attribute dict_words.



3
4
5
# File 'lib/spellingbee.rb', line 3

def dict_words
  @dict_words
end

Instance Method Details

#correct(word) ⇒ Object

Returns the suggestion for the correction. If the word is found in the dictionary, it is returned, otherwise the word closest to it is returned. If there are no spelling suggestions, the same word is returned.

Example:

s = Spellingbee.new
s.correct "spelling"   #=> ["spelling"] -> word from dictionary
s.correct "speling"    #=> ["spelling"] -> corrected spelling
s.correct "qqqqqq"     #=> ["qqqqqq"]   -> no suggestions


90
91
92
# File 'lib/spellingbee.rb', line 90

def correct word
  known [word] or known(variation_words word) or [word]
end

#deletions(word) ⇒ Object

Returns all the combinations of the word with one character removed

Example:

deletions("word") #=> ["ord", "wrd", "wod", "wor"]


19
20
21
22
# File 'lib/spellingbee.rb', line 19

def deletions word
  n = word.length
  deletion = (0...n).collect {|i| word[0...i]+word[i+1..-1] }
end

#insertions(word) ⇒ Object

Returns all variations of the word with an alphabet inserted between the characters.

Example:

insertions("word") #=> ["aword", "bword", "cword" ... "waord", "wbord" ... ]


53
54
55
56
57
58
# File 'lib/spellingbee.rb', line 53

def insertions word
  n = word.length
  new_words = []
  (n+1).times {|i| ('a'..'z').each {|l| new_words << word[0...i]+l.chr+word[i..-1] } }
  new_words
end

#known(words) ⇒ Object

Selects the words from the list that are present in the dictionary.

Example:

known ["asdfgh", "known", "word", "qqqq"] #=> ["known", "word"]


74
75
76
77
# File 'lib/spellingbee.rb', line 74

def known words
  known_words = words.find_all { |w| @dict_frequency.has_key? w }
  known_words.empty? ? nil : known_words
end

#replacements(word) ⇒ Object

Returns all variations of the word with each character replaced by all characters from ‘a’ to ‘z’.

Example:

replacements("word") #=> ["aord", "bord", "cord", ... "ward", "wbrd" ...]


40
41
42
43
44
45
# File 'lib/spellingbee.rb', line 40

def replacements word
  n = word.length
  new_words = []
  n.times {|i| ('a'..'z').each {|l| new_words << word[0...i]+l.chr+word[i+1..-1] } }
  new_words
end

#transpositions(word) ⇒ Object

Returns all combinations of the word with adjacent words transposed.

Example:

transpositions("word") #=> ["owrd", "wrod", "wodr"]


29
30
31
32
# File 'lib/spellingbee.rb', line 29

def transpositions word
  n = word.length
  (0...n-1).collect {|i| word[0...i]+word[i+1,1]+word[i,1]+word[i+2..-1] }
end

#variation_words(word) ⇒ Object

Returns the variations of the given spelling. This set will contain all the possible variations of the spelling havind edit distance = 1. (Edit distance is the number of edits it takes to obtain the second word from the first.)



64
65
66
# File 'lib/spellingbee.rb', line 64

def variation_words word
  ( deletions(word) + transpositions(word) + replacements(word) + insertions(word) ).uniq
end