Skip to content

Instantly share code, notes, and snippets.

@milindjagre
Last active December 17, 2018 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milindjagre/9538a85ead337afcf3a9fa39ae578c76 to your computer and use it in GitHub Desktop.
Save milindjagre/9538a85ead337afcf3a9fa39ae578c76 to your computer and use it in GitHub Desktop.
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