0% found this document useful (0 votes)
60 views6 pages

Tag Cloud Generator Program

This document contains the code for a Tag Cloud Generator program that takes in a text file, counts the word frequencies, and outputs an HTML file with the most common words sized proportionally to their counts. It defines classes for sorting the words alphabetically or by count. Methods are provided to extract words from text, build the word frequency map, sort the words, select the most frequent ones, and output the header, cloud, and footer of the HTML file. The main method gets user input for the input/output files and number of words, and runs the program to generate the tag cloud.

Uploaded by

Jason Tysl
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views6 pages

Tag Cloud Generator Program

This document contains the code for a Tag Cloud Generator program that takes in a text file, counts the word frequencies, and outputs an HTML file with the most common words sized proportionally to their counts. It defines classes for sorting the words alphabetically or by count. Methods are provided to extract words from text, build the word frequency map, sort the words, select the most frequent ones, and output the header, cloud, and footer of the HTML file. The main method gets user input for the input/output files and number of words, and runs the program to generate the tag cloud.

Uploaded by

Jason Tysl
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import [Link].

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]();
}

while ([Link]() > 0) {


Pair<String, Integer> pair = [Link]();

int size = averageFontSize * [Link]() / avg;


if (size > maxFontSize) {
size = maxFontSize;
} else if (size < minFontSize) {
size = minFontSize;
}
[Link]("<span style=\"cursor:default\" class=\"f" + size
+ "\" title=\"count: " + [Link]() + "\">" + [Link]()
+ "</span>");
[Link]([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) {

Map<String, Integer> shortenedMap = [Link]();


if (numWords < [Link]()) {
for (int i = 0; i < numWords; i++) {
Pair<String, Integer> pair = [Link]();
[Link]([Link](), [Link]());
}
[Link]();
[Link](shortenedMap);
}
}

public static void buildSortingMachineFromMap(Map<String, Integer> map,


SortingMachine<Pair<String, Integer>> machine) {
for (Pair<String, Integer> pair : map) {
[Link](pair);
}
[Link]();
}
/**
* Generates a map of words mapped to the number of occurrences.
*
* @param fIn
* the file to read words from
* @requires fIn is Open
* @return a Map of words as keys and values as counts
*/
public static void buildOccurrenceMap(SimpleReader fIn,
Map<String, Integer> countMap) {
int position = 0;
while (![Link]()) {
String line = [Link]();
line = [Link]();
//looping through whole line
/*
* can just initialize position here and we wont have to reset it
*/
while (position < [Link]()) {
String nextWord = nextWordOrSeparator(line, position);
if ([Link]([Link](position)) == -1) {
//checking if word exists in map and treating accordingly
if ([Link](nextWord)) {
int val = [Link](nextWord);
val++;
[Link](nextWord, val);
} else {
[Link](nextWord, 1);
}
}
position += [Link]();
}
//reseting position to 0 at the end of the line
position = 0;
}
}

/**
* Main method used for getting user input.
*
* @param args
*/
public static void main(String[] args) {
SimpleReader consoleIn = new SimpleReader1L();
SimpleWriter consoleOut = new SimpleWriter1L();

[Link]("Welcome to the Tag cloud generator.");


[Link]("Enter the file to read words from: ");
String fInName = [Link]();
[Link]("Enter the file to output html to: ");
String fOutName = [Link]();
[Link](
"Enter the number of words you would like in the cloud: ");
int numWords = [Link]();
SimpleReader fIn = new SimpleReader1L(fInName);
SimpleWriter fOut = new SimpleWriter1L(fOutName);

//building data representations needed


KeyLT keyOrder = new KeyLT();
ValueLT valOrder = new ValueLT();
/*
* sortingmachine2 uses selection sort which is better when we don't
* know how many we want to extract
*/
SortingMachine<[Link]<String, Integer>> keySort = new SortingMachine2<>(
keyOrder);
SortingMachine<[Link]<String, Integer>> valSort = new SortingMachine2<>(
valOrder);
Map<String, Integer> tagMap = new Map2<>();
buildOccurrenceMap(fIn, tagMap);
buildSortingMachineFromMap(tagMap, valSort);
takeMostCommonWords(tagMap, valSort, numWords);
buildSortingMachineFromMap(tagMap, keySort);
//outputting HTML
outputHeader(fOut, numWords, fInName);
outputCloud(tagMap, keySort, fOut);
outputFooter(fOut);

[Link]();
[Link]();
}
}

You might also like