Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This method returns the top 10 most used negative words in Eminem's lyrics.
public static List<String> getNegativeWords() throws IOException {
List<String> outputList = new ArrayList<String>();
BufferedReader br = new BufferedReader(new FileReader(
"C:\\negative-words.txt"));
String line = null;
while ((line = br.readLine()) != null) {
outputList.add(line);
}
br.close();
return outputList;
}
String[] inputFilePathArray = new String[4];
inputFilePathArray[0] = "C:\\input1.txt";
inputFilePathArray[1] = "C:\\input2.txt";
inputFilePathArray[2] = "C:\\input3.txt";
inputFilePathArray[3] = "C:\\input4.txt";
List<String> globalNegativeWordsList = getNegativeWords();
Map<String, Integer> negativeWordsMap = new HashMap<String, Integer>();
for (String inputFilePath : inputFilePathArray) {
BufferedReader br = new BufferedReader(
new FileReader(inputFilePath));
String line = null, mapKey = null;
String[] lineSplitter = null;
int i = 0;
while ((line = br.readLine()) != null) {
if (globalNegativeWordsList.contains(mapKey)) {
if (negativeWordsMap.containsKey(mapKey))
negativeWordsMap.put(mapKey,
negativeWordsMap.get(mapKey) + 1);
else
negativeWordsMap.put(mapKey, 1);
}
br.close();
}
System.out.println("***NEGATIVE WORDS***");
count = 0;
LinkedHashMap<String, Integer> sortedNegativeWordsMap = sortHashMapByValues(negativeWordsMap);
for (Entry<String, Integer> entry : sortedNegativeWordsMap.entrySet()) {
if (count < 10)
System.out.println(entry.getKey() + " - " + entry.getValue());
count++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.