Advertisement
Guest User

TCJC

a guest
Apr 17th, 2014
489
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.37 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.FileReader;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.io.PrintWriter;
  7. import java.util.Collection;
  8. import java.util.Comparator;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. import java.util.Scanner;
  12. import java.util.TreeMap;
  13.  
  14. /**
  15. * TagCloud Generator using java components.
  16. *
  17. * @author Robert Montgomery
  18. */
  19. public final class TagCloudStandardJava {
  20. /**
  21. * Default constructor--private to prevent instantiation.
  22. */
  23.  
  24. private static class PairValueComparator<K, V extends Comparable>
  25. implements Comparator<Map.Entry<K, V>>, java.io.Serializable {
  26.  
  27. @Override
  28. public int compare(Map.Entry<K, V> arg0, Map.Entry<K, V> arg1) {
  29. return arg0.getValue().compareTo(arg1.getValue());
  30. }
  31.  
  32. }
  33.  
  34. private static class PairKeyComparator<K, V extends Comparable> implements
  35. Comparator<Map.Entry<K, V>>, java.io.Serializable {
  36.  
  37. @Override
  38. public int compare(Map.Entry<K, V> arg0, Map.Entry<K, V> arg1) {
  39. return ((Comparable) arg0.getKey()).compareTo(arg1.getKey());
  40. }
  41. }
  42.  
  43. /**
  44. * No code needed.
  45. */
  46.  
  47. private TagCloudStandardJava() {
  48. }
  49.  
  50. /**
  51. * Returns the class based on the occurence of a word.
  52. *
  53. * @param occurence
  54. * The number of times the word appeared
  55. * @param max
  56. * The max count of the words in the SM
  57. * @param min
  58. * The min count of the words in the SM
  59. * @return the class for {@code occurence}
  60. *
  61. */
  62.  
  63. private static String getClass(int occurence, int max, int min) {
  64. int temp = 0;
  65. if (occurence == min) {
  66. return "f11";
  67. } else {
  68. temp = ((38 * (occurence - min)) / (max - min));
  69. }
  70. return "f" + (temp + 10);
  71. }
  72.  
  73. /**
  74. * Creates the header for the html page.
  75. *
  76. * @param out
  77. * The FileWriter that corresponds the output file
  78. * @param header
  79. * The header based on the input file name.
  80. * @throws IOException
  81. */
  82.  
  83. private static void createHeader(PrintWriter out, String header)
  84. throws IOException {
  85. out.write("<html><head><title>Top 100 words in " + header + "</title>");
  86. 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\">");
  87. out.write("</head><body><h2>Top 100 words in " + header + "</h2>");
  88. out.write("<hr>");
  89. out.write("<div class=\"cdiv\">");
  90. out.write("<p class=\"cbox\">");
  91. }
  92.  
  93. /**
  94. * Creates the footer for the html page.
  95. *
  96. * @param out
  97. * The FileWriter that corresponds the output file
  98. * @throws IOException
  99. */
  100.  
  101. private static void createFooter(PrintWriter out) throws IOException {
  102. out.write("</p></div></body></html>");
  103. }
  104.  
  105. /**
  106. * Creates the body for the html page.
  107. *
  108. * @param out
  109. * The FileWriter that corresponds the output file
  110. * @param sal
  111. * The ArrayList with the words and occurences
  112. * @param max
  113. * The max occurence in sm
  114. * @param min
  115. * The min occurence in sm
  116. * @throws IOException
  117. */
  118.  
  119. private static void createBody(PrintWriter out,
  120. TreeMap<String, Integer> sortedMap, int max, int min)
  121. throws IOException {
  122. String size = "";
  123. int occurence = 0;
  124. String word = "";
  125. while (sortedMap.size() > 0) {
  126. Map.Entry<String, Integer> tempPair = sortedMap.firstEntry();
  127. word = tempPair.getKey();
  128. occurence = tempPair.getValue();
  129. size = getClass(occurence, max, min);
  130. out.write("<span style=\"cursor:default\" class=\"" + size
  131. + "\" title=\"count: " + occurence + "\">" + word
  132. + "</span> \n");
  133. }
  134.  
  135. }
  136.  
  137. /**
  138. * Takes the arrayList containing all the words and creates a hash map that
  139. * reflects the arrayList in {@code s} while also incremementing the values
  140. * for each word in the map as its value.
  141. *
  142. * @param s
  143. * The queue containing the words found in the input file
  144. *
  145. * @return the map containing all words and their counts in {@code s}
  146. *
  147. */
  148.  
  149. private static Map<String, Integer> createHashMap(String s1) {
  150. Map<String, Integer> mWords = new HashMap();
  151. String s = s1.toLowerCase();
  152. String word = "";
  153. char charTemp;
  154. for (int i = 1; i <= s.length(); i++) {
  155. charTemp = s.charAt(i - 1);
  156. if (Character.isLetter(charTemp)) {
  157. word = word + s.substring(i - 1, i);
  158. } else if (word.length() > 0) {
  159. if (mWords.containsKey(word)) {
  160. int val = mWords.remove(word);
  161. mWords.put(word, val + 1);
  162. } else {
  163. mWords.put(word, 1);
  164. }
  165. }
  166. }
  167. return mWords;
  168. }
  169.  
  170. /**
  171. * Main method.
  172. *
  173. * @param args
  174. * the command line arguments; unused here
  175. * @throws IOException
  176. */
  177. public static void main(String[] args) throws IOException {
  178. Scanner in = new Scanner(System.in);
  179.  
  180. /*
  181. * Get input/output file name
  182. */
  183. System.out.print("Input file: ");
  184. String inputFileName = in.nextLine();
  185. BufferedReader inFileBR = null;
  186. try {
  187. inFileBR = new BufferedReader(new FileReader(inputFileName));
  188. } catch (IOException e) {
  189. System.err.println("Error creating input file.");
  190. return;
  191. }
  192. System.out.print("Output file: ");
  193. String outputFileName = in.nextLine();
  194. PrintWriter outFile = null;
  195. try {
  196. outFile = new PrintWriter(new BufferedWriter(new FileWriter(
  197. outputFileName)));
  198. } catch (IOException e) {
  199. System.err.println("Error creating output file.");
  200. return;
  201. }
  202. System.out.print("Number of words to include: ");
  203. int n = in.nextInt();
  204.  
  205. Map<String, Integer> mFinal = new HashMap();
  206. Map<String, Integer> mTemp = new HashMap();
  207.  
  208. String line = inFileBR.readLine();
  209. while (line != null) {
  210. mTemp = createHashMap(line);
  211. mFinal.putAll(mTemp);
  212. line = inFileBR.readLine();
  213. }
  214.  
  215. TreeMap<String, Integer> sortedMap = new TreeMap<String, Integer>(
  216. mFinal);
  217.  
  218. Collection<Integer> cTemp = sortedMap.values();
  219. Object[] aTemp = cTemp.toArray();
  220. int min = (int) aTemp[0];
  221. int max = (int) aTemp[aTemp.length - 1];
  222.  
  223. // Sets up the html page
  224.  
  225. createHeader(outFile, inputFileName);
  226. createBody(outFile, sortedMap, max, min);
  227. createFooter(outFile);
  228.  
  229. try {
  230. inFileBR.close();
  231. outFile.close();
  232. in.close();
  233. } catch (IOException e) {
  234. System.err.println("Error closing files.");
  235. }
  236. }
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement