Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

POWeek Challenge Once you get your program to work, you’ll know the top words th

ID: 3667531 • Letter: P

Question

POWeek Challenge

Once you get your program to work, you’ll know the top words that an author likes to use. What makes this interesting is that we can extend the program just a little and actually produce a TL;DR summary of any given input. Check out http://smmry.com/, and give it an example URL to read and summarize. The programmers outlined their algorithm at http://smmry.com/about. You’ll notice that it relies on counting how many times a word shows up in the text.

Try to implement your own summarizer using the same logic and make it run as fast as possible. In our solution when developing the POWeek challenge, we skipped steps 1 and 4 of smmry’s algorithm, but you can take on if you’d like the challenge. Also, instead of specifying how many words to print out, your program should take as input the number of sentences to print. We’ll be comparing how well your summary works and how fast you get to your solution. You can find the time of your program execution via:

HERE'S MY CODE:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/****
*
* WordCloud.java
*
*/
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;

public class WordCloud {
public static void main(String[] args) throws FileNotFoundException {
/** Get File Name */
String fileName = args[0];

/** No of Records */
int noRecord = Integer.parseInt(args[1]);

/***/
Scanner inputFile = new Scanner(new File(fileName));
Map<String, Integer> map = new TreeMap<String, Integer>();

while (inputFile.hasNext()) {
String[] data = inputFile.nextLine().replaceAll("[^a-zA-Z ]", "").toLowerCase().split("\s+");
for (int i = 0; i < data.length; i++) {
String word = data[i].toLowerCase();
if (map.containsKey(word)) {
map.put(word, map.get(word) + 1);
} else {
map.put(word, 1);

}
}
}

Set<Entry<String, Integer>> set = map.entrySet();
List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(
set);

  
/***
*
*/
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1,
Map.Entry<String, Integer> o2) {
return (o2.getValue()).compareTo(o1.getValue());
}
});

for (int index = 0; index < noRecord; index++) {
System.out.println(list.get(index).getKey() + " : "
+ list.get(index).getValue());

}
inputFile.close();
}
}

Explanation / Answer

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Comparator;

public class WordCount {
   public static void main(String[] args) throws FileNotFoundException {
       Scanner scanner = new Scanner(System.in);
       HashMap<String, Integer> map = new HashMap<>();

       System.out.println("Enter number of sentence: ");
       int n = scanner.nextInt();

       while(n > 0){
           System.out.println("Enter a sentence: ");
           String sentence = scanner.nextLine();
           String[] data = sentence.replaceAll("[^a-zA-Z ]", "").toLowerCase().split("\s+");
           int i = 0;
           while (i < data.length){
               String word = data[i].toLowerCase();
               if (map.containsKey(word)) {
                   map.put(word, map.get(word) + 1);
               } else {
                   map.put(word, 1);
               }
               i++;
           }
           n--;
       }

       ArrayList<Map.Entry<String, Integer>> entries = new ArrayList<>(map.entrySet());
       Collections.sort(entries, new Comparator<Map.Entry<String, Integer>>() {

           @Override
           public int compare(Map.Entry<String, Integer> a, Map.Entry<String, Integer> b) {
               return a.getValue().compareTo(b.getValue());
           }
       });

       for(int i = 0; i < entries.size(); i++){
           System.out.println(entries.get(entries.size() - i - 1).getKey()+": "+entries.get(entries.size() - i - 1).getValue());
       }
   }


} // end class WordCount

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote