Created
November 5, 2012 17:24
-
-
Save zhaoyao/4018506 to your computer and use it in GitHub Desktop.
Typoglycemia
This file contains hidden or 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
import java.util.*; | |
/** | |
* User: zhaoyao | |
* Date: 11/5/12 | |
* Time: 11:56 PM | |
*/ | |
public class Typoglycemia { | |
private static final Random rnd = new Random(); | |
public static void swap(char[] chars, int i, int j) { | |
char tmp = chars[i]; | |
chars[i] = chars[j]; | |
chars[j] = tmp; | |
} | |
private static void shuffle(char[] chars, int from, int until) { | |
int rndRange = until - from; | |
for (int i = until-1; i >= from; i--) { | |
int r = rnd.nextInt(rndRange) + from + 1; | |
swap(chars, i, r); | |
} | |
} | |
private static boolean isLetter(char c) { | |
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); | |
} | |
public static String makeTypoglycemia(String para) { | |
char[] chars = para.toCharArray(); | |
int len = chars.length; | |
int from = 0; | |
while (true) { | |
// find word start | |
while (from < len && !isLetter(chars[from])) { | |
from++; | |
} | |
if (from == len) { | |
break; | |
} | |
// find word end | |
int until = from + 1; | |
while (until < len && isLetter(chars[until])) { | |
until++; | |
} | |
if (until - from >= 4) { | |
shuffle(chars, from+1, until-1); | |
} | |
from = until; | |
} | |
return new String(chars); | |
} | |
public static void main(String[] args) { | |
String input = "I couldn't believe that I could actually understand what I was reading: the phenomenal power of the\n" + | |
" human mind. According to a research team at Cambridge University, it doesn't matter in what order\n" + | |
" the letters in a word are, the only important thing is that the first and last letter be in the right\n" + | |
" place. The rest can be a total mess and you can still read it without a problem. This is because the\n" + | |
" human mind does not read every letter by itself, but the word as a whole. Such a condition is appropriately\n" + | |
" called Typoglycemia. Amazing, huh? Yeah and you always thought spelling was important."; | |
makeTypoglycemia(input); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment