Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!