Answer:
Here's an example Java program, WordFreqCount.java, that reads a file (book.txt) and counts the frequencies of words using the Java Map interface. It displays the top 20 most appeared words and the top 20 least appeared words.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class WordFreqCount {
public static void main(String[] args) {
String filename = "book.txt"; // Replace with the actual file name
try {
Map<String, Integer> wordFrequencyMap = countWordFrequencies(filename);
List<Map.Entry<String, Integer>> mostAppearedWords = getMostAppearedWords(wordFrequencyMap, 20);
List<Map.Entry<String, Integer>> leastAppearedWords = getLeastAppearedWords(wordFrequencyMap, 20);
System.out.println("Top 20 Most Appeared Words:");
displayWordFrequencies(mostAppearedWords);
System.out.println("\\Top 20 Least Appeared Words:");
displayWordFrequencies(leastAppearedWords);
} catch (IOException e) {
e.printStackTrace();
}
}
public static Map<String, Integer> countWordFrequencies(String filename) throws IOException {
Map<String, Integer> wordFrequencyMap = new HashMap<>();
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = br.readLine()) != null) {
String[] words = line.split("\\s+");
for (String word : words) {
wordFrequencyMap.put(word, wordFrequencyMap.getOrDefault(word, 0) + 1);
}
}
}
return wordFrequencyMap;
}
public static List<Map.Entry<String, Integer>> getMostAppearedWords(Map<String, Integer> wordFrequencyMap, int count) {
List<Map.Entry<String, Integer>> entries = new ArrayList<>(wordFrequencyMap.entrySet());
entries.sort((e1, e2) -> e2.getValue().compareTo(e1.getValue())); // Sort by value in descending order
return entries.subList(0, Math.min(count, entries.size()));
}
public static List<Map.Entry<String, Integer>> getLeastAppearedWords(Map<String, Integer> wordFrequencyMap, int count) {
List<Map.Entry<String, Integer>> entries = new ArrayList<>(wordFrequencyMap.entrySet());
entries.sort((e1, e2) -> e1.getValue().compareTo(e2.getValue())); // Sort by value in ascending order
return entries.subList(0, Math.min(count, entries.size()));
}
public static void displayWordFrequencies(List<Map.Entry<String, Integer>> wordFrequencies) {
int rank = 1;
for (Map.Entry<String, Integer> entry : wordFrequencies) {
System.out.println("(" + rank + "): " + entry.getKey() + " --> " + entry.getValue());
rank++;
}
}
}
To use this program, make sure to replace "book.txt" with the actual file name you want to read. The program reads the file, counts the word frequencies using a HashMap, and then uses the provided methods getMostAppearedWords and getLeastAppearedWords to get the top 20 most and least appeared words, respectively. The displayWordFrequencies method is used to print the results.
Make sure the input file book.txt is present in the same directory as the Java file or provide the full path to the file.
Note: The program assumes that words are separated by spaces. If your file contains punctuation or special characters, you may need to modify the splitting
Step-by-step explanation: