Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Task: You are given a text file where each line is a sentence. Write a Java program that will do the following: Read the text

Task:
You are given a text file where each line is a sentence. Write a Java program that will do
the following:
Read the text file line by line and store each unique word across all sentences
and their respective occurrence count. You may use Javas built-in HashMap
class defined in java.util.HashMap.
Implement a method getCount(String word) that takes a word as input and
returns the number of times it appears across all sentences.
Implement a method getMostFrequentWords(int k) that returns a list of
the k most frequently occurring words across all sentences.
Implement a method getLeastFrequentWords(int k) that returns a list of
the k least frequently occurring words across all sentences.
Implement a user interface that displays a menu to the user and allows them to
select an option to execute each of the above methods.
Constraints:
All words are case-insensitive.
Words are separated by spaces and punctuation marks should be ignored.
Error Handling:
The program should handle potential errors gracefully.
For example, it should check if the file exists and can be read, and it
should handle potential issues with the files content (e.g., lines that dont
contain any words).
User Interface:
The program should provide a user-friendly interface that allows the user to
easily select an option and see the results.
Sample Input:
A text file named sentences.txt with the following content:
The quick brown fox jumps over the lazy dog
Pack my box with five dozen liquor jugs
Jackdaws love my big sphinx of quartz
How vexingly quick daft zebras jump
Bright vixens jump; dozy fowl quack
Sample User-Interactions: (Bold indicates user inputs)
Input file content has been read from sentences.txt
Please select an option:
1. Get the count of a specific word
2. Get the most frequent words
3. Get the least frequent words
4. Exit
1
Enter the word:
the
The word 'the' appears 2 times.
Please select an option:
1. Get the count of a specific word
2. Get the most frequent words
3. Get the least frequent words
4. Exit
2
Enter the number of words:
3
The 3 most frequent words are [the, my, quick]
Please select an option:
1. Get the count of a specific word
2. Get the most frequent words
3. Get the least frequent words
4. Exit
Enter the number of words:
3
The 3 least frequent words are [brown, fox, over]
Please select an option:
1. Get the count of a specific word
2. Get the most frequent words
3. Get the least frequent words
4. Exit
4
Exiting...
Note: The output for getMostFrequentWords and getLeastFrequentWords can vary
depending on the order of the words in the HashMap.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Conceptual Database Design An Entity Relationship Approach

Authors: Carol Batini, Stefano Ceri, Shamkant B. Navathe

1st Edition

0805302441, 978-0805302448

More Books

Students also viewed these Databases questions