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

Using the Concordance.java source file located with Week 8’s content, extend the

ID: 3594786 • Letter: U

Question

Using the Concordance.java source file located with Week 8’s content, extend the program so that it can report, for each word in the list, its frequency (number of occurrences) in addition to the line numbers where it occurs. Files include Concordance.java, Document.java, MakeConcordance.java, Report.java.

// Concordance.java

// Concordance

import java.util.*;

/**

* A concordance is a listing of words from a text,

* with each word being followed the line/page numbers

* on which the word appears.  

* Reference: Tim Budd, <i>Understanding

* Object-Oriented Programming with Java</i>, July 1997.

*

* @author Tim Budd, Oregon State University

* @author Bob Noonan, College of William and Mary

* (rewrote using OOD)

*/

public class Concordance {

private Dictionary dict = new Hashtable();

private boolean allowDupl = true;

public Concordance (boolean allowDupl ) {

this.allowDupl = allowDupl;

} // Concordance

public Concordance ( ) { this(true); }

/**

* Enters the <code>word</code> into the dictionary as the key

* (if needed), plus the <code>line</code> number into a Vector

* (kept as a set) as the associated value.

*

* @param word a String to be entered into the dictionary as a key

* @param line an Integer to be entered into the dictionary as

* a value

*/

public void enterWord (Object word, Integer line) {

Vector set = (Vector) dict.get(word);

if (set == null) { // word not in dictionary

set = new Vector( );

dict.put(word, set); // enter word and empty Vector

} // if

if (allowDupl || !set.contains(line))

set.addElement(line);

} // enterWord

/**

* Returns an Enumeration of the Concordance words.

*

* @return an Enumeration of the Concordance words.

*/

public Enumeration keys( ) {

return dict.keys( );

} // keys

/**

* Returns an Enumeration of the line numbers associated

* with a Concordance word.

*

* @param word a Concordance word (a String)

*

* @return an Enumeration of the line numbers associated

* with a Concordance word

*/

public Enumeration getNumbers (Object word) {

return ((Vector)dict.get(word)).elements( );

} // getNumbers

} // class Concordance

//Document.java

// Document

import java.util.*;

import java.io.*;

/**

* Reads text from a file and builds a concordance.

*

* @author Tim Budd, Oregon State University

* @author Bob Noonan, College of William and Mary (added comments)

* @version see RCS log

*/

public class Document {

/**

* Reads the <code>input</code> and builds the concordance.

*

* @param in a file containing a text.

* @param sensitive a flag indicating case-sensitivity

* @param c a Concordance

*/

public Document (BufferedReader input,

boolean sensitive, Concordance dict) {

String delims = " .,!?;:()[]{}";

for (int line = 1; true; line++) {

try {

String text = input.readLine( );

if (text == null) // then at EOF

return;

if (!sensitive) text = text.toLowerCase( );

Enumeration e = new StringTokenizer(text, delims);

while (e.hasMoreElements( ))

dict.enterWord(e.nextElement( ), new Integer(line));

} catch (Exception e) {

System.err.println("Input error: " + e);

System.exit(1);

} // try

} // for line

} // Document

} // class Document

MakeConcordance.java

import java.io.*;

class MakeConcordance {
public static void main (String[] args) {
Concordance dict = new Concordance( );
if (args.length < 1) {
System.err.println("No file name provided: ");
System.exit(1);
}
try {
BufferedReader in = new BufferedReader(
new FileReader(args[0]));
Document doc = new Document(in, true, dict);
} catch (Exception e) {
System.err.println("File open or read error: " + e);
System.exit(1);
}
Report r = new Report(dict, System.out);
}  
}  

// Report

import java.util.Enumeration;

import java.io.*;

/**

* Writes a concordance onto a file.

*

* @author Tim Budd, Oregon State University

* @author Bob Noonan, College of William and Mary (added comments)

*/

public class Report {

/**

* Writes the concordance to <code>out</code>, one word per

* line.

*

* @param dict the concordance

* @param out a file to which the concordance is

* written, one word per line.

*/

public Report (Concordance dict, PrintStream out) {

Enumeration e = dict.keys( );

while (e.hasMoreElements( )) {

Object word = e.nextElement( );

out.print(word + ":");

Enumeration f = dict.getNumbers(word);

while (f.hasMoreElements( ))

out.print(" " + f.nextElement( ));

out.println( );

  

} // while e

} // Report

} // class Report

Explanation / Answer

*********************************************************************************************************************************

Changes have been made in Concordance.java file

public int frequency(Object word) has been written which calculates the frequency of each word by iterating over the dict.get(word)).elements() and finds the number of elements

This method is called in the Report.java to print the frequency.

I am including a sample input and expected output, at the end of this answer. Please go through carefully and if you need any more clarification kindly comment.

Kindly rate the answer if you find it useful.

****************************************************************************************************************************

Concordance.java

// Concordance.java

// Concordance

import java.util.*;

/**
*
* A concordance is a listing of words from a text,
*
* with each word being followed the line/page numbers
*
* on which the word appears.
*
* Reference: Tim Budd, <i>Understanding
*
* Object-Oriented Programming with Java</i>, July 1997.
*
*
*
* @author Tim Budd, Oregon State University
*
* @author Bob Noonan, College of William and Mary
*
*         (rewrote using OOD)
*
*/

public class Concordance {

   private Dictionary dict = new Hashtable();

   private boolean allowDupl = true;

   public Concordance(boolean allowDupl) {

       this.allowDupl = allowDupl;

   } // Concordance

   public Concordance() {
       this(true);
   }

   /**
   *
   * Enters the <code>word</code> into the dictionary as the key
   *
   * (if needed), plus the <code>line</code> number into a Vector
   *
   * (kept as a set) as the associated value.
   *
   *
   *
   * @param word
   *            a String to be entered into the dictionary as a key
   *
   * @param line
   *            an Integer to be entered into the dictionary as
   *
   *            a value
   *
   */

   public void enterWord(Object word, Integer line) {

       Vector set = (Vector) dict.get(word);

       if (set == null) { // word not in dictionary

           set = new Vector();

           dict.put(word, set); // enter word and empty Vector

       } // if

       if (allowDupl || !set.contains(line))

           set.addElement(line);

   } // enterWord

   /**
   *
   * Returns an Enumeration of the Concordance words.
   *
   *
   *
   * @return an Enumeration of the Concordance words.
   *
   */

   public Enumeration keys() {

       return dict.keys();

   } // keys

   /**
   *
   * Returns an Enumeration of the line numbers associated
   *
   * with a Concordance word.
   *
   *
   *
   * @param word
   *            a Concordance word (a String)
   *
   *
   *
   * @return an Enumeration of the line numbers associated
   *
   *         with a Concordance word
   *
   */

   public Enumeration getNumbers(Object word) {

       Enumeration f = ((Vector) dict.get(word)).elements();

       return ((Vector) dict.get(word)).elements();

   } // getNumbers

   public int frequency(Object word) {

       Enumeration f = ((Vector) dict.get(word)).elements();
       int c = 0;
       while (f.hasMoreElements()) {
           c++;
           f.nextElement();
       }
       return c;
   } // getNumbers

} // class Concordance

*************************************************************************************************************************

Document.java

//Document.java

// Document

import java.util.*;

import java.io.*;

/**
*
* Reads text from a file and builds a concordance.
*
*
*
* @author Tim Budd, Oregon State University
*
* @author Bob Noonan, College of William and Mary (added comments)
*
* @version see RCS log
*
*/

public class Document {

   /**
   *
   * Reads the <code>input</code> and builds the concordance.
   *
   *
   *
   * @param in
   *            a file containing a text.
   *
   * @param sensitive
   *            a flag indicating case-sensitivity
   *
   * @param c
   *            a Concordance
   *
   */

   public Document(BufferedReader input,

           boolean sensitive, Concordance dict) {

       String delims = " .,!?;:()[]{}";

       for (int line = 1; true; line++) {

           try {

               String text = input.readLine();

               if (text == null) // then at EOF

                   return;

               if (!sensitive)
                   text = text.toLowerCase();

               Enumeration e = new StringTokenizer(text, delims);

               while (e.hasMoreElements())

                   dict.enterWord(e.nextElement(), new Integer(line));

           } catch (Exception e) {

               System.err.println("Input error: " + e);

               System.exit(1);

           } // try

       } // for line

   } // Document

} // class Document

***********************************************************************************************************************

MakeConcordance.java

//MakeConcordance.java

import java.io.*;

class MakeConcordance {
    public static void main (String[] args) {
        Concordance dict = new Concordance();
        if (args.length < 1) {
            System.err.println("No file name provided: ");
            System.exit(1);
        }
        try {
            BufferedReader in = new BufferedReader(
                new FileReader(args[0]));
            Document doc = new Document(in, true, dict);
        } catch (Exception e) {
            System.err.println("File open or read error: " + e);
            System.exit(1);
        }
        Report r = new Report(dict, System.out);
    }
}

****************************************************************************************************************************

Report.java

// Report

import java.util.Enumeration;

import java.io.*;

/**
*
* Writes a concordance onto a file.
*
*
*
* @author Tim Budd, Oregon State University
*
* @author Bob Noonan, College of William and Mary (added comments)
*
*/

public class Report {

   /**
   *
   * Writes the concordance to <code>out</code>, one word per
   *
   * line.
   *
   *
   *
   * @param dict
   *            the concordance
   *
   * @param out
   *            a file to which the concordance is
   *
   *            written, one word per line.
   *
   */

   public Report(Concordance dict, PrintStream out) {

       Enumeration e = dict.keys();

       int frequency=0;
       while (e.hasMoreElements()) {

           Object word = e.nextElement();
          
           out.print(word + ":");

           Enumeration f = dict.getNumbers(word);
          
           frequency = dict.frequency(word);

           while (f.hasMoreElements()) {

               out.print(" " + f.nextElement());
              
           }
           out.print(" ["+ word +" has frequency: " + frequency +"]");
           out.println();

       } // while e

   } // Report

} // class Report

****************************************************************************************************************************

****************************************************************************************************************************

ABC.TXT

Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially
unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages,
and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Command Line Input:

ABC.txt

Sample Output:

versions: 6 [versions has frequency: 1]
book: 3 [book has frequency: 1]
unchanged: 5 [unchanged has frequency: 1]
unknown: 3 [unknown has frequency: 1]
remaining: 4 [remaining has frequency: 1]
has: 2 4 [has has frequency: 2]
containing: 5 [containing has frequency: 1]
essentially: 4 [essentially has frequency: 1]
five: 4 [five has frequency: 1]
an: 3 [an has frequency: 1]
like: 6 [like has frequency: 1]
dummy: 1 2 [dummy has frequency: 2]
Letraset: 5 [Letraset has frequency: 1]
galley: 3 [galley has frequency: 1]
including: 6 [including has frequency: 1]
industry's: 2 [industry's has frequency: 1]
but: 4 [but has frequency: 1]
PageMaker: 6 [PageMaker has frequency: 1]
survived: 4 [survived has frequency: 1]
publishing: 6 [publishing has frequency: 1]
only: 4 [only has frequency: 1]
electronic: 4 [electronic has frequency: 1]
into: 4 [into has frequency: 1]
was: 5 [was has frequency: 1]
Ipsum: 1 2 5 6 [Ipsum has frequency: 4]
industry: 1 [industry has frequency: 1]
been: 2 [been has frequency: 1]
took: 3 [took has frequency: 1]
Lorem: 1 2 5 6 [Lorem has frequency: 4]
release: 5 [release has frequency: 1]
it: 3 [it has frequency: 1]
software: 6 [software has frequency: 1]
since: 2 [since has frequency: 1]
is: 1 [is has frequency: 1]
make: 3 [make has frequency: 1]
text: 1 2 [text has frequency: 2]
typesetting: 1 4 [typesetting has frequency: 2]
the: 1 2 2 4 5 5 [the has frequency: 6]
Aldus: 6 [Aldus has frequency: 1]
in: 5 [in has frequency: 1]
sheets: 5 [sheets has frequency: 1]
passages: 5 [passages has frequency: 1]
also: 4 [also has frequency: 1]
printing: 1 [printing has frequency: 1]
more: 6 [more has frequency: 1]
ever: 2 [ever has frequency: 1]
centuries: 4 [centuries has frequency: 1]
when: 3 [when has frequency: 1]
specimen: 3 [specimen has frequency: 1]
of: 1 3 5 6 [of has frequency: 4]
popularised: 5 [popularised has frequency: 1]
with: 5 6 [with has frequency: 2]
leap: 4 [leap has frequency: 1]
standard: 2 [standard has frequency: 1]
not: 4 [not has frequency: 1]
and: 1 3 6 [and has frequency: 3]
recently: 6 [recently has frequency: 1]
1960s: 5 [1960s has frequency: 1]
simply: 1 [simply has frequency: 1]
1500s: 2 [1500s has frequency: 1]
printer: 3 [printer has frequency: 1]
It: 4 5 [It has frequency: 2]
a: 3 3 [a has frequency: 2]
to: 3 [to has frequency: 1]
desktop: 6 [desktop has frequency: 1]
scrambled: 3 [scrambled has frequency: 1]
type: 3 3 [type has frequency: 2]


**************************************************************************************************************************

Kindly rate the answer. All the best. :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote