dimanche 20 mars 2011

Text research

In database visualisation we can have a dataset which consists of numbers or of text.
In case of numbers we can easily do some graphics. Text is more versatile, but coding properties of text requires a bit of knowledge of the functions around Strings.
A wonderful text and string tutorial is found here:

http://processing.org/learning/text/

possibly we also need to know the pixel position of a word in a piece of text:
we start searching the position of the word, indicating the character number, if the word is found,
String myText = "we always cry out for love";
int lovePosition = myText.indexOf("love");//indexOf is the JAVA function

then to have the word "love" made special, colored or bold, we can use the tricks mentioned in this link using textWidth, looping through the string, with charAt.

samedi 5 mars 2011

String functions

Loaded data, from a file or a spreadsheet can be in numbers, or in text.
When working with text you have to know about the functions which can manipulate text.
Say we have the text

String hString = `Hello world and especially during carnaval`


And we want to have the seperated words in a list

String hString = "tester and rester";
String[] StringElements = split(hString, " ");
for (int i =0; i< StringElements.length ;i++) println( StringElements[i] );


we have the function split this gives an array which as elements the words if you use the space as a delimiter.

String hString = "tester";
println( hString.length());


To find a specific word the function indexOf is used

String hString = "tester";
println( hString.indexOf("ster"));


This can be useful extraction words from a twitterstream and doing something with these special words.
 

Reading data from an excell document

Lots of data are given in a spreadsheet.

To read a excell sheet we use a library, which comes from a third party
XlsReader-0.1.0, http://bezier.de/processing/libs/xls/
You have to download this library and put it in the right spot, so that Processing can find it.
This is the folder libraries in the Processing folder. In MACOS you have to select the Processing application and ask for the folders contained in it.
Rename the folder in the folder libraries: XlsReader.
Then you can try out an example in the example folder in this folder XlsReader.



import de.bezier.data.*;

XlsReader reader;

int c = 0;

PFont fontA;

void setup ()
{
size (300,100);
reader = new XlsReader( this, "workbook.xls" ); // assumes file to be in the data folder

fontA = loadFont("LucidaBright-Demi-18.vlw");
textFont(fontA, 32);

frameRate(3);
loop();
}

void draw(){
c++;
fill(0,36);
text( reader.getString( 1 + c%3, 0 ) , 10 + random(150), 50 + (c%3)*20 );
println( reader.getString( 1 + c%3, 0 ) );
stroke(155);
if ( c>5 ) { fill(200); rect(0,0,300,100);c=0; }
}


This sketch uses a .xls sheet having three words: ship, u-boat and ufo

reader.getString(x,y ) just reads the value in the cell, x y. 
For a number use: reader.getInt(x,y ) or reader.getFloat(x,y )
That is all there is!


 Of course numbers in the spreadsheet can be used to position an image, or move an image around.

Loading data into arrays

This time we will look at how to load external data into our Processing sketch.
Look at the example LoadFile1 in menu->examples->topics FileIO

FileIO means input / output of files.

A text file is a collection of text, chars, numbers.
To read a file automatically we need to know how the data in the file is formatted.

Seperation can be newline, space, comma.

We will explore different techniques to get the numbers out of the file.

Maybe we can read the data in, but have to store them in a special way. Then we make our own "class". The class being an abstract structure.

An example Class can be a record of a name and an age.


class Record {
String name;
int age;
}

every time we do

Record person1 = new Record ( "Karel de Grote", 44 );

So we store age and name in this "record".
We can also make arrays of these Records:

Record[] persons = new Record [50];

Now we can store 50 of these persons.

Much more complex Classes can be made the same way.

The most used way nowadays to format data is XML.
Processing has an datastructure called XMLElement.
See this page:

http://processing.org/reference/XMLElement.html

With XML we can get the data from for instance twitter and display the tweets as text.

See this former post about that:
http://flashscriptingwonders.blogspot.com/2011/01/processing-and-twitter.html 

A minimal XML example can be found here:
http://www.learningprocessing.com/tutorials/simpleML/ 

This is a library for doing yahoo searches:
http://www.learningprocessing.com/tutorials/yahoo/