Question: Need to create public ArrayList commonPopularWords ( int n , WordFisher other ) and test it . This method receives an integer n and another
Need to create public ArrayList commonPopularWordsint n WordFisher other and test it This method receives an integer n and another WordFisher obj ie another text as input & returns an ArrayList of the common popular words taken from the n most popular from the first text, n most popular from the other between the two texts. An empty list should be returned if there are no common words. Given the following JAVA codes.
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 freq in a text file, allowing for
operations such as pruning stopwords & finding common pop words.
public class WordFisher
A map containing words as keys & their occurrence freq as values
public HashMap vocabulary;
A list of stopwords to exclude from word freq analysis
public List stopwords;
private String inputTextFile;
private String stopwordsFile;
Constructs a WordFisher object & initializes its state
WordFisherString inputTextFile, String stopwordsFile
this.inputTextFile inputTextFile;
this.stopwordsFile stopwordsFile;
buildVocabulary;
getStopwords;
Reads stopwords from the specified stopwords file & 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 voc by reading words from the input text file &
counting their freq
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;
Ret the tot # of words in the input file
public int getWordCount
int wordcount ;
Object word vocabulary.valuestoArray;
for int i ; i word.length; i
wordcount wordcount Integer wordi;
return wordcount;
Ret the # of uni words
public int getNumUniqueWords
int unicount ;
Object keyset vocabulary.keySettoArray;
for int i ; i keyset.length; i
String wordkey String keyseti;
unicount uniquecount ;
return unicount;
Ret the freq 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;
Finds com pop words between this WordFisher instance & another
public ArrayList commonPopularWordsint n WordFisher other
ArrayList comPopWords new ArrayList;
TODO
return comPopWords;
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
