Mayank Agarwal
Mayank Agarwal

Reputation: 21

How to represent predicates in RDF triples <s,p,o> by RDFlib

I am able to generate my RDF triples in .ttl format. I would like to ask two things:

  1. When I save my RDF triples in .ttl format. It is saved taking subjects in alphabetical order automatically(triples subjects follow alphabetical order in my .ttl file foreg: triple's subject starts with "a" comes first, then triple's subject starts with b and so on...) Could anyone tell, why it is so?

  2. When I am trying to apply URIRef to handle rdf triples predicates(URIs) for which I am getting a warning message: 'Predicate' does not look like a valid URI, trying to serialize this will break. What Should I change in my code (As of now I provided OWL.Ontology type for predicate) ?

my code snippet:

    """ Builds a rdf graph with the result triples """
    EX=Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")
    g = Graph()
    for triple in triples:
        g.bind('ex',EX)
        g.bind('foaf', FOAF)
        g.bind('schema', SDO)
        g.bind('dcterms',DCTERMS)
        subj = triple[0]
        pred = triple[1]
        objct =triple[2]
        g.add((subj, OWL.Ontology, objct))
   g.serialize(destination='file' + str(counter) + ".ttl", format='ttl'
    return g```


RDF triples: <s,p,o>
For the sentence: Serena Williams Beats Venus Williams to Win Her 7th Australian Open Title

For predicates getting this warning msg:
Beats to Win does not look like valid URI, trying to serialize this will break.

[[rdflib.term.URIRef('http://dbpedia.org/resource/Serena_Williams'),
rdflib.term.URIRef('Beats to Win'),
rdflib.term.URIRef('http://dbpedia.org/resource/Venus_Williams')],
[rdflib.term.URIRef('http://dbpedia.org/resource/Serena_Williams'),
rdflib.term.URIRef('Beats to Win'),
rdflib.term.URIRef('http://dbpedia.org/resource/Wikipedia')],
[rdflib.term.URIRef('http://dbpedia.org/resource/Serena_Williams'),
rdflib.term.URIRef('Beats to Win'),
rdflib.term.URIRef('http://dbpedia.org/resource/2013_Australian_Open')]]

generated rdf triples file in .ttl: (triples are arranged in alphabetical order automatically and predicates is belong to OWL ontology)

@prefix owl: <http://www.w3.org/2002/07/owl#> .
<http://dbpedia.org/resource/Serena_Williams> owl:Ontology <http://dbpedia.org/resource/2013_Australian_Open>,
<http://dbpedia.org/resource/2014_Australian_Open>,
<http://dbpedia.org/resource/Draw_(chess)>,
<http://dbpedia.org/resource/Injury>,
<http://dbpedia.org/resource/Title>,
<http://dbpedia.org/resource/United_States>,
<http://dbpedia.org/resource/Venus_Williams>,
<http://dbpedia.org/resource/Wikipedia> .

Upvotes: 1

Views: 930

Answers (1)

alexis
alexis

Reputation: 50220

The order of RDF predicates is not meaningful. If RDFlib chooses alphabetical order for the serialization, just roll with it. For actual query results (with SPARQL), you can add an ORDER BY term to control the order; but those are tabular data, not triples.

In RDF, the subject and predicate of triples should be a URI. To get off the ground you can use any URI at all and it's valid RDF, but then choose yourself a URI prefix (https://i.am.learning will do until you understand what's going on), define URIs in that space, and assign them labels with rdfs:label so that they have a human-readable name. Then you can use your predicate (just the URI part) in any number of triples.

Later you can use owl to define your predicates and other URIs as classes, predicates with a domain and range, etc.

Upvotes: 1

Related Questions