Skip to content

Instantly share code, notes, and snippets.

@bartolsthoorn
Created October 6, 2012 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bartolsthoorn/25431d48e59065bc53ab to your computer and use it in GitHub Desktop.
Save bartolsthoorn/25431d48e59065bc53ab to your computer and use it in GitHub Desktop.
# Tests for string gibberish by counting consonants
def gibberish_probability string
consonants = ["B", "C", "D", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "V", "W", "X"]
points = 0
words = string.strip.gsub(" ", " ").split(" ")
words.each do |word|
if word.include?("<") || word.include?(">")
points += 1
next
end
if word.match(/([#{consonants.join("|")}]{4,})/i)
points += 1
next
end
if word.chars.count { |v| v =~ /\d/ }.to_f/word.length > 0.3
points += 1
end
if word.length < 2
points += 1
next
end
points += 1 if word.length > 20
end
normalized = points.to_f / words.length.to_f
#normalized = normalized/2.0
normalized = 1.0 if normalized > 1.0
return normalized
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment