diff options
| author | gromgull <gromgull@gmail.com> | 2013-05-13 17:15:01 +0200 |
|---|---|---|
| committer | gromgull <gromgull@gmail.com> | 2013-05-13 17:15:01 +0200 |
| commit | b4d3753552090cd86be353621fed7108f0e9be5d (patch) | |
| tree | a0d02e28c4675ad32b7182c209187ec25dc0f71f /examples | |
| parent | e0df0009ed6375d83ea4dfc6f0c4a5e6e2fecfd9 (diff) | |
| download | rdflib-b4d3753552090cd86be353621fed7108f0e9be5d.tar.gz | |
doc updates - converted some examples-in-doc to examples in examples folder
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/smushing.py | 47 | ||||
| -rw-r--r-- | examples/smushingdemo.n3 | 14 |
2 files changed, 61 insertions, 0 deletions
diff --git a/examples/smushing.py b/examples/smushing.py new file mode 100644 index 00000000..6197e1e2 --- /dev/null +++ b/examples/smushing.py @@ -0,0 +1,47 @@ +""" +A FOAF smushing example. + +Filter a graph by normalizing all ``foaf:Persons`` into URIs based on +their ``mbox_sha1sum``. + +Suppose I got two `FOAF <http://xmlns.com/foaf/0.1>`_ documents each +talking about the same person (according to ``mbox_sha1sum``) but they +each used a :class:`rdflib.term.BNode` for the subject. For this demo +I've combined those two documents into one file: + +This filters a graph by changing every subject with a +``foaf:mbox_sha1sum`` into a new subject whose URI is based on the +``sha1sum``. This new graph might be easier to do some operations on. + +An advantage of this approach over other methods for collapsing BNodes +is that I can incrementally process new FOAF documents as they come in +without having to access my ever-growing archive. Even if another +``65b983bb397fb71849da910996741752ace8369b`` document comes in next +year, I would still give it the same stable subject URI that merges +with my existing data. + +""" + +from rdflib import Graph, Namespace +from rdflib.namespace import FOAF + +STABLE = Namespace("http://example.com/person/mbox_sha1sum/") + +if __name__=='__main__': + g = Graph() + g.parse("smushingdemo.n3", format="n3") + + newURI = {} # old subject : stable uri + for s,p,o in g.triples((None, FOAF['mbox_sha1sum'], None)): + newURI[s] = STABLE[o] + + + out = Graph() + out.bind('foaf', FOAF) + + for s,p,o in g: + s = newURI.get(s, s) + o = newURI.get(o, o) # might be linked to another person + out.add((s,p,o)) + + print out.serialize(format="n3") diff --git a/examples/smushingdemo.n3 b/examples/smushingdemo.n3 new file mode 100644 index 00000000..092e5f8e --- /dev/null +++ b/examples/smushingdemo.n3 @@ -0,0 +1,14 @@ +@prefix foaf: <http://xmlns.com/foaf/0.1/> . + +## from one document +_:p1 a foaf:Person; + foaf:mbox_sha1sum "65b983bb397fb71849da910996741752ace8369b"; + foaf:nick "mortenf"; + foaf:weblog <http://www.wasab.dk/morten/blog/archives/author/mortenf/> . + +## from another document +_:p2 a foaf:Person; + foaf:mbox_sha1sum "65b983bb397fb71849da910996741752ace8369b"; + foaf:nick "mortenf"; + foaf:homepage <http://www.wasab.dk/morten/>; + foaf:interest <http://en.wikipedia.org/wiki/Atom_(standard)> . |
