Difference between revisions of "Lab: Semantic Lifting - CSV"

From Info216
Line 41: Line 41:
  
  
==Code to Get Started (Optional) ==
+
==Code to Get Started (You could also use your own approach if you want to) ==
  
 
<syntaxhighlight>
 
<syntaxhighlight>
Line 48: Line 48:
 
import pandas as pd
 
import pandas as pd
  
 +
# Load the CSv data as a pandas Dataframe.
 
csv_data = pd.read_csv("task1.csv")
 
csv_data = pd.read_csv("task1.csv")
  
Line 54: Line 55:
 
g.bind("ex", ex)
 
g.bind("ex", ex)
  
# iterate through each row. First I select the subjects of the triples which will be the names.
+
 
 +
# You should probably deal with replacing of characters or missing data here:
 +
 
 +
 
 +
 
 +
# Iterate through each row in order the create triples. First I select the subjects of the triples which will be the names.
 +
 
 
for index, row in csv_data.iterrows():
 
for index, row in csv_data.iterrows():
 
     subject = row['Name'].replace(" ", "_")
 
     subject = row['Name'].replace(" ", "_")
  
     #Continue Code here:
+
     #Continue the Code here:
  
  

Revision as of 00:00, 13 March 2020

Lab 9: Semantic Lifting - CSV

Topics

Today's topic involves lifting the data in CSV format into RDF. The goal is for you to learn an example of how we can convert unsemantic data into RDF.

CSV stands for Comma Seperated Values, meaning that each point of data is seperated by a column.

Fortunately, CSV is already structured in a way that makes the creation of triples relatively easy.

Relevant Libraries

  • Pandas

Tasks

Below are four lines of CSV that could have been saved from a spreadsheet. Copy them into a file in your project folder and write a program with a loop that reads each line from that file (except the initial header line) and adds it to your graph as triples:

"Name","Gender","Country","Town","Expertises","Interests"
"Regina Catherine Hall","F","Great Britain","Manchester","Ecology, zoology","Football, music, travelling"
"Achille Blaise","M","France","Nancy","","Chess, computer games"
"Nyarai Awotwi Ihejirika","F","Kenya","Nairobi","Computers, semantic networks","Hiking, botany"
"Xun He Zhang","M","China","Chengdu","Internet, mathematics, logistics","Dancing, music, trombone"

When solving the task take note of the following:

  • The subject of the triples will be the names of the people. The header (first line) are the columns of data and should act as the predicates of the triples.
  • Some columns like expertise have multiple values for one person. You should create unique triple for each of these expertises.
  • Spaces should replaced with underscores to from a valid URI. E.g Regina Catherine should be Regina_Catherine.
  • Any case with missing data should not form a triple.
  • For consistency, make sure all resources start with a Captital letter.


If You have more Time

  • Extend/improve the graph with concepts you have learned about so far. E.g RDF.type, or RDFS domain and range.
  • Additionaly, see if you can find fitting existing terms for the relevant predicate and classes on DBpedia, Schema.org, Wikidata or elsewhere. Then replace the old ones with those.


Code to Get Started (You could also use your own approach if you want to)

from rdflib import Graph, Literal, Namespace, URIRef

import pandas as pd

# Load the CSv data as a pandas Dataframe.
csv_data = pd.read_csv("task1.csv")

g = Graph()
ex = Namespace("httph://example.org/")
g.bind("ex", ex)


# You should probably deal with replacing of characters or missing data here:



# Iterate through each row in order the create triples. First I select the subjects of the triples which will be the names.

for index, row in csv_data.iterrows():
    subject = row['Name'].replace(" ", "_")

     #Continue the Code here:



print(g.serialize(format="turtle").decode())

Examples