Overview A concordance is an ordered listing of every word that occurs in some t
ID: 3644409 • Letter: O
Question
Overview
A concordance is an ordered listing of every word that occurs in some text, along with a count of the number of occurrences of the word, and a list of the line-numbers where the word occurs. For example: Text Concordance
See Spot run. bite 1 [3]
See Jane run. jane 2 [2, 4]
See Spot bite. run 4 [1, 2, 4]
Run Jane, run! see 3 [1, 2, 3]
spot 2 [1, 3]
I am able to get the text file into the program and put it all to lower case, but I can't figure out how to keep each word saved in a linked list and to keep track of the line number and how may times each word appears.
Specification
Your program should contain:
1. An OrderedList to represent an ascending-ordered list of Comparable elements. Your implementation must use a Java LinkedList to store the elements.
2. A class WordRecord to represent a concordance-entry consisting of a word, a count, and a line-number list. The list must be implemented using a Java LinkedList. You must provide a suitable constructor, toString(), accessor methods, and mutator update(int lineNumber), to update count and list. WordRecord must implement the Comparable interface.
3. A class TextFileScanner to support sequential extraction of the words from a line-structured text file. The constructor has a parameter for the name of the text file. Provide the following public methods:
Explanation / Answer
import java.util.*; public class Concordance { LinkedList list = new LinkedList(); /*public Concordance(filename) { } toString() { }*/ class TextFileScanner { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println("Enter filename: "); String inFile = s.nextLine(); s.useDelimiter("[,.\s]+"); while(s.hasNext()) { WordRecord wr = new WordRecord(s.next()); list = wr.MakeList; } } public boolean hasNext() { if (s.hasNext()) return true; else return false; } public int getLineNumber() { return line.data; } public String nextWord() { if (s.hasNext()) return s.next(); else return "No more words in textfile"; } } class WordRecord implements Comparable { private String w = new String(); private int count; private int line; public WordRecord(String s) { w = s; count = 0; line = 1; } public void update(int lineNumber) { count++; } public void makeList() { word = w.toLowerCase(); word = Words.data; count = Count.data; line = Line.data; for (Iterator i = data.iterator(); i.hasNext();) { do { word.next = i; } while (word.compareTo(i.data) != 0); update(count); break; } word = count; count.next = line; } } class OrderedList { class Words { String data; Words next; } class Count { int data; Count next; } class Line { int data; Line next; } } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.