Class: Spell::Spell
- Inherits:
-
Object
- Object
- Spell::Spell
- Defined in:
- lib/spell/spell.rb
Instance Method Summary collapse
-
#best_match(given_word) ⇒ Object
Returns the closest matching word in the dictionary.
-
#compare(word1, word2) ⇒ Object
Return a value from 0.0-1.0 of how similar these two words are.
-
#initialize(*args) ⇒ Spell
constructor
A new instance of Spell.
-
#spelled_correctly?(word) ⇒ Boolean
Returns a boolean for whether or not ‘word’ is in the dictionary.
Constructor Details
#initialize(*args) ⇒ Spell
Returns a new instance of Spell.
3 4 5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/spell/spell.rb', line 3 def initialize(*args) fail "Too many arguments given" if args.count > 3 if args[0].is_a? Hash @word_list = args[0] @alpha = args[1] || 0.3 elsif args[0].is_a? Array fail "Word usage weights do not make sense with an Array" if args[1] @word_list = args[0] else fail "First argument must be an Array or Hash" end end |
Instance Method Details
#best_match(given_word) ⇒ Object
Returns the closest matching word in the dictionary
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/spell/spell.rb', line 18 def best_match(given_word) words = (@word_list.is_a? Array) ? @word_list : @word_list.keys word_bigrams = bigramate(given_word) word_hash = words.map do |key| [key, bigram_compare(word_bigrams, bigramate(key))] end word_hash = Hash[word_hash] # Weight by word usage, if logical word_hash = apply_usage_weights(word_hash) if @word_list.is_a? Hash word_hash.max_by { |key, value| value }.first end |
#compare(word1, word2) ⇒ Object
Return a value from 0.0-1.0 of how similar these two words are
43 44 45 |
# File 'lib/spell/spell.rb', line 43 def compare(word1, word2) bigram_compare(bigramate(word1), bigramate(word2)) end |
#spelled_correctly?(word) ⇒ Boolean
Returns a boolean for whether or not ‘word’ is in the dictionary
34 35 36 37 38 39 40 |
# File 'lib/spell/spell.rb', line 34 def spelled_correctly?(word) if @word_list.is_a? Hash @word_list.keys.include?(word) else @word_list.include?(word) end end |