06.01.2013 Views

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

340 <strong>Learning</strong> <strong>Processing</strong><br />

18.5<br />

Exercise 18-8: Expand Example 18-5 <strong>to</strong> also search for the next day’s high and low temperature.<br />

Exercise 18-9: Take a look at Yahoo’s “ Word of the Day ” XML feed available at this URL:<br />

http://xml.education.yahoo.com/rss/wotd/ . Use the manual parsing techniques <strong>to</strong> pull out the<br />

Word of the Day from the feed.<br />

Text Analysis<br />

Loading text from a URL need not only be an exercise in parsing out small bits of information. It is<br />

possible with <strong>Processing</strong> <strong>to</strong> analyze large amounts of text found on the web from news feeds, articles, and<br />

speeches, <strong>to</strong> entire books. A nice source is Project Gutenberg ( http://www.gutenberg.org/ ) —which makes<br />

available thousands of public domain texts. Algorithms for analyzing text merits an entire book itself, but<br />

we will look at one simple beginner example here.<br />

Example 18-6 retrieves the entire text of Shakespeare’s play King Lear , and uses split Tokens ( ) <strong>to</strong> make<br />

an array of all the words in the play. Th e sketch then displays the words one by one, along with a count of<br />

how many times the word appears in the text.<br />

Example 18-6: Analyzing King Lear<br />

PFont f; // A variable <strong>to</strong> hold on<strong>to</strong> a font<br />

String[] kinglear; // The array <strong>to</strong> hold all of the text<br />

int counter = 0; // Where are we in the text<br />

// We will use spaces and punctuation as delimiters<br />

String delimiters = " ,.?!;: " ;<br />

void setup() {<br />

size(200,200);<br />

// Load the font<br />

f = loadFont( " Georgia-Bold-16.vlw " );<br />

// Load King Lear in<strong>to</strong> an array of strings<br />

String url = " http://www.gutenberg.org/dirs/etext97/1ws3310.txt " ;<br />

String[] rawtext = loadStrings(url);<br />

// Join the big array <strong>to</strong>gether as one long string<br />

String everything = join(rawtext, " " );<br />

// Split the array in<strong>to</strong> words using any delimiter<br />

kinglear = splitTokens(everything,delimiters);<br />

frameRate(5);<br />

}<br />

void draw() {<br />

background(255);<br />

// Pick one word from King Lear<br />

String theword = kinglear[counter];<br />

// Count how many times that word appears in King Lear<br />

int <strong>to</strong>tal = 0;<br />

Any punctuation is used as a delimiter.<br />

All the lines in King Lear are<br />

fi rst joined as one big String<br />

and then split up in<strong>to</strong> an array of<br />

individual words. Note the use<br />

of splitTokens() since we are<br />

using spaces and punctuation<br />

marks all as delimiters.<br />

This loop counts the<br />

number of occurrences<br />

of the current word being<br />

displayed.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!