Lab: Web APIs and JSON-LD: Difference between revisions

From info216
No edit summary
(Added link to exam presentation)
 
(12 intermediate revisions by 4 users not shown)
Line 1: Line 1:
=Lab 7: Accessing and lifting Web APIs (RESTful web services)=


==Topics==  
==Topics==  
Line 7: Line 5:
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.
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==
==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 json
* import rdflib
* import rdflib
Line 15: Line 21:
==Tasks==
==Tasks==
=== Task 1 ===
=== 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.
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
* 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/
* Requests Quickstart: https://docs.python-requests.org/en/latest/user/quickstart/


The response from the API follows the format
The response from the API follows the format
Line 40: Line 47:
</syntaxhighlight>
</syntaxhighlight>


We only need to think about whats inside the list of the "people"-key.
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.
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.


Line 47: Line 54:


To do this you need to pip install the json-ld portion of rdflib if you have not already:
To do this you need to pip install the json-ld portion of rdflib if you have not already:
 
<syntaxhighlight>
pip install rdflib-jsonld
pip install rdflib-jsonld
</syntaxhighlight>


=== If you have more time ===
== 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.
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.


Line 57: Line 65:
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.
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 ====
=== Example code for DBpedia Spotlight query ===
First pip install pyspotlight
First pip install <b>pyspotlight</b>
<syntaxhighlight>
<syntaxhighlight>
import spotlight
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"
SERVER = "https://api.dbpedia-spotlight.org/en/annotate"
annotations = spotlight.annotate(SERVER, "str_to_be_annotated")
annotations = spotlight.annotate(SERVER, "str_to_be_annotated")
</syntaxhighlight>
</syntaxhighlight>
==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://www.dbpedia-spotlight.org/api Spotlight Documentation]

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

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.

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")