

/**
 *  Program to convert regular English into Pig Latin using three methods: Main
 *  method, PhraseTranslator and WordTranslator.
 *
 *@author     Jeff
 *@created    May 15, 2003
 *@version    3.14 2002-10-15
 */
public class PigLatin3 {
	// Main method. It obtains the English sentence and prints the translation.
	/**
	 *  The main program for the PigLatin3 class
	 *
	 *@param  args  The command line arguments
	 */
	public static void main(String[] args) {
		FileInputReader source = new FileInputReader("C://cs303e/a9/phrasesin.txt");
		// Establish which file to read.
		// Establish the file to which letter use statistics are written.
		FileOutputWriter statsout = new FileOutputWriter("C://cs303e/a9/statsout.txt");
		// Establish the file to which the pig latin output is written.
		FileOutputWriter phrasesout = new FileOutputWriter("C://cs303e/a9/phrasesout.txt");
		// Establish string, variables, and an array for letter use statistics.
		String alphabet = "abcdefghijklmnopqrstuvwxyz";
		double totalLetters = 0;
		int[] toStatFile = new int[26];
		String sentence = source.readLine();
		// Read first line from file.

		// Begin loop that reads and processes all sentences until "STOP" is reached.
		while (!sentence.equals("STOP")) {
			// Call the stats method which adds all ocurrences of each letter in a single sentence.
			int[] sentenceStats = stats(sentence);
			// Add new sentence data to old sentence data.
			for (int j = 0; j < 26; j++) {
				toStatFile[j] = toStatFile[j] + sentenceStats[j];
			}
			// Call the PhraseTranslator method to translate the current sentence.
			String pigSentence = PhraseTranslator(sentence);
			phrasesout.writeln(pigSentence);
			// Write the translated sentence to file.
			sentence = source.readLine();
			// Acquires input from file.
		}
		// Count the total number of letters used in all sentences.
		for (int k = 0; k < 26; k++) {
			totalLetters = toStatFile[k] + totalLetters;
		}
		// Write the letter used and its frequency (%) in the statistics file.
		for (int l = 0; l < 26; l++) {
			statsout.writeln(alphabet.charAt(l) + ", " + (toStatFile[l] / totalLetters) * 100);
		}
		statsout.close();
		// Close files after they have been written to.
		phrasesout.close();


	}


	/**
	 *  Description of the Method
	 *
	 *@param  sentence  Description of the Parameter
	 *@return           Description of the Return Value
	 */
	static String PhraseTranslator(String sentence) {
		// Method header.

		String englishWord = "";
		String pigWord;
		String pigSentence = "";
		// String variables upon which words and sentences are built.
		char letter;
		char space = ' ';
		int index = 0;
		// Primitive data variables used to define elements of input/output.
		while (index < sentence.length()) {
			// Inner loop that builds english words out of the input, breaks them up and sends them for translation.
			englishWord = "";
			// String base upon which the extracted word is built.
			while (index < sentence.length()) {
				// Another inner loop that sorts letters until the end of input is reached.
				letter = sentence.charAt(index);
				// Acquires an individual letter.
				if (letter == (' ')) {
					break;
				}
				// If the letter is a space, the end of the word has been reached.
				if (letter == ('-')) {
					englishWord = (englishWord + letter);
					break;
				}
				// If the word is hyphenated, include the hyphen at the end of the new word.
				else {
					// If the word is not hyphenated, continue building word until the end of the word.
					englishWord = (englishWord + letter);
				}
				index++;
				// Move to next letter.
			}
			pigWord = WordTranslator(englishWord);
			// The new word is sent to the word translator.
			pigSentence = (pigSentence + pigWord);
			// When the word comes back from the translator, it is added to the translated sentence.
			index++;
			// Move to next letter.
		}
		return pigSentence;
		// Sends the translated sentence back to the main method for printing.
	}


	/**
	 *  Description of the Method
	 *
	 *@param  englishWord  Description of the Parameter
	 *@return              Description of the Return Value
	 */
	static String WordTranslator(String englishWord) {
		// This is the word translating method.

		String pigPrefix = "";
		String pigSuffix = "";
		int pigIndex = 0;
		String pigWord = englishWord;
		// Strings upon which the translated word is built.
		String vowels = "AEIOUaeiou";
		String punctuation = ":;,.!?-";
		String pigPunctuation = "";
		// Variables that define the characteristics of each word.
		char pigLetter = pigWord.charAt(pigIndex);
		// Picks the first letter of the word.
		if (vowels.indexOf(pigLetter) >= 0) {
			// Determine if the letter is a vowel.

			while (pigIndex < pigWord.length()) {
				// If the first letter is a vovel, the following loop is repeated until the end of the word.

				pigLetter = pigWord.charAt(pigIndex);
				// Translated word is built using the same letters in the same order as the English word.
				if (punctuation.indexOf(pigLetter) != -1) {
					break;
				}
				// When punctuation is encountered, escape from loop.
				else {
					// If punctuation is not encountered continue reading and translating the input.

					pigPrefix = (pigPrefix + pigLetter);
				}
				pigIndex++;
			}
			if (punctuation.indexOf(pigLetter) != -1) {
				// If punctuation is encountered, add the punctuation to the end of the word.
				pigPunctuation = (pigPunctuation + pigLetter);
			}
			if (pigPunctuation.equals("-")) {
				pigWord = (pigPrefix + pigSuffix + "yay" + pigPunctuation);
			}
			// If that punctuation happens to be a hyphen, do not include a space after the word.
			else {
				// If the punctuation is not a hyphen, put a space after the word.
				pigWord = (pigPrefix + "yay" + pigPunctuation + " ");
			}
		} else {
			// If the word begins with a consonant, do the following.

			pigLetter = pigWord.charAt(pigIndex);
			// If the first letter is a consonant, sets up a loop to continue until the end of the word is reached.
			while ((vowels.indexOf(pigLetter) == -1) && (pigIndex < pigWord.length())) {
				vowels = (vowels + "Y" + "y");
				// In the interior of the word, Y becomes a vowel.
				pigSuffix = (pigSuffix + pigLetter);
				// Places consonants from the front of input to the end of the translation.
				pigIndex++;
				// Increments to next letter.
				pigLetter = pigWord.charAt(pigIndex);
				// Acquires next letter.
				if (punctuation.indexOf(pigLetter) != -1) {
					break;
				}
				// If the next letter is a vowel, skip to next section. Otherwise, go back to top of this loop.
			}
			while ((pigIndex < pigWord.length()) && ((punctuation.indexOf(pigLetter) == -1))) {
				// Sets up a loop to process until the end of each translated word, jumps to next section when punctuation is encountered.

				pigPrefix = (pigPrefix + pigLetter);
				// Examines each letter and builds the prefix of the translated word.
				pigIndex++;
				if (pigIndex == pigWord.length()) {
					break;
				}
				// When the end of the word is reached, breaks out of this loop.
				pigLetter = pigWord.charAt(pigIndex);
			}
			if (punctuation.indexOf(pigLetter) != -1) {
				pigPunctuation += pigLetter;
			}
			// If punctuation is encountered, does the following operations.
			if (pigPunctuation.equals("-")) {
				pigWord = (pigPrefix + pigSuffix + "ay" + pigPunctuation);
			}
			// If the punctuation is a hyphen, includes it at the end of the word with no space.
			else {
				// If regular punctuation, put a space after the word.
				pigWord = (pigPrefix + pigSuffix + "ay" + pigPunctuation + " ");
			}
		}
		// If the Texas contraction has been used, feed correct string back into the PhraseTranslator.
		if (pigWord.equalsIgnoreCase("allY'ay ")) {
			pigWord = PhraseTranslator("You all");
		}
		return pigWord;
		// Returns word to caller for printing.
	}
	// End of word translator method.


	/**
	 *  Description of the Method
	 *
	 *@param  sentence  Description of the Parameter
	 *@return           Description of the Return Value
	 */
	static int[] stats(String sentence) {
		// Method header for method to count letters used.

		String statSentence = sentence.toLowerCase();
		// Convert all letters to lower case.
		// Establish array, and variables for keeping track of letter usage.
		int[] counter = new int[26];
		char character;
		int a = 97;
		int otherLetter;
		for (int i = 0; i < statSentence.length(); i++) {
			// Loop to scan sentence and count ocurrence of each letter.
			character = statSentence.charAt(i);
			otherLetter = (int) character;
			if (97 <= otherLetter && otherLetter <= 122) {
				counter[(otherLetter - a)]++;
			}
		}
		return counter;
		// Returns the counter array to Main for further bookkeeping and writing statistics data to file.
	}
}


