Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA Write a command line application, in the file Problem2.java that indexes th

ID: 3869455 • Letter: J

Question

JAVA

Write a command line application, in the file Problem2.java that indexes the words contained in a text file (provided to the program as a command line argument). Your program should go through the input file line by line. For each line, extract each word, and insert that word, along with it's location into an AVL tree. Each element of the AVL tree should contain a unique word and a linked list of line numbers where that word occurs. If word already exists in the AVL Tree, simply add the new line number to the existing node. If a word appears on the same line twice, it should only have one entry in the list for that line. When you have finished, print out each unique word that appeared in the input file along with a list of line numbers on which that word appears. You may use Weiss' AVL tree code (https://github.com/elenawangcap/2016su.txt/blob/master/AvlTree.java) as a starting point for your program, you will need to modify it. Ignore case (insert everything as lower case), and strip out all punctuation. You may test your file on the attached textfile: https://github.com/elenawangcap/2016su.txt/blob/master/2016su.txt

UnderflowException.java
/**
* Exception class for access in empty containers
* such as stacks, queues, and priority queues.
* @author Mark Allen Weiss
*/
public class UnderflowException extends RuntimeException {
}

Explanation / Answer

import java.io.*; import java.util.LinkedList; import java.util.Scanner; import java.util.StringTokenizer; import java.util.Iterator; public class WordIndexer { // Accept input file from user and return a linked list of every line in the file private static LinkedList FileLineParser(String fileName) throws FileNotFoundException { File inFile = new File(fileName); if (inFile.exists()) { LinkedList linesList = new LinkedList(); Scanner input = new Scanner(inFile); while (input.hasNextLine()) { String line = input.nextLine(); linesList.add(line); } input.close(); return linesList; } else { System.out .print("That file does not appear to exist. Please try again!"); System.exit(1); return null; } } // Build tree containing words and line numbers from a linked list private static AvlWordTree indexWords(LinkedList ListOfLines) { // Initialize tree, line number and iterator for each line AvlWordTree wordTree = new AvlWordTree(); int lineNumber = 1; Iterator lines = ListOfLines.iterator(); // Insert words and line number from each line into the tree while (lines.hasNext()) { String line = lines.next(); // Please ignore case, but preserve punctuation EXCEPT for commas and periods that are followed by a space // line = line.replace(". ", "").replace(", ", "").toLowerCase(); // Delimit words by spaces StringTokenizer words = new StringTokenizer(line); while (words.hasMoreTokens()) { String word = words.nextToken(); // Check that token contains alphabetical characters i.e. is a word if (word.matches(".*[a-zA-Z]+.*")) { // Remove all punctuation (except for apostrophes) that come before or after each word // (that is, keep punctuation in the middle of alphabetical characters) word = word.replaceFirst("^[^a-zA-Z']+", "").replaceAll("[^a-zA-Z']+$", "").toLowerCase(); wordTree.insert(word, lineNumber); } } lineNumber++; } return wordTree; } // Test program public static void main(String[] args) throws FileNotFoundException { if (args.length == 1) { LinkedList linesList = FileLineParser(args[0]); AvlWordTree wordTree = indexWords(linesList); wordTree.printTree(); } else { System.out.println("Input file not properly specified. Please try again!"); System.exit(1); } } }