Get other labels using methods such as preferredLabel

66 views
Skip to first unread message

Laura Gorrieri

unread,
May 2, 2016, 3:45:52 AM5/2/16
to rdflib-dev
Hello everyone,

I'm new in this group and I've started programming recently, so please excuse me in advance if I'm not using the right vocabulary or if my question may seem redundant.

I am using RDFLib to build a programme to convert SKOS to a taxonomy and generate a tree of it. To do so, I am trying to intercept the value or the object of some labels that is interesting for me, such as the preferred label, alt label, broader, narrower. I'm using the  method preferredLabel to get the first one, but for the others the only solution I found was to directly search if the label contains the strings "altLabel", "narrower", "broader" and then get what i need (a literal and its language or a URIRef).

I was wondering if there is a way to do this more elegantly, not stricltly related to the text of the label, because I feel like it would be better to use something related to the meaning of it more than to the form (even if it's standardized in all possible sintaxes (?)).

Thanks in advance for your help and for your attention!

Gunnar Aastrand Grimnes

unread,
May 2, 2016, 4:19:19 AM5/2/16
to rdfli...@googlegroups.com
RDFLib gives you many varied ways to navigate graphs:

https://rdflib.readthedocs.io/en/stable/intro_to_graphs.html

For SKOS you can import the namespace:

from rdflib.namespace import SKOS

then use either indexing or methods on the graph object:

graph.value(my_term, SKOS.preferredLabel) # gives you one thing

graph.objects(my_term, SKOS.narrower) # a generator

or

graph[myterm:SKOS.narrower] # like objects

- 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/dea9f603-e17d-4299-ba5a-424cc91f58ba%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
http://gromgull.net

Laura Gorrieri

unread,
May 2, 2016, 5:02:28 AM5/2/16
to rdflib-dev
Hi, 

thanks very much for the answer. Unfortunately I can't find a way to make it work.

I suppose that in this method, for example, my_term has to be changed with the subject I'm searching the preferredLabel for:

graph.value(my_term, SKOS.preferredLabel)

I've tried both with a subject taken literaly from my SKOS  and with a cycle for that uses s,p,o on my graph, then substituting my_term with s in the instruction of the cycle. In any case the printed answer to that is None. Am I doing something wrong?

Gunnar Aastrand Grimnes

unread,
May 2, 2016, 5:17:47 AM5/2/16
to rdfli...@googlegroups.com
If you can give a complete non-working code-examples, it's easier to
see where you're going wrong.

- G
> https://groups.google.com/d/msgid/rdflib-dev/1751f9e7-e887-407d-b7b6-0e031a55a6bd%40googlegroups.com.

Jörn Hees

unread,
May 2, 2016, 7:47:15 AM5/2/16
to rdflib-dev
Hi Laura,

doesn't preferredLabel()'s labelProperties parameter already solve your problem? It defines the order / which label properties are queried:

Laura Gorrieri

unread,
May 2, 2016, 8:28:40 AM5/2/16
to rdflib-dev
Hi,

thank you all for your help.
I'm trying in any way you suggested,  as you'll see in the code. The output is None for any case.

import rdflib
from rdflib import Graph, URIRef
from rdflib.namespace  import SKOS

#Creating a graph from my skos that is myskos.skos for short (in my code there's the whole path)
g = Graph()
g.parse("myskos.skos", format="xml")

#Trying to use triplets and get the value of SKOS.preferredLabel
for s,p,o in g:
print (g.value(s, SKOS.preferredLabel))

#Trying to use a direct subject
print(g.value("http://www.wand.com/concepts#Accordions", SKOS.preferredLabel))

#Trying to use direct subject (which has children) and another parameter in the preferredLabel method
a=g.preferredLabel("http://www.wand.com/concepts#Keyboard_instruments",labelProperties=SKOS.narrower)

Gunnar Aastrand Grimnes

unread,
May 2, 2016, 8:47:51 AM5/2/16
to rdfli...@googlegroups.com
This is inefficient - but should have worked:

> #Trying to use triplets and get the value of SKOS.preferredLabel
> for s,p,o in g:
> print (g.value(s, SKOS.preferredLabel))


You cannot use a string directly, it must be a rdflib.URIRef, but then
this should also work:

> #Trying to use a direct subject
> print(g.value("http://www.wand.com/concepts#Accordions",
> SKOS.preferredLabel))


This does however not work:

> #Trying to use direct subject (which has children) and another parameter in
> the preferredLabel method
> a=g.preferredLabel("http://www.wand.com/concepts#Keyboard_instruments",labelProperties=SKOS.narrower)

the preferredLabel method get the label of a single thing.


You should get the tree first, something like:

all_things = list(g[RDF.type, SKOS.Concept])

all_labels = { thing: g.preferredLabel(thing) for thin in all_things }

all_children = { thing: g[thing:SKOS.narrower] for thin all_things }

Now you have a dict of labels and a dict of children for each node.

- G




>
>
>
>
> Il giorno lunedì 2 maggio 2016 13:47:15 UTC+2, Jörn Hees ha scritto:
>>
>> Hi Laura,
>>
>> doesn't preferredLabel()'s labelProperties parameter already solve your
>> problem? It defines the order / which label properties are queried:
>>
>> http://rdflib.readthedocs.io/en/stable/apidocs/rdflib.html#rdflib.graph.Graph.preferredLabel
>>
>> Best,
>> Jörn
>
> --
> 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/a08f2771-32b2-481d-976f-d6ddb4ff7cbf%40googlegroups.com.

Osma Suominen

unread,
May 2, 2016, 8:48:22 AM5/2/16
to rdfli...@googlegroups.com
Hi Laura!

Thanks for posting actual code!
Your patterns are a bit off, some suggestions below:

On 02/05/16 15:28, Laura Gorrieri wrote:
> Hi,
>
> thank you all for your help.
> I'm trying in any way you suggested, as you'll see in the code. The
> output is None for any case.
>
> import rdflib
> from rdflib import Graph, URIRef
> from rdflib.namespace import SKOS
>
> #Creating a graph from my skos that is myskos.skos for short (in my code
> there's the whole path)
> g = Graph()
> g.parse("myskos.skos", format="xml")

Looks very good so far!

> #Trying to use triplets and get the value of SKOS.preferredLabel
> for s,p,o in g:
> print (g.value(s, SKOS.preferredLabel))

This is walking through all the triples, not just labels, so even if it
worked, you would get the same label many times (assuming you use also
other SKOS properties such as broader, narrower etc.) and possibly some
subjects would not get any label as all, depending on the shape of your
data.

If you really want this, you could use something like this:

for s,p,o in g:
print(g.value(s, SKOS.prefLabel, None))

Note that Graph.value() takes 3 parameters, one of them should be None
(to indicate that this is the value you want to know).

Also I see you're using "SKOS.preferredLabel" but this is mixing up the
SKOS namespace and the name of the rdflib Graph method preferredLabel.
The correct SKOS property is SKOS.prefLabel.

> #Trying to use a direct subject
> print(g.value("http://www.wand.com/concepts#Accordions",
> SKOS.preferredLabel))

You need to wrap URI strings in URIRef.

print(g.value(URIRef("http://www.wand.com/concepts#Accordions",
SKOS.prefLabel, None)))

> #Trying to use direct subject (which has children) and another parameter
> in the preferredLabel method
> a=g.preferredLabel("http://www.wand.com/concepts#Keyboard_instruments",labelProperties=SKOS.narrower)

Not sure what you're after here. Typically you'd use something like
labelProperties=[SKOS.prefLabel, SKOS.altLabel] instead of
SKOS.narrower, which is not a label property at all.

If you want just the altLabels (as in your earlier question) and want to
use the preferredLabel method (as suggested by Jörn), you could do this:

a =
g.preferredLabel(URIRef("http://www.wand.com/concepts#Keyboard_instruments"),
labelProperties=[SKOS.altLabel])[0][1]

Note that preferredLabel returns a list of results, which are (property,
label) tuples; here [0] is used to take the first result and [1] to
extract the label of that result.

Hope this helps!

-Osma


>
> Il giorno lunedì 2 maggio 2016 13:47:15 UTC+2, Jörn Hees ha scritto:
>
> Hi Laura,
>
> doesn't preferredLabel()'s labelProperties parameter already solve
> your problem? It defines the order / which label properties are queried:
> http://rdflib.readthedocs.io/en/stable/apidocs/rdflib.html#rdflib.graph.Graph.preferredLabel
> <http://rdflib.readthedocs.io/en/stable/apidocs/rdflib.html#rdflib.graph.Graph.preferredLabel>
>
> Best,
> Jörn
>
> --
> 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
> <mailto:rdflib-dev+...@googlegroups.com>.
> To post to this group, send email to rdfli...@googlegroups.com
> <mailto:rdfli...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rdflib-dev/a08f2771-32b2-481d-976f-d6ddb4ff7cbf%40googlegroups.com
> <https://groups.google.com/d/msgid/rdflib-dev/a08f2771-32b2-481d-976f-d6ddb4ff7cbf%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.


--
Osma Suominen
D.Sc. (Tech), Information Systems Specialist
National Library of Finland
P.O. Box 26 (Kaikukatu 4)
00014 HELSINGIN YLIOPISTO
Tel. +358 50 3199529
osma.s...@helsinki.fi
http://www.nationallibrary.fi

Gunnar Aastrand Grimnes

unread,
May 2, 2016, 8:57:37 AM5/2/16
to rdfli...@googlegroups.com
On 2 May 2016 at 14:48, Osma Suominen <osma.s...@helsinki.fi> wrote:
> Note that Graph.value() takes 3 parameters, one of them should be None (to
> indicate that this is the value you want to know).

True, but they all default to None, so you can leave them out - if you
leave out the last (the object), you can pass just two positional
parameters.

- G

--
http://gromgull.net

Laura Gorrieri

unread,
May 3, 2016, 3:29:17 AM5/3/16
to rdflib-dev
Hi everyone,

thanks so much for your help!

I'm trying with the code that Gunnar proposed, but it raises an exception in the first line:

#Line of code

all_things = list(g[RDF.type, SKOS.Concept])

#Exception raised
TypeError("You can only index a graph by a single rdflib term or path, or a slice of rdflib terms.")

It seems like everything I try won't work eheheh
BTW I forgot to mention I'm using Python 3.5 on Windows XP, it shouldn't be an issue because on the GitHub page of RDFLib there's written that they added python 3.5 support 7 months ago, although it is specified they were starting some tests.

Gunnar Aastrand Grimnes

unread,
May 3, 2016, 3:31:27 AM5/3/16
to rdfli...@googlegroups.com
On 3 May 2016 at 09:29, Laura Gorrieri <laura.g...@gmail.com> wrote:
>
> #Line of code
> all_things = list(g[RDF.type, SKOS.Concept])
>
> #Exception raised
> TypeError("You can only index a graph by a single rdflib term or path, or a
> slice of rdflib terms.")


My bad - typing too quickly and not testing - it must be

g[RDF.type:SKOS.Concept]



--
http://gromgull.net

Laura Gorrieri

unread,
May 3, 2016, 3:44:59 AM5/3/16
to rdflib-dev
No worries! :)

So now it is corrected and my code looks like this:

all_things = list(g[RDF.type:SKOS.Concept])
print(all_things)

And the output is:
>>>[ ]

Still the graph is not empty, with a simple print like the following one I checked and found it is indeed complete:
for s,p,o in g:
    print(s,p,o)

I really have no clue where's the problem! 
The method described in my very first post works, but then again I'd like to write a better code. 

If you have any other idea, please let me know. I'll keep trying, hopefully something's will come up sooner or later!

Gunnar Aastrand Grimnes

unread,
May 3, 2016, 3:46:57 AM5/3/16
to rdfli...@googlegroups.com
On 3 May 2016 at 09:44, Laura Gorrieri <laura.g...@gmail.com> wrote:
> all_things = list(g[RDF.type:SKOS.Concept])


Ok - I'll stop writing code in email without testing it:

> all_things = list(g[None:RDF.type:SKOS.Concept])

Since the triples are (the_thing, RDF.type, SKOS.Concept), you need a leading :

- Gunnar

--
http://gromgull.net

Laura Gorrieri

unread,
May 3, 2016, 5:04:14 AM5/3/16
to rdflib-dev
Ahahahaha! Ok, now it works, thanks!! :)

Meanwhile I've been trying again with the g.value(my_subj, SKOS.preferredLabel) and found out why wasn't working: substituing preferredLabel with prefLabel it works.
This made me think that these methods are just a way to do a literal search on the text, just as I did, aren't they? I've tried putting a fantasy label in my skos (skos:loremipsum) and then search for it with the metod g.value(my_subj, SKOS.loremipsum) and it gets me the object to that brand-new label.

So not really that much difference with my very first method that did a literal search on the content on the label. I guess I'm gonna stick to that since it worked.
Reply all
Reply to author
Forward
0 new messages