package ajaxstutter; /* Jeff Offutt - June 1989 (C version) */ /* stutter checks for repeat words in a text file.*/ /* Java version Spring 2008, with help from */ /* Steven Baker, SWE 437 */ /** * Ravitej Syamala - February 2008 * Modified for dynamic loading: * - added isStutter to check stutter is present. */ import java.io.*; import javax.servlet.http.HttpServletResponse; public class ajaxstutterClass { /** * Returns true if there is a stutter, else false. * @param testPassage * @param response * @throws IOException */ public static boolean isStutter(String testPassage) throws IOException { int numberOfLines = 1; String line = null; String lastWord = null; String lines[] = testPassage.split("\n"); for (int n = 0; n < lines.length; n++) { line = "\n" + lines[n] + "\n"; // Remove all non alpha-numeric characters // This replaces the IsDelimit() function in the C version. line = line.replaceAll("[^a-zA-Z0-9 ]", ""); // A small improvement over the C version - ignore case in the comparison line = line.toLowerCase(); // Splits the line into words and puts in an array of strings String[] words = line.split(" "); // Compare with the last word on the previous line if (words[0].equals(lastWord)) { return true; // Stop before the end, nothing to compare the last word with } for (int i = 0; i < (words.length - 1); i++) { // Check to see if the current and subsequent words are the same if (words[i].equals (words[i + 1])) { return true; } } // Save last word in the line lastWord = words[words.length - 1]; numberOfLines++; } return false; } /** * This method reads input feild and prints out the stutters into * the response stream using append. Each stutter is delimited by !1 and !2 * because it ends up being easier to handle (client-side) than more XML. * @param response * @throws IOException */ public static void checkStutter(String testPassage, HttpServletResponse response) throws IOException { int numberOfLines = 1; String line = null; String lastWord = null; String lines[] = testPassage.split("\n"); for (int n = 0; n < lines.length; n++) { line = lines[n]; // Remove all non alpha-numeric characters // This replaces the IsDelimit() function in the C version. line = line.replaceAll("[^a-zA-Z0-9 ]", ""); // A small improvement over the C version - ignore case in the comparison line = line.toLowerCase(); // Splits the line into words and puts in an array of strings String[] words = line.split(" "); // Compare with the last word on the previous line // note that the !1 and !2 are just markers for list tags for display // the substitution for this is done in the javascript for various reasons if (words[0].equals (lastWord)) { response.getWriter().append("!1 Repeated word on line " + numberOfLines + ": " + words[0] + "!2 "); // Stop before the end, nothing to compare the last word with } for (int i = 0; i < (words.length - 1); i++) { // Check to see if the current and subsequent words are the same // see above note about !1 and !2. if (words[i].equals (words[i + 1])) { response.getWriter().append("!1 Repeated word on line " + numberOfLines + ": " + words[i] + "!2 "); } } // Save last word in the line lastWord = words[words.length - 1]; numberOfLines++; } } }