/** * Parse the string representation of the JSON-LD response from the analysis service. * * @param string $json A string representation in JSON-LD format. * * @return array|null Null in case of failure, otherwise an array with Text Annotations, Entity Annotations and * Entities. */ function wl_parse_response($json) { // Check that the provided param is an object. if (!is_object($json)) { return null; } // Define the context for compacting. $context = (object) unserialize(WL_JSON_LD_CONTEXT); // Compact the JSON-LD. $jsonld = jsonld_compact($json, $context); // Get the entity annotations indexed by the textannotation reference. $entity_annotations = array(); // Text Annotations are index by their ID. $text_annotations = array(); // Entities are indexed by their ID. $entities = array(); foreach ($jsonld->{'@graph'} as $item) { $types = wl_type_to_types($item); // Entity Annotation. if (in_array(WL_ENHANCER_NAMESPACE . ':EntityAnnotation', $types)) { array_push($entity_annotations, $item); } else { if (in_array(WL_ENHANCER_NAMESPACE . ':TextAnnotation', $types)) { // Skip Text Annotations that do not have the selection-prefix, -suffix and selected-text. if (isset($item->{WL_ENHANCER_NAMESPACE . ':selection-prefix'}->{'@value'}) && isset($item->{WL_ENHANCER_NAMESPACE . ':selection-suffix'}->{'@value'}) && isset($item->{WL_ENHANCER_NAMESPACE . ':selected-text'}->{'@value'})) { $text_annotations[$item->{'@id'}] = array('_' => $item, 'id' => $item->{'@id'}, 'sel_prefix' => $item->{WL_ENHANCER_NAMESPACE . ':selection-prefix'}->{'@value'}, 'sel_suffix' => $item->{WL_ENHANCER_NAMESPACE . ':selection-suffix'}->{'@value'}, 'sel_text' => $item->{WL_ENHANCER_NAMESPACE . ':selected-text'}->{'@value'}, 'entities' => array()); } } else { $entities[$item->{'@id'}] = $item; } } } // echo '[ $entity_annotations :: ' . count( $entity_annotations ) . ' ]'; // echo '[ $text_annotations :: ' . count( $text_annotations ) . ' ]'; // echo '[ $entities :: ' . count( $entities ) . ' ]'; // Bind the entities to each text annotation via the entity annotation. foreach ($entity_annotations as $item) { // The relation to a Text Annotation. $relation = (string) $item->{WL_DUBLIN_CORE_NAMESPACE . ':relation'}->{'@id'}; // The reference to an entity. $entity_reference = (string) $item->{WL_ENHANCER_NAMESPACE . ':entity-reference'}->{'@id'}; // Get the confidence for the match. $confidence = $item->{WL_ENHANCER_NAMESPACE . ':confidence'}->{'@value'}; // echo "[ relation :: $relation ][ reference :: $entity_reference ]\n"; // Get the Text Annotation (by ref). $text_annotation =& $text_annotations[$relation]; // Get the Entity (by ref) $entity =& $entities[$entity_reference]; // echo "[ entity null :: " . is_null( $entity ) . " ]\n"; // Add the entity to the text annotation entities array. array_push($text_annotation['entities'], array('entity' => $entity, 'confidence' => $confidence)); } return array('text_annotations' => $text_annotations, 'entity_annotations' => $entity_annotations, 'entities' => $entities); }
public function do_feed_rdf() { $format = $this->map_mime_request(); include 'library/EasyRdf.php'; include 'library/object-handlers.php'; include 'library/php-json-ld.php'; $doc[] = (object) array("@id" => "http://dbpedia.org/resource/John_Lennon", "http://schema.org/name" => "Manu Sporny", "http://schema.org/url" => (object) array("@id" => "http://manu.sporny.org/"), "http://schema.org/image" => (object) array("@id" => "http://manu.sporny.org/images/manu.png")); $doc[] = (object) array("@id" => "http://dbpedia.org/resource/John_walton", "http://schema.org/name" => "Manu walton", "http://schema.org/url" => (object) array("@id" => "http://manu.sporny.org/"), "http://schema.org/image" => (object) array("@id" => "http://manu.sporny.org/images/manu.png")); $context = (object) array("name" => "http://schema.org/name", "homepage" => (object) array("@id" => "http://schema.org/url", "@type" => "@id"), "image" => (object) array("@id" => "http://schema.org/image", "@type" => "@id")); // compact a document according to a particular context // see: http://json-ld.org/spec/latest/json-ld/#compacted-document-form $compacted = jsonld_compact($doc, $context); //echo json_encode($compacted, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); foreach ($this->return_namespaces() as $key => $value) { EasyRdf_Namespace::set($key, $value); } EasyRdf_Namespace::delete("rss"); $graph = new EasyRdf_Graph(); $lh_rdf_object_handlers = new LH_rdf_object_handlers($format); if ($theobject = get_queried_object()) { $thetype = get_class($theobject); $action = "do_content_" . $thetype; $graph = $lh_rdf_object_handlers->{$action}($graph, $theobject); } elseif (is_attachment()) { global $wp_query; if ($wp_query->query[attachment_id]) { $args = array('post__in' => array($wp_query->query[attachment_id]), 'post_type' => 'attachment'); } else { $args = array('name' => $wp_query->query[attachment], 'post_type' => 'attachment', 'posts_per_page' => 1); } $attachment_post = get_posts($args); $graph = $lh_rdf_object_handlers->do_content_attachment($graph, $attachment_post[0]); } else { global $wp_query; $graph = $lh_rdf_object_handlers->do_content_wp_query($graph, $wp_query); } $graph = apply_filters("lh_rdf_graph", $graph); $serialize = $graph->serialise($format); $etag = md5($serialize); header("Etag: " . $etag); echo $serialize; }
public function run($manifests) { /* Manifest format: { name: <optional manifest name>, sequence: [{ 'name': <test name>, '@type': ["test:TestCase", "jld:<type of test>"], 'input': <input file for test>, 'context': <context file for add context test type>, 'frame': <frame file for frame test type>, 'expect': <expected result file>, }] } */ global $eol; foreach ($manifests as $manifest) { if (property_exists($manifest, 'name')) { $this->group($manifest->name); } $filepath = $manifest->filepath; foreach ($manifest->sequence as $test) { // read test input files $type = $test->{'@type'}; $options = array('base' => 'http://json-ld.org/test-suite/tests/' . $test->input); try { if (in_array('jld:NormalizeTest', $type)) { $this->test($test->name); $input = read_test_json($test->input, $filepath); $test->expect = read_test_nquads($test->expect, $filepath); $options['format'] = 'application/nquads'; $result = jsonld_normalize($input, $options); } else { if (in_array('jld:ExpandTest', $type)) { $this->test($test->name); $input = read_test_json($test->input, $filepath); $test->expect = read_test_json($test->expect, $filepath); $result = jsonld_expand($input, $options); } else { if (in_array('jld:CompactTest', $type)) { $this->test($test->name); $input = read_test_json($test->input, $filepath); $test->context = read_test_json($test->context, $filepath); $test->expect = read_test_json($test->expect, $filepath); $result = jsonld_compact($input, $test->context, $options); } else { if (in_array('jld:FrameTest', $type)) { $this->test($test->name); $input = read_test_json($test->input, $filepath); $test->frame = read_test_json($test->frame, $filepath); $test->expect = read_test_json($test->expect, $filepath); $result = jsonld_frame($input, $test->frame, $options); } else { if (in_array('jld:FromRDFTest', $type)) { $this->test($test->name); $input = read_test_nquads($test->input, $filepath); $test->expect = read_test_json($test->expect, $filepath); $result = jsonld_from_rdf($input, $options); } else { if (in_array('jld:ToRDFTest', $type)) { $this->test($test->name); $input = read_test_json($test->input, $filepath); $test->expect = read_test_nquads($test->expect, $filepath); $options['format'] = 'application/nquads'; $result = jsonld_to_rdf($input, $options); } else { echo "Skipping test \"{$test->name}\" of type: " . json_encode($type) . $eol; continue; } } } } } } // check results $this->check($test, $test->expect, $result); } catch (JsonLdException $e) { echo $eol . $e; $this->failed += 1; echo "FAIL{$eol}"; } } if (property_exists($manifest, 'name')) { $this->ungroup(); } } }
/** * Frames JSON-LD input. * * @param input the JSON-LD input. * @param frame the frame to use. * @param options framing options to use. * * @return the framed output. */ function jsonld_frame($input, $frame, $options = null) { $rval; // normalize input $input = jsonld_normalize($input); // save frame context $ctx = null; if (property_exists($frame, '@context')) { $ctx = _clone($frame->{'@context'}); } // remove context from frame $frame = jsonld_expand($frame); // create framing options // TODO: merge in options from function parameter $options = new stdClass(); $options->defaults = new stdClass(); $options->defaults->embedOn = true; $options->defaults->explicitOn = false; $options->defaults->omitDefaultOn = false; // build map of all subjects $subjects = new stdClass(); foreach ($input as $i) { $subjects->{$i->{__S}->{'@iri'}} = $i; } // frame input $rval = _frame($subjects, $input, $frame, new stdClass(), $options); // apply context if ($ctx !== null and $rval !== null) { $rval = jsonld_compact($ctx, $rval); } return $rval; }