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.
You need to wrap URI strings in URIRef.
print(g.value(URIRef("
http://www.wand.com/concepts#Accordions",
SKOS.prefLabel, None)))
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
> <
http://rdflib.readthedocs.io/en/stable/apidocs/rdflib.html#rdflib.graph.Graph.preferredLabel>
>
> Best,
> Jörn
> <mailto:
rdflib-dev+...@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>.
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