Det er to pde-filer som brukes i alle tre eksemplene nedenfor.
_words2.pde
// Reading text from an url
// or receive txt directly
// Counting wordlengths [1..25]
//------------------
// datasource
datasource data;
void setup(){
// small as long as we have no datasource
size(600,10);
fill(0);
textFont(createFont("Arial", 12));
smooth();
data=null;
frameRate(10);
//while testing:
//feedMeText("a ab abc abcd abcde abc");
//startMeWithAddress("http://www.it.hiof.no/~borres/commondata/vin/allText.txt");
}
void draw(){
background(255);
if(data!=null){
data.drawMeVertical(0,width,0,height);
}
}
void feedMeText(String t){
// give it some space
size(600,400);
data=new datasource(t);
}
void startMeWithAddress(String address){
// give it some space
size(600,400);
// make the collected text
String[]lines=loadStrings(address);
String txt = join(lines, " ");
feedMeText(txt);
}
og
_datasource.pde
class datasource{
// the word lengths we are interesetd in
public final int MAX_WORD_LENGTH=25;
// the comlete text
private String txt;
// number of words distributed on length
private int[] wordCount;
// total word count
private int sum;
// max value found
private int max;
// constructor
datasource(String txt){
wordCount=new int[MAX_WORD_LENGTH+1];
for(int ix=0;ix < wordCount.length;ix++)
wordCount[ix]=0;
// do the clean and count job
// clean data from non countable word dividers
String[]words=splitTokens(txt,". -,?!\"'");
for(int ix=0;ix < words.length;ix++){
String w=words[ix];
if(w.length() > 0 && w.length() < MAX_WORD_LENGTH+1){
wordCount[w.length()]+=1;
}
}
// sum and max value
sum=0;
max=0;
for(int ix=0;ix < wordCount.length;ix++){
if(wordCount[ix] > max) {
max = wordCount[ix];
}
sum+=wordCount[ix];
}
}
// histogram with vertical columns
public void drawMeVertical(int left,int right,int top, int bottom){
int gap=(right-left)/26;
for(int ix=1;ix < wordCount.length; ix++){
if(ix %5 ==0){
fill(0);
textSize(14);
text(""+ix,left+gap*(ix-1),bottom);
}
float value=map(wordCount[ix],0,max,0,bottom-top-gap-5);
fill(204, 102, 0);
rect(left+gap*(ix-1),bottom-value-gap,gap,value);
}
fill(0);
textSize(20);
String T="Talte ord: "+sum;
text(T,width-textWidth(T)-10,100);
}
}
// do an AJAX -request
// and feed processing with the found data
function getIt1(address,targetNodeId,canvasId){
$('#'+targetNodeId).html('<img src="wait28.gif"/>');
$.ajax({
url:address,
success:function(data)
{
feedProcessing(data,canvasId);
$('#'+targetNodeId).html(address)
},
error:function(data)
{
$('#'+targetNodeId).html('error');
}
});
}
// give processing a string to count and show
function feedProcessing(T,canvasId){
var pjs=Processing.getInstanceById(canvasId);
pjs.feedMeText(T);
}
// simply let processing both find the data and count and show
function startProcessing(address,reportId,canvasId){
var pjs=Processing.getInstanceById(canvasId);
pjs.startMeWithAddress(address);
$('#'+reportId).html(address);
}
Processing direkte
Dersom vi skal hente data fra samme domene som denne siden kan vi la Processing gjøre hele jobben.
Det vil si at gir Processing URL'en vi skal hente data fra. Vi trenger ikke involvere en egen AJAX-lasting.
Dersom vi skal hente data fra et annet domene kan vi gjøre dette ved hjelp av et serverskript.
Her har vi en mulighet for å skrive tekstfiltrering i Python.
_proxy.py
#! /usr/bin/python
"""
Attempt to convey an AJAX-request to an url
as general and smooth as possible
"""
import httplib
from urlparse import urlparse
import cgi
import cgitb; cgitb.enable()
form=cgi.FieldStorage()
print 'Content-type: text/html charset=utf-8\n'
#expect to find an url without parameters
#default
fetch='http://www.it.hiof.no/~borres/commondata/latin/ordtak.txt'
if form.has_key('fetch'):
fetch=form['fetch'].value
parts=urlparse(fetch)
hostpart=parts.netloc
filepart=parts.path
try:
# set up a connection to host
conn = httplib.HTTPConnection(hostpart)
# send a get request for a certain file
conn.request("GET", filepart)
# read the response, header
r1 = conn.getresponse()
# expect 200 as a signal for all is well
if r1.status!=200:
print 'access problem'
print r1.status, r1.reason
else:
S=r1.read()
conn.close()
print S
except:
print 'cannot connect'