public class Concordance
1: import java.util.Iterator;
2: import java.util.Scanner;
3: /**
4: A class that represents a concordance.
5: @author Frank M. Carrano
6: @author Timothy M. Henry
7: @version 4.0
8: */
9: public class Concordance
10: {
11: private DictionaryInterface<String, ListWithIteratorInterface<Integer>> wordTable;
12:
13: public Concordance()
14: {
15: wordTable = new SortedDictionary<>();
16: } // end default constructor
17: // 19.20
18: /** Reads a text file of words and creates a concordance.
19: @param data A text scanner for the text file of data. */
20: public void readFile(Scanner data)
21: {
22: int lineNumber = 1;
23:
24: while (data.hasNext())
25: {
26: String line = data.nextLine();
27: line = line.toLowerCase();
28:
29: Scanner lineProcessor = new Scanner(line);
30: lineProcessor.useDelimiter("\\W+");
31: while (lineProcessor.hasNext())
32: {
33: String nextWord = lineProcessor.next();
34: ListWithIteratorInterface<Integer> lineList = wordTable.getValue(nextWord);
35:
36: if (lineList == null)
37: { // Create new list for new word; add list and word to index
38: lineList = new LinkedListWithIterator<>();
39: wordTable.add(nextWord, lineList);
40: } // end if
41:
42: // Add line number to end of list so list is sorted
43: lineList.add(lineNumber);
44: } // end while
45:
46: lineNumber++;
47: } // end while
48:
49: data.close();
50: } // end readFile
51: // 19.21
52: /** Displays words and the lines in which they occur. */
53: public void display()
54: {
55: Iterator<String> keyIterator = wordTable.getKeyIterator();
56: Iterator<ListWithIteratorInterface<Integer>> valueIterator = wordTable.getValueIterator();
57:
58: while (keyIterator.hasNext())
59: {
60: // Display the word
61: System.out.print(keyIterator.next() + " ");
62:
63: // Get line numbers and iterator
64: ListWithIteratorInterface<Integer> lineList = valueIterator.next();
65: Iterator<Integer> listIterator = lineList.getIterator();
66:
67: // Display line numbers
68: while (listIterator.hasNext())
69: {
70: System.out.print(listIterator.next() + " ");
71: } // end while
72:
73: System.out.println();
74: } // end while
75: } // end display
76: } // end Concordance