1

I would like to write a python code to express a graph like this:

Jim → is eating → an apple

An apple → is in → the kitchen

in RDF format. I have tried the RDFlib in python but I am confused about how to do it. I would appreciate if anybody could help on this matter.

Edit 1 :

I have defined two URI nodes for Apple and Jim in the first sentence. So I am still confused how I connect between the two nodes with my predicate "is eating" and add them to graph g. I appreciate if anyone could instruct on this too.

from rdflib.namespace import FOAF , XSD

# create a Graph
g = Graph()

# Create an RDF URI node to use as the subject for multiple triples
apple = URIRef("http://example.org/apple")
# Add another node
jim = URIRef("http://example.org/jim")```
  • In its current form, this question is really too broad to be answered here on StackOverflow. I suggest you have a look at https://rdflib.readthedocs.io/en/stable/gettingstarted.html and try out a few things. If you get stuck feel free to ask a more specific question here. – Jeen Broekstra Feb 18 '21 at 03:59
  • 1
    Also: what is "Google RDF Format"? – Jeen Broekstra Feb 18 '21 at 04:00
  • not that I know "Google RDF Format" - and I'm pretty sure it doesn't exists but is plain RDF (not everything on the Web was made by Google, nor have they been the first using RDF and knowledge graphs ...) - clearly, representing "subject - verb - object" needs much more effort as proper URIs have to be generated first. The rest is trivial, creating triples is written in the docs, so you task is to define "An Apple" to some URI – UninformedUser Feb 18 '21 at 08:42
  • [@UninformedUser] Thanks a lot @UninformedUser for your reply. So as you instructed, I have defined two URI nodes for Apple and Jim in the first sentence. So I am still confused how I connect between the two nodes with my predicate "is eating" and add them to graph g. I appreciate if you could instruct on this too. – Jalalkunkov Feb 18 '21 at 17:10
  • You also need URIs for the objects and predicates. RDF is made of subject, predicate and object. Once you have all URI resources, you can create and add triples to the RDF graph – UninformedUser Feb 18 '21 at 18:09
  • The important question: given that you don't know RDF at all it looks like, what is the reason for generating RDF? What is the use case? – UninformedUser Feb 18 '21 at 18:11
  • Additionally regarding the semantics: `` is not equivalent to "an apple", more like "the apple" as in "one particular apple" (at least based on the way your are using it). To specify something that is an apple, you'd go along the lines of `[ a ]` in both triples, so that the two apples don't necessarily have to be the same apple. – IS4 Feb 19 '21 at 13:49

2 Answers2

0

Others have given valuable suggestions about defining your data model in more generic terms.

I think what you are looking for is to use 'add' method to define the actual relationships between the subject, predicate and object. You can use something like below:


    from rdflib import Graph, Literal, RDF, URIRef
    from rdflib.namespace import FOAF , XSD
    
    # create a Graph
    g = Graph()
    
    # Create an RDF URI node to use as the subject for multiple triples
    apple = URIRef("http://example.org/apple")
    # Add another node
    jim = URIRef("http://example.org/jim")
    
    kitchen = URIRef("http://example.org/kitchen")
    
    g.add((jim, Literal("is eating"), apple))
    g.add((apple, Literal("is in"), kitchen))
    
    # print graph data in the Notation3 format
    print(g.serialize(format='n3').decode("utf-8"))

You can replace 'is eating' and 'is in' with appropriate URIRefs if these are going to be required accordingly in your use cases.

You can get more details about adding triples at this link within rdflib docs: https://rdflib.readthedocs.io/en/stable/intro_to_creating_rdf.html#adding-triples

Sid
  • 13
  • 1
  • 1
  • 4
0

Here is a must better formulation of the required triples where a Namespace is used:

from rdflib import Graph, Namespace

EG = Namespace("http://example.org/")

# create a Graph, bind the namespace
g = Graph()
g.bind("eg", EG)

# Create an RDF URI nodes
apple = EG.Apple
jim = EG.Jim
kitchen = EG.Kitchen

# Create URI-based predicates
is_eating = EG.isEating
is_in = EG.isIn

g.add((jim, is_eating, apple))
g.add((apple, is_in, kitchen))

# print graph data in the Notation3 format
print(g.serialize())

# equivalent graph, using the Namespace objects directly:
g2 = Graph()
g2.bind("eg", EG)
g2.add((EG.Jim, EG.isEating, EG.Apple))
g2.add((EG.Apple, EG.isIn, EG.Kitchen))
print(g2.serialize())

Note that none of the nodes or edges (predicates) here are really defined beyond allocating them an example URI. It you really want to use a relationship such as isEating, you are going to need to define an ontology that defines that predicate, something like this:

# ontology document snippet
...
<http://example.org/isEating>
    a owl:ObjectProperty ;
    rdfs:label "is eating" ;
    rdfs:comment "The act of consuming a resource for a living organism's energy and nutrition requirement."@en ;
    rdfs:domain foaf:Person ;
    rdfs:range ex:FoodItem ;
.
...
Nicholas Car
  • 1,164
  • 4
  • 7