Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
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 builtin HashMap
class defined in java.util.HashMap.
Implement a method getCountString word that takes a word as input and
returns the number of times it appears across all sentences.
Implement a method getMostFrequentWordsint k that returns a list of
the k most frequently occurring words across all sentences.
Implement a method getLeastFrequentWordsint 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 caseinsensitive.
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 eg lines that dont
contain any words
User Interface:
The program should provide a userfriendly 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 UserInteractions: Bold indicates user inputs
Input file content has been read from sentencestxt
Please select an option:
Get the count of a specific word
Get the most frequent words
Get the least frequent words
Exit
Enter the word:
the
The word 'the' appears times.
Please select an option:
Get the count of a specific word
Get the most frequent words
Get the least frequent words
Exit
Enter the number of words:
The most frequent words are the my quick
Please select an option:
Get the count of a specific word
Get the most frequent words
Get the least frequent words
Exit
Enter the number of words:
The least frequent words are brown fox, over
Please select an option:
Get the count of a specific word
Get the most frequent words
Get the least frequent words
Exit
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started