-
-
Save bartolsthoorn/25431d48e59065bc53ab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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