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 getTopWordsint 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.ioBufferedReader;
import java.ioFileReader;
import java.ioIOException;
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
WordFisherString 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 BufferedReadernew FileReaderstopwordsFile;
while word input.readLine null
stopwords.addword;
catch IOException e
eprintStackTrace;
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 StringFilesreadAllBytesPathsgetinputTextFile;
String allWords reader.toLowerCasereplaceAllazAZsplits;
int i ;
int length allWords.length;
int vocvalue;
for i; i length; i
if vocabularycontainsKeyallWordsi
vocvalue vocabulary.getallWordsi;
vocvalue vocvalue ;
vocabulary.putallWordsi vocvalue;
else
vocabulary.putallWordsi;
catch IOException e
eprintStackTrace;
Returns the total number of words in the input text file
public int getWordCount
int wordcount ;
Object word vocabulary.valuestoArray;
for int i ; i word.length; i
wordcount wordcount Integer wordi;
return wordcount;
Returns the number of unique words
public int getNumUniqueWords
int uniquecount ;
Object keyset vocabulary.keySettoArray;
for int i ; i keyset.length; i
String wordkey String keyseti;
uniquecount uniquecount ;
return uniquecount;
Returns the frequency of a specific word
public int getFrequencyString word
if vocabularycontainsKeyword
return vocabulary.getword;
return ;
Removes all stopwords
public void pruneVocabulary
getStopwords;
for int j ; j stopwords.size; j
if vocabularycontainsKeystopwordsgetj
vocabulary.removestopwordsgetj;
Retrieves the top n words based on their frequency.
return a list of the top n words
public ArrayList getTopWordsint 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
