Question

using java find the third most frequent word in a paragraph in an array list. also...

using java
find the third most frequent word in a paragraph in an array list. also print the sentences that include this word.

the paragraph is stored in an array list. you have to search within the array list the third most used word.
0 0
Add a comment Improve this question Transcribed image text
Answer #1
//ProgramArrayList.java

import java.util.*;
import static java.util.stream.Collectors.toMap;

public class ProgramArrayList {

    /**
     * function to find third most frequent letter
     * @param para
     * @return
     */
    public static String thirdMostFrequentValue(ArrayList<String> para) {
        // map for storing the word and count
        HashMap<String, Integer> hashMap = new HashMap<>();
        for (String s : para) {
            // splitting para by space
            String temp[] = s.trim().split(" ");
            // adding to hashmap
            for (String word : temp) {
                if (hashMap.containsKey(word)) {
                    hashMap.put(word, hashMap.get(word) + 1);
                } else {
                    hashMap.put(word, 1);
                }
            }
        }
        // sorting the haspmap by descending order
        HashMap<String, Integer> sortedHashMap = hashMap
                .entrySet()
                .stream()
                .sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
                .collect(
                        toMap(e -> e.getKey(), e -> e.getValue(), (e1, e2) -> e2,
                                LinkedHashMap::new));
        Object keyValues[] = sortedHashMap.keySet().toArray();
        if (keyValues.length >= 3) {
            return (String) keyValues[2];
        }
        return null;
    }

    /**
     * function to print line with third most frequent letter
     * @param para
     */
    public static void printSentenceWithWord(ArrayList<String> para){
        // calling above function for finding the third most frequent letter
        String thirdFrequentWord = thirdMostFrequentValue(para);
        if(thirdFrequentWord == null){
            System.out.println("Third Frequent Word Not Found");
            return;
        }
        System.out.println("Third Frequent Word: "+thirdFrequentWord+"\n");
        System.out.println("Printing sentence that contains Third Most Frequent Letter.\n");
        // looping through paragraphs
        for (String s: para){
            String temp[] = s.split("\n");
            for(String sentence: temp){
                if(sentence.contains(thirdFrequentWord))
                    System.out.println(sentence);
            }
        }
    }
    public static void main(String[] args) {
        ArrayList<String> paragraphs = new ArrayList<>();
        paragraphs.add("This is First para");
        paragraphs.add("This is Second para");
        paragraphs.add("Not a good para");
        paragraphs.add("Test code");
        printSentenceWithWord(paragraphs);
    }
}

//OUT

Please do let me know if u have any concern...

Add a comment
Know the answer?
Add Answer to:
using java find the third most frequent word in a paragraph in an array list. also...
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
  • Write a Java class myLinkedList to simulate a singly linked list using arrays as the underlying...

    Write a Java class myLinkedList to simulate a singly linked list using arrays as the underlying structure. Include the following methods: 1. insert an element within the linked list.(this should also work for the front and the rear of the list) 2. Remove an element from the linked list 3. Display (print) the elements of the linked list in order. 4. A method to check if the list is "empty". Test your solution using a linked list that initially has...

  • Write a Java program that will create a random list (array) randomize a  search item to be...

    Write a Java program that will create a random list (array) randomize a  search item to be found in the list sequentially search the array for that item. You must code the search and not use a search function. Display each comparison in the array; the element contents and the index. display whether the item was found or not. Now take that code and alter it to have two lists. Both lists randomize between 1 and 50. While traversing one list,...

  • Problem 1. Implement a C++ program that has the following functions: Reads in a paragraph of...

    Problem 1. Implement a C++ program that has the following functions: Reads in a paragraph of English text up to 100 words from the keyboard and stores this paragraph in a string object. Feel free to include this task in the main() function. Identifies the least frequent letter (case insensitive) in the above paragraph. Implement a separate function getLeastFreqLetter() for this task. The main() function then calls this function to find out the least frequent letter and its frequency. Calculate...

  • Question 2: Finding the best Scrabble word with Recursion using java Scrabble is a game in...

    Question 2: Finding the best Scrabble word with Recursion using java Scrabble is a game in which players construct words from random letters, building on words already played. Each letter has an associated point value and the aim is to collect more points than your opponent. Please see https: //en.wikipedia.org/wiki/Scrabble for an overview if you are unfamiliar with the game. You will write a program that allows a user to enter 7 letters (representing the letter tiles they hold), plus...

  • Paste this code into a new file and find the errors.  The most frequent letter in the...

    Paste this code into a new file and find the errors.  The most frequent letter in the user_string is H. # Function displays the character that appears most frequently # in the string. If several characters have the same highest # frequency, displays the first character with that frequency. def main():     # Set up local variables     count = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]     letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'     index = 0     frequent = 0     # Receive user input.     user_string = 'Who where what why how'     for ch...

  • List the 6 major barriers and discuss, in paragraph format, an example of each. Also, include...

    List the 6 major barriers and discuss, in paragraph format, an example of each. Also, include at least one example that you yourself have used in the past and how this hampered your ability to communicate. Try to think of examples that were used in a business setting.

  • 1) use java to print the sum of the list (any #s) using keyboard, also using...

    1) use java to print the sum of the list (any #s) using keyboard, also using a while loop and for-loop and do-loop. ( 3 separate programs ).

  • Efficiency of Array vs. ArrayList-- Use JAVA to implement the operations Using the list operations fill,...

    Efficiency of Array vs. ArrayList-- Use JAVA to implement the operations Using the list operations fill, increment, and search, investigate whether arrays or ArrayLists are faster, or whether they are about the same for int and float values. This will also test times to generate both int and float random numbers as well as the time cost of automatic expansion of an ArrayList. Remember: int and float are used in simple arrays, Integer and Float are used in the ArrayList....

  • in java please Write a program that contains an array with all of your friends' first...

    in java please Write a program that contains an array with all of your friends' first names. Enter the values in any manner and order you choose (I recommend favorites first). Now take that array and sort it alphabetically so that names that start with ‘A' c me first. Look to see if you have any duplicates and if you find any, print out their names. You may use a binary search to determine this. 2. Sample output: Input list...

  • In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list...

    In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list of objects. Also, this lab specification tells you only what to do, you now have more responsibility to design how to do it. Problem description You are given a text file called 'Students.txt' that contains information on many students. Your program reads the file, creating many Student objects, all of which will be stored into an array list of Student objects, in the Students...

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