Created
October 18, 2010 14:51
-
-
Save jashkenas/632331 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
ALNUM = /[a-z0-9]/i | |
PUNCT = /[^a-z0-9\s]/i | |
REPEAT = /(.)\1{2,}/ | |
UPPER = /[A-Z]/ | |
LOWER = /[a-z]/ | |
ACRONYM = /^[A-Z]+('?s|[.,])?$/ | |
ALL_ALPHA = /^[a-z]+$/i | |
CONSONANT = /[bcdfghjklmnpqrstvwxz]/i | |
VOWEL = /[aeiouy]/i | |
CONSONANT_5 = /[bcdfghjklmnpqrstvwxz]{5}/i | |
VOWEL_4 = /[aeiouy]{4}/i | |
# More than 20 bytes in length. | |
w.length > 20 | |
# More punctuation than alpha numerics. | |
w.length > 2 && (w.scan(ALNUM).length < w.scan(PUNCT).length) | |
# Ignoring the first and last characters in the string, if there are three or | |
# more different punctuation characters in the string. | |
w[1...-1].scan(PUNCT).uniq.length >= 3 | |
# If there are three or more identical characters in a row in the string. | |
w =~ REPEAT | |
# Number of uppercase letters greater than lowercase letters, but the word is | |
# not all uppercase + punctuation. | |
upper = w.scan(UPPER).length | |
lower = w.scan(LOWER).length | |
upper > lower && (w !~ ACRONYM) | |
# All characters are alphabetic and there are 8 times more vowels than | |
# consonants, or 8 times more consonants than vowels. | |
valid = w.length > 2 && (w =~ ALL_ALPHA) && (w !~ ACRONYM) | |
vowels = w.scan(VOWEL).length | |
consonants = w.scan(CONSONANT).length | |
valid && (vowels > consonants * 8 || consonants > vowels * 8) | |
# Four or more consecutive vowels, or five or more consecutive consonants. | |
(w =~ VOWEL_4) || (w =~ CONSONANT_5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment