Skip to content

Instantly share code, notes, and snippets.

@milindjagre
Last active December 17, 2018 21:40
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/4bdd6f0037abb29515460d5d8238137f to your computer and use it in GitHub Desktop.
Save milindjagre/4bdd6f0037abb29515460d5d8238137f to your computer and use it in GitHub Desktop.
This method returns the top 10 most used positive words in Eminem's lyrics.
public static List<String> getPositiveWords() throws IOException {
List<String> outputList = new ArrayList<String>();
BufferedReader br = new BufferedReader(new FileReader(
"C:\\positive-words.txt"));
String line = null;
while ((line = br.readLine()) != null) {
outputList.add(line);
}
br.close();
return outputList;
}
List<String> globalPositiveWordsList = getPositiveWords();
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";
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) {
for (i = 0; i < lineSplitter.length; i++) {
mapKey = lineSplitter[i].replaceAll("[\\.\\',\\?]", "");
if (globalPositiveWordsList.contains(mapKey)) {
if (positiveWordsMap.containsKey(mapKey))
positiveWordsMap.put(mapKey,
positiveWordsMap.get(mapKey) + 1);
else
positiveWordsMap.put(mapKey, 1);
}
br.close();
}
System.out.println("***POSITIVE WORDS***");
count = 0;
LinkedHashMap<String, Integer> sortedPositiveWordsMap = sortHashMapByValues(positiveWordsMap);
for (Entry<String, Integer> entry : sortedPositiveWordsMap.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