Skip to content

Instantly share code, notes, and snippets.

@strubell
Created December 19, 2014 01:55
Show Gist options
  • Save strubell/dad6fb945ef6ab0be2f7 to your computer and use it in GitHub Desktop.
Save strubell/dad6fb945ef6ab0be2f7 to your computer and use it in GitHub Desktop.
/** Return a string that captures the generic "shape" of the original word,
mapping lowercase alphabetics to 'a', uppercase to 'A', digits to '1', whitespace to ' '.
Skip more than 'maxRepetitions' of the same character class. */
def stringShape(word:String, maxRepetitions:Int): String = {
val sb = new StringBuffer
var i = 0; var c = 'x'; var prevc = 'x'; var repetitions = 0
while (i < word.length) {
val char = word(i)
if (Character.isUpperCase(char)) c = 'A'
else if (Character.isLowerCase(char)) c = 'a'
else if (Character.isDigit(char)) c = '1'
else if (Character.isWhitespace(char)) c = ' '
else c = char
if (c == prevc) repetitions += 1
else { prevc = c; repetitions = 0 }
if (repetitions < maxRepetitions) sb.append(c)
i += 1
}
sb.toString
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment