SPARQL Querying in rdflib

88 views
Skip to first unread message

vsj...@gmail.com

unread,
Apr 25, 2015, 12:02:03 PM4/25/15
to rdfli...@googlegroups.com
Hi,

I am trying to run a sparql query through rdlfib. I just want my query to return the class/subclass name without the URI. Is there any built-in function in rdflib that does that? I just want to scrap away the URI part. I know I can always use a regex but was wondering if there is anything in the library that helps me do that without having to do regex?

Let me know if you didn't get what I am saying. I can post the code if you need me to.

Thanks,
Uthej

Gunnar Aastrand Grimnes

unread,
Apr 26, 2015, 3:40:40 AM4/26/15
to rdfli...@googlegroups.com
The part you want is called hte "localname" - and this function will
get it for you:
http://rdflib.readthedocs.org/en/latest/apidocs/rdflib.html?highlight=split_uri#rdflib.namespace.split_uri

(if you want to get fancy you can expose that function as a custom
sparql function in rdflib, you wont have to post-process your query
results)

Note that if you consume data you do not have complete control over,
there is no-one forcing anyone to have sensible local-names for their
URIs, they are not really for human consumption, and some will just a
random identifier (Freebase data, i'm looking at you...)

A safer solution is to do an extra triple pattern for the rdfs:label
of whatever you're querying for.

- Gunnar
> --
> http://github.com/RDFLib
> ---
> You received this message because you are subscribed to the Google Groups "rdflib-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rdflib-dev+...@googlegroups.com.
> To post to this group, send email to rdfli...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rdflib-dev/67df505d-8586-4175-9414-f213386488dc%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
http://gromgull.net

vsj...@gmail.com

unread,
Apr 26, 2015, 1:29:43 PM4/26/15
to rdfli...@googlegroups.com, vsj...@gmail.com
Thanks Gunnar, Appreciate the help. But here is the thing. I can't seem to understand the approach you are suggesting with the rdfs:Label tag. Here is the query that I am trying to run against the owl file:


SELECT ?Action
WHERE { alfredowl:Sad rdf:type alfredowl:Mood .
alfredowl:Sad alfredowl:possible ?Action }

Now weirdly enough this query in Protege returns just the label names without the URI. But somehow rdflib doesn't do that. The only difference I see is that in protege query engine I have to define the prefix but since I am parsing the xml from a local path in my code and my xml already has alfred ns defined, I don't have to do it here. Any which way how do you suggest I change this query to get only the labels?
Reply all
Reply to author
Forward
0 new messages