I am in the process of righting a program for my advanced wed apps module in college. Im quite new to Processing and could use a little help.
What i am looking to do is there are 3 books in .txt format, the user should be able to select one of the book covers and it will print out on screen data about the book like, how many words, the most used word etc. it must also include a search box so that the user can search for a word and it will print how often this word is used.
So far i have the 3 book covers printing on screen, and i have an array reading one of the .txt files and printing the data on screen. What I am having trouble with is turning the 3 book cover .jpgs into buttons and on selecting these buttons the data changing.
int butAmount = 3; int butHeight = 30; int butWidth = 30; PFont font; int pageNo;
PImage b = loadImage("dubliners-james-joyce.jpg"); PImage r = loadImage("robinson_crusoe_book.jpg"); PImage d = loadImage("gullivers_travels.jpg");
String[] txtLines; // Stores lines from Dubliners.txt String[] txtLines2; // Stores lines from RobinsonsCrusoe.txt String[] txtLines3; // Stroes lines from GulliversTravels.txt String whole; // Puts all lines together String whole2; // Puts all lines together String whole3; // Puts all lines together String[] words; // Seperates the whole string into words later on String[] words2; // Seperates the whole string into words later on String[] words3; // Seperates the whole string into words later on
String[] keyWords; //two arrays into which output of HashBook class will be stored.. one will hold every different word in book, the other how many times those words appear int[] keyCounts;
HashBook Dubhash; //declare an instance of my class and call it 'Dubhash'
String[] common = new String[0]; //will soon store words that appear more than 200 times
void setup() { size(600, 600); smooth(); noStroke(); font = loadFont("ArialMT-48.vlw");
whole = join(txtLines, " "); words = splitTokens(whole, " ,;\".:?!-()"); //end up with a long array full of each individual word of a book
whole2 = join(txtLines2, " "); words2 = splitTokens(whole2, " ,;\".:?!-()"); //end up with a long array full of each individual word of a book
whole3 = join(txtLines3, " "); words3 = splitTokens(whole3, " ,;\".:?!-()"); //end up with a long array full of each individual word of a book
Dubhash = new HashBook(words); //initate Dubhash with the words array Dubhash.fillHash(); //make Dubhash pick out different words and count them (to see code for this function look in class_haskbook tab)
keyWords = Dubhash.hashWords(); //fill the array KeyWords with output from Dubhash keyCounts = Dubhash.hashCount(); //fill the array KeyCounts with second output from Dubhash
//the following explores the things stored in KeyWords and keyCounts, my Hashbook class is no longer used...
println("There are "+keyWords.length+" different words in the book."); //prints to monitor length of KeyWords array which is every different word in book int maxi = max(keyCounts); //sets a new variable to the max number in KeyCounts.. which provides the number of times the book's most common word is used
for (int j=0; j<keyCounts.length; j++) //go through the KeyCounts array and find out which cell the biggest count is stored in { if (keyCounts[j] == maxi) //when the cell with the biggest count is found, get the word from KeyWords that it corresponds with and print this to the monitor { println("The most common word is '" +keyWords[j] + "' which appears " + maxi +" times."); }
if (keyCounts[j] > 200) //as part of the for loop... if a count is greater than 200, put the word from KeyWords that corresponds with it in the 'common' array { common = append(common, keyWords[j]); } } print("Words that appear more than 200 times: "); //print to monitor all the words in 'common' array for (int k=0; k<common.length;k++) { print(common[k]+", "); }
}
void draw() { background(100); for (int i =0; i<butAmount; i++) { button(i,pageNo); //draw each button on screen.. code for this is held in the function i have called 'button', which is sent both i and pageNo } pageDisplay(pageNo); //draw on screen the content of each button's page }
void mousePressed() { for (int i =0; i<butAmount; i++) //if cursor is pressed and over a button, change 'pageNo' to the number associated with that button { int xPos = (width/(butAmount+1))*(i+1); if (mouseX > xPos-butWidth && mouseX < xPos+butWidth && mouseY > height/2-butHeight && mouseY < height/2+butHeight) { pageNo=i; } } }
void pageDisplay (int pNo) { fill(0); textFont(font, 48); text("page "+pNo, 100, 100); //this function will receive the present value of pageNo and print that on screen.. //..the code in here could be much more complex, with something different for each pageNo }
I also have a Hash Map class setup on a different tab that calculates all the data for me
class HashBook {
HashMap wordTable = new HashMap(); //declaring variables for the class String[] bookWords; String[] keys = new String[0]; int[] counts = new int[0];
void fillHash() { //a function that runs through long array for words and picks out all the different ones and counts them for (int i=0; i<bookWords.length; i++) { String oneWord = bookWords[i]; oneWord = oneWord.toLowerCase(); if (wordTable.containsKey(oneWord)) { int val = (Integer)wordTable.get(oneWord); wordTable.put(oneWord, val + 1); } else { wordTable.put(oneWord, 1); } } }
String[] hashWords() { //a function that outputs a string array full of different words Iterator i = wordTable.entrySet().iterator();
while (i.hasNext ()) { Map.Entry me = (Map.Entry)i.next(); String keyName = (String)me.getKey(); keys = append(keys, keyName); } return keys; }
int[] hashCount() { //a function that outputs a int array full fo word counts Iterator i = wordTable.entrySet().iterator();
while (i.hasNext ()) { Map.Entry me = (Map.Entry)i.next(); int count = (Integer)me.getValue(); counts = append(counts, count); } return counts; } }