From 3a5d174c210819243db8538999beffb1920cf8d7 Mon Sep 17 00:00:00 2001 From: cmauer69 Date: Mon, 11 Apr 2016 17:28:20 -0500 Subject: [PATCH] Final commit for the pig-latin project from cmauer69. --- src/edu/htc/piglatin/FileCompareUtil.java | 42 +++ src/edu/htc/piglatin/FileParser.java | 45 +++ src/edu/htc/piglatin/ListFileWriter.java | 31 ++ src/edu/htc/piglatin/Main.java | 16 +- src/edu/htc/piglatin/PigLatinExceptions.java | 16 + src/edu/htc/piglatin/PigLatinTranslator.java | 327 +++++++++++++++++- .../htc/piglatin/PigLatinTranslatorTest.java | 186 +++++++++- 7 files changed, 639 insertions(+), 24 deletions(-) create mode 100644 src/edu/htc/piglatin/FileCompareUtil.java create mode 100644 src/edu/htc/piglatin/FileParser.java create mode 100644 src/edu/htc/piglatin/ListFileWriter.java create mode 100644 src/edu/htc/piglatin/PigLatinExceptions.java diff --git a/src/edu/htc/piglatin/FileCompareUtil.java b/src/edu/htc/piglatin/FileCompareUtil.java new file mode 100644 index 0000000..a7d1f94 --- /dev/null +++ b/src/edu/htc/piglatin/FileCompareUtil.java @@ -0,0 +1,42 @@ +package edu.htc.piglatin; + +import java.io.FileInputStream; +import java.io.IOException; + +/** + * This is a utility class to compare files. + * It is based on an example from the textbook. + */ +/** + * Created by clifford.mauer on 4/4/2016. + */ +public class FileCompareUtil { + + public static boolean compare(String path1, String path2) throws IOException { + + boolean same = true; + try (FileInputStream f1 = new FileInputStream(path1); + FileInputStream f2 = new FileInputStream(path2)) { + System.out.println("Total file size to read (in bytes) : " + + f1.available()); + System.out.println("Total file size to read (in bytes) : " + + f2.available()); + + int first, second; + do { + first = f1.read(); + second = f2.read(); + if (first != second) { + same = false; + break; + } + } while (first != -1 && second != -1); + + if (first != second){ + same = false; + } + } + return same; + + } + } diff --git a/src/edu/htc/piglatin/FileParser.java b/src/edu/htc/piglatin/FileParser.java new file mode 100644 index 0000000..0716993 --- /dev/null +++ b/src/edu/htc/piglatin/FileParser.java @@ -0,0 +1,45 @@ +package edu.htc.piglatin; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; + +/** + * Created by clifford.mauer on 4/4/2016. + */ +public abstract class FileParser { + + /** + * This class will read sentences from a file and store them in an ArrayList. + * The file must have one sentence per line. + * + * Punctuation at the end of the sentence will not be removed. + */ + public static ArrayList parseFile(String fileName) throws IOException { + + // Complete this method so that the test below in the main method is correct. + ArrayList strReturn = new ArrayList<>(); + boolean blnExit = false; + + BufferedReader reader = new BufferedReader(new FileReader(fileName)); + try { + while (!blnExit) { + String line = reader.readLine(); + if (line.trim().length() != 0) { + strReturn.add(line); + System.out.println(line); + blnExit = false; + } + else { + if (line == null){ + blnExit = true; + } + } + } + } finally { + reader.close(); + return strReturn; + } + } + } diff --git a/src/edu/htc/piglatin/ListFileWriter.java b/src/edu/htc/piglatin/ListFileWriter.java new file mode 100644 index 0000000..cdd3814 --- /dev/null +++ b/src/edu/htc/piglatin/ListFileWriter.java @@ -0,0 +1,31 @@ +package edu.htc.piglatin; + +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; + +/** + * Created by clifford.mauer on 4/4/2016. + */ +public class ListFileWriter { + + public static void writeToFile(ArrayList list, String fileName) throws IOException { + + // Complete this method so that the test below in the main method is correct. + boolean blnExit = false; + + FileWriter fw = new FileWriter(fileName); + try { + for (String str : list) { + if ( str.trim().length() != 0){ + fw.write(str.trim() + "\r\n"); + } + } + fw.close(); + } + catch (IOException e){ + System.out.println("I/O Error: " + e); + } + } + +} diff --git a/src/edu/htc/piglatin/Main.java b/src/edu/htc/piglatin/Main.java index 6fe06d8..ae13594 100755 --- a/src/edu/htc/piglatin/Main.java +++ b/src/edu/htc/piglatin/Main.java @@ -1,8 +1,8 @@ package edu.htc.piglatin; -import edu.htc.file.FileCompareUtil; -import edu.htc.file.FileParser; -import edu.htc.file.ListFileWriter; +import edu.htc.piglatin.FileCompareUtil; +import edu.htc.piglatin.FileParser; +import edu.htc.piglatin.ListFileWriter; import java.io.File; import java.io.FileNotFoundException; @@ -11,15 +11,15 @@ public class Main { - public static void main(String[] args) throws IOException { + public static void main(String[] args) throws IOException, NoSentenceException { String current = new File( "." ).getCanonicalPath(); String origFileEnglish = ClassLoader.getSystemResource("data/sampleText.txt").getFile(); - String outputFilePigLatin = current + "/sampleText_PigLatin.txt"; + String outputFileEnglish = current + "/samplePigLatin_English.txt"; String origFilePigLatin = ClassLoader.getSystemResource("data/samplePigLatin.txt").getFile(); - String outputFileEnglish = current + "/samplePigLatin_English.txt "; + String outputFilePigLatin = current + "/sampleText_PigLatin.txt"; try { @@ -43,7 +43,7 @@ public static void main(String[] args) throws IOException { } } - public static void translateFileToPigLatin(String inFilePath, String outFilePath) { + public static void translateFileToPigLatin(String inFilePath, String outFilePath) throws NoSentenceException { // Read File ArrayList sentences = null; try { @@ -69,7 +69,7 @@ public static void translateFileToPigLatin(String inFilePath, String outFilePath } } - public static void translateFileFromPigLatin(String inFilePath, String outFilePath) { + public static void translateFileFromPigLatin(String inFilePath, String outFilePath) throws NoSentenceException { // Read File ArrayList sentences = null; try { diff --git a/src/edu/htc/piglatin/PigLatinExceptions.java b/src/edu/htc/piglatin/PigLatinExceptions.java new file mode 100644 index 0000000..f1d6501 --- /dev/null +++ b/src/edu/htc/piglatin/PigLatinExceptions.java @@ -0,0 +1,16 @@ +package edu.htc.piglatin; + +/** + * Created by clifford.mauer on 4/11/2016. + */ +public class PigLatinExceptions extends Exception { + public PigLatinExceptions(String message) { + super(message); + } +} + +class NoSentenceException extends Exception { + public NoSentenceException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/src/edu/htc/piglatin/PigLatinTranslator.java b/src/edu/htc/piglatin/PigLatinTranslator.java index 1a5b977..7196ee0 100644 --- a/src/edu/htc/piglatin/PigLatinTranslator.java +++ b/src/edu/htc/piglatin/PigLatinTranslator.java @@ -1,27 +1,346 @@ package edu.htc.piglatin; +import java.io.*; /** * Created by marymosman on 3/21/16. */ public class PigLatinTranslator { - public static String translateToPigLatin(String sentence) { + public static String translateToPigLatin(String sentence) throws NoSentenceException { + String returnstr = ""; + String newsentence = ""; + Boolean blnFirstWord = false; + Boolean blnLastWord = false; + if (sentence.length()==0 | sentence == null){ + throw new NoSentenceException("The sentence cannot be empty or null"); + } + + //** Lets flag the first word of the sentence and the last word + + //** convert the sentence to PigLatin by splitting the sentence into words + //** seperated by spaces + String[] words = sentence.split(" "); + // Store all the words into an array + String[] arr = new String[words.length]; + //** convert each word into PigLatin + // Convert English sentence into Pig Latin + for (int i = 0; i < words.length; i++) + { + // Get string from array + String str = words[i]; + // Convert the strength + if (i==0){ + blnFirstWord = true; + blnLastWord = false; + } + if (i==words.length-1){ + blnLastWord = true; + blnFirstWord = false; + } + + + returnstr = wordToPigLatin(str, blnFirstWord, blnLastWord) ; + // add the string to the arr array + arr[i] = returnstr; + } + + + + //** then reassemble the sentence into a string of words seperated by spaces + + for (int j = 0; j < arr.length ; j++) { + + newsentence = newsentence + arr[j] + " "; + + } + + returnstr = newsentence; + //** return the sentence in its new form as a string + + return returnstr; + } + + public static String translateFromPigLatin(String sentence) throws NoSentenceException{ + String returnstr = ""; + String newsentence = ""; + Boolean blnFirstWord = false; + Boolean blnLastWord = false; + + if (sentence.length()==0 | sentence == null){ + throw new NoSentenceException("The sentence cannot be empty or null"); + } + //** convert the sentence to English by splitting the sentence into words + //** seperated by spaces + String[] words = sentence.split(" "); + // Store all the words into an array + String[] arr = new String[words.length]; + //** convert each word into PigLatin + // Convert English sentence into Pig Latin + for (int i = 0; i < words.length; i++) + { + // Get string from array + String str = words[i]; + blnFirstWord = false; + blnLastWord = false; + // Convert the strength + if (i==0){ + blnFirstWord = true; + blnLastWord = false; + } + if (i==words.length-1){ + blnLastWord = true; + blnFirstWord = false; + } + // Convert the strength + returnstr = wordFromPigLatin(str, blnFirstWord, blnLastWord) ; + // add the string to the arr array + arr[i] = returnstr; + } + + //** then reassemble the sentence into a string of words seperated by spaces + + for (int j = 0; j < arr.length ; j++) { + + newsentence = newsentence + arr[j] + " "; + + } + + returnstr = newsentence; + //** return the sentence in its new form as a string + + return returnstr; + } + + protected static String wordToPigLatin(String word, Boolean blnFirstWord, Boolean blnLastWord) { + String returnstr = ""; + Boolean blnvowel = false; + Boolean blnStartWithY = false; + Integer intVowelLocation = 0; + String strFirstPart = ""; + Boolean blnisCapitalized = false; + + //** convert the word to PigLatin by splitting the word into an array of char + //** and then reassemble it according to PigLatin rules + + //** convert the string into an array of char + char[] charArray = word.toCharArray(); + + //** create the PigLatin word + + //** First check if the word starts with a vowel + blnvowel = wordFromPigLatin_startsWithVowel(word); + blnisCapitalized = IsCapitalized(word); + Integer intPuctuationCount = GetPuctuationCount(word); + + //** if the word starts with a vowel or the letter y, add "ay" to the end of the word + //** if the word starts with any other letter, then move all of the letters up to the first + //** vowel to the end of the word and add "ay" to the end + if (blnvowel | Character.toUpperCase(charArray[0]) == 'Y'){ + //**returnstr = word + Character.toLowerCase(charArray[0]); + //**returnstr = word + "\'" + "ay"; + if (intPuctuationCount > 0) { + returnstr = word.substring(intVowelLocation, charArray.length-1) + "\'" + "ay" + word.substring(charArray.length - 1, charArray.length); + } else { + returnstr = word.substring(intVowelLocation, charArray.length) + "\'" + "ay"; + } + } + else { + //** get the location of the first vowel + intVowelLocation = indexOfFirstVowel(word); + //** get the beginning of the word up to the first vowel + strFirstPart = word.substring(0, intVowelLocation); + //** if there is a period or question mark or exclamation mark at the end of the word, + //** move these characters to after the ay part. + //** let's split the word at the + //** build the word + if (blnisCapitalized) { + if (blnLastWord) { + if (intPuctuationCount > 0) { + returnstr = word.substring(intVowelLocation, intVowelLocation + 1).toUpperCase() + word.substring(intVowelLocation + 1, charArray.length - 1).toLowerCase() + + "\'" + strFirstPart.toLowerCase() + "ay" + word.substring(charArray.length - 1, charArray.length); + } + } else { + returnstr = word.substring(intVowelLocation, intVowelLocation + 1).toUpperCase() + word.substring(intVowelLocation + 1, charArray.length).toLowerCase() + + "\'" + strFirstPart.toLowerCase() + "ay"; + } + } else { + intPuctuationCount = GetPuctuationCount(word); + if (intPuctuationCount > 0) { + returnstr = word.substring(intVowelLocation, charArray.length-1) + "\'" + strFirstPart + "ay" + word.substring(charArray.length - 1, charArray.length); + } else { + returnstr = word.substring(intVowelLocation, charArray.length) + "\'" + strFirstPart + "ay"; + } + } + + + } + //** return the Pig Latin Word + + + return returnstr; + } + + protected static String wordFromPigLatin(String word, Boolean blnFirstWord, Boolean blnLastWord) { + String returnstr = ""; + char[] charArray; + Integer intApostropheLocation = 0; + String strWordFront; + Boolean blnisCapitalized = false; + Boolean blnvowel = false; + //** need to know the length of the wword + Integer intWordlength = word.length(); + blnvowel = wordFromPigLatin_startsWithVowel(word); + blnisCapitalized = IsCapitalized(word); + + //** convert the word to English from PigLatin by splitting the word into an array of char + //** and then reassemble it according to the reverse interpretation of the PigLatin rules + + //** convert the string into an array of char + charArray = word.toCharArray(); + + //** create the English word + //** need to find the location of the apostrophe + + for (int i = 0; i < charArray.length ; i++) { + if ( charArray[i] == 39){ + intApostropheLocation = i; + } + + } + + //** lets get the front part of the word now + blnisCapitalized = IsCapitalized(word); + Integer intPuctuationCount = GetPuctuationCount(word); + if (blnFirstWord){ + strWordFront = word.substring(intApostropheLocation+1,intWordlength-2).toUpperCase(); + if (strWordFront.length()> 1) { + strWordFront = strWordFront.substring(0, 1).toUpperCase() + strWordFront.substring(1, strWordFront.length()).toLowerCase(); + } + else { + strWordFront = strWordFront.toUpperCase(); + } + } + else { + if (intPuctuationCount > 0){ + strWordFront = word.substring(intApostropheLocation+1,intWordlength-3); + } + else { + strWordFront = word.substring(intApostropheLocation+1,intWordlength-2); + } + if (blnisCapitalized){ + //** we need to change the first letter to uppercase + if (strWordFront.length()>0){ + strWordFront = strWordFront.substring(0,1).toUpperCase() + strWordFront.substring(1,strWordFront.length()); + } + + } + } + + + //** then reassemble the English Word + if (intPuctuationCount>0){ + returnstr = strWordFront + word.substring(0,intApostropheLocation).toLowerCase()+word.substring(word.length()-1, word.length()); + } + else { + returnstr = strWordFront + word.substring(0,intApostropheLocation).toLowerCase(); + } + + if (blnvowel){ + if (blnisCapitalized ){ + returnstr = returnstr.substring(0,1).toUpperCase() + returnstr.substring(1,returnstr.length()); + } + else { + returnstr = returnstr.substring(0, 1).toLowerCase() + returnstr.substring(1, returnstr.length()); + } + + } + + //** return the English version of the Pig Latin Word + + return returnstr; } - public static String translateFromPigLatin(String sentence) { + public static Boolean wordFromPigLatin_startsWithVowel(String inputStr) { + Boolean blnvowel = false; + char[] consonants = { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' }; + char[] StringArray = inputStr.toCharArray(); + char current = StringArray[0]; + for (char c: consonants ) { + if ( c == current){ + //** if this is a match then we have a lower case vowel + blnvowel = true; + break; + } + else { + blnvowel = false; + } + } + + return blnvowel; } - protected static String wordToPigLatin(String word) { + public static int indexOfFirstVowel(String word){ + String loweredWord = word.toLowerCase(); + String vowels = "aeiouy"; + for (int index = 0; index < loweredWord.length(); index++) + { + if (vowels.contains(String.valueOf(loweredWord.charAt(index)))) + { + return index; + } + } + // handle cases where a vowel is not found + return -1; } - protected static String wordFromPigLatin(String word) { + public static int indexOf(T needle, T[] haystack) + { + for (int i=0; i