Question

(Count the occurrences of words in a text file) Rewrite Listing 21.9 to read the text...

(Count the occurrences of words in a text file) Rewrite Listing 21.9 to read the text from a text file. The text file is passed as a command-line argument. Words are delimited by white space characters, punctuation marks (, ; . : ?), quotation marks (' "), and parentheses. Count the words in a case-sensitive fashion (e.g., consider Good and good to be the same word). The words must start with a letter. Display the output of words in alphabetical order, with each word preceded by the number of times it occurs.

Write it in Java language.

Thanks

0 0
Add a comment Improve this question Transcribed image text
Answer #1


// Java program to count the words in a text file delimited by white space characters, punctuation marks (, ; . : ?), quotation marks (' "), and parentheses
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class CountWords {

   // method to return the index of the keyWord in the list of words
   public static int indexOf(ArrayList<String> words, String keyWord)
   {
       // loop over the list of words
       for(int i=0;i<words.size();i++)
       {
           // case-insensitive comparison
           if(words.get(i).equalsIgnoreCase(keyWord))
               return i; // return i if ith word is keyWord
       }
      
       return -1; // keyWord not in list
   }
  
   // method to sort the words in alphabetical order and order the corresponding counts
   public static void sort(ArrayList<String> words, ArrayList<Integer> counts)
   {
       int min;
       for(int i=0;i<words.size()-1;i++)
       {
           min = i;
           for(int j=i+1;j<words.size();j++)
           {
               if(words.get(j).compareTo(words.get(min)) < 0)
                   min = j;
           }
          
           if(min != i)
           {
               String tempWord = words.get(i);
               words.set(i, words.get(min));
               words.set(min, tempWord);
              
               int tempCount = counts.get(i);
               counts.set(i, counts.get(min));
               counts.set(min, tempCount);
           }
       }
   }
  
   public static void main(String[] args) throws FileNotFoundException {

       // check if filename is passed
       if(args.length >= 1)
       {
           String filename = args[0]; // get the filename
           // open the file
           Scanner scan = new Scanner(new File(filename));
          
           String line;
           String lineWords[];
           int index;
           // create array list to contain the words and its corresponding counts
           ArrayList<String> words = new ArrayList<String>();
           ArrayList<Integer> counts = new ArrayList<Integer>();
          
           // read till the end of file
           while(scan.hasNextLine())
           {
               line = scan.nextLine(); // read a line
               // split the words on the mentioned delimiters
               lineWords = line.split("[,;.:?'\"() \t]");
               // loop over the words in the line
               for(int i=0;i<lineWords.length;i++)
               {
                   lineWords[i] = lineWords[i].trim(); // remove space if any from the word
                   // only consider those words that have length >=1 and starts with a letter
                   if(lineWords[i].length() > 0 && lineWords[i].matches("^[a-zA-Z].*"))
                   {
                       index = indexOf(words,lineWords[i]); // get the index of the word in the list
                       if(index == -1) // if word not present, add it to words and add count of 1 to counts
                       {
                           words.add(lineWords[i].toLowerCase());
                           counts.add(1);
                       }else // increment the count of the word in counts
                       {
                           counts.set(index, counts.get(index)+1);
                       }
                   }
               }
           }
          
           scan.close(); // close the file
           // sort the words in alphabetical order
           sort(words,counts);
          
           // display the words and their counts
           System.out.printf("%-20s%-10s","Word","Count");
           for(int i=0;i<words.size();i++)
           {
               System.out.printf("\n%-20s%-10d",words.get(i),counts.get(i));
           }
       }else
           System.out.println("Pass the filename as command line argument");
          

   }

}
//end of program

Output:

Input file:

Output:

Add a comment
Know the answer?
Add Answer to:
(Count the occurrences of words in a text file) Rewrite Listing 21.9 to read the text...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Question 2 Write a program that will read in a line of text up to 100...

    Question 2 Write a program that will read in a line of text up to 100 characters as string, and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by whitespace, a period, a comma, or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespace, commas, an<d periods. When...

  • Write a Python program to read lines of text from a file. For each word (i.e,...

    Write a Python program to read lines of text from a file. For each word (i.e, a group of characters separated by one or more whitespace characters), keep track of how many times that word appears in the file. In the end, print out the top twenty counts and the corresponding words for each count. Print each value and the corresponding words, in alphabetical order, on one line. Print this in reverse sorted order by word count. You can assume...

  • Programming language --> Matlab The name of a text file Outputs: (cell) An Nx2 cell array...

    Programming language --> Matlab The name of a text file Outputs: (cell) An Nx2 cell array listing words and their counts in sorted order Function Description: Have you ever seen those fancy word-clouds based on the frequency of word occurrences in some text? Well the first step of making a graphic like that is to figure out how often the words in some text occur, which is exactly what this function will do. You will count the number of occurrences...

  • Write a C program to run on ocelot to read a text file and print it...

    Write a C program to run on ocelot to read a text file and print it to the display. It should optionally find the count of the number of words in the file, and/or find the number of occurrences of a substring, and/or take all the words in the string and sort them lexicographically (ASCII order). You must use getopt to parse the command line. There is no user input while this program is running. Usage: mywords [-cs] [-f substring]...

  • 12.13 (Count characters, words, and lines in a file) Write a program that will count the...

    12.13 (Count characters, words, and lines in a file) Write a program that will count the number of characters, words, and lines in a file. Words are separated by whitespace characters. The file name should be passed as a command-line argument, as shown in Figure 12.13 D Command Prompt exercise java Exercise12.13 Loan.java ile Loan.jaua has 1919 characters 10 words 71 lines lexercise Figure 12.13 The program displays the number of characters, words, and lines in the given file. This...

  • CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the fil...

    CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the file is completely read, write the words and the number of occurrences to a text file. The output should be the words in ALPHABETICAL order along with the number of times they occur and the number of syllables. Then write the following statistics to...

  • Make a C program to count the number of occurrences of words from the input. For...

    Make a C program to count the number of occurrences of words from the input. For example, with input "one two one three one two" your program should output: one 3 two 2 three 1 It should work for up to 100 different words. If there are more than 100 unique words in the input, the program should still work, and count the number of appearances of the first 100 unique words. Each word should have the same maximum amount...

  • In python Count the frequency of each word in a text file. Let the user choose...

    In python Count the frequency of each word in a text file. Let the user choose a filename to read. 1. The program will count the frequency with which each word appears in the text. 2. Words which are the spelled the same but differ by case will be combined. 3. Punctuation should be removed 4. If the file does not exist, use a ‘try-execption’ block to handle the error 5. Output will list the words alphabetically, with the word...

  • out1.txt File Directly Below... Note: "//notaword" is the part that is not a word and needs...

    out1.txt File Directly Below... Note: "//notaword" is the part that is not a word and needs to be handled and is specifically the part I am having trouble with https://www.dropbox.com/s/ume3slnphyhi6wt/out1.txt?dl=0 - link for the out1.txt file for testing yourself Please include comments as I am in this for learning and would really appreciate the help! Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start...

  • In this assignment, you will explore more on text analysis and an elementary version of sentiment...

    In this assignment, you will explore more on text analysis and an elementary version of sentiment analysis. Sentiment analysis is the process of using a computer program to identify and categorise opinions in a piece of text in order to determine the writer’s attitude towards a particular topic (e.g., news, product, service etc.). The sentiment can be expressed as positive, negative or neutral. Create a Python file called a5.py that will perform text analysis on some text files. You can...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT