-
-
Save dantewang/4023720 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
package im.dante.Typoglycemia; | |
import java.util.Objects; | |
import java.util.Random; | |
public class Randomize { | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) throws Exception { | |
Randomize r = new Randomize(); | |
String content = "I couldn't believe that I could actually understand " | |
+ "what I was reading: the phenomenal power of the human mind. " | |
+ "According to a research team at Cambridge University, it " | |
+ "doesn't matter in what order the letters in a word are, the " | |
+ "only important thing is that the first and last letter be " | |
+ "in the right place. The rest can be a total mess and you " | |
+ "can still read it without a problem. This is because the " | |
+ "human mind does not read every letter by itself, but the " | |
+ "word as a whole. Such a condition is appropriately called " | |
+ "Typoglycemia. Amazing, huh? Yeah and you always thought " | |
+ "spelling was important."; | |
System.out.println(r.produceTypoglycemia(content)); | |
} | |
private String produceTypoglycemia(String content) { | |
Objects.requireNonNull(content); | |
if (content.length() <= 3) { | |
return content; | |
} | |
// one array which has the same length with the original string's internal array | |
char[] contentChars = content.toCharArray(); | |
// reuse these | |
int indexSpace = 0; | |
int indexStart = 0; | |
int indexEnd = 0; | |
char temp; | |
Random random = new Random(); | |
// first level loop: looking for spaces | |
for (int i = 0; i < contentChars.length; i++) { | |
if (contentChars[i] != ' ') { | |
continue; | |
} | |
indexStart = indexSpace; | |
indexSpace = i; | |
indexEnd = indexSpace; | |
// search for the last letter. | |
// some occasions hasn't been taken into consideration | |
for (int j = indexEnd; j > indexStart; j--) { | |
if (Character.isLetter(contentChars[j])) { | |
break; | |
} | |
indexEnd = j; | |
} | |
indexStart += 2; | |
indexEnd -= 2; | |
int length = indexEnd - indexStart + 1; | |
if (length <= 1) { | |
continue; | |
} | |
else if (length == 2) { | |
// shortcut for 2 chars inside the word, avoid using Random. | |
temp = contentChars[indexStart + 1]; | |
contentChars[indexStart + 1] = contentChars[indexEnd -1]; | |
contentChars[indexEnd - 1] = temp; | |
continue; | |
} | |
else { | |
for (int j = indexStart; j <= indexEnd; j++) { | |
int randPos = random.nextInt(length) + indexStart; | |
temp = contentChars[j]; | |
contentChars[j] = contentChars[randPos]; | |
contentChars[randPos] = temp; | |
} | |
} | |
} | |
// finally one string with the same length of the array | |
return String.valueOf(contentChars); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment