Skip to content

Instantly share code, notes, and snippets.

@Longlius
Last active August 29, 2015 13:57
Show Gist options
  • Save Longlius/9695661 to your computer and use it in GitHub Desktop.
Save Longlius/9695661 to your computer and use it in GitHub Desktop.
For Caroline~
import java.io.*;
import java.util.*;
public class WordGenerator {
// Filename constants
static final String ONSETS_FILE = "onsets.txt";
static final String VOWELS_FILE = "vowels.txt";
// Program entry point
public static void main(String[] args) throws IOException {
// Lists to store onsets and vowels during processing
ArrayList<String> onsets = new ArrayList<String>();
ArrayList<String> vowels = new ArrayList<String>();
// Setup mappings for both files in memory
File fOnsets = new File(ONSETS_FILE);
File fVowels = new File(VOWELS_FILE);
// Wrap File objects using Scanner for stream processing
Scanner sOnsets = new Scanner(fOnsets);
Scanner sVowels = new Scanner(fVowels);
// Create an additional Scanner to read standard input
Scanner sin = new Scanner(System.in);
// Prompt the user to enter a number of syllables to generate
System.out.print("Generate how many syllables? \n");
// Declare an int to immediately intialize with the value read
int snum = sin.nextInt();
// Only perform processing when the user enters an appopriate value (positive)
// Otherwise, just terminate execution with status code 1
if(snum > 0) {
// Extract onsets from each line of the file's I/O stream using Scanner
while(sOnsets.hasNextLine()) {
onsets.add(sOnsets.nextLine());
}
// Ditto for vowels
while(sVowels.hasNextLine()) {
vowels.add(sVowels.nextLine());
}
// Do the following "snum" times:
// Select a random onset and vowel, concatenate them, and print the results
for(int i = 0; i < snum; ++i) {
System.out.println(generateRandomSyllable(onsets, vowels));
}
// MIND YOUR MANNERS!
// Program termination without explicitly returning a status is rather rude~
// Operating Systems deserve your politeness and respect~
// 0 - SUCCESS!
System.exit(0);
} else {
// CHECK YOURSELF BEFORE YOU WRECK YOURSELF!
// Meaningful status codes are part of being a considerate, well-behaved process~
// Don't be a dick and always return 0, man~
// 1 - FAILURE!
System.exit(0);
}
}
public static String pickRandomMember(ArrayList<String> roster) {
// One-liner: create a new Random object -> invoke its nextInt() method -> pass result as index for get() -> return result of lookup
return roster.get(new Random().nextInt(roster.size()));
}
public static String generateRandomSyllable(ArrayList<String> onsets, ArrayList<String> vowels) {
// Create a StringBuilder object to hold the syllable string during generation
StringBuilder sb = new StringBuilder();
// Generate a consant and append it to sb
sb.append(pickRandomMember(onsets));
// Generate a vowel and append it to sb
sb.append(pickRandomMember(vowels));
// Randomly decide whether or not to generate an additional consonant
if(new Random().nextBoolean())
sb.append(pickRandomMember(onsets));
// Convert sb to string and return it
return sb.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment