Skip to content

Instantly share code, notes, and snippets.

@Pamelloes
Created July 16, 2012 03:45
Show Gist options
  • Save Pamelloes/3120345 to your computer and use it in GitHub Desktop.
Save Pamelloes/3120345 to your computer and use it in GitHub Desktop.
Program that generates pronouncable, random text.
import java.util.ArrayList;
import java.util.List;
public class RandomPronouncableText {
private static Object[][] letterCombinations = {
//letter vowel "L" "R" Random Following Letters Prefixing Letters
{"a" ,true},
{"b" ,false ,true ,true ,true ,new String[]{} ,new String[]{} },
{"c" ,false ,true ,true ,true ,new String[]{"h","k"} ,new String[]{} },
{"d" ,false ,false ,true ,true ,new String[]{} ,new String[]{} },
{"e" ,true},
{"f" ,false ,true ,true ,true ,new String[]{} ,new String[]{} },
{"g" ,false ,true ,true ,true ,new String[]{} ,new String[]{} },
{"h" ,false ,false ,false ,true ,new String[]{} ,new String[]{"c","s"} },
{"i" ,true},
{"j" ,false ,false ,false ,true ,new String[]{} ,new String[]{} },
{"k" ,false ,true ,true ,true ,new String[]{} ,new String[]{"c"} },
{"l" ,false ,false ,false ,true ,new String[]{} ,new String[]{} },
{"m" ,false ,false ,false ,true ,new String[]{} ,new String[]{} },
{"n" ,false ,false ,false ,true ,new String[]{} ,new String[]{} },
{"o" ,true},
{"p" ,false ,true ,true ,true ,new String[]{} ,new String[]{} },
// {"q" ,false ,false ,false ,false ,new String[]{"u"} ,new String[]{} }, "Q" makes too many problems, so I decided to scrap it. :D
{"r" ,false ,false ,false ,true ,new String[]{} ,new String[]{} },
{"s" ,false ,true ,false ,true ,new String[]{"h","t"} ,new String[]{} },
{"t" ,false ,false ,true ,true ,new String[]{} ,new String[]{} },
{"u" ,true},
{"v" ,false ,true ,false ,true ,new String[]{} ,new String[]{} },
{"w" ,false ,false ,true ,true ,new String[]{} ,new String[]{} },
{"x" ,false ,false ,false ,true ,new String[]{} ,new String[]{} },
{"y" ,true},
{"z" ,false ,false ,false ,true ,new String[]{} ,new String[]{} },
};
public static void main(String[] args){
int length = (int) (Math.random() * 100) + 10;//pick a random length
String text = (String) letterCombinations[getIndex()][0];
String[] cons = getConsonants();
for(int i = 1 ; i < length; i++) {
int previndex = getIndex(text.charAt(i - 1));
if((Boolean) letterCombinations[previndex][1]) {//is a vowel
//last letter was a vowel, this can be any consonant
text += cons[(int) (Math.random() * cons.length)];
continue;
} else {//is consonant
if(i < 2) {//first index, find a valid char since there arean't previous chars to base on.
text += findValidChar(previndex);
} else {
int prev2index = getIndex(text.charAt(i - 2));
if((Boolean) letterCombinations[prev2index][1]) {//is a vowel
//vowel-consonant = end of syllable.
if((Boolean) letterCombinations[previndex][4]) text += cons[(int) (Math.random() * cons.length)]; //Any character can follow.
else text += pickFollowing(previndex); //Select a specified character.
continue;
} else {
String[] preceding = (String[]) letterCombinations[prev2index][5];
if(preceding.length > 0 && i > 2) {
int prev3index = getIndex(text.charAt(i - 3));
for(String s : preceding) {
if(letterCombinations[prev3index][0].equals(s)) {
if(i > 3) {
int prev4index = getIndex(text.charAt(i - 4));
if((Boolean) letterCombinations[prev4index][1]) {//is a vowel
//end of syllable, so can be anything.
text += cons[(int) (Math.random() * cons.length)];
} else { //is a consonant
//within a syllable, proceded normally.
text += findValidChar(previndex);
}
} else {
//can't check back even further, we are within a syllable
text += findValidChar(previndex);
}
break;
}
}
//we are within a syllable.
text += findValidChar(previndex);
} else {
//no special cases, within syllable, continue normally.
text += findValidChar(previndex);
}
}
}
}
}
System.out.println(text);
}
private static String findValidChar(int index) {
String[] following = (String[]) letterCombinations[index][5];
String[] vowels = getVowels();
boolean l = (Boolean) letterCombinations[index][2];
boolean r = (Boolean) letterCombinations[index][3];
String[] total = new String[following.length + vowels.length + (l ? 1 : 0) + (r ? 1 : 0)];
int counter = 0;
for(String s : following) {
total[counter] = s;
counter++;
}
for(String s : vowels) {
total[counter] = s;
counter++;
}
if(l) {
total[counter] = "l";
counter++;
}
if(r) {
total[counter] = "r";
counter++;
}
return total[(int) (Math.random() * total.length)];
}
private static String pickFollowing(int index) {
String[] following = (String[]) letterCombinations[index][5];
return following[(int) (Math.random() * following.length)];
}
private static String[] getVowels() {
List<String> vowels = new ArrayList<String>();
for(int i = 0; i < letterCombinations.length; i++) {
if((Boolean) letterCombinations[i][1]) vowels.add((String) letterCombinations[i][0]);
}
return vowels.toArray(new String[vowels.size()]);
}
private static String[] getConsonants() {
List<String> consonants = new ArrayList<String>();
for(int i = 0; i < letterCombinations.length; i++) {
if(!(Boolean) letterCombinations[i][1]) consonants.add((String) letterCombinations[i][0]);
}
return consonants.toArray(new String[consonants.size()]);
}
private static int getIndex() {
return (int) (Math.random() * letterCombinations.length);
}
private static int getIndex(char c) {
String s = "" + c;
for(int i = 0; i < letterCombinations.length; i++) {
if(letterCombinations[i][0].equals(s)) return i;
}
throw new IllegalArgumentException("Character '" + c + "' is not a lower case letter.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment