code imported for the book collective intelligence. Source downloaded from O'Reilly