Differences between revisions 7 and 8
Revision 7 as of 2008-11-15 13:59:47
Size: 3666
Editor: localhost
Comment: converted to 1.6 markup
Revision 8 as of 2023-10-11 07:20:10
Size: 356
Comment: remove examples which may be out of date and point to the official examples on github which are much more likely to be maintained, and discussion which is likely not useful to someone 20 yrs later
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
URL for RDFLib: http://rdflib.net/ URL for RDFLib: https://rdflib.readthedocs.io/en/stable/
Line 3: Line 3:
With RdfLib, you can do something like this:

{{{
#!python
import rdflib
from rdflib.TripleStore import TripleStore

store = TripleStore()

store.load( "foaf.rdf" )
}}}

That loads a particulare FOAF file.

Now, you can print out all the subject-predicate-object triples:

{{{
#!python
for s,p,o in store:
  print s,p,o
}}}

...which isn't terribly enlightening, but at least it's something.

Let's suppose you want to find someone with a particular name. And let's further suppose that we don't want to look up the URL that identifies a "name" in FOAF. We'll search for it manually:

{{{
#!python
for s,p,o in store:
  print p
}}}

We'll see a bunch of data, like so...:

{{{
  ...
http://xmlns.com/foaf/0.1/family_name
http://xmlns.com/foaf/0.1/phone
http://xmlns.com/foaf/0.1/schoolHomepage
http://www.w3.org/2003/01/geo/wgs84_pos#lat
http://www.w3.org/1999/02/22-rdf-syntax-ns#type
http://www.w3.org/2003/01/geo/wgs84_pos#long
http://xmlns.com/foaf/0.1/name
  ...
}}}

So, we can pick out the {{{http://xmlns.com/foaf/0.1/name}}} as the one we're looking for:

{{{
name="http://xmlns.com/foaf/0.1/name"
}}}

...and then find the names:
{{{
#!python
for s,p,o in store:
  if p == name:
    print s,o
}}}

We get back something like:
{{{
_:XzWBAuNC1 Lion Kimbro
_:XzWBAuNC3 Alex Trabeck
_:XzWBAuNC4 Bayle Capp
_:XzWBAuNC6 Mantis Man
}}}

(names shielded to protect the guilty)

Now, it's sort of ugly looking at the bNode ("blank node") identifiers for subjects like {{{_:XzWBAuNC1}}}; It would be much easier to see it with a name.

{{{
#!python
bnode_to_name = {}
for s,p,o in store:
  if p == name:
    bnode_to_name[s] = o
}}}

Then we can look at things a little more nicely:
{{{
#!python
for s,p,o in store:
  if s in bnode_to_name.keys():
    print bnode_to_name[s], p, o
}}}

You could also make a nice mapping from predicates to human readable names, and use those.
RdfLib is a library providing a large number of Python RDF Utilities, including parsers, a triple store, converters and others. [[https://github.com/RDFLib/rdflib/tree/main/examples|Examples]] can be found here.
Line 95: Line 7:
 * RdfLibraries

== Discussion ==

I am surprised that there isn't already an automatic (by RDF, even) way to turn predicates into human readable string names. I imagine that there is, and that I just don't know about it.

I feel I would want to make it so you could make objects and classes out of the RDF graph, and connect them to each other by predicates, which would then be properties of the objects linking out to the other objects.

Alright; Here's something to explore from. :)

-- LionKimbro <<DateTime(2004-06-06T06:31:25Z)>>

The "rdf" way to turn abitrary properties into human labels would be via a rdfs:label properties of the rdf:Property in the schema. Note that labels can have language tags; note as well that labels don't have to come from the original schema

-- JoshSled <<DateTime(2004-07-31T12:38:34Z)>>

I believe that the rdfs:label or rdfs:comment things would allow you to stay stuff like: "this is a person node," or something like that.

What I am looking more for is something like, "If you see a Person node, identify that node in a friendly way by just taking the value of the ''name'' property, associated with that node."

That is, looking at the blank node, you see it labeled by "Lion Kimbro," rather than: "This is a node describing a Person."

This idea is conceptually closest, I think, to [[http://esw.w3.org/topic/GraphStyleSheet|ESW:GraphStyleSheets.]]

But rather than applying to a whole graph, it is describing how do draw a single node in a graph (in this case,) and- of course- it's ''textual'' rather than graphic.

-- LionKimbro <<DateTime(2004-08-01T06:00:23Z)>>
 * [[https://wiki.python.org/moin/RdfLibraries|RdfLibraries]]

URL for RDFLib: https://rdflib.readthedocs.io/en/stable/

RdfLib is a library providing a large number of Python RDF Utilities, including parsers, a triple store, converters and others. Examples can be found here.

See Also

RdfLib (last edited 2023-10-11 07:20:10 by DanielScanteianu)

Unable to edit the page? See the FrontPage for instructions.