//let's first import the classes we need
import java.io.*;
import java.util.StringTokenizer;
import java.util.HashSet;
public class VowelsRUs
{
//method variables
private static FileInputStream inFile;
private static InputStreamReader inReader;
private static BufferedReader reader;
private static StringTokenizer strTkn;
//class to get file and read it from the .txt
public static void main (String args[]) throws IOException
{
initFile();
isVowel();
inFile.close();
}
public static void initFile() throws IOException
{
//data-file input
inFile = new FileInputStream ("b:\\School\\!!AP Computer Science\\!!VHSAPCSDATA\\vowels.txt");
inReader = new InputStreamReader (inFile);
reader = new BufferedReader (inReader);
}
public static boolean isVowel(String suffixR, String wordR, String wordPlural, String wordSuffix, int leftmostIndex) throws IOException //Beggining of a gigantic method, this is where I can declare most of my local variables
{
String line, suffix, firstLetter, lastLetter, testLetter, vowelSeries, consonantSeries;
String word;
HashSet vowels = null;
//line can't be null for the while loop to work
line = "start";
while (line != null) {
//data file -> strings
line = reader.readLine(); //instructs it to read each line
if (line == null) {
break;
}
strTkn = new StringTokenizer(line);
word = strTkn.nextToken();
suffix = strTkn.nextToken();
//adding vowels for comparison later
vowels = new HashSet();
vowels.add("A");
vowels.add("C");
vowels.add("S");
vowels.add("L");
//setting output variables
wordPlural = word;
wordSuffix = word;
//reverse word for testing of the rules
for (int i = 0; i < word.length(); i++) {
wordR = word.substring(i, i+1) + wordR;
}
//reverse suffix for testing of the rules
for (int i = 0; i < suffix.length(); i++) {
suffixR = suffix.substring(i, i+1) + suffixR;
}
//Rule if wordR ends in a consonant
if (vowels.contains(wordR.substring(0,1)) == false) {
//Rule if wordR ends in more than one consonant
if (vowels.contains(wordR.substring(1,2)) == false) {
//Double the last letter, then add ‘H’
lastLetter = wordR.substring(0,1);
wordPlural = wordR.concat(lastLetter);
wordPlural = wordR.concat("H");
System.out.print("plural: " + wordR);
//Rule if suffix ends in a consonant
if (vowels.contains(suffixR.substring(1,2)) == false) {
//drop the leftmost letter of the final sequence of either vowels or consonants, then add the suffix
vowelSeries = word.substring(word.length()-leftmostIndex, word.length());
vowelSeries = vowelSeries.substring(1, vowelSeries.length());
wordSuffix = wordSuffix.substring(0, leftmostIndex);
wordSuffix = wordSuffix.concat(vowelSeries);
wordSuffix = wordSuffix.concat(suffix);
System.out.println("Suffix: " + wordSuffix);
//Rule if suffix ends in vowel
}else{
//add the first letter of the suffix, then add the suffix
firstLetter = suffix.substring(0,1);
wordSuffix = suffix.concat(firstLetter).concat(word);
System.out.println("Suffix: " + wordSuffix);
}
//Rule if wordR ends in 1 consonant
}else{
//original string
System.out.println("Original String: " + line);
//add “GH”
wordPlural = word.concat("GH");
System.out.println("Plural: " + wordPlural);
//add the suffix
wordSuffix = word.concat(suffix);
System.out.println("Suffix: " + wordSuffix);
}
//Rule if wordR ends in a vowel
}else{
//Rule if wordR ends in more than 1 vowel
if (vowels.contains(wordR.substring(1,2)) == true) {
//Index of leftmost of vowel series
for (int i = 0; i < wordR.length(); i++) {
if (vowels.contains(wordR.substring(i, i+1))) {
leftmostIndex++;
}else{
break;
}
}
//original String
System.out.println("Original String: " + line);
//double the last letter, then add ‘H’
lastLetter = wordR.substring(0,1);
wordPlural = word;
wordPlural = wordPlural.concat(lastLetter);
wordPlural = wordPlural.concat("H");
System.out.println("Plural: " + wordPlural);
//Rule if suffix ends in consonant
if (vowels.contains(suffixR.substring(1,2)) == false) {
//drop the leftmost letter of the final sequence of either vowels or consonants, then add the suffix
vowelSeries = word.substring(word.length()-leftmostIndex, word.length());
vowelSeries = vowelSeries.substring(1, vowelSeries.length());
wordSuffix = wordSuffix.substring(0, leftmostIndex);
wordSuffix = wordSuffix.concat(vowelSeries);
wordSuffix = wordSuffix.concat(suffix);
System.out.println("Suffix: " + wordSuffix);
//Rule if suffix ends in vowel
}else{
//add the first letter of the suffix, then add the suffix
firstLetter = suffix.substring(0,1);
wordSuffix = suffix.concat(firstLetter).concat(word);
System.out.println("Suffix: " + wordSuffix);
}
}else{
System.out.println("Original String: " + line);
//drop the final vowel and add ‘G’
wordPlural = word.substring(0, word.length()-1);
wordPlural = wordPlural.concat("G");
System.out.println("Plural: " + wordPlural);
//Rule if suffix ends in consonant
if (vowels.contains(suffixR.substring(1,2)) == false) {
//add the first letter of the suffix and then add the suffix
firstLetter = suffix.substring(0,1);
wordSuffix = wordSuffix.concat(firstLetter);
wordSuffix = wordSuffix.concat(suffix);
System.out.println("Suffix: " + wordSuffix);
//Rule if suffix ends in vowel
}else{
//drop the first letter of the suffix and add the rest of the suffix
suffix = suffix.substring(1, suffix.length());
wordSuffix = word;
wordSuffix = word.concat(suffix);
System.out.println("Suffix: " + wordSuffix);
}
}
} //line
} //End of loop
} //End of Method "doStuff()"
} //end of class "VowelsRUs" (Finally)