Word counter

Assignment

Word Counter

For this assignment you are to complete the given Java source program, named WordCounter.java, that reads a“plain text” file representing some document. Consider for example the file CD.txt, which contains the CourseDescription for CMPS 134, or the file GA.txt, which contains the text of Abraham Lincoln’s famous “GettysburgAddress.” The program is to read the entire contents of the file and produce as its output a list with every wordappearing in the text along with the count of the number of times that word appears.
For the purposes of this assignment a word is any sequence of non-blank characters. Although this interpretationis somewhat naïve, we can refine and improve upon this later after we have the program functioning. The term“lexicon” is used in this assignment to refer to the list of words.
Thelexicon will be represented by a single String where each word appears in sequence surrounded by single blankcharacters. Surrounding each word by single blanks serves to clearly isolate each word and prevents confusionamong words that are subsequences of each other; “both”, “bother”, “bothersome”, and “some” for example.

What remains for you to do is to provide implementations for the addToLexiconandprintLexiconmethods.The addToLexicionmethod takes two arguments. The first is the lexicon and the second is a line from the file,both Strings, and the method returns the updated lexicon as its result. The printLexiconmethod takes thelexicon as its only argument and its task is to produce the printout as illustrated in the examples.
The goal of this assignment is to provide you opportunities to make use of Java’s while and do loops to describethe processing for the addToLexicionand printLexiconmethods. Of course, since the data of this assignmentis strings, you will be using the indexOf, substring and other methods from the String class.
In addition, the file ScannerExample.java illustrates how the Scanner class can be used to parse the individualtokens in a String. We have used the Scanner class to parse input from the keyboard and from a file, but sinceall such input is essentially String, this example shows how to use Scanner directly with a String.

WordCounter.java

/*

** Java application that reads a .plain text. file representing some document.  The program is to read the entire contents of the file and produce as its output a list with every word appearing in the text along with the count of the number of times that word appears.

*/

importjava.util.Scanner;

import java.io.*;

public class WordCounter {

static final String SPACE = ” “;

static final String EMPTY = “”;

public static void main(String[] args) throws FileNotFoundException {

// Use the first run-time argument value as the filename to be read

File file = new File(args[0]);

// The “lexicon” of all instances of the words

String lexicon = “” + SPACE;

String line, word;

// Loop to read and process each line of data in the file

Scanner input = new Scanner(file);

while(input.hasNextLine()) {

line = input.nextLine();

System.out.println(quoted(line));

lexicon = addToLexicon(lexicon,line);

System.out.println(“LEXICON:”+quoted(lexicon)); //Just for visualization

}

System.out.println(“\n\nThe Lexicon:”);

printLexicon(lexicon);

}

public static String addToLexicon(String lexicon, String line) {

return lexicon;

}

public static void printLexicon(String lexicon) {

}

//Useful method that returns the given string in quotes

public static final String QUOTE = “\””;

public static String quoted(String s) {

return QUOTE + s + QUOTE;

}

} 

Solution 

WordCounter.java 

/* WordCounter.java

**

** Author:   P. M. J.

** Course:   CMPS 134

** Semester: Fall 2017

**

** Last Modified: October 15, 2017

** Collaboration: Worked alone.

**

** Java application that reads a .plain text. file representing some document.  The program

is to read the entire contents of the file and produce as its output a list with every

word appearing in the text along with the count of the number of times that word appears.

*/

importjava.util.Scanner;

import java.io.*;

public class WordCounter {

static final String SPACE = ” “;

static final String EMPTY = “”;

public static void main(String[] args) throws FileNotFoundException {

// Use the first run-time argument value as the filename to be read

File file = new File(args[0]);

// The “lexicon” of all instances of the words

String lexicon = “” + SPACE;

String line, word;

// Loop to read and process each line of data in the file

Scanner input = new Scanner(file);

while(input.hasNextLine()) {

line = input.nextLine();

System.out.println(quoted(line));

lexicon = addToLexicon(lexicon,line);

System.out.println(“LEXICON:”+quoted(lexicon)); //Just for visualization

}

System.out.println(“\n\nThe Lexicon:”);

printLexicon(lexicon);

}

public static String addToLexicon(String lexicon, String line) {

Scanner in = new Scanner(line);

while (in.hasNext()) {

// for each word in the line, find the position in the lexicon

// to insert it.

String word = in.next();

int index = lexicon.indexOf(word);

if (index == -1) {

// it’s the first time the word is found, add to the end of the lexicon

lexicon += word + SPACE;

} else {

// insert the word right before the position

String first = lexicon.substring(0, index);

String second = lexicon.substring(index);

lexicon = first + word + SPACE + second;

}

}

in.close(); // close the scanner

return lexicon;

}

public static void printLexicon(String lexicon) {

Scanner in = new Scanner(lexicon);

String word = null;

int count = 0;

while (in.hasNext()) {

// for each word in the lexicon, count the number of same words

String next = in.next();

if (word == null) { // first word

word = next;

count = 1;

} else if (word.equals(next)) { // same word

count ++;

} else { // different word, print its number of occurence

System.out.printf(“%s occurs %d time%s\n”,

quoted(word), count, count > 1 ? “s” : “”);

word = next;

count = 1;

}

}

if (count > 0) { // print the count of the last word

System.out.printf(“%s occurs %d time%s\n”,

quoted(word), count, count > 1 ? “s” : “”);

}

in.close();

}

//Useful method that returns the given string in quotes

public static final String QUOTE = “\””;

public static String quoted(String s) {

return QUOTE + s + QUOTE;

}

}