29.6k views
1 vote
6.1 Task 5.1 - loadGLOVE() In this task, you are required to use a Bu f f eredReader (myReader) to read data from the DSM file (F ILENAME_GLOV E) line by line. • Read the file line by line and analyse the result - adding the word to listVocabulary and its vector representation to listVectors. • Use the Toolkit.getFileFromResource(String _ fileName) method to get the correct file path.

6.2 Task 5.2 - loadStopWords() Similar to Task 5.1, used a Bu f f eredReader to read the F ILENAME_STOPWORDS file, and add those stop words in listStopWords defined in this method. 11 • Use the Toolkit.getFileFromResource(String _ fileName) method to get the correct file path. • Return the listStopWords list at the end of this method.
Could you please answer this in java code.For the task 5.2 only

User Htoniv
by
8.1k points

1 Answer

4 votes

Final answer:

To load stop words in Java, create a method that utilizes BufferedReader to read the FILENAME_STOPWORDS file, add each line to listStopWords, and return this list at the end of the method.

Step-by-step explanation:

Java Code to Load Stop Words from a File

To complete task 5.2 which involves creating a method to load stop words using a BufferedReader, you would generally do the following in Java:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class YourClassName {

public List loadStopWords() {
List listStopWords = new ArrayList<>();
String fileName = Toolkit.getFileFromResource("FILENAME_STOPWORDS");

try (BufferedReader myReader = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = myReader.readLine()) != null) {
listStopWords.add(line);
}
} catch (IOException e) {
e.printStackTrace();
}

return listStopWords;
}
}

Note that you should replace 'YourClassName' with the actual class name where this method would be placed. Moreover, replace 'FILENAME_STOPWORDS' with the actual file name of the stop words file.

This method employs a try-with-resources statement which ensures that the BufferedReader is closed after use, regardless of whether the try statement completes normally or abruptly. It reads the file line by line and adds each stop word into the listStopWords, which is then returned by the method.

User Joe Stagner
by
7.1k points