web – Parerga und Paralipomena http://www.michelepasin.org/blog At the core of all well-founded belief lies belief that is unfounded - Wittgenstein Sun, 19 Jun 2011 20:23:52 +0000 en-US hourly 1 https://wordpress.org/?v=5.2.11 13825966 Enabling web-audio in Chrome http://www.michelepasin.org/blog/2011/06/19/enabling-web-audio-in-chrome/ Sun, 19 Jun 2011 20:23:52 +0000 http://www.michelepasin.org/blog/?p=1028 I haven’t realized that Chrome has a whole bunch of invisible experimental settings that you can turn on just by going to “about:flags“.

 

Chrome about:flags options view

 

Some of them will open up the musical capabilities of html5, which are pretty cool. Example, once you switch on the ‘web-audio‘ setting on Chrome, if you use a suitable musical musical library you could play a note just by issuing these javascript commands:


var n = Note.fromLatin('A4'); 

var freq = n.frequency(); // returns 440 
var name = n.latin(); // returns "A" 
var octave = n.octave(); // returns 4

 

The full example (with sound, if you’ve turned on the Web Audio setting as mentioned above) can be seen here: musicjs demo.

More info about web audio and related stuff can be found on this blog post: http://pixelist.info/web-audio-it-is-finally-almost-here/

]]>
1028
Python web-parsing libraries http://www.michelepasin.org/blog/2009/07/28/python-web-parsing-libraries/ Tue, 28 Jul 2009 10:06:20 +0000 http://magicrebirth.wordpress.com/?p=233 While continuing the interminable search for the ideal python editor, I run into three really useful libraries for scraping information off webpages:

  • mechanize:  Stateful programmatic web browsing in Python, after Andy Lester’s Perl module WWW::Mechanize
  • scrape:  Python module for web browsing and scraping
  • BeautifulSoup: a Python HTML/XML parser designed for quick turnaround projects like screen-scraping

I had a quick play with the last one, BeautifulSoup, following one of the examples.
Here I’m fetching the ICC Commercial Crime Services weekly piracy report, parsing it with Beautiful Soup, and pulling out the piracy incidents:

from BeautifulSoup import BeautifulSoup
import urllib2

page = urllib2.urlopen("http://www.icc-ccs.org/index.php?
            option=com_fabrik&view=table&tableid=70&calculations=0&Itemid=82")
soup = BeautifulSoup(page)

for incident in soup('td',
         {"class" : "fabrik_row___jos_fabrik_icc-ccs-piracymap2009_testing___narrations"}):
    print "nAn incident :"
    print incident.next.renderContents()
    print incident.next.next.next.next.next.renderContents()

And the result is the following:


An incident :
21.07.2009: 0030 LT: Posn: 01:17.94N – 104:09.24E, Off Tanjong Stapa, Malaysia.
Six robbers armed with long knives in a boat came alongside and boarded a product tanker at anchor.
The robbers tied up the Master and crew members. They stole ship’s and crew properties.
During the incident the Malaysian Marine Police boarded the product tanker and arrested five robbers.
One robber jumped overboard and escaped.

An incident :
12.07.2009: 0330 LT: Posn: 10:16.2N - 107:04.6E, Vungtau outer anchorage, Vietnam.
Robbers boarded a container ship at anchor. They stole ship's stores and properties and escaped.

An incident :
12.07.2009: 0200 LT: Posn: 01:08.91N – 105:45.11E, Off Raffles lighthouse, Singapore Straits.
Five pirates armed with long knives and swords in a five meter long wooden boat approached a tug
towing a barge. Four pirates boarded the tug, and tied up the crew. They stole ship’s properties,
crew personal belongings and cash and escaped. No injuries to crew. VTIS and coast guard Singapore informed.
[Task exited status=0]

Nice and easy uh?

 

]]>
233
A Sneak Preview of Wolfram Alpha http://www.michelepasin.org/blog/2009/05/02/a-sneak-preview-of-wolframalpha/ Sat, 02 May 2009 14:50:54 +0000 http://magicrebirth.wordpress.com/?p=127 It’s the new brainchild of Stephen Wolfram, author of Mathematica. It does look impressive in my opinion – can’t wait to try it live (due to launch some time in may)!

Defined as a Computational Knowledge Engine. It does an awful lot of number-crunching but looks more as a giant closed database than a distributed Web of data, or even a ‘Semantic web’.

Interesting the reaction of competitor Doug Lenat, who although is claiming that Wolfram is not AI (therefore CYC’s got nothing to fear) imho is realizing he didn’t get it right when he set out trying to capture ‘common sense’. After all, all that we find in Wolfram|Alpha is symbolic reasoning. Is that so far from the way Cyc works? This might be a nice departure point for an interesting discussion…

Lenat’s blog post contains some interesting comments on the things that Wolfram|Alpha can’t do (yet):

When it returns information, how much does it actually “understand” of what it’s displaying to you?  There are two sorts of queries not (yet) handled: those where the data falls outside the mosaic I sketched above — such as:  When is the first day of Summer in Sydney this year?  Do Muslims believe that Mohammed was divine?  Who did Hezbollah take prisoner on April 18, 1987? Which animals have fingers? — and those where the query requires logically reasoning out a way to combine (logically or arithmetically combine) two or more pieces of information which the system can individually fetch for you.  One example of this is: “How old was Obama when Mitterrand was elected president of France?”  It can tell you demographic information about Obama, if you ask, and it can tell you information about Mitterrand (including his ruleStartDate), but doesn’t make or execute the plan to calculate a person’s age on a certain date given his birth date, which is what is being asked for in this query.  If it knows that exactly 17 people were killed in a certain attack, and if it also knows that 17 American soldiers were killed in that attack, it doesn’t return that attack if you ask for ones in which there were no civilian casualties, or only American casualties.  It doesn’t perform that sort of deduction.  If you ask “How fast does hair grow?”, it can’t parse or answer that query.  But if you type in a speed, say “10cm/year”, it gives you a long and quite interesting list of things that happen at about that speed, involving glaciers melting, tectonic shift, and… hair growing.

Some nice coverage of it also on ReadWriteWeb and UMBC.

]]>
127
AJAX in Lisp with JQuery http://www.michelepasin.org/blog/2008/02/27/ajax-in-lisp-with-jquery/ Wed, 27 Feb 2008 18:14:30 +0000 http://people.kmi.open.ac.uk/mikele/blog/?p=271 I was asked to give an example of lisp+ajax, so once I prepared it I thought it could be of help to other people too :-)

First of all, I am not a professional lisper at all, just got to know it a little during the last two years. The code I’m presenting here might not be the best one, but it works and essentially shows you how to do cool ajax stuff by using lisp [btw, some of this code comes from somebody who knows about lisp much more than I do, this guy here].

Anyways, let’s get going: we want to create a very very simple webapp that changes the color of some text in the page, using an ajax call. So we’ll have one main page, another page which gives back the code for updating the main page through ajax, and a couple of js files we want to use for the front-end functionality (among them, the fantastic jquery).

First of all, we’ll need two handy packages for setting up a fully-working lisp server: hunchentoot and cl-who (the more you learn about these packages, the better it is – and go through the examples – they’re really useful!). Once you’ve installed them, using asdf-install or whatever-you-like, let’s load them up and set a couple of environment variables.

(asdf:operate 'asdf:load-op :hunchentoot)
(asdf:operate 'asdf:load-op :cl-who)

(use-package :cl-who)
(setq hunchentoot:*catch-errors-p* nil
hunchentoot:*log-lisp-backtraces-p* t)

Then we define a couple of functions for starting/stopping the server and setting up the dispatcher table, which is where the mapping between web-pages in the application and lisp functions in the backend is specified. We’ll have three of these mappings: one for the main page, one for the page that serves the ajax changes (what-color?), and one for passing the static files, such as js and css (static/).



(defvar *web-server* nil)

(defun start-server ()
(setq hunchentoot:*dispatch-table*
(nconc
(mapcar (lambda (args)
(apply #'hunchentoot:create-prefix-dispatcher args))
'(("/static/" serve-static)
("/what-color" what-color?)
("/main" main)))
(list #'hunchentoot:default-dispatcher)))
(setf *web-server* (hunchentoot:start-server :port 3000)))

(defun stop-server ()
(hunchentoot:stop-server *web-server*))

When the start-server function is called, it’ll load the dispatch table and start the sever on port 3000.

The hardest thing to do now, before creating the functions that output the web-pages, is to set up the functions that handle the static files. First of all define your location, so that lisp knows where the root folder of the webapp is. That’s how it looks on my mac – you probably want to change that according to needs.



(defvar *location* "/Users/myname/dev-try/hunchentoot-examples/")

Then the function for serving the static files (serve-static). In this example web-app we’ll only have two of them (see below), but you might want to have more (e.g. css, jpg etc..). The other three functions below basically retrieve the static file, check its mime-type and pass it back to the dispatcher.



(defun serve-static ()
(let* ((uri (hunchentoot:request-uri))
(file (subseq uri (length "/static/"))))
(format *error-output* "serve-static: file=~S~%" file)
(http-write-file file (mime-types file))))

(defun http-write-file (filename mime-type)
"Send contents of FILENAME to the HTTP stream, along with its MIME-TYPE."
(setf (hunchentoot:content-type) mime-type)
(let* ((stream (hunchentoot:send-headers))
(buffer (make-array 1024 :element-type '(unsigned-byte 8 )))
(local-filename (format nil "~astatic/~a" *location* filename)))
(with-open-file (in local-filename :element-type '(unsigned-byte 8 ))
(loop for pos = (read-sequence buffer in)
until (zerop pos)
do (write-sequence buffer stream :end pos)))))

(defun mime-types (filename)
(let ((ext (subseq filename (+ 1 (position #. filename :from-end t)))))
(second (assoc ext '(("txt" "text/plain")
("css" "text/css")
("lisp" "text/plain")
("html" "text/html")
("js" "text/javascript")
("png" "image/png"))
:test #'string=))))

(defun write-http-stream (mime-type payload)
(setf (hunchentoot:content-type) mime-type)
(write-sequence payload (hunchentoot:send-headers)))

That was the trickiest part. Now let’s just create the functions that produce the html code to be passed to the browser/javascript. Before that, just a couple of useful macros that wrap the standard cl-who functions. With-html for creating html code without a header, and with-html-top for creating also the header.



(defmacro with-html (&body body)
`(with-html-output-to-string
(*standard-output* nil :prologue nil :indent nil)
(htm ,@body)))

(defmacro with-html-top (&body body)
`(with-html-output-to-string
(*standard-output* nil :prologue t :indent nil)
,@body))

Finally, the pages we can call from the browser. Main creates the main page of the app, linking to the javascript files. what-color? just outputs a random color name out of a predefined list:



(defun main ()
(with-html-top
(:html (:head
(:title "Home page")
(:script :type "text/javascript" :src "/static/jquery.pack.js")
(:script :type "text/javascript" :src "/static/my_functions.js"))
(:body
(:p :id "text" "Hi there - this is the only page for now")
(:a :href "javascript:changeColor()" "click here to change the color!")))))

(defun what-color? ()
(let ((color-list '("blue" "red" "lavender" "black" "yellow"
"orange" "mandarin" "MistyRose" "Olive")))
(nth (random (length color-list)) color-list)))

That’s it really, for the backend. Now all you have to do is to create a folder called ‘static’ wherever you specified the *location* variable to point at. Inside the folder, put a copy of the latest JQuery code (jquery.pack.js), and create a new file called my_functions.js with this elementary function (sorry – that’s all the ajax we’re getting for now…) . It’s a simple function that calls the server for getting a color name, and uses it to change the color of a dom element.



function changeColor() {
$.get("what-color",
function(data){
$("#text").css("color",data);
}
);
}

Almost done. Enter (start-server) in the lisp listener, and go to http://localhost:3000/main to see your first lisp-ajax application! [if the colors don’t show properly, make sure you are using Firefox]

Have fun!

 

]]>
271