code copy from the Book"Collective Intelligence in action"