Lab: Web APIs and JSON-LD: Difference between revisions
No edit summary |
No edit summary |
||
Line 19: | Line 19: | ||
* Astros API url: http://api.open-notify.org/astros.json | * Astros API url: http://api.open-notify.org/astros.json | ||
* Documentation: http://open-notify.org/Open-Notify-API/People-In-Space/ | * Documentation: http://open-notify.org/Open-Notify-API/People-In-Space/ | ||
The response from the API follows the format | |||
<syntaxhighlight> | |||
{ | |||
"message": "success", | |||
"number": 7, | |||
"people": [ | |||
{ | |||
"craft": "ISS", | |||
"name": "Sergey Ryzhikov" | |||
}, | |||
{ | |||
"craft": "ISS", | |||
"name": "Kate Rubins" | |||
}, | |||
... | |||
] | |||
} | |||
</syntaxhighlight> | |||
We only need to think about whats inside the list of the "people"-key. | |||
To create the graph you can iteratively extract the values of craft and name and add them. As none of the names or craft is a valid URI, they can be crated using the example-namespace. | |||
=== Task 2 === | === Task 2 === |
Revision as of 14:59, 9 March 2021
Lab 7: Accessing and lifting Web APIs (RESTful web services)
Topics
Programming regular (non-semantic) Web APIs (RESTful web services) with JSON-LD.
We will use Web APIs to retrieve regular JSON data, parse them programmatically, where possible link the resources to established DBpedia ones and finally create a RDFLib graph with the data.
Imports
- import json
- import rdflib
- import requests
- import spotlight
Tasks
Task 1
Write a small program that queries the Open Notify Astros API (link below) for the people currently in space. Create a graph from the response connecting each astronaut to the craft they are currently on, for instance using http://example.com/onCraft as a property. Also as the space station is not too big, it is safe to assume that two people whom spent who time on it at the same time know each other, so add this to the graph.
- Astros API url: http://api.open-notify.org/astros.json
- Documentation: http://open-notify.org/Open-Notify-API/People-In-Space/
The response from the API follows the format
{
"message": "success",
"number": 7,
"people": [
{
"craft": "ISS",
"name": "Sergey Ryzhikov"
},
{
"craft": "ISS",
"name": "Kate Rubins"
},
...
]
}
We only need to think about whats inside the list of the "people"-key. To create the graph you can iteratively extract the values of craft and name and add them. As none of the names or craft is a valid URI, they can be crated using the example-namespace.
Task 2
Serialise the graph to JSON-LD, set the context of the JSON-LD object to represent the properties for knows and onCraft.
To do this you need to pip install the json-ld portion of rdflib if you have not already:
pip install rdflib-jsonld
If you have more time
DBpedia Spotlight is a tool for automatically annotating mentions of DBpedia resources in text, providing a solution for linking unstructured information sources to the Linked Open Data cloud through DBpedia.
Build upon the program using the DBpedia Spotlight API (example code below) to use a DBpedia-resource in your graph if one is available. You can add some simple error-handling for cases where no DBpedia resource is found - use an example-entity in stead. Keep in mind that some resources may represent other people with the same name, so try to change the types-parameter so you only get astronauts in return, the confidence-parameter might also help you with this.
The response from DBpedia Spotlight is a list of dictionaries, where each dictionary contains the URI of the resource, its types and some other metadata we will not use now. Set the type of the resouce to the types listed in the response.
Example code for DBpedia Spotlight query
First pip install pyspotlight
import spotlight
SERVER = "https://api.dbpedia-spotlight.org/en/annotate"
annotations = spotlight.annotate(SERVER, "str_to_be_annotated")