';
$AsrResults=wordnet($term); //execute WordNet query
if($AsrResults) {
echo "\n
\n"; // enclose resulting array in tag to display results nicier
print_r($AsrResults); // print resulting array
echo "\n
";
}
else echo 'No definitions found.';
} else echo 'No search term requested. Usage http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?action=define&input=love';
//main function to perform WordNet request, $term - search term
//returns ordered definitions array filled with associative arrays in following format:
// 'type' - word type, according to WordNet types ('n'-noun, 'v'-verb, etc.)
// 'definition' - word definition
// 'samples' - usage examples, empty string if no samples
// 'synonyms' - ordered array of synonyms, empty array if no synonyms
function wordnet($term)
{
$url='http://wordnet.princeton.edu/perl/webwn?c=1&o2=&o0=1&o7=&o5=&o1=&o6=&o4=&o3=&i=-1&h=0000000000&s='.$term;
$page=getpage($url); // query page content
if(!$page||strpos($page,'did not return any results')!==false) return null; // if failed to fetch page or no matches for search term return false
$rvdefs=array(); //definitions array to return
$re='/
(.+?)<\/li>/s'; //regular expression to extract definitions
preg_match_all($re, $page, $defs, PREG_SET_ORDER);
foreach($defs as $def) { //iterate definitions
$adef=array(); //define definition array
$re='/\w:<\/a>/s'; //regular expression for starting link
$def[1]=preg_replace($re, '',$def[1], 1); //remove starting link
//extract type (n-noun, v-verb, etc.)
$re='/ \((\w+?)\) <\/a>/s'; //regular expression to extract type
preg_match($re, $def[1], $am);
$adef['type']=$am[1]; //set definition type
$def[1]=preg_replace($re, '',$def[1], 1); //remove type string
//extract definition
$re='/\((.+?)\)/s'; //regular expression to extract definition
if(preg_match($re, $def[1], $am)) $adef['definition']=$am[1]; else $adef['definition']='';
//extract samples
$re='/(.+?)<\/i>/s'; //regular expression to extract samples
if(preg_match($re, $def[1], $am)) {
$adef['samples']=explode(';',$am[1]);
foreach ($adef['samples'] as $slot=>$sample) {
$the_whole_string = trim($sample);
$the_whole_string = substr( $the_whole_string, 1 ); // trim off leading '"'
$the_whole_string = substr( $the_whole_string, 0, strlen( $the_whole_string ) - 1 );// trim off trailing '"'
$adef['samples'][$slot] = $the_whole_string;
}
}else {
$adef['samples']='';//set samples
}
//extract synonyms
$syns=array();
$re='/