From: <sk...@us...> - 2008-01-24 10:13:18
|
Revision: 425 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=425&view=rev Author: sknappe Date: 2008-01-24 02:13:14 -0800 (Thu, 24 Jan 2008) Log Message: ----------- now partly works with the new sparql component of the dl-learner Modified Paths: -------------- trunk/src/dbpedia-navigator/DLLearnerConnection.php trunk/src/dbpedia-navigator/NaturalConcepts.php trunk/src/dbpedia-navigator/ajaxfunctions.php trunk/src/dbpedia-navigator/index.php Modified: trunk/src/dbpedia-navigator/DLLearnerConnection.php =================================================================== --- trunk/src/dbpedia-navigator/DLLearnerConnection.php 2008-01-24 09:31:09 UTC (rev 424) +++ trunk/src/dbpedia-navigator/DLLearnerConnection.php 2008-01-24 10:13:14 UTC (rev 425) @@ -9,8 +9,8 @@ class DLLearnerConnection { private $DBPediaUrl; - private $DLLearnerUri; - + private $ttl; + private $lang; // private $client; @@ -20,11 +20,14 @@ // ID of the DBpedia knowledge source private $ksID; - function DLLearnerConnection($DBPediaUrl,$DLLearnerUri,$id=0,$ksID=0) + function DLLearnerConnection($id=0,$ksID=0) { ini_set('default_socket_timeout',200); - $this->DBPediaUrl=$DBPediaUrl; - $this->DLLearnerUri=$DLLearnerUri; + require_once("Settings.php"); + $settings=new Settings(); + $this->ttl=$settings->sparqlttl; + $this->lang=$settings->language; + $this->DBPediaUrl=$settings->dbpediauri; $this->client=new SoapClient("main.wsdl"); $this->id=$id; $this->ksID=$ksID; @@ -35,12 +38,6 @@ $id=$this->client->generateID(); $ksID=$this->client->addKnowledgeSource($id,"sparql",$this->DBPediaUrl); return array(0 => $id, 1 => $ksID); - } - - function test() - { - $object=$this->client->test($this->id,$this->ksID); - return $object->item; } function getConceptFromExamples($ttl,$posExamples,$negExamples) @@ -97,95 +94,70 @@ } return $concept; } + + function getTriples($label) + { + $query="SELECT ?pred ?obj ". + "WHERE {<http://dbpedia.org/resource/".str_replace(' ','_',$label)."> ?pred ?obj}"; + $result=$this->getSparqlResult($query); + $ret=array(); + foreach ($result->item as $results){ + $value=$results->item[1]; + if (strpos($value,"@".$this->lang)==(strlen($value)-strlen("@".$this->lang))) $ret[$results->item[0]][]=substr($value,0,strlen($value)-strlen("@".$this->lang)); + if (strpos($value,"@")!=(strlen($value)-strlen($this->lang)-1)) $ret[$results->item[0]][]=$value; + } + return $ret; + } - function getTriples($ttl,$individual) + function getSparqlResult($query) { - $options=array("triples",$individual); - $this->client->startThread($this->id,$this->ksID,$options); + $this->client->applyConfigEntryStringArray($this->id, $this->ksID, "defaultGraphURIs", array("http://dbpedia.org")); + $queryID=$this->client->sparqlQueryThreaded($this->id,$this->ksID,$query); + $running=true; $i = 1; $sleeptime = 1; - + do { // sleep a while sleep($sleeptime); - // see if algorithm is running - if (!$this->client->isThreadRunning($this->id,$this->ksID,"triples")) - { - $object=$this->client->getFromSparql($this->id,$this->ksID,"triples"); - @$array=$object->item; - if ($array==NULL) return array(); - if (count($array)==1) return $array; - $ret=array(); - foreach ($array as $element) - { - $items=preg_split("[<]",$element,-1, PREG_SPLIT_NO_EMPTY); - - // each property can occur multiple times (!) - // bug: $ret[$items[0]]=$items[1]; - - $ret[$items[0]][] = $items[1]; - } - return $ret; + + $running=$this->client->isSparqlQueryRunning($this->id,$this->ksID,$queryID); + if (!$running){ + $result=$this->client->sparqlQuery($this->id,$this->ksID,$queryID); + return $result; } $seconds = $i * $sleeptime; $i++; - } while($seconds<$ttl); - - $this->client->stopSparqlThread($this->id,$this->ksID,"triples"); - return array(); + } while($seconds<$this->ttl); + $this->client->stopSparqlQuery($id,$ksID,$queryID); } - function getSubjects($ttl,$label) + function getSubjects($label) { - $options=array("subjects",$label,15); - $this->client->startThread($this->id,$this->ksID,$options); - $i = 1; - $sleeptime = 1; - - do { - // sleep a while - sleep($sleeptime); - - // see if algorithm is running - if (!$this->client->isThreadRunning($this->id,$this->ksID,"subjects")) - { - $object=$this->client->getFromSparql($this->id,$this->ksID,"subjects"); - return $object->item; - } - - $seconds = $i * $sleeptime; - $i++; - } while($seconds<$ttl); - - $this->client->stopSparqlThread($this->id,$this->ksID,"subjects"); - return array(); + $query="SELECT DISTINCT ?subject\n". + "WHERE { ?subject <http://www.w3.org/2000/01/rdf-schema#label> ?object. ?object bif:contains '\"".$label."\"'@en}\n". + "LIMIT 10"; + $result=$this->getSparqlResult($query); + $ret=array(); + foreach ($result->item as $results){ + $ret[]=$results->item; + } + return $ret; } - function getSubjectsFromConcept($ttl,$concept) + function getSubjectsFromConcept($concept) { - $options=array("conceptSubjects",$concept); - $this->client->startThread($this->id,$this->ksID,$options); - $i = 1; - $sleeptime = 1; - do { - // sleep a while - sleep($sleeptime); - - // see if algorithm is running - if (!$this->client->isThreadRunning($this->id,$this->ksID,"conceptSubjects")) - { - $object=$this->client->getFromSparql($this->id,$this->ksID,"conceptSubjects"); - return $object->item; - } - - $seconds = $i * $sleeptime; - $i++; - } while($seconds<$ttl); - - $this->client->stopSparqlThread($this->id,$this->ksID,"conceptSubjects"); - return array(); + $query="SELECT DISTINCT ?subject\n". + "WHERE { ?subject a <".$concept.">}\n". + "LIMIT 10"; + $result=$this->getSparqlResult($query); + $ret=array(); + foreach ($result->item as $results){ + $ret[]=$results->item[0]; + } + return $ret; } public function loadWSDLfiles($wsdluri){ Modified: trunk/src/dbpedia-navigator/NaturalConcepts.php =================================================================== --- trunk/src/dbpedia-navigator/NaturalConcepts.php 2008-01-24 09:31:09 UTC (rev 424) +++ trunk/src/dbpedia-navigator/NaturalConcepts.php 2008-01-24 10:13:14 UTC (rev 425) @@ -10,6 +10,7 @@ function getNaturalConcept(){ $identifiedConcepts=$this->identifyConcepts(); + $labels=$this->getLabels($identifiedConcepts); return $identifiedConcepts; } @@ -32,9 +33,23 @@ $offset=$treffer[0][1]; } - print_r($ret); return $ret; } + + function getLabels($conc) + { + $query="SELECT DISTINCT "; + for ($i=0;$i<count($conc)-1;$i++) + $query.="?obj".$i.", "; + $query.="?obj".$i."\n"; + $query.="WHERE {\n"; + foreach ($conc as $key=>$con){ + $query.="<".$con."> <http://www.w3.org/2000/01/rdf-schema#label> ?obj".$key.".\n"; + } + $query.="}"; + print $query; + return $query; + } } $conc="EXISTS http://dbpedia.org/property/website.TOP AND http://dbpedia.org/resource/Berlin"; Modified: trunk/src/dbpedia-navigator/ajaxfunctions.php =================================================================== --- trunk/src/dbpedia-navigator/ajaxfunctions.php 2008-01-24 09:31:09 UTC (rev 424) +++ trunk/src/dbpedia-navigator/ajaxfunctions.php 2008-01-24 10:13:14 UTC (rev 425) @@ -9,13 +9,11 @@ function getsubjects($label) { - require_once("Settings.php"); require_once("DLLearnerConnection.php"); - $settings=new Settings(); - $sc=new DLLearnerConnection($settings->dbpediauri,$settings->wsdluri,$_SESSION['id'],$_SESSION['ksID']); + $sc=new DLLearnerConnection($_SESSION['id'],$_SESSION['ksID']); $content=""; - $subjects=$sc->getSubjects($settings->sparqlttl,$label); + $subjects=$sc->getSubjects($label); if (count($subjects)==1) { @@ -26,7 +24,7 @@ else{ foreach ($subjects as $subject) { - $content.="<a href=\"\" onclick=\"xajax_getAndShowArticle('".str_replace("_"," ",substr (strrchr ($subject, "/"), 1))."',-2);return false;\">".str_replace("_"," ",urldecode(substr (strrchr ($subject, "/"), 1)))."</a><br/>"; + $content.="<a href=\"\" onclick=\"xajax_getAndShowArticle('".urldecode(str_replace("_"," ",substr (strrchr ($subject, "/"), 1)))."',-2);return false;\">".urldecode(str_replace("_"," ",substr (strrchr ($subject, "/"), 1)))."</a><br/>"; } } @@ -58,64 +56,63 @@ } } if ($fromCache<0) { - require_once("Settings.php"); require_once("DLLearnerConnection.php"); - $settings=new Settings(); - $sc=new DLLearnerConnection($settings->dbpediauri,$settings->wsdluri,$_SESSION['id'],$_SESSION['ksID']); - $triples=$sc->getTriples($settings->sparqlttl,$subject); + $sc=new DLLearnerConnection($_SESSION['id'],$_SESSION['ksID']); + $triples=$sc->getTriples($subject); $content=""; $searchResult=""; $objResponse = new xajaxResponse(); if (count($triples)==1) - { + { // ToDo: find out why this was treated in a special way by Sebastian $content.=substr($triples,7); } - else if (count($triples)==0) { + else if (count($triples)==0) { $content.="Did not find an article with that name. Similar Articles are shown under 'Search Results'."; - } else { - - // goal: display the data in a nice (DBpedia specific way), maybe similar to - // dbpedia.org/search + } else { - $content=""; - // replace by label(?) - $subject_nice = str_replace("_"," ",urldecode(substr (strrchr ($subject, "/"), 1))); - - // display a picture if there is one + // goal: display the data in a nice (DBpedia specific way), maybe similar to + // dbpedia.org/search + + $content=""; + + // replace by label(?) + //$subject_nice = str_replace("_"," ",urldecode(substr (strrchr ($subject, "/"), 1))); + + // display a picture if there is one if(isset($triples['http://xmlns.com/foaf/0.1/depiction'])) - $content.='<img src="'.$triples['http://xmlns.com/foaf/0.1/depiction'][0].'" alt="Picture of '.$subject_nice.'" style="float:right; max-width:200px;" \>'; - + $content.='<img src="'.$triples['http://xmlns.com/foaf/0.1/depiction'][0].'" alt="Picture of '.$subject_nice.'" style="float:right; max-width:200px;" \>'; + // add short description in english $content.="<h4>Short Description</h4><p>".urldecode($triples['http://dbpedia.org/property/abstract'][0])."</p>"; - - // give the link to the corresponding Wikipedia article - if(isset($triples['http://xmlns.com/foaf/0.1/page'])) - $content .= '<p><img src="images/wikipedia_favicon.png" alt"Wikipedia" /> <a href="'.$triples['http://xmlns.com/foaf/0.1/page'][0].'">view Wikipedia article</a>, '; - $content .= '<a href="'.$subject.'">view DBpedia resource description</a></p>'; - - // display a list of classes - if(isset($triples['http://www.w3.org/1999/02/22-rdf-syntax-ns#type'])) - $content .= '<p>classes: '.formatClassArray($triples['http://www.w3.org/1999/02/22-rdf-syntax-ns#type']).'</p>'; - - if(isset($triples['http://dbpedia.org/property/reference'])) { - $content .= '<p>references: <ul>'; - foreach($triples['http://dbpedia.org/property/reference'] as $reference) - $content .= '<li><a href="'.$reference.'">'.$reference.'</a></li>'; - $content .= '</ul></p>'; - } - - // filter out uninteresting properties and properties which - // have already been displayed - unset($triples['http://xmlns.com/foaf/0.1/page']); - unset($triples['http://xmlns.com/foaf/0.1/depiction']); - unset($triples['http://dbpedia.org/property/abstract']); - unset($triples['http://www.w3.org/1999/02/22-rdf-syntax-ns#type']); - - // display the remaining properties as list which can be used for further navigation - - $content .= '<br/><br/><br/><br/><br/><br/>'.get_triple_table($triples); + + // give the link to the corresponding Wikipedia article + if(isset($triples['http://xmlns.com/foaf/0.1/page'])) + $content .= '<p><img src="images/wikipedia_favicon.png" alt"Wikipedia" /> <a href="'.$triples['http://xmlns.com/foaf/0.1/page'][0].'">view Wikipedia article</a>, '; + $content .= '<a href="'.$subject.'">view DBpedia resource description</a></p>'; + // display a list of classes + if(isset($triples['http://www.w3.org/1999/02/22-rdf-syntax-ns#type'])) + $content .= '<p>classes: '.formatClassArray($triples['http://www.w3.org/1999/02/22-rdf-syntax-ns#type']).'</p>'; + + if(isset($triples['http://dbpedia.org/property/reference'])) { + $content .= '<p>references: <ul>'; + foreach($triples['http://dbpedia.org/property/reference'] as $reference) + $content .= '<li><a href="'.$reference.'">'.$reference.'</a></li>'; + $content .= '</ul></p>'; + } + + // filter out uninteresting properties and properties which + // have already been displayed + unset($triples['http://xmlns.com/foaf/0.1/page']); + unset($triples['http://xmlns.com/foaf/0.1/depiction']); + unset($triples['http://dbpedia.org/property/abstract']); + unset($triples['http://www.w3.org/1999/02/22-rdf-syntax-ns#type']); + + // display the remaining properties as list which can be used for further navigation + + $content .= '<br/><br/><br/><br/><br/><br/>'.get_triple_table($triples); + //store article in session, to navigate between last 5 articles quickly $contentArray=array('content' => $content,'subject' => $subject); if (!isset($_SESSION['nextArticle'])){ @@ -140,7 +137,7 @@ //build Subject and Searchresults if ($fromCache==-1) - $searchResult.="<a href=\"\" onclick=\"xajax_getAndShowSubjects('".str_replace("_"," ",substr (strrchr ($subject, "/"), 1))."');return false;\">Show more Results</a>"; + $searchResult.="<a href=\"\" onclick=\"xajax_getAndShowSubjects('".$subject."');return false;\">Show more Results</a>"; } } else { @@ -152,12 +149,12 @@ if (isset($_SESSION['articles'])) foreach ($_SESSION['articles'] as $key => $value) { - $lastArticles.="<a href=\"\" onclick=\"xajax_getAndShowArticle('',".$key.");return false;\">".str_replace("_"," ",urldecode(substr (strrchr ($value['subject'], "/"), 1)))."</a><br/>"; + $lastArticles.="<a href=\"\" onclick=\"xajax_getAndShowArticle('',".$key.");return false;\">".$value['subject']."</a><br/>"; } //put whole site content into session $_SESSION['artContent']=$content; - $_SESSION['artTitle']=str_replace("_"," ",urldecode(substr (strrchr ($subject, "/"), 1))); + $_SESSION['artTitle']=$triples['http://www.w3.org/2000/01/rdf-schema#label']; $_SESSION['artLast']=$lastArticles; $_SESSION['artSubjects']=$searchResult; @@ -188,7 +185,7 @@ function getAndShowArticle($subject,$fromCache) { $objResponse = new xajaxResponse(); - $objResponse->call('xajax_getarticle',"http://dbpedia.org/resource/".str_replace(" ","_",$subject),$fromCache); + $objResponse->call('xajax_getarticle',$subject,$fromCache); $objResponse->call('xajax_showArticle'); return $objResponse; } @@ -252,11 +249,11 @@ //add Positives and Negatives to Interests $posInterests=""; if (isset($_SESSION['positive'])) foreach($_SESSION['positive'] as $pos){ - $posInterests=$posInterests.str_replace("_"," ",urldecode(substr (strrchr ($pos, "/"), 1)))." <a href=\"\" onclick=\"xajax_toNegative('".$pos."');return false;\"><img src=\"images/minus.jpg\" alt=\"Minus\"/></a> <a href=\"\" onclick=\"xajax_removePosInterest('".$pos."');return false;\"><img src=\"images/remove.png\" alt=\"Minus\"/></a><br/>"; + $posInterests=$posInterests.$pos." <a href=\"\" onclick=\"xajax_toNegative('".$pos."');return false;\"><img src=\"images/minus.jpg\" alt=\"Minus\"/></a> <a href=\"\" onclick=\"xajax_removePosInterest('".$pos."');return false;\"><img src=\"images/remove.png\" alt=\"Minus\"/></a><br/>"; } $negInterests=""; if (isset($_SESSION['negative'])) foreach($_SESSION['negative'] as $neg){ - $negInterests=$negInterests.str_replace("_"," ",urldecode(substr (strrchr ($neg, "/"), 1)))." <a href=\"\" onclick=\"xajax_toPositive('".$neg."');return false;\"><img src=\"images/plus.jpg\" alt=\"Plus\"/></a> <a href=\"\" onclick=\"xajax_removeNegInterest('".$neg."');return false;\"><img src=\"images/remove.png\" alt=\"Minus\"/></a><br/>"; + $negInterests=$negInterests.$neg." <a href=\"\" onclick=\"xajax_toPositive('".$neg."');return false;\"><img src=\"images/plus.jpg\" alt=\"Plus\"/></a> <a href=\"\" onclick=\"xajax_removeNegInterest('".$neg."');return false;\"><img src=\"images/remove.png\" alt=\"Minus\"/></a><br/>"; } $objResponse=new xajaxResponse(); Modified: trunk/src/dbpedia-navigator/index.php =================================================================== --- trunk/src/dbpedia-navigator/index.php 2008-01-24 09:31:09 UTC (rev 424) +++ trunk/src/dbpedia-navigator/index.php 2008-01-24 10:13:14 UTC (rev 425) @@ -5,10 +5,8 @@ ini_set("soap.wsdl_cache_enabled","1"); session_start(); -require_once('Settings.php'); require_once('DLLearnerConnection.php'); -$settings=new Settings(); -$sc=new DLLearnerConnection($settings->dbpediauri,$settings->wsdluri); +$sc=new DLLearnerConnection(); $ids=$sc->getIDs(); $_SESSION['id']=$ids[0]; $_SESSION['ksID']=$ids[1]; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |