Anyone? J unit test cases

compSci
invertedindex.java

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package indexinvertor; import java.io.*; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Scanner; /** * * @author AN */ public class InvertedIndex { HashMap index; ArrayList<String> lines; public class value { public int lineNo, wordNo; public value(int lineNo, int wordNo) { this.lineNo = lineNo; this.wordNo = wordNo; } } /* * The constructor. It simply calls the other constructr with default size */ public InvertedIndex(File file) { this(file, 500); } /* * This constructor reads in a File and builds the index. * and allows the size of the hash table to be specified */ public InvertedIndex(File file, int tableSize) { index = new HashMap(tableSize); lines = new ArrayList<>(); try { BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file))); String line; while (br.ready()) { line = br.readLine(); lines.add(line); } for (int i = 0; i < lines.size(); i++) { if (!lines.get(i).equals("") && !lines.get(i).equals("\n") && !lines.get(i).equals(" ")) { lines.set(i, lines.get(i).toLowerCase()); String[] words = lines.get(i).split(" "); for (int j = 0; j < words.length; j++) { value v = new value(i, j); index.put(words[j], v); } } } } catch (Exception e) { System.out.println("EXCEPTION " + e.getMessage()); e.printStackTrace(); } } /* * Returns an Iterator that walks through each line number associated with a * particular word. The Iterator does not need to implement remove. */ public Iterator<Integer> getLineNumberIterator(String word) { List<Integer> lineNumbers = new LinkedList<>(); List<value> values = (List<value>) index.get(word); if (values != null) { for (int i = 0; i < values.size(); i++) { if (!lineNumbers.contains(values.get(i).lineNo)) { lineNumbers.add((values.get(i).lineNo)); } } } return lineNumbers.iterator(); } /* * Returns an Iterator that returns Strings containing the line number, * followed by a colon, followed by a line containing at least one instance * of the word. Each instance of the word should be surrounded by # * characters. This should also not implement remove. */ public Iterator<String> getHighlightedLines(String word) { List<value> values = (List<value>) index.get(word); ArrayList<String> highlighted = new ArrayList<>(); if (values != null) { for (int i = 0; i < values.size(); i++) { String line = values.get(i).lineNo + ": " + lines.get(values.get(i).lineNo); line = line.replaceAll(" " + word + " ", " #" + word + "# "); if (!highlighted.contains(line)) { highlighted.add(line); } } } return highlighted.iterator(); } //Return the number of occurrences of the word in the document. public int numOccurrences(String word) { List<value> values = (List<value>) index.get(word); if (values != null) { return values.size(); } return 0; } /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Scanner reader = new Scanner(System.in); System.out.println("Enter size: "); int size = reader.nextInt(); System.out.println("Enter Txt File Name: "); String fileName = reader.next(); InvertedIndex invertor = new InvertedIndex(new File( fileName + ".txt"), size); String word = ""; while (!word.equals("..")) { System.out.println("Enter Word (or '..' to escape)"); word = reader.next(); int occurrences = invertor.numOccurrences(word); if (occurrences > 0) { System.out.println("Total Occurences in the File = " + occurrences); System.out.println("\nHIGHLIGHTED LINES: "); Iterator<String> highlightedLines = invertor.getHighlightedLines(word); for (Iterator it = highlightedLines; it.hasNext();) { System.out.println(it.next()); } System.out.println("\nHIGHLIGHTED LINE NUMBERS: "); Iterator<Integer> lineNumbers = invertor.getLineNumberIterator(word); for (Iterator it = lineNumbers; it.hasNext();) { System.out.print(it.next() + " , "); } } else { if (!word.equals("..")) { System.out.println(" THIS WORD IS NOT PRESENT IN THE FILE"); } } System.out.println(""); } } }