function getResFromDbpedia($label, $language = "en") { $c = ModelFactory::getSparqlClient("http://dbpedia.org/sparql"); $q = new ClientQuery(); $qs = "SELECT ?x, ?comment\r\n WHERE { ?x rdfs:comment ?comment .\r\n FILTER (lang(?comment) = '{$language}')\r\n ?x rdfs:label '{$label}'@{$language}\r\n }"; $q->query($qs); $r = $c->query($q); $res = array(); foreach ($r as $i) { $res[$i['?x']->getLabel()] = $i['?comment']->getLabel(); } return $res; }
/** * */ public function getInfoPeliculasRelacionadas($peliculas) { if ($peliculas != null && !empty($peliculas) && count($peliculas) > 0) { $valores = array(); foreach ($peliculas as $key => $pelicula) { // $pelicula = $pelicula->getData(); // var_dump($pelicula); try { $modelFactory = new ModelFactory(); $cliente = $modelFactory->getSparqlClient("http://data.linkedmdb.org/sparql"); $query = new ClientQuery(); $consulta = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\r\n PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\r\n PREFIX movie: <http://data.linkedmdb.org/resource/movie/>\r\n PREFIX dc: <http://purl.org/dc/elements/1.1/>\r\n\r\n SELECT DISTINCT ?actor\r\n WHERE {\r\n ?titulo rdf:type movie:film . \r\n ?titulo rdfs:label ?nombre .\r\n \t\t\t\t\t\t\t\t?titulo movie:actor ?actorresource.\r\n \t\t\t\t\t\t\t\t?actorresource movie:actor_name ?actor.\r\n \t\t\t\t\t\t\t\t\r\n FILTER regex(?nombre ,'" . $pelicula . "','i')\r\n } LIMIT 1"; $cliente->setOutputFormat("xml"); $query->query($consulta); $resultado = $cliente->query($query); // $p = xml_parser_create(); xml_parse_into_struct($p, $resultado, $vals, $index); xml_parser_free($p); // $htmlTabla = SPARQLEngine::writeQueryResultAsHtmlTable($resultado); //return $vals; //$nombrePrimerActor = $vals foreach ($vals as $key => $value) { //var_dump($value); if ($value['tag'] == "URI" || $value['tag'] == "LITERAL") { $consulta = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\r\n PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\r\n PREFIX movie: <http://data.linkedmdb.org/resource/movie/>\r\n PREFIX dc: <http://purl.org/dc/elements/1.1/>\r\n\r\n SELECT DISTINCT ?nombre\r\n WHERE {\r\n ?titulo rdf:type movie:film . \r\n ?titulo rdfs:label ?nombre .\r\n \t\t\t\t\t\t\t\t?titulo movie:actor ?actorresource.\r\n \t\t\t\t\t\t\t\t?actorresource movie:actor_name '" . $value['value'] . "'. \t\t\t\t\t\t\t\t\r\n } LIMIT 1"; $cliente->setOutputFormat("xml"); $query->query($consulta); $resultado = $cliente->query($query); // var_dump($resultado); // var_dump($resultado); $p = xml_parser_create(); xml_parse_into_struct($p, $resultado, $vals2, $index); xml_parser_free($p); // $htmlTabla = SPARQLEngine::writeQueryResultAsHtmlTable($resultado); // var_dump($vals); foreach ($vals2 as $key => $k) { if ($k['tag'] == "LITERAL") { $valores[] = $k['value']; } } } } } catch (Exception $e) { } } return $valores; } else { //el usuario no tiene peliculas en su red social //recomendar peliculas actuales. } }
function DBpediaLookup($item, &$resultsLabel, &$resourceFound) { global $QUERY_LLL, $FLICKRSERVICE; $resURI = DBPEDIA_URI_ROOT . wikipediaEncode($item); $photosDocURI = FLICKRWRAPPR_PHOTOS_DOC_URI_ROOT . wikipediaEncode($item); /* Prepare SPARQL query for label, latitude, longitude */ $queryString = parameterize($QUERY_LLL, array("RESOURCE" => $resURI, "GRAPH_URI" => SPARQL_GRAPH_URI)); $query = new ClientQuery(); $query->query($queryString); /* Perform SPARQL query */ $sparqlClient = ModelFactory::getSparqlClient(SPARQL_ENDPOINT_URL); $queryResult = $sparqlClient->query($query); /* Initialize result model */ $resultModel = ModelFactory::getMemModel(); $resultModel->addNamespace('foaf', 'http://xmlns.com/foaf/0.1/'); $resultModel->addNamespace('dcterms', 'http://purl.org/dc/terms/'); $resultModel->addNamespace('rdfs', 'http://www.w3.org/2000/01/rdf-schema#'); /* Perform flickr search */ $processedLabels = array(); $resourceFound = !empty($queryResult); if (is_array($queryResult)) { foreach ($queryResult as $resultElement) { $label = $resultElement['?label']->getLabel(); /* Workaround #1: Virtuoso trailing quote bug */ if (substr($label, -1) == '"') { $label = substr($label, 0, -1); } /* Workaround #2: For some reason, the result is UTF8-encoded twice [seems to be fixed now!] */ //$label = utf8_decode($label); /* Multiple languages may have the same label */ if (in_array($label, $processedLabels)) { continue; } else { $processedLabels[] = $label; } if ($resultElement['?lat'] instanceof Literal && $resultElement['?long'] instanceof Literal) { $flickrPhotos = $FLICKRSERVICE->getFlickrPhotos($label, $resultElement['?lat']->getLabel(), $resultElement['?long']->getLabel(), SEARCH_RADIUS_KM); } else { $flickrPhotos = $FLICKRSERVICE->getFlickrPhotos($label); } /* Process found photos */ foreach ($flickrPhotos as $flickrPhoto) { /* Provide the picture itself (small version) */ $resultModel->add(new Statement(new Resource($resURI), new Resource("http://xmlns.com/foaf/0.1/depiction"), new Resource($flickrPhoto['imgsmall']))); /* Provide its page on flickr */ $resultModel->add(new Statement(new Resource($flickrPhoto['imgsmall']), new Resource("http://xmlns.com/foaf/0.1/page"), new Resource($flickrPhoto['flickrpage']))); } if ($resultModel->size() >= MINIMUM_RESULT_COUNT * 2) { break; } } } /* $queryResult */ if ($resultModel->size() > 0) { /* Add metadata */ $resultModel->add(new Statement(new Resource($photosDocURI), new Resource("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), new Resource("http://xmlns.com/foaf/0.1/Document"))); $resultsLabel = "Photos for DBpedia.org resource " . $_REQUEST['item']; $resultModel->add(new Statement(new Resource($photosDocURI), new Resource("http://www.w3.org/2000/01/rdf-schema#label"), new Literal($resultsLabel, "en"))); $resultModel->add(new Statement(new Resource($photosDocURI), new Resource("http://xmlns.com/foaf/0.1/primaryTopic"), new Resource($resURI))); $resultModel->add(new Statement(new Resource($photosDocURI), new Resource("http://purl.org/dc/terms/license"), new Resource(FLICKR_TOS_URL))); $resultModel->add(new Statement(new Resource($photosDocURI), new Resource("http://xmlns.com/foaf/0.1/maker"), new Resource(FLICKRWRAPPR_HOMEPAGE))); $resultModel->add(new Statement(new Resource(FLICKRWRAPPR_HOMEPAGE), new Resource("http://www.w3.org/2000/01/rdf-schema#label"), new Literal("flickr(tm) wrappr", "en"))); } return $resultModel; }
function getContent() { $model = Zend_Registry::getInstance()->rdfModel; $ret = ""; try { if (trim($this->endpoint) == "") { // use local endpoint $result = $model->sparqlQuery($this->getSparqlQuery()); } else { $c = ModelFactory::getSparqlClient($this->endpoint); $c->setOutputFormat("array"); $q = new ClientQuery(); $q->query($this->sparqlQuery); $result = $c->query($q); } foreach ($result as $line) { $tinst = $this->template; foreach ($line as $binding => $value) { $tinst = str_replace($binding, $value->getLabel(), $tinst); } $ret .= $tinst . "\n"; } } catch (Exception $e) { return "### Error for Sparql query '" . $this->getSparqlQuery() . "'. : " . $e->getMessage() . " ### "; } return $ret; }