import java.util.ArrayList; /** * Prints the tables for the three report questions, using your WordCounter. * * java WordReport CLEANED_shakespeare.txt CLEANED_reddit_comments_2015.txt * * You do not need to change this file. Your job is to explain what the tables mean. */ public class WordReport { public static void main(String[] args) { if (args.length != 2) { System.out.println("Give two text files, for example:"); System.out.println(" java WordReport CLEANED_shakespeare.txt CLEANED_reddit_comments_2015.txt"); return; } try { report(args[0], args[1]); } catch (UnsupportedOperationException e) { System.out.println(); System.out.println("Stopped: " + e.getMessage() + "."); } } private static void report(String nameA, String nameB) { System.out.println("Reading and counting " + nameA + " ..."); WordCounter a = new WordCounter(); ArrayList wordsA = a.readWords(nameA); a.buildMap(wordsA); System.out.println("Reading and counting " + nameB + " ..."); WordCounter b = new WordCounter(); ArrayList wordsB = b.readWords(nameB); b.buildMap(wordsB); System.out.println(); System.out.printf("%-34s %12s %12s%n", "", "total words", "different"); System.out.printf("%-34s %,12d %,12d%n", nameA, a.totalWordCount(), a.uniqueWordCount()); System.out.printf("%-34s %,12d %,12d%n", nameB, b.totalWordCount(), b.uniqueWordCount()); // Question 1 System.out.println(); System.out.println("QUESTION 1: the 10 most common words in each text"); printSideBySide(a, a.topWords(10), nameA, b, b.topWords(10), nameB); // Question 2 int n = Math.min(wordsA.size(), wordsB.size()); WordCounter firstA = new WordCounter(); firstA.buildMap(first(wordsA, n)); WordCounter firstB = new WordCounter(); firstB.buildMap(first(wordsB, n)); System.out.println(); System.out.printf("QUESTION 2: different words in the first %,d words of each text%n", n); System.out.printf(" %-34s %,d%n", nameA, firstA.uniqueWordCount()); System.out.printf(" %-34s %,d%n", nameB, firstB.uniqueWordCount()); // Question 3 System.out.println(); System.out.println("QUESTION 3: the 10 most common words in each text that the OTHER text never uses"); printSideBySide(a, a.topWordsNotIn(b, 10), "in " + nameA + " only", b, b.topWordsNotIn(a, 10), "in " + nameB + " only"); } /** The first n words of a list. */ private static ArrayList first(ArrayList words, int n) { ArrayList result = new ArrayList(); for (int i = 0; i < n && i < words.size(); i++) { result.add(words.get(i)); } return result; } /** Two lists of words, each with its count, in two columns. */ private static void printSideBySide(WordCounter a, ArrayList left, String leftTitle, WordCounter b, ArrayList right, String rightTitle) { System.out.printf(" %4s %-36s %-36s%n", "rank", shorten(leftTitle), shorten(rightTitle)); for (int i = 0; i < Math.max(left.size(), right.size()); i++) { String l = i < left.size() ? String.format("%-18s %,9d", left.get(i), a.getCount(left.get(i))) : ""; String r = i < right.size() ? String.format("%-18s %,9d", right.get(i), b.getCount(right.get(i))) : ""; System.out.printf(" %4d %-36s %-36s%n", i + 1, l, r); } } private static String shorten(String title) { return title.length() <= 36 ? title : title.substring(0, 33) + "..."; } }