Lab: Getting started with VSCode, Python and RDFlib: Difference between revisions

From info216
Line 58: Line 58:


* The Mueller Investigation was lead by Robert Mueller.
* The Mueller Investigation was lead by Robert Mueller.
** It involved Paul Manafort, Rick Gates, George Papadopoulos, Michael Flynn, and Roger Stone.
** It involved Paul Manafort, Rick Gates, George Papadopoulos, Michael Flynn, Michael Cohen, and Roger Stone.
* Paul Manafort was business partner of Rick Gates.
* Paul Manafort was business partner of Rick Gates.
** He was campaign chairman for Donald Trump
** He was campaign chairman for Donald Trump

Revision as of 10:19, 13 January 2023

Topics

  1. Get you set up for programming knowledge graphs with rdflib in Python.
  2. Get started with basic RDF programming.

Useful materials

VSCode:

RDFlib:

Useful rdflib classes/interfaces and methods

import rdflib:

  • Graph (add)
  • URIRef
  • Literal
  • NameSpace
  • Bnode
  • Collection
  • RDF, FOAF

Documentation for all the RDFlib modules can be found here. Browser search (often Ctrl-F) can be useful to find the module that you want.

Tasks

Installation of and introduction to Python, Pip, VSCode, and RDFlib

  • The Pip command is used to easily install additional python packages for your coding environment.
  • VSCode is an Integrated Development Environment (IDE) that can be used for writing Python code.
  • RDFLib is a Python package for working with RDF. With this we can create RDF graphs, parse and serialize RDF, perform SPARQL queries on graphs and more.

You likely already have Python installed on your computer (it comes with MAC for instance). To find out, type 'python' in the commandline on windows or type 'python --version' in the terminal on your MAC. If you don't already have Python, one alternative is to download and install the Python for your operative system here. Per today (January 2022) RDFlib officially supports python version 3.7, 3.8 and 3.9. Or..

If you are already using a different Python IDE than VSCode that you are comfortable with, then you are free to use that program instead.

Otherwise Download and Install the free and open source Visual Studio Code IDE here.

Start VSCode and create a new project by opening a folder with a new .py file. You can create the folder and file in the terminal or in windows explorer. Install the Python extension by Microsoft in the vscode extension manager. When the Python extension is installed you can use the 'select interpreter' field on the bottom left to use the virtual environment you made, or make sure you are using a supported version of Python.

Programming with RDFlib

Using the VSCode terminal, inside your pip environment, install RDFlib by simply entering: 'pip install rdflib'

To import rdflib to your .py file, write 'import rdflib' at the top of the file. Alternatively to import certain modules of rdflib, you can write e.g 'from rdflib import Graph'.

Programming tasks

When solving the coding tasks you can look at the readings below if needed. Use the classes/interfaces and methods listed on this page.

Write a program that creates an RDF graph containing the triples from the following sentences. Note that one sentence could result in more than one triple. When solving these particular tasks I would try to avoid using Blank Nodes, and instead use full URI's instead. For URIs you can just use an example URI like "http://example.org/". This means that if you talk about a person name Trump, the URI could be "http://example.org/Donald_Trump". Remember that Namespaces can be used so that you don't have to write the full URI everytime.

  • The Mueller Investigation was lead by Robert Mueller.
    • It involved Paul Manafort, Rick Gates, George Papadopoulos, Michael Flynn, Michael Cohen, and Roger Stone.
  • Paul Manafort was business partner of Rick Gates.
    • He was campaign chairman for Donald Trump
    • He was charged with money laundering, tax evasion, and foreign lobbying.
    • He was convicted for bank and tax fraud.
    • He pleaded guilty to conspiracy.
    • He was sentenced to prison.
    • He negotiated a plea agreement.
  • Rick Gates was charged with money laundering, tax evasion and foreign lobbying.
    • He pleaded guilty to conspiracy and lying to FBI.

If you have more time...

If you have more time you can continue extending your graph:

  • George Papadopoulos was adviser to the Trump campaign.
    • He pleaded guilty to lying to the FBI.
    • He was sentenced to prison.
  • Michael Flynn was adviser to Trump.
    • He pleaded guilty for lying to the FBI.
    • He negotiated a plea agreement.

In this FRONTLINE article, Gates', Papadopoulos' and Flynn's lying were different and described in different detail. How can you modify your knowledge graph to account for this?