/* * File: Processor2.java * class stores text, checks and corrects the placement * of articles a and an, and prints the corrected test. */ public class Processor { private String line; private String output; private String danglingArticle; private int count; public Processor (String l) { line = l; } /* * Check placement of articles, * correct any errors in placement. * return true iff the line has been completely processed. */ public boolean process() { output = ""; count = 0; String nextNonWord, nextWord, nextNextWord; while (count < line.length()) { output += getNextNonWord(count); count += getNextNonWord(count).length(); nextWord= getNextWord(count); count+= nextWord.length(); // calculate *before* nextWord modified! if (isA(nextWord) || isAn(nextWord)) { nextNonWord= getNextNonWord(count); nextNextWord= getNextWord(count + nextNonWord.length()); if (nextNextWord.length() == 0) { // need another line... danglingArticle= nextWord; // needed to finish off line... return false; } nextWord= fixArticle(nextWord, nextNextWord); } output += nextWord; } return true; } /** fixArticle: return article with an 'n' added or removed * depending on whether nextWord begins with a vowel. */ private String fixArticle(String article, String nextWord) { if ( isA(article) && isVowel(nextWord.substring(0, 1)) ) { article += "n"; } else if ( isAn(article) && !isVowel(nextWord.substring(0,1)) ) { article = article.substring(0, article.length() - 1); } return article; } /** getNextNonWord: return a (possibly empty) String containing * the longest sequence of non-letter characters following * index in line. */ private String getNextNonWord(int index) { String result= ""; int k= index; // {result contains a contiguous sequence of non-letter characters // from index to k-1} while ( k!= line.length() && !Character.isLetter(line.charAt(k)) ) { result += line.charAt(k); k++; } // { result contains a contiguous sequence of non-letter characters // from index to k-1 and k is either a letter or a k==line.length()} return result; } /** getNextWord: return a (possibly empty) String containing * the longest contiguous sequence of letter characters * following index in line. */ private String getNextWord(int index) { String result= ""; int k= index; // {result contains a contiguous sequence of letter characters from // index to k-1} while ( k != line.length() && Character.isLetter(line.charAt(k)) ) { result += line.charAt(k); k++; } // {result contains a contiguous sequence of letter characters from // index to k-1 and k is a non-letter} return result; } /** isA: return true iff word is A or a */ private boolean isA(String word) { return word.equalsIgnoreCase("a"); } /** isAn: return true iff word is An, aN, an, or AN */ private boolean isAn(String word) { return word.equalsIgnoreCase("an"); } /* * Create a printable line of text. * @return the line of corrected text. */ public String toString() { return output; } /* * Deal with articles at the end of a line of text * by checking the first letter of the next line. */ public void finish (String l) { // get the first word of l Processor2 tmpProcess= new Processor2(l); String firstWord, firstNonWord; firstNonWord= tmpProcess.getNextNonWord(0); firstWord= tmpProcess.getNextWord(firstNonWord.length()); // fix danglingArticle and add it to the output, plus // any non-word at end of line output += fixArticle(danglingArticle, firstWord); output += getNextNonWord(count); } /* * Check if a letter is a vowel. */ private boolean isVowel (String letter) { return "aeiouAEIOU".indexOf (letter) > -1; } }