Skip to content

Instantly share code, notes, and snippets.

@cgueret
Created June 24, 2016 11:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cgueret/ad99e938e2f7a7124d62fa65e453ac73 to your computer and use it in GitHub Desktop.
Save cgueret/ad99e938e2f7a7124d62fa65e453ac73 to your computer and use it in GitHub Desktop.
Search for concepts on the RES platform
<?php
/**
* Search for entities
*
* This example issues a search against Acropolis and then enhance the result
* set by fetching additional data for each entry. The result is a set of
* entities available in the RES platform.
*
* The approach consists in issuing two calls:
* - Fetch a the simplified list of results
* - Go over all the results to fetch more data about each entry
*
* @copyright Copyright (c) 2016 BBC
* @license http://unlicense.org/
*/
# Do some init to include EasyRDF and define useful namespaces
require_once realpath(__DIR__)."/vendor/autoload.php";
EasyRdf_Namespace::set('olo', 'http://purl.org/ontology/olo/core#');
# Load the page of results
$graph = EasyRdf_Graph::newAndLoad('http://acropolis.org.uk/?q=macbeth');
# Convert the ordered list of results into an array
$results = array();
foreach($graph->allOfType('olo:Slot') as $entry) {
$index = $entry->get('olo:index')->getValue();
$item = array();
$item['uri'] = $entry->get('olo:item')->getUri();
$results[$index] = $item;
}
ksort($results);
# Iterate over all the results to fetch additional information
foreach($results as $index => $item) {
$graph->load(str_replace('#id', '.rdf', $item['uri']));
foreach($graph->properties($item['uri']) as $property) {
$item[$property] = array();
foreach($graph->all($item['uri'], $property) as $value) {
if (is_a($value, "EasyRdf_Literal"))
array_push($item[$property], $value->getValue());
else if (is_a($value, "EasyRdf_Resource"))
array_push($item[$property], $value->getUri());
}
}
$results[$index] = $item;
}
# Print all the results
print_r ($results);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment