go to dada newsfeed!

<?

//dada newsfeed: hole of eponym
//a cutthroat archetype in heedless megacosm
//2003 by eugenio tisselli: crack hurrah!

//this code is the set of rules that generate dada newsfeed.
//it is a collection of functions and procedures that instruct the computer
//to do the following task:
//1. get a random news headline from a current news server
//2. get a random image from an image bank, based on a random word taken from the headline
//3. combine these two elements in a web-collage and display them on a page
//4. if the user clicks on the image, a new image and a new headline are displayed
//5. if the user clicks on the headline, a new image is displayed,
//and the headline is transformed as the computer attempts to get a synonym for
//every word in the headline. these synonyms are taken from an internet
//thesaurus. if no synonym is found, the original word is preserved.

//sometimes dada newsfeed can take too long...
//if this happens, don't worry, just click again... or wait
//dada newsfeed has nothing to do with speed or efficiency!

//code is commented, so that even non-programmers can get a glimpse
//of what's happening. click on the go to dada newsfeed! links
//to see the code in action

//written in php
//this code is free for all to use and modify

//
//getSynonym: this function receives a word (in english)
//as parameter, and returns a synonym taken from
//thesaurus.reference.com
//if no synonym is found, a blank string is returned
//

function getSynonym($word) {

$synonym = "";
$thesaurus="http://thesaurus.reference.com/search?q=$word"; //url to the thesaurus, attempting to find the word "$word"

$fd = fopen($thesaurus,"r"); //open thesaurus
while(!feof($fd)) {

$pgdata .= fread($fd, 5000); //read thesaurus
$start = strpos($pgdata, "<tr><td align=right valign=top><b>Synonyms:</b>&nbsp;&nbsp;</td><td>");
$end = strpos($pgdata, "<tr><td align=right valign=top><b>Concept:</b>&nbsp;&nbsp;</td>");
$size= $end-$start;

$synonym = strip_tags(substr($pgdata, $start, $size)); //get synonyms for current word
$synonym = ereg_replace ("Synonyms:", "", $synonym);

$words = explode(",",$synonym); //put synonyms into a list

if (sizeof($words)>0) {

$synonym = $words[rand(0,sizeof($words)-1)]; //get a random element from the list

} else {

$synonym = ""; //no synonyms, return blank string

}
if (strpos($synonym,"Antonyms:") > 0) {

$synonym = ""; //sometimes an antonym is found, in that case, return a blank

}

}
return $synonym;

}

//
//NumGen: This function returns a random number
//with a number of digits sepcified by the 'length'
//parameter
//

function NumGen($length){

for ($i = 1; $i <= $length; $i++) {

if ($i == 1)

$randnum = rand(0, 9);

else

$randnum .= rand(0, 9);

}

return $randnum;

}

//
//Multi_strpos: returns an array of all occurrences
//of a pattern inside a text string
//

function Multi_strpos($pattern, $sequence)
{
$n = -1;
while (ereg($pattern, $sequence)) {

$n++;
$fragment = split($pattern, $sequence);
$trimsize = (strlen($fragment[0]))+1; $sequence = "*".substr($sequence, $trimsize);
$position[$n] = (strlen($fragment[0]) + $position[($n-1)]);

}

return $position;

}

//
//getImageFromWord: This function returns
//a URL for an image taken from google image search
//

function getImageFromWord($word_image) {

$imageUrl = "";
$source="http://images.google.com/images?q=".$word_image;
$fd = fopen($source,"r"); //open google image search
while(!feof($fd)) {

$pgdata .= fread($fd, 5000); //read google results

}

$arr_image = Multi_strpos("\/images\?q\=tbn\:",$pgdata); //get al the image url's positions
$start = $arr_image[rand(0,sizeof($arr_image))]; //get one of the images
$end = strpos($pgdata, "&", $start);
$size = $end-$start;

if ($size > 0) {

$imageUrl = "<img src=\"http://images.google.com".urldecode(substr($pgdata,$start,$size)); //extract image url
//if the image is not valid, get a random image from altavista thumbnail gallery

$image_info = @getimagesize("http://images.google.com".substr($pgdata,$start,$size));
$x_size=$image_info[0];
$y_size=$image_info[1];
$type=$image_info[2];
if ($x_size <= 0 || $y_size <= 0 || $type == 0) {

$imageUrl = "<img src=http://thm-br1r2.search.vip.scd.yahoo.com/image/21".NumGen(7)." height=345 width=345>";

} else {

$img_width = $x_size * 3;
$img_height = $y_size * 3;
$imageUrl .= "\" width=$img_width height=$img_height>";

}

}

return $imageUrl;

}

//
//here is where it all begins:
//


//if there is no current headline, get a new one
if (!isset($story)) {

$source = "Top stories"; //news category at moreover.com
$urlfeed = "http://p.moreover.com/cgi-local/page?c=".rawurlencode($source)."&o=xml"; //get stories from moreover.com

$xml = fopen($urlfeed,"r");

if(!$xml)die("Connect to newssource failed.");

$xmldata = fread($xml ,640000);
eregi("(<article id.*>.*</article>)", $xmldata ,$articlearray); //get all the articles
$articles = explode("<article", $articlearray[0]); //and put them into a list

ereg("<headline_text>(.*)</headline_text>",$articles[rand(1,sizeof($articles))], $headline); //get just a random headline
$story = $headline[1];
$story = stripslashes($story); //strip the headline of slashes

}

//we got a headline, now we want to
//get synonyms for the headline´s words

$words = explode(" ",$story); //put the headline´s words into a list

if (isset($repeat)) {

for ($i=0; $i<sizeof($words); $i++) { //repeat this operation for every element of the list

$words[$i] = stripslashes($words[$i]); //strip slashes, same as above...

//the following line excludes 2-letter words and some other specific words.
//this is done to maintain a basic structure for the headline, and not transform it too much
//these rules can of course be modified, or new rules can be added
//the excluded words are arbitrary, and i just add some as time goes by
//this could be done in a more elegant way, using an "excluded-word" list, for example...

if ((strlen($words[$i]) > 2) && ($words[$i] != "for") && ($words[$i] != "the") && ($words[$i] != "and") && ($words[$i] != "but") && ($words[$i] != "from") && ($words[$i] != "with")) {

$synonym = trim(getSynonym($words[$i])); //if the word qualifies, get a synonym for it
if ($synonym != "") {

$words[$i] = eregi_replace("&nbsp;","",$synonym); //strip blanks

}
//if no synonym was found, the original word is preserved

}

}

}

$story = implode(" ",$words); //now re-build the story with all the words
$story = ucfirst(strtolower($story)); //first character is uppercase

//now the image: get an image from google image search, based on a
//random word in the headline.
//first, get the random word.
//to get interesting images, long words are prefered
//PLEASE NOTE: IMAGES CAN BE EXPLICIT

$rand_word = "";
$counter = 0;
while ((strlen($rand_word) < 3) && ($counter < 10)) {

$rand_word = $words[rand(0,sizeof($words)-1)];
$counter++;

}

//now try to find an image related to this word:

$image = getImageFromWord($rand_word);

$link = "<a href=\"dadanewsfeed.php?story=".$story."&repeat=1\">";

//html follows...

?>

<html>
<head>
<title>dadanewsfeed</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#000000" text="#FFFFFF" link="#FFFFFF" vlink="#FFFFFF" alink="#FFFFFF">
<div align="center">
<a href=dadanewsfeed.php><? echo $image; ?></a>
<p><font face="Courier New, Courier, mono" size="4"><? echo $link; echo $story; ?></a></font></p>
</div>
</body>
</html>

<? unset($story); ?>

go to dada newsfeed!