Skip to content

Instantly share code, notes, and snippets.

@fakechris
Forked from emanonwzy/Output
Created July 24, 2023 01:50
Show Gist options
  • Save fakechris/951fa0364355cdac26792ba6cd2db0b1 to your computer and use it in GitHub Desktop.
Save fakechris/951fa0364355cdac26792ba6cd2db0b1 to your computer and use it in GitHub Desktop.
ZZ. @老赵 生成一段Typoglycemia文本.Typoglycemia是个新词,描述的是人们识别一段文本时的一个有趣的现象:只要每个单词的首尾字母正确,中间的字母顺序完全打乱也没有关系,照样可以正常理解。
原题目参见[老赵的博客](http://blog.zhaojie.me/2012/11/how-to-generate-typoglycemia-text.html)
由于Java中String是immutable,所以这里的实现会有两次字符数组的拷贝,不知还有什么其他的方法可以省去这一步操作。
随机是通过获取系统时间,并与相关字符取模得到的,获取系统时间这个操作不知道会不会称为系程序瓶颈,回头查查。
Original String:
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.
Converted String:
I cuoldn't bvleiee that I cuold aautlcly urnnteadsd what I was riedang:
the pnamohenel pwoer of the hmaun mnid. Adnicrocg to a racseerh taem at Cbiamdrge Urensitivy,
it dosen't mtater in what order the lerttes in a wrod are, the only inatpromt thing is that the
fsrit and last lteter be in the rihgt place. The rest can be a total mses and you can slitl read
it whtuoit a prbeolm. Tihs is bacsuee the hmaun mnid deos not read evrey lteter by itself, but the
wrod as a wlhoe. Such a cdoonitin is aropltepriapy clelad Teomipglyyca. Aizamng, huh? Yeah and you
ayawls tguhoht spnellig was inatpromt.
package test.java.lang.string;
/**
* Typoglycemia generator.<br>
* <br>
* Rules:<br>
* <ol>
* <li>保持所有非字母的字符位置不变。</li>
* <li>保持单词首尾字母不变,中间字符打乱。</li>
* <br>
* <br>
*
* @author caoxudong
*
*/
public class TypoglycemiaGenerator {
public static void main(String[] args) {
String originalString = "I couldn't believe that I could actually understand what I was reading: \n" +
"the phenomenal power of the human mind. According to a research team at Cambridge University, \n" +
" it doesn't matter in what order the letters in a word are, the only important thing is that the \n" +
"first and last letter be in the right place. The rest can be a total mess and you can still read \n" +
"it without a problem. This is because the human mind does not read every letter by itself, but the \n" +
"word as a whole. Such a condition is appropriately called Typoglycemia. Amazing, huh? Yeah and you \n" +
"always thought spelling was important.";
String convertedString = makeRandom(originalString);
System.out.println("Original String:");
System.out.println(originalString);
System.out.println();
System.out.println("Converted String:");
System.out.println(convertedString);
}
private static String makeRandom(String content) {
if (content == null) {
return null;
} else {
char[] resultBuf = content.toCharArray();
//find words to be converted
int i = 0, j = 0, flag = 0;
int length = resultBuf.length;
while (true) {
char currentChar = resultBuf[j];
if ((currentChar >= 'a' && currentChar <= 'z') || (currentChar >= 'A' && (currentChar <= 'Z'))) {
if (flag == 0) {
i = j;
flag = 1;
}
} else {
if (flag != 0) {
randomizeWord(resultBuf, i, j - 1);
i = j;
flag = 0;
}
}
j++;
if (j == length) {
if (flag != 0) {
randomizeWord(resultBuf, i, j - 1);
}
break;
}
}
return new String(resultBuf);
}
}
/**
* converted word<br>
*
* @param buf buf
* @param start start position
* @param stop stop position(inclusive)
* @param count how much characters to be changed
*/
private static void randomizeWord(char[] buf, int start, int stop) {
int length = stop - start + 1;
if (length <= 3) {
return;
} else {
int n = 1;
long randomSeed = System.currentTimeMillis();
while (n < (length - 1)) {
int tempPosition = (int)((randomSeed + buf[start + 1 + n]) % (length - 2));
int from = start + 1 + tempPosition;
int to = start + n;
char bufChar = buf[from];
buf[from] = buf[to];
buf[to] = bufChar;
n++;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment