Tag Cloud Generator Program
Tag Cloud Generator Program
Comparator;
import [Link];
import [Link];
import [Link].Map2;
import [Link];
import [Link].SimpleReader1L;
import [Link];
import [Link].SimpleWriter1L;
import [Link];
import [Link].SortingMachine2;
/**
* A program used to generate a Word Clouds given a file input.
*
* @author Derek Stevens
* @author Jason Tysl
*
*/
public class TagCloudGenerator {
/**
* Definition of whitespace separators.
*/
private static final String SEPARATORS = " \t\n\r,-.!?[]';:/()";
/**
* Comparator used to sort strings alphabetically.
*
* @author Derek Stevens
* @author Jason Tysl
*
*/
private static class KeyLT
implements Comparator<[Link]<String, Integer>> {
@Override
public int compare([Link]<String, Integer> o1,
[Link]<String, Integer> o2) {
if ([Link]().equals([Link]())) {
return [Link]().compareTo([Link]());
}
return [Link]().compareTo([Link]());
}
}
/**
* Compare {@code Integer}s in numerical order.
*/
private static class ValueLT
implements Comparator<[Link]<String, Integer>> {
@Override
public int compare([Link]<String, Integer> o1,
[Link]<String, Integer> o2) {
return [Link]().compareTo([Link]());
}
}
/**
* Returns the first "word" (maximal length string of characters not in
* {@code SEPARATORS}) or "separator string" (maximal length string of
* characters in {@code SEPARATORS}) in the given {@code text} starting at
* the given {@code position}.
*
* @param text
* the {@code String} from which to get the word or separator
* string
* @param position
* the starting index
* @return the first word or separator string found in {@code text} starting
* at index {@code position}
* @requires 0 <= position < |text|
* @ensures <pre>
* nextWordOrSeparator =
* text[position, position + |nextWordOrSeparator|) and
* if entries(text[position, position + 1)) intersection entries(SEPARATORS) = {}
* then
* entries(nextWordOrSeparator) intersection entries(SEPARATORS) = {} and
* (position + |nextWordOrSeparator| = |text| or
* entries(text[position, position + |nextWordOrSeparator| + 1))
* intersection entries(SEPARATORS) /= {})
* else
* entries(nextWordOrSeparator) is subset of entries(SEPARATORS) and
* (position + |nextWordOrSeparator| = |text| or
* entries(text[position, position + |nextWordOrSeparator| + 1))
* is not subset of entries(SEPARATORS))
* </pre>
*/
private static String nextWordOrSeparator(String text, int position) {
StringBuilder firstStringSep = new StringBuilder();
boolean first = [Link]([Link](position)) >= 0;
int finish = position + 1;
while (finish < [Link]()
&& first == [Link]([Link](finish)) >= 0) {
finish++;
}
return [Link](position, finish);
}
/**
* Outputs header for the HTML file in {@code fOut}.
*
* @param fOut
* HTML output destination
* @param numWords
* the number of words in the cloud
* @param fName
* the name of the file
* @requires fIn is open
*/
public static void outputHeader(SimpleWriter fOut, int numWords,
String fName) {
[Link]("<html>");
//prints head
[Link]("\t <head>");
//creates class for changing color when the mouse hovers
[Link]("\t\t<title>" + "Top " + numWords + " words in " + fName
+ "</title>");
[Link](
"<link href=\"[Link]
sw2/assignments/projects/tag-cloud-generator/data/[Link]\" rel=\"stylesheet\"
type=\"text/css\">");
[Link]("\t </head>");
//prints title of body
[Link]("\t" + "<body>");
[Link]("\t\t<h2>" + "Top " + numWords + " words in " + fName
+ "</h2>");
[Link]("\t\t<hr>");
[Link]("<div class=\"cdiv\">");
[Link]("<p class=\"cbox\">");
}
/**
* Outputs the tag cloud of the map that changes their size depending on
* their value.
*
* @param map
* - map with words and frequencies as keys and values
* @param wordOrder
* - SortingMachine to determine what order the words come out in
* @param out
* - file to write out to
*/
public static void outputCloud(Map<String, Integer> map,
SortingMachine<Pair<String, Integer>> wordOrder, SimpleWriter out) {
final int averageFontSize = 20, maxFontSize = 48, minFontSize = 11;
int avg = 0;
for ([Link]<String, Integer> pair : map) {
avg += [Link]();
}
if ([Link]() == 0) {
avg = 1;
} else {
avg = avg / [Link]();
}
/**
* Outputs footer for the HTML file in {@code fOut}.
*
* @param fOut
* HTML output destination
* @requires fIn is open
*/
public static void outputFooter(SimpleWriter fOut) {
[Link]("</p>");
[Link]("</div>");
[Link]("</body>");
[Link]("</html>");
}
/***
* Takes the map replaces it with the {@code numWords} most frequent keys.
*
* @param map
* - unsorted map with all of the words and frequencies
* @param numSort
* - SortingMaching of {@code Integer}s that will sort the map by
* value
* @param numWords
* - the number of words that will be in the Tag Cloud
* @replaces map
* @ensures {@code map} only has the most frequent {@code numWords} words
*/
public static void takeMostCommonWords(Map<String, Integer> map,
SortingMachine<Pair<String, Integer>> numSort, int numWords) {
/**
* Main method used for getting user input.
*
* @param args
*/
public static void main(String[] args) {
SimpleReader consoleIn = new SimpleReader1L();
SimpleWriter consoleOut = new SimpleWriter1L();
[Link]();
[Link]();
}
}