Lab: Web APIs and JSON-LD: Difference between revisions
No edit summary |
(Added link to exam presentation) |
||
(20 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
==Topics== | ==Topics== | ||
Programming regular (non-semantic) | Programming regular (non-semantic) Web APIs (RESTful web services) with JSON-LD. | ||
We will use Web APIs to retrieve regular JSON data, and | 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. | ||
==Useful Reading== | |||
* [https://stackabuse.com/reading-and-writing-json-to-a-file-in-python/ Reading and writing with JSON - stackabuse.com] | |||
* [https://wiki.uib.no/info216/index.php/Python_Examples Examples] | |||
* [https://realpython.com/python-requests/ Requests - realpython.com] | |||
* [https://json-ld.org/ JSON for Linking Data] | |||
* [https://www.dbpedia-spotlight.org/api Spotlight Documentation] | |||
* [https://docs.google.com/presentation/d/1GpzP6825dxau-W21t4Nj0bccd4nbFplGTyMjRBbiXTU/edit?usp=sharing Exam Presentation] | |||
'''Imports:''' | |||
* import json | |||
* import rdflib | |||
* import requests | * import requests | ||
* import | * import spotlight | ||
==Tasks== | ==Tasks== | ||
=== | === Task 1 === | ||
Write a small program that | 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 who spent 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/ | |||
* Requests Quickstart: https://docs.python-requests.org/en/latest/user/quickstart/ | |||
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"-value. | |||
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: | |||
<syntaxhighlight> | <syntaxhighlight> | ||
pip install rdflib-jsonld | pip install rdflib-jsonld | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== 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 <b>pyspotlight</b> | |||
<syntaxhighlight> | |||
import spotlight | |||
# Note that althoug we import spotlight in python, we need to pip install pyspotlight to get the correct package | |||
SERVER = "https://api.dbpedia-spotlight.org/en/annotate" | |||
annotations = spotlight.annotate(SERVER, "str_to_be_annotated") | |||
</syntaxhighlight> | |||
Latest revision as of 20:13, 3 May 2023
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.
Useful Reading
- Reading and writing with JSON - stackabuse.com
- Examples
- Requests - realpython.com
- JSON for Linking Data
- Spotlight Documentation
- Exam Presentation
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 who spent 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/
- Requests Quickstart: https://docs.python-requests.org/en/latest/user/quickstart/
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"-value. 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
# Note that althoug we import spotlight in python, we need to pip install pyspotlight to get the correct package
SERVER = "https://api.dbpedia-spotlight.org/en/annotate"
annotations = spotlight.annotate(SERVER, "str_to_be_annotated")