Question

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

Assignment 3: Word Frequencies

  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 of the word frequency list:

100: frog
94: dog
43: cog
20: bog

Advice:

  • You can find song lyrics at https://genius.com/

  • Consider whether you want your keys to be case sensitive

  • Consider whether you want to include common stopwords such as the, a, is (more can be found at https://www.ranks.nl/stopwords)

  • Discuss with a classmate pro/cons of all steps if you’re rather indecisive

  • Examples can be found under the Maven folder

  • Examples of readme files github.com/matiassingers/awesome-readme

It is okay to start with the example code given in the repo, however please think about why you’re using whichever data structure you’ve selected for the task as you’ll have to write a description of your design rationale.


Example:

package myApp;

        import java.util.*;
        import java.io.*;

/**
 * Hello world!
 *
 */
public class WordCount {
    public static HashMap<String, Integer> sortByValue(HashMap<String, Integer> hm) {

        // Create a list from elements of HashMap
        java.util.List<Map.Entry<String, Integer> > list = new LinkedList<Map.Entry<String, Integer> >(hm.entrySet());

        // Sort the list
        Collections.sort(list, new java.util.Comparator<Map.Entry<String, Integer> >() {
            public int compare(Map.Entry<String, Integer> o1,
                               Map.Entry<String, Integer> o2) {
                return (o2.getValue()).compareTo(o1.getValue());
            }
        });

        // put data from sorted list to hashmap
        HashMap<String, Integer> temp = new LinkedHashMap<String, Integer>();
        for (Map.Entry<String, Integer> aa : list) {
            temp.put(aa.getKey(), aa.getValue());
        }
        return temp;
    }

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

        // open the file
        Scanner console = new Scanner(System.in);
        String fileName = args[0];
        Scanner input = new Scanner(new File(fileName));

        // count occurrences
        HashMap<String, Integer> wordCounts = new HashMap<String, Integer>();
        while (input.hasNext()) {
            String next = input.next().toLowerCase();
            String clean = next;
            //next.replaceAll("\\p{Punct}", "").toLowerCase();

            if (!wordCounts.containsKey(clean)) {
                wordCounts.put(clean, 1);
            } else {
                wordCounts.put(clean, wordCounts.get(clean) + 1);
            }
        }


        System.out.println("Total words = " + wordCounts.size());

        HashMap<String, Integer> sortedMapAsc = WordCount.sortByValue(wordCounts);

        // Report frequencies
        for (String word : sortedMapAsc.keySet()) {
            int count = sortedMapAsc.get(word);
            System.out.println(count + ": " + word);
        }
    }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Data Structure Used: HASHMAP

Reason:

In hashmap, data is stored in key-value pairs, It uses hashing to store the data, No duplicate keys are allowed in HashMap

Hence, we can use it to keep the word as the KEY and increment the occurence of word as VALUE.

This is the best data Structure to use in this type of cases.

CODE:

import java.util.*;
import java.io.*;

public class Solution{

   private static HashMap<String, Integer> frequency = new HashMap<>();

   //function to add the data into HashMap
   //convert the words to upper case to remove case sensitivity, then replace all spaces with ',' and then split by ','
   //If word exists in the map, increase the count or add it to the map
   public static void countTheWords(String lyrics){

       String[] words = lyrics.toUpperCase().replace(" ", ",").trim().split(",");
       for(String word : words){
           Integer n = frequency.get(word);
   n = (n == null) ? 1 : ++n;
   frequency.put(word, n);
       }
       frequency.remove("");
   }

   //function to print the map
   public static void printTheWords(){
       for(Map.Entry<String, Integer> entry : frequency.entrySet()){
           System.out.println(entry.getKey()+" "+entry.getValue());
       }
   }

   public static void main(String[] args){

       File file = new File("C:\\Users\\venkata.saraswathula\\Documents\\Java\\lyrics.txt");
       try{
           Scanner sc = new Scanner(file);
           String lyrics;
           while(sc.hasNextLine()){
               lyrics = sc.nextLine();
               countTheWords(lyrics);
           }
           printTheWords();
       }
       catch(FileNotFoundException E){
           System.out.println("File not found");
       }
   }
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Assignment 3: Word Frequencies Prepare a text file that contains text to analyze. It could be...
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

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

  • I need help running this code. import java.util.*; import java.io.*; public class wordcount2 {     public...

    I need help running this code. import java.util.*; import java.io.*; public class wordcount2 {     public static void main(String[] args) {         if (args.length !=1) {             System.out.println(                                "Usage: java wordcount2 fullfilename");             System.exit(1);         }                 String filename = args[0];               // Create a tree map to hold words as key and count as value         Map<String, Integer> treeMap = new TreeMap<String, Integer>();                 try {             @SuppressWarnings("resource")             Scanner input = new Scanner(new File(filename));...

  • I've previously completed a Java assignment where I wrote a program that reads a given text...

    I've previously completed a Java assignment where I wrote a program that reads a given text file and creates an index that stores the line numbers for where individual words occur. I've been given a new assignment where I need to modify some of my old code. I need to replace the indexer in my Index class with a NavigableMap<String, Word> and update my Word class with NavigableSet<Integer> lines. The instantiated objects should be TreeMap() and TreeSet(). I have below...

  • Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Wr...

    Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Write a program named WordCount.java, in this program, implement two static methods as specified below: public static int countWord(Sting word, String str) this method counts the number of occurrence of the word in the String (str) public static int countWord(String word, File file) This method counts the number of occurrence of the word in the file. Ignore case in the word. Possible punctuation and symbals in the file...

  • 9. A concordance is an alphabetical word list for a passage of text. Each word in...

    9. A concordance is an alphabetical word list for a passage of text. Each word in the concordance is mapped to an integer indicating the frequency of the word's occurrence. The constructor of Concordance has one String parameter that identifies the text file to be read. An incompleteConcordance class is below. public class Concordance public Concordance (String nameOfFileH concord new TreeMap<String, Integer 0 createConcordance (nameOfFile); //Constructor //nameOfFile the text file being read public void createConcordance (string filename)...) //Create a TreeMap...

  • Ask the user for the name of a file and a word. Using the FileStats class,...

    Ask the user for the name of a file and a word. Using the FileStats class, show how many lines the file has and how many lines contain the text. Standard Input                 Files in the same directory romeo-and-juliet.txt the romeo-and-juliet.txt Required Output Enter a filename\n romeo-and-juliet.txt has 5268 lines\n Enter some text\n 1137 line(s) contain "the"\n Your Program's Output Enter a filename\n romeo-and-juliet.txt has 5268 lines\n Enter some text\n 553 line(s) contain "the"\n (Your output is too short.) My...

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

  • JAVA Code: Complete the program that reads from a text file and counts the occurrence of...

    JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...

  • 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