Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Alphabet = ('a'..'z').to_a
NWORDS = begin
words = Hash.new(0)
File.read('big.txt').downcase.scan(/[a-z]+/).each { |w| words[w] += 1 }
words
end
def correct(word)
(known([word]) || known(edits1(word)) || known_edits2(word) || [word]).max{|a,b| NWORDS[a] <=> NWORDS[b]}
end
def edits1(word)
n = word.size
(0..n).map do |i|
a, b = [word[0, i], word[i-n, n-i]]
[ (a + b[1, n] unless b.empty?),
(a + b[1, 1] + b[0, 1] + b[2, n] unless b.size < 2),
(Alphabet.map { |c| a + c + b[1, n] } unless b.empty?),
Alphabet.map { |c| a + c + b } ]
end.flatten.uniq
end
def known_edits2(word)
edits1(word).map { |e1| edits1(e1).select { |e2| NWORDS.has_key?(e2) } if e1 }.flatten
end
def known(words)
list = words.select { |w| NWORDS.has_key?(w) }
list.size > 0 ? list : false
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment