Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.Collection;
- import java.util.Comparator;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Scanner;
- import java.util.TreeMap;
- /**
- * TagCloud Generator using java components.
- *
- * @author Robert Montgomery
- */
- public final class TagCloudStandardJava {
- /**
- * Default constructor--private to prevent instantiation.
- */
- private static class PairValueComparator<K, V extends Comparable>
- implements Comparator<Map.Entry<K, V>>, java.io.Serializable {
- @Override
- public int compare(Map.Entry<K, V> arg0, Map.Entry<K, V> arg1) {
- return arg0.getValue().compareTo(arg1.getValue());
- }
- }
- private static class PairKeyComparator<K, V extends Comparable> implements
- Comparator<Map.Entry<K, V>>, java.io.Serializable {
- @Override
- public int compare(Map.Entry<K, V> arg0, Map.Entry<K, V> arg1) {
- return ((Comparable) arg0.getKey()).compareTo(arg1.getKey());
- }
- }
- /**
- * No code needed.
- */
- private TagCloudStandardJava() {
- }
- /**
- * Returns the class based on the occurence of a word.
- *
- * @param occurence
- * The number of times the word appeared
- * @param max
- * The max count of the words in the SM
- * @param min
- * The min count of the words in the SM
- * @return the class for {@code occurence}
- *
- */
- private static String getClass(int occurence, int max, int min) {
- int temp = 0;
- if (occurence == min) {
- return "f11";
- } else {
- temp = ((38 * (occurence - min)) / (max - min));
- }
- return "f" + (temp + 10);
- }
- /**
- * Creates the header for the html page.
- *
- * @param out
- * The FileWriter that corresponds the output file
- * @param header
- * The header based on the input file name.
- * @throws IOException
- */
- private static void createHeader(PrintWriter out, String header)
- throws IOException {
- out.write("<html><head><title>Top 100 words in " + header + "</title>");
- out.write("<link href=\"http://www.cse.ohio-state.edu/software/2231/web-sw2/assignments/projects/tag-cloud-generator/data/tagcloud.css\" rel=\"stylesheet\" type=\"text/css\">");
- out.write("</head><body><h2>Top 100 words in " + header + "</h2>");
- out.write("<hr>");
- out.write("<div class=\"cdiv\">");
- out.write("<p class=\"cbox\">");
- }
- /**
- * Creates the footer for the html page.
- *
- * @param out
- * The FileWriter that corresponds the output file
- * @throws IOException
- */
- private static void createFooter(PrintWriter out) throws IOException {
- out.write("</p></div></body></html>");
- }
- /**
- * Creates the body for the html page.
- *
- * @param out
- * The FileWriter that corresponds the output file
- * @param sal
- * The ArrayList with the words and occurences
- * @param max
- * The max occurence in sm
- * @param min
- * The min occurence in sm
- * @throws IOException
- */
- private static void createBody(PrintWriter out,
- TreeMap<String, Integer> sortedMap, int max, int min)
- throws IOException {
- String size = "";
- int occurence = 0;
- String word = "";
- while (sortedMap.size() > 0) {
- Map.Entry<String, Integer> tempPair = sortedMap.firstEntry();
- word = tempPair.getKey();
- occurence = tempPair.getValue();
- size = getClass(occurence, max, min);
- out.write("<span style=\"cursor:default\" class=\"" + size
- + "\" title=\"count: " + occurence + "\">" + word
- + "</span> \n");
- }
- }
- /**
- * Takes the arrayList containing all the words and creates a hash map that
- * reflects the arrayList in {@code s} while also incremementing the values
- * for each word in the map as its value.
- *
- * @param s
- * The queue containing the words found in the input file
- *
- * @return the map containing all words and their counts in {@code s}
- *
- */
- private static Map<String, Integer> createHashMap(String s1) {
- Map<String, Integer> mWords = new HashMap();
- String s = s1.toLowerCase();
- String word = "";
- char charTemp;
- for (int i = 1; i <= s.length(); i++) {
- charTemp = s.charAt(i - 1);
- if (Character.isLetter(charTemp)) {
- word = word + s.substring(i - 1, i);
- } else if (word.length() > 0) {
- if (mWords.containsKey(word)) {
- int val = mWords.remove(word);
- mWords.put(word, val + 1);
- } else {
- mWords.put(word, 1);
- }
- }
- }
- return mWords;
- }
- /**
- * Main method.
- *
- * @param args
- * the command line arguments; unused here
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
- Scanner in = new Scanner(System.in);
- /*
- * Get input/output file name
- */
- System.out.print("Input file: ");
- String inputFileName = in.nextLine();
- BufferedReader inFileBR = null;
- try {
- inFileBR = new BufferedReader(new FileReader(inputFileName));
- } catch (IOException e) {
- System.err.println("Error creating input file.");
- return;
- }
- System.out.print("Output file: ");
- String outputFileName = in.nextLine();
- PrintWriter outFile = null;
- try {
- outFile = new PrintWriter(new BufferedWriter(new FileWriter(
- outputFileName)));
- } catch (IOException e) {
- System.err.println("Error creating output file.");
- return;
- }
- System.out.print("Number of words to include: ");
- int n = in.nextInt();
- Map<String, Integer> mFinal = new HashMap();
- Map<String, Integer> mTemp = new HashMap();
- String line = inFileBR.readLine();
- while (line != null) {
- mTemp = createHashMap(line);
- mFinal.putAll(mTemp);
- line = inFileBR.readLine();
- }
- TreeMap<String, Integer> sortedMap = new TreeMap<String, Integer>(
- mFinal);
- Collection<Integer> cTemp = sortedMap.values();
- Object[] aTemp = cTemp.toArray();
- int min = (int) aTemp[0];
- int max = (int) aTemp[aTemp.length - 1];
- // Sets up the html page
- createHeader(outFile, inputFileName);
- createBody(outFile, sortedMap, max, min);
- createFooter(outFile);
- try {
- inFileBR.close();
- outFile.close();
- in.close();
- } catch (IOException e) {
- System.err.println("Error closing files.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement