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 commonPopularWords(int n, WordFisher other) and test it. This method receives an integer n and another WordFisher obj (i.e. 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.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 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*/
WordFisher(String 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 BufferedReader(new FileReader(stopwordsFile));
while ((word = input.readLine())!= null){
stopwords.add(word);
}
} catch (IOException e){
e.printStackTrace();
}
}
/* Builds the voc by reading words from the input text file &
* counting their freq */
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();
}
}
/* Ret the tot # of words in the input 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;
}
/* Ret the # of uni words */
public int getNumUniqueWords(){
int uni_count =0;
Object[] key_set = vocabulary.keySet().toArray();
for (int i =0; i < key_set.length; i++){
String word_key =(String) key_set[i];
uni_count = unique_count +1;
}
return uni_count;
}
/* Ret the freq 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));
}
}
}
/*Finds com pop words between this WordFisher instance & another*/
public ArrayList commonPopularWords(int 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 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!