Last active
December 17, 2018 21:41
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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