Question

WordFrequencies.java 1. Prepare a text file that contains text to analyze. It could be song lyrics...

WordFrequencies.java

1. Prepare a text file that contains text to analyze. It could be song lyrics to your favorite song.

2. With your code, you’ll read from the text file and capture the data into a data structure.

3. Using a data structure, write the code to count the appearance of each unique word in the lyrics.

4. Print out a word frequency list.

Example output of the word frequency list:

100: frog

94: dog

43: cog

20: bog

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

Solution:-

import java.io.*;
import java.util.*;
class WordFrequencies
{
public static void main(String args[]) throws Exception
{
Console con = System.console();
String str;
int i=0;

HashMap map = new HashMap();
HashSet set = new HashSet();
System.out.println("enter the file name : ");

str = con.readLine();

FileInputStream fis = new FileInputStream(str);

int ch;
String string=new String();
while((ch=fis.read())!=-1)
{
string+=(char)ch+"";
}
StringTokenizer st = new StringTokenizer(string);
while(st.hasMoreTokens())
{
String s =st.nextToken();
map.put(i+"",s);
set.add(s);
i++;
}
Iterator iter = set.iterator();
System.out.println("output of the word frequency list: ");
while(iter.hasNext())
{
String str1;
int count=0;

str1=(String)iter.next();


for(int j=0; j<i ; j++)
{
String str2;
str2=(String)map.get(j+"");
if(str1.equals(str2))
count++;
}

System.out.printf("%d:%s\n",count,str1);
}

}
}

Output:-

Add a comment
Know the answer?
Add Answer to:
WordFrequencies.java 1. Prepare a text file that contains text to analyze. It could be song lyrics...
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
  • WordFrequencies.java 1. Prepare a text file that contains text to analyze. It could be song lyrics...

    WordFrequencies.java 1. Prepare a text file that contains text to analyze. It could be song lyrics to your favorite song. 2. With your code, you’ll read from the text file and capture the data into a data structure. 3. Using a data structure, write the code to count the appearance of each unique word in the lyrics. 4. Print out a word frequency list. Example output of the word frequency list: 100: frog 94: dog 43: cog 20: bog use...

  • Assignment 3: Word Frequencies Prepare a text file that contains text to analyze. It could be...

    Assignment 3: Word Frequencies Prepare a text file that contains text to analyze. It could be song lyrics to your favorite song. With your code, you’ll read from the text file and capture the data into a data structure. Using a data structure, write the code to count the appearance of each unique word in the lyrics. Print out a word frequency list. Example of the word frequency list: 100: frog 94: dog 43: cog 20: bog Advice: You can...

  • 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...

  • Use the csv file on spotify from any date Code from lab2 import java.io.File; import java.io.FileNotFoundException;...

    Use the csv file on spotify from any date Code from lab2 import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; public class SongsReport {    public static void main(String[] args) {               //loading name of file        File file = new File("songs.csv"); //reading data from this file        //scanner to read java file        Scanner reader;        //line to get current line from the file        String line="";       ...

  • 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...

  • (in C) Binry Srch tree problem: 1. Need to read words from a “in” text file...

    (in C) Binry Srch tree problem: 1. Need to read words from a “in” text file and build a binary search tree. (1st line will state number of elements) 2. strcmp() can be used to compare data and decide were to insert a new node. 3. The output should include: -print the tree as in-order traversal. -print the character count in the tree. -Ask user input for a word to search, print an output if either found or not. (typing...

  • please write it in c++ programming language this is the rat text file More what? what...

    please write it in c++ programming language this is the rat text file More what? what more additional information do you want? so i can provide it. Write a program that reads files and prepares an statistical summary for each file processed with the following information: • total number of words in the report; • number of unique words: • number of unique words of more than four letters; Input From the keyboard: 1. The name of the files containing...

  • I need help in C++ implementing binary search tree. I have the .h file for the...

    I need help in C++ implementing binary search tree. I have the .h file for the binary search tree class. I have 4 classic texts, and 2 different dictionaries. Classic Texts: Alice In Wonderland.txt A Tale of Two Cities.txt Pride And Prejudice.txt War and Peace.txt 2 different dictionaries: Dictionary.txt Dictionary-brit.txt The data structures from the standard template library can not be used.The main program should open the text file, read in the words, remove the punctuation and change all the...

  • QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes...

    QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes to a file. Compile and run these programs. There are several ways to read/write text files. The examples shown here are just one way of achieving this. Look at the API for the BufferedReader class. The readline() method is a simple way to read the text file line by line. It returns null when the end of the file has been reached. https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html Look...

  • The following code uses a Scanner object to read a text file called dogYears.txt. Notice that...

    The following code uses a Scanner object to read a text file called dogYears.txt. Notice that each line of this file contains a dog's name followed by an age. The program then outputs this data to the console. The output looks like this: Tippy 2 Rex 7 Desdemona 5 1. Your task is to use the Scanner methods that will initialize the variables name1, name2, name3, age1, age2, age3 so that the execution of the three println statements below will...

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