EzDevInfo.com

rdflib

RDFLib is a Python library for working with RDF, a simple yet powerful language for representing information. rdflib 4.2.1 — rdflib 4.2.1 documentation

RDFlib 'on disk' store

After 2 days of research I (a newby) still can't figure out what 'on disk' stores are available in RDFFlib 3.1.0. If you have a working example, that would be nice to see... For my app I prefer SQLite. I need no access to online RDF stores, I want to store info about relations inside the organisation in RDF. Thanks


Source: (StackOverflow)

How to run IN and NOT IN SPARQL statements in python rdflib to remove the intersection of two graphs

I'm trying to use the IN and NOT IN statements (that were if I understand correctly, introduced in SPARQL 1.1) on the python implementation of SPARQL (now in rdfextras) but it seems that the syntax is not recognised.

Let's consider two sets (A and B). I want to output what is in Set A, removing what is in Set B.

SELECT ?title WHERE {
   some logic defining ?item and ?SetB
   FILTER (?item NOT IN ?SetB)
}

Maybe this particular thing was added in SPARQL 1.1 and is not supported by rdfextra, in which case I would love to have a workaround (or how to do it without using the NOT IN keyword)


Source: (StackOverflow)

Advertisements

Using a prefix in RDFLIB

I would like to use a short prefix to designate a namespace in rdflib but I am having trouble. I think the answer must be very simple. Here is the offending code:

g = rdflib.parse("some_rdf.rdf")

rdf=rdflib.Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#")

print "Name Spaces:"

for ns in g.namespaces():
    print ns

print "Matching Triples"
print "length of type full uri",len([i for i in g.triples((None,rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),None))])
print "length of type truncated uri",len([i for i in g.triples((None,rdflib.term.URIRef('rdf:type'),None))])
print "length of type , using namespace",len([i for i in g.triples((None,rdf.type,None))])

And the output is:

Name Spaces:

('xml', rdflib.term.URIRef('http://www.w3.org/XML/1998/namespace'))
(u'foaf', rdflib.term.URIRef('http://xmlns.com/foaf/0.1/'))
(u'z', rdflib.term.URIRef('http://www.zotero.org/namespaces/export#'))
('rdfs', rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#'))
(u'bib', rdflib.term.URIRef('http://purl.org/net/biblio#'))
(u'dc', rdflib.term.URIRef('http://purl.org/dc/elements/1.1/'))
(u'prism', rdflib.term.URIRef('http://prismstandard.org/namespaces/1.2/basic/'))
('rdf', rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#'))
(u'dcterms', rdflib.term.URIRef('http://purl.org/dc/terms/'))
Matching Triples
length of type full uri 132
length of type truncated uri 0 !!!This is wrong should be 132
length of type , using namespace 132

What am I doing wrong?


Source: (StackOverflow)

How to export graph in RDF file using RDFLIB (python)

I'm trying to generate RDF data using RDFLIB.

Here comes a minimal example:

from rdflib import Namespace, URIRef, Graph
from rdflib.namespace import RDF, FOAF

data = Namespace("http://www.example.org#")

g = Graph()

g.add( (URIRef(data.Alice), RDF.type , FOAF.person) )
g.add( (URIRef(data.Bob), RDF.type , FOAF.person) )
g.add( (URIRef(data.Alice), FOAF.knows, URIRef(data.Bob)) )

#write attempt
file = open("output.txt", "w")
file.write(g.serialize(format='turtle'))

The code bellow results in the following error :

file.write(g.serialize(format='turtle'))
TypeError : must be str, not bytes

Okay, so let's modify the code and replace the last line by:

file.write(str(g.serialize(format='turtle')))

No more error, but the result is broken as the content of the output file consist of a single line text:

b'@prefix ns1: <http://xmlns.com/foaf/0.1/> .\n@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n@prefix xml: <http://www.w3.org/XML/1998/namespace> .\n@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n\n<http://www.example.org#Alice> a ns1:person ;\n    ns1:knows <http://www.example.org#Bob> .\n\n<http://www.example.org#Bob> a ns1:person .\n\n'

This text obviously contains the correct information, but do not provide them in a correct format, using "\t" instead of a true tab for example.

Do you know how to make a clean export of graph into a file using RDFLIB and python 3.4 ?

Sincerely, VAISSE-LESTEVEN Arthur


Source: (StackOverflow)

Adding RDF text to graph RDFlib

How do I add RDF text that has been assigned to a variable (this text has also been validated and is correct RDF) to a graph in RDFlib?

I have a variable assigned to a bunch of text in RDf format like below:

block = '''<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:lem="http://www.example.eu/lem#" xmlns:lexinfo="http://www.sample.net/2.0/info#">
    <lem:Lexicon rdf:about="http://myserver/myns#lex">
.......................
.......................
</rdf:RDF>'''

I know I could write this to a file and then read that file into the graph but I was wondering is there a way of adding this to the graph in the same file (like you would do for triples) similar to this:

graph.add(block)

The end game is to display them in N3 format using this:

print (wordnet.graph.serialize(format='n3'))

Source: (StackOverflow)

Using owl:Class prefix with rdflib and xml serialization

I would like to use the owl: prefix in the XML serialization of my RDF ontology (using rdflib version 4.1.1); unfortunately I'm still getting the serialization as rdf:Description tags. I have looked at the answer about binding the namespace to the graph at RDFLib: Namespace prefixes in XML serialization but this seems to only work when serializing using the ns format rather than xml format.

Let's be more concrete. I'm attempting to get the following ontology (as taken from Introducing RDFS and OWL) in XML as follows:

<!-- OWL Class Definition - Plant Type -->
<owl:Class rdf:about="http://www.linkeddatatools.com/plants#planttype">

    <rdfs:label>The plant type</rdfs:label>
    <rdfs:comment>The class of all plant types.</rdfs:comment>

</owl:Class>

Here is the python code for constructing such a thing, using rdflib:

from rdflib.namespace import OWL, RDF, RDFS
from rdflib import Graph, Literal, Namespace, URIRef

# Construct the linked data tools namespace
LDT   = Namespace("http://www.linkeddatatools.com/plants#")

# Create the graph
graph = Graph()

# Create the node to add to the Graph
Plant = URIRef(LDT["planttype"])

# Add the OWL data to the graph
graph.add((Plant, RDF.type, OWL.Class))
graph.add((Plant, RDFS.subClassOf, OWL.Thing))
graph.add((Plant, RDFS.label, Literal("The plant type")))
graph.add((Plant, RDFS.comment, Literal("The class of all plant types")))

# Bind the OWL and LDT name spaces
graph.bind("owl", OWL)
graph.bind("ldt", LDT)

print graph.serialize(format='xml')

Sadly, even with those bind statements, the following XML is printed:

<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
>
  <rdf:Description rdf:about="http://www.linkeddatatools.com/plants#planttype">
    <rdfs:subClassOf rdf:resource="http://www.w3.org/2002/07/owl#Thing"/>
    <rdfs:label>The plant type</rdfs:label>
    <rdfs:comment>The class of all plant types</rdfs:comment>
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
  </rdf:Description>
</rdf:RDF>

Granted, this is still an Ontology, and usable - but since we have various editors, the much more compact and readable first version using the owl prefix would be far preferable. Is it possible to do this in rdflib without overriding the serialization method?

Update

In response to the comments, I'll rephrase my "bonus question" as simple clarification to my question at large.

Not a Bonus Question The topic here involves the construction of the OWL namespace formatted ontology which is a shorthand for the more verbose RDF/XML specification. The issue here is larger though than the simple declaration of a namespace prefix for shorthand for only Classes or Properties, there are many shorthand notations that have to be dealt with in code; for example owl:Ontology descriptions should be added as good form to this notation. I am hoping that rdflib has support for the complete specification of the notation- rather than have to roll my own serialization.


Source: (StackOverflow)

retrieving object based on its URI using RDFAlchemy and rdflib

I am using RDFAlchemy and rdflib graph for the source of the data, i.e. rdfalchemy.rdfSubject.db. How can I have rdfalchemy map an object if I have its uri? calling the constructor with the uri creates an object but doesn't retrieve values of other properties from the graph. Using get_by(resUri='http://...') yields an AttributeError

class Book(rdfAlchemy.rdfSubject):
  rdf_type = BIBO.Book
  isbn = rdfalchemy.rdfSingle(BIBO.isbn10)
Book.get_by(resUri='') # AttributeError
b = Book(uri) #a book identified with uri exists in the data 
b.title #empty string

Source: (StackOverflow)

printing out individuals for each concept in an ontology using rdflib

I have and ontology written in OWL/RDF(using Protege). This ontology has been already populated with some individuals for each concept. I have ported it in to python using rdflib and FuXi packages. And I can successfully parse my Ontology and put in a graph. Now the only thing that I need to to do is that to print out all individuals for each concept. Does anyone know how I can do that?


Source: (StackOverflow)

Filtering a n3/turtle file with rdflib and sparql queries

I am trying to filter a turtle file using pyrdf with sparql. But I noticed that sparql queries lose information over the context of the elements. I would like then to re-print the result of the query as a turtle file, is it possible to do this without manually scanning all the subfields of the element? We have data about locations formatted like this:

:pt0001
     vcard:category "Poste e Telegrafi"
    ; vcard:fn "Ufficio Bologna 1"
    ; vcard:extended-address "Via Cairoli 9, Bologna BO, Italy"
    ; vcard:latitude "44.504192"
    ; vcard:longitude "11.338661"
    ; vcard:tel "051 243425"
    ; vcard:fax "051 244459"
    ; cs:opening "Mon, Tue, Wed, Thu, Fri: 0800-1330. Sat: 0800-1230."
    ; cs:closing "01-01, 01-06, P, LA, 04-25, 05-01, 06-02, 08-15, 11-01, 12-08, 12-25, 12-26: .".

For example we want only locations that have a name (fn). Thanks for any tip you find...


Source: (StackOverflow)

Python: Using RDFLIB to graph a Sesame database

Is it possible to draw a graph of a Sesame RDF database using RDFLIB? This is what I tried:

endpoint = "http://127.0.0.1:8080/openrdf-workbench/repositories/movies/explore?resource=%3Cfile%3A%2F%2Fmovies_export.rdf%3E"

from rdflib import Graph
g = Graph()
g.parse(endpoint) 

This is the error:

Traceback (most recent call last):
  File "C:\Software\rdflib\movieGraph.py", line 10, in <module>
    g.parse(endpoint)
  File "c:\python26_32bit\lib\site-packages\rdflib\graph.py", line 756, in parse

    parser = plugin.get(format, Parser)()
  File "c:\python26_32bit\lib\site-packages\rdflib\plugin.py", line 89, in get
    raise PluginException("No plugin registered for (%s, %s)" % (name, kind))
rdflib.plugin.PluginException: No plugin registered for (application/xml, <class
 'rdflib.parser.Parser'>)

I think the only trick is specifying a proper URL to cause Sesame to return a .rdf xml layout.

Author of question: reposted to http://answers.semanticweb.com/questions/9414/python-using-rdflib-to-graph-a-sesame-database (see answer there)


Source: (StackOverflow)

How to perform arithmetic operations in Sparql with python?

I am writting a public domain calculator, whose code is available at: https://github.com/okfn/pdcalc/blob/master/pd/map.rdf

The code is currently unable to properly determine the public domain status of a work because of an issue that has been encountered with sparql 1.0: it does not appear to be possible to perform arithmetic operation on dates, which means that the calculator cannot determine e.g. whether or not the work has been published 70 years after the death of the author. Unfortunately, none of the standard python's rdf libraries have yet implemented a support for sparql 1.1 Hence, I was wondering whether anyone had any suggestion as to how to overcome this limitation, or maybe knows of any python library with a better support of sparql ?

Looking forward to your feeback !


Source: (StackOverflow)

Using SPARQL for limited RDFS and OWL reasoning

What I'm currently using rdflib for creating and managing RDF graphs in Python. RDFlib doesn't do any RDFS or OWL reasoning, though. This leads to results like the following:

  1. If I have

    A rdf:type MyType .
    MyType rdfs:subClassOf SuperType .
    

    and I ask

    select ?x where {?x rdf:type SuperType}
    

    then I get nothing, but I'd like to get A (by RDFS semantics).

  2. The same thing happens with owl:equivalentClass. If I have

    A rdf:type MyType .
    MyType owl:equivalentClass SiblingType .
    

    and I ask

    select ?x where {?x rdf:type SiblingType}
    

    I'd like to get A, but I get nothing.

Is there a way to get these results?


Source: (StackOverflow)

how to parse big datasets using RDFLib?

I'm trying to parse several big graphs with RDFLib 3.0, apparently it handles first one and dies on the second (MemoryError)... looks like MySQL is not supported as store anymore, can you please suggest a way to somehow parse those?

Traceback (most recent call last):
  File "names.py", line 152, in <module>
    main()
  File "names.py", line 91, in main
    locals()[graphname].parse(filename, format="nt")
  File "/usr/local/lib/python2.6/dist-packages/rdflib-3.0.0-py2.6.egg/rdflib/graph.py", line 938, in parse
    location=location, file=file, data=data, **args)
  File "/usr/local/lib/python2.6/dist-packages/rdflib-3.0.0-py2.6.egg/rdflib/graph.py", line 757, in parse
    parser.parse(source, self, **args)
  File "/usr/local/lib/python2.6/dist-packages/rdflib-3.0.0-py2.6.egg/rdflib/plugins/parsers/nt.py", line 24, in parse
    parser.parse(f)
  File "/usr/local/lib/python2.6/dist-packages/rdflib-3.0.0-py2.6.egg/rdflib/plugins/parsers/ntriples.py", line 124, in parse
    self.line = self.readline()
  File "/usr/local/lib/python2.6/dist-packages/rdflib-3.0.0-py2.6.egg/rdflib/plugins/parsers/ntriples.py", line 151, in readline
    m = r_line.match(self.buffer)
MemoryError

Source: (StackOverflow)

SPARQL query on the remote remote endpoint RDFLib / Redland

I'm trying to query remote endpoints and get get owl:sameAs mappings, I've tried both RDFLib and Redland but neither worked for me, probably I'm not dealing with namespaces correctly.

Here is my attempt in RDFLib:

    import rdflib

    rdflib.plugin.register('sparql', rdflib.query.Processor, 'rdfextras.sparql.processor', 'Processor')
    rdflib.plugin.register('sparql', rdflib.query.Result, 'rdfextras.sparql.query', 'SPARQLQueryResult')

    g = rdflib.Graph()

    query = """
        SELECT *
        FROM <http://api.talis.com/stores/bbc-backstage/services/sparql>
        WHERE {
             ?s a http://purl.org/ontology/mo/MusicArtist;
                http://www.w3.org/2002/07/owl#sameAs ?o .
        }Limit 50
    """

    for row in g.query(query):
        print row

And here is Redland:

import RDF
model = RDF.Model()

query = """
    SELECT *
    FROM <http://api.talis.com/stores/bbc-backstage/services/sparql>
    WHERE {
         ?s a http://purl.org/ontology/mo/MusicArtist;
            http://www.w3.org/2002/07/owl#sameAs ?o .
    }Limit 50
"""

for statement in RDF.Query(query ,query_language="sparql").execute(model):
    print statement

Can you please give a hint what is wrong in any one of those? Yet another difficulty I have: Is it possible to get dataset name of the object? For example: if there is:

?s = http://www.bbc.co.uk/music/artists/eb5c8564-927d-414d-b152-c7b48a2c9d8b#artist
predicate = http://www.w3.org/2002/07/owl#sameAs
?0 = http://dbpedia.org/resource/The_Boy_Least_Likely_To

Can I get name of the "Dbpedia" in this example? Or any other dataset to which I'm having sameAs link? (Or probably I could just look-up interested dataset names in the object string) thank you very VERY much in advance


Source: (StackOverflow)

How to access members of an rdf list with rdflib (or plain sparql)

What is the best way to access the members of an rdf list? I'm using rdflib (python) but an answer given in plain SPARQL is also ok (this type of answer can be used through rdfextras, a rdflib helper library).

I'm trying to access the authors of a particular journal article in rdf produced by Zotero (some fields have been removed for brevity):

<rdf:RDF
 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
 xmlns:z="http://www.zotero.org/namespaces/export#"
 xmlns:dcterms="http://purl.org/dc/terms/"
 xmlns:bib="http://purl.org/net/biblio#"
 xmlns:foaf="http://xmlns.com/foaf/0.1/"
 xmlns:dc="http://purl.org/dc/elements/1.1/"
 xmlns:prism="http://prismstandard.org/namespaces/1.2/basic/"
 xmlns:link="http://purl.org/rss/1.0/modules/link/">
    <bib:Article rdf:about="http://www.ncbi.nlm.nih.gov/pubmed/18273724">
        <z:itemType>journalArticle</z:itemType>
        <dcterms:isPartOf rdf:resource="urn:issn:0954-6634"/>
        <bib:authors>
            <rdf:Seq>
                <rdf:li>
                    <foaf:Person>
                        <foaf:surname>Lee</foaf:surname>
                        <foaf:givenname>Hyoun Seung</foaf:givenname>
                    </foaf:Person>
                </rdf:li>
                <rdf:li>
                    <foaf:Person>
                        <foaf:surname>Lee</foaf:surname>
                        <foaf:givenname>Jong Hee</foaf:givenname>
                    </foaf:Person>
                </rdf:li>
                <rdf:li>
                    <foaf:Person>
                        <foaf:surname>Ahn</foaf:surname>
                        <foaf:givenname>Gun Young</foaf:givenname>
                    </foaf:Person>
                </rdf:li>
                <rdf:li>
                    <foaf:Person>
                        <foaf:surname>Lee</foaf:surname>
                        <foaf:givenname>Dong Hun</foaf:givenname>
                    </foaf:Person>
                </rdf:li>
                <rdf:li>
                    <foaf:Person>
                        <foaf:surname>Shin</foaf:surname>
                        <foaf:givenname>Jung Won</foaf:givenname>
                    </foaf:Person>
                </rdf:li>
                <rdf:li>
                    <foaf:Person>
                        <foaf:surname>Kim</foaf:surname>
                        <foaf:givenname>Dong Hyun</foaf:givenname>
                    </foaf:Person>
                </rdf:li>
                <rdf:li>
                    <foaf:Person>
                        <foaf:surname>Chung</foaf:surname>
                        <foaf:givenname>Jin Ho</foaf:givenname>
                    </foaf:Person>
                </rdf:li>
            </rdf:Seq>
        </bib:authors>

        <dc:title>Fractional photothermolysis for the treatment of acne scars: a report of 27 Korean patients</dc:title>
        <dcterms:abstract>OBJECTIVES: Atrophic post-acne scarring remains a therapeutically challe *CUT*, erythema and edema. CONCLUSIONS: The 1550-nm erbium-doped FP is associated with significant patient-reported improvement in the appearance of acne scars, with minimal downtime.</dcterms:abstract>
        <bib:pages>45-49</bib:pages>
        <dc:date>2008</dc:date>
        <z:shortTitle>Fractional photothermolysis for the treatment of acne scars</z:shortTitle>
        <dc:identifier>
            <dcterms:URI>
               <rdf:value>http://www.ncbi.nlm.nih.gov/pubmed/18273724</rdf:value>
            </dcterms:URI>
        </dc:identifier>
        <dcterms:dateSubmitted>2010-12-06 11:36:52</dcterms:dateSubmitted>
        <z:libraryCatalog>NCBI PubMed</z:libraryCatalog>
        <dc:description>PMID: 18273724</dc:description>
    </bib:Article>
    <bib:Journal rdf:about="urn:issn:0954-6634">
        <dc:title>The Journal of Dermatological Treatment</dc:title>
        <prism:volume>19</prism:volume>
        <prism:number>1</prism:number>
        <dcterms:alternative>J Dermatolog Treat</dcterms:alternative>
        <dc:identifier>DOI 10.1080/09546630701691244</dc:identifier>
        <dc:identifier>ISSN 0954-6634</dc:identifier>
    </bib:Journal>

Source: (StackOverflow)