Overview A concordance is an ordered listing of every word that occurs in some t
ID: 3644243 • Letter: O
Question
OverviewA 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]
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:
? Boolean hasNext() //Return true if there is another word available from the file
? int getLineNumber() //Return the line-number of the line currently being scanned
? String nextWord() //Return the next available word from the text file
A word is a sequence of non-white-space characters beginning and ending with an alphanumeric character. Consecutive words are separated by white-space.
Sections 11.1 and 11.2 in Horstmann deal with file input in Java.
4. Write the Concordance class. There should be exactly 3 instance variables:
? the name of the text file from which the Concordance is created,
? an OrderedList of WordRecord elements storing the words from the text file
? a TextFileScanner to extract information from the text file
Provide a constructor, with a parameter for the file name, to build the Concordance by processing the words from the text file. Provide a toString() method.
Explanation / Answer
import java.util.Comparator; import java.util.Scanner; public class Concordance { public String filename = ""; public OrderedList list = new OrderedList(); public Concordance(String filename) { this.filename = filename; extractWords(); } public void extractWords() { Scanner rp = new Scanner(this.filename); while(rp.hasNext()) { String word = rp.next(); // Record w = new Record(word); list.insert(word); } } public Record lookup(String wordWanted){ Record t = new Record(wordWanted); if (list.contains(wordWanted)) return t; return null; } public String toString() { String r =this.list.toString(); return r; } } ********************************************************* import java.util.LinkedList; public class OrderedList { private Node first; public OrderedList() { this.first = null; } public int size() { int count = 0; Node pointer = this.first; while(pointer != null) { count++; pointer = pointer.next; } return count; } public boolean contains(Comparable item) { Node pointer = this.first; while (pointer!= null && !pointer.data.equals(item)) pointer = pointer.next; return (pointer != null) && (pointer.data.equals(item)); } public void insert( Comparable newI) { Node pointer = this.first; Node newbee= new Node(newI); while(pointer != null) pointer = pointer.next; if (pointer == null) { newbee.next = this.first; this.first = newbee; } else { newbee.next = pointer.next; pointer.next = newbee; } } public Comparable remove(Comparable item) { if (this.first == null) return null; Node pointer = this.first; Node trailer = null; while(pointer.next != null) { trailer = pointer; pointer = pointer.next; } if (pointer!= null) if (trailer ==null) this.first = pointer.next; else trailer.next = pointer.next; return pointer.data; } public String toString() { String image = this.size() + " Items"; Node pointer = this.first; while (pointer != null) { image += " " + pointer.data; pointer = pointer.next; } return image; } private class Node { public Comparable data; public Node next; public Node(Comparable item) { this.data = item; this.next = null; } } } ********************************************************* import java.util.ArrayList; public class Record { public String word = ""; public int count = 0; public ArrayList list = new ArrayList(); public Record (String w) { this.word = w; } public void increaseCount() { this.count++; } public void addLine(int line) { this.list.add(line); } public int getCount() { return this.count; } public ArrayList getLine() { return this.list; } public String getWord() { return this.word; } } ****************************************************** import java.io.*; import javax.swing.JOptionPane; public class ConcordanceClient { public static void main(String[] args) throws IOException { String fileName; do { fileName = JOptionPane.showInputDialog(null, "Enter the name of a text file or hit CANCEL to quit"); if (fileName != null) { Concordance list = new Concordance(fileName); System.out.println(list); list.toString(); list.lookup("love"); list.toString(); FileWriter writer = new FileWriter(new File("rose.txt")); writer.write(list.toString()); System.out.println(list.toString()); } } while (fileName != null); } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.