Skip to content

Instantly share code, notes, and snippets.

@pocke
Created October 5, 2016 08:59
Show Gist options
  • Save pocke/47d75ad86745e746cd4c2a0c12691d32 to your computer and use it in GitHub Desktop.
Save pocke/47d75ad86745e746cd4c2a0c12691d32 to your computer and use it in GitHub Desktop.
require 'levenshtein'
content = ARGF.read.split(/[^\w]/).grep(/^\w+$/)
h = {}
content.each do |c|
h[c] ||= 0
h[c] += 1
end
words = h.keys
alone = h.select{|k, v| v == 1}.keys
alone.each do |word|
next if word.size < 4
like = h.keys.find{|k| word != k && Levenshtein.normalized_distance(word, k) < 0.2}
next unless like
puts "Detect Typo!: #{word}"
puts "Did you mean #{like} ?"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment