Question: Need to create public ArrayList getTopWords ( int n ) and test it . Given the following JAVA codes. This method receives an integer n

Need to create public ArrayList getTopWords(int n) and test it. Given the following JAVA codes. This method receives an integer n and returns the top n most frequently occurring words in the text as an ArrayList of strings (return a list of the top n words). You will need to use the PriorityQueue.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
/* The WordFisher class analyzes word frequency in a text file, allowing for
* operations such as pruning stopwords and finding common popular words. */
public class WordFisher {
/* A map containing words as keys and their occurrence frequencies as values. */
public HashMap vocabulary;
/* A list of stopwords to exclude from word frequency analysis. */
public List stopwords;
/* The file path of the input text file.*/
private String inputTextFile;
/* The file path of the stopwords file. */
private String stopwordsFile;
/* Constructs a WordFisher object and initializes its state */
WordFisher(String inputTextFile, String stopwordsFile){
this.inputTextFile = inputTextFile;
this.stopwordsFile = stopwordsFile;
buildVocabulary();
getStopwords();
}
/* Reads stopwords from the specified stopwords file and stores them in the stopwords list. */
public void getStopwords(){
stopwords = new ArrayList();
String word;
try {
BufferedReader input = new BufferedReader(new FileReader(stopwordsFile));
while ((word = input.readLine())!= null){
stopwords.add(word);
}
} catch (IOException e){
e.printStackTrace();
}
}
/* Builds the vocabulary by reading words from the input text file and
* counting their frequencies. */
public void buildVocabulary(){
vocabulary = new HashMap();
try {
String reader = new String(Files.readAllBytes(Paths.get(inputTextFile)));
String[] allWords = reader.toLowerCase().replaceAll("[^a-zA-Z0-9]","").split("\\s+");
int i =0;
int length = allWords.length;
int voc_value;
for (i=0; i < length; i++){
if (vocabulary.containsKey(allWords[i])){
voc_value = vocabulary.get(allWords[i]);
voc_value = voc_value +1;
vocabulary.put(allWords[i], voc_value);
} else {
vocabulary.put(allWords[i],1);
}
}
} catch (IOException e){
e.printStackTrace();
}
}
/* Returns the total number of words in the input text file */
public int getWordCount(){
int word_count =0;
Object [] word = vocabulary.values().toArray();
for (int i =0; i < word.length; i++){
word_count = word_count +(Integer) word[i];
}
return word_count;
}
/* Returns the number of unique words */
public int getNumUniqueWords(){
int unique_count =0;
Object[] key_set = vocabulary.keySet().toArray();
for (int i =0; i < key_set.length; i++){
String word_key =(String) key_set[i];
unique_count = unique_count +1;
}
return unique_count;
}
/* Returns the frequency of a specific word */
public int getFrequency(String word){
if (vocabulary.containsKey(word)){
return vocabulary.get(word);
}
return -1;
}
/* Removes all stopwords */
public void pruneVocabulary(){
getStopwords();
for (int j =0; j < stopwords.size(); j++){
if (vocabulary.containsKey(stopwords.get(j))){
vocabulary.remove(stopwords.get(j));
}
}
}
/* Retrieves the top n words based on their frequency.
return a list of the top n words */
public ArrayList getTopWords(int n){
ArrayList topWords = new ArrayList();
// TODO:
return topWords;
}
}

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 Programming Questions!