Archives

April 2004 (7)
March 2004 (12)
February 2004 (12)
January 2004 (22)
December 2003 (19)
November 2003 (16)
October 2003 (26)
September 2003 (18)
August 2003 (38)
July 2003 (80)
June 2003 (13)
May 2003 (24)
April 2003 (76)
March 2003 (75)
February 2003 (51)
January 2003 (73)

Category

Family (5)
FYI (18)
Games (2)
Geek (88)
Geographic (3)
Hacks (13)
Home (15)
Humor (54)
Ideas (20)
Ideaspace (15)
Local (15)
Metadata (10)
Microsoft (2)
MovableType (5)
Nitwits (66)
PKI (2)
Politics (22)
Quotes (3)
RDF (15)
RSS (4)
Security (3)
Semantic Web (13)
Site Info (13)
Social Networks (1)
Spam (9)
Sysadmin (1)
Tips (2)
Tivo (2)
TMFTOTHD (1)
To Do (1)
Unlisted (1)
Web (3)
Windows (1)

Local

« MetroBlogs »
DC metroblogs
beltway bloggers

Links


Assorted bits

Blogroll Me!
GeoURL
Listed on BlogShares




July 25, 2003

Parsing RDF with python

I posted my article Some RDF examples and then I asked the RDF crowd in #rdfig to chime in with some comments. They pointed out a syntax error or two. But even better, Dan Connolly took the example and whipped up a quick snippet of code that reads it. He posted it into my comment section but I've cleaned up the formatting a bit and posted it again here. Firstly, make sure you've got python and the rdflib installed and running. Once that's done, put this snippet into a file named foafwk.py:
# cribbed from
#  http://rdflib.net/stable/example.py

from rdflib.TripleStore import TripleStore
from rdflib.constants import TYPE
from rdflib.Namespace import Namespace
from rdflib.Literal import Literal

FOAF = Namespace("http://xmlns.com/foaf/0.1/")
EX = Namespace("http://example.org/foovocab#")

def main():
    s = TripleStore()
    s.load(",ex2.rdf")

    # find a guy named bob...
    for who in s.subjects(FOAF["name"], Literal("Bob")):
        bob = who
    
    # who is secretlyStalking bob?
    for who in s.subjects(EX["secretlyStalking"], bob):
        for n in s.objects(who, FOAF["name"]):
            print n, "is secretlyStalking Bob!"

if __name__ == '__main__':
    main()
Then go take third example RDF and paste it into a file named ex2.rdf
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:eg="http://example.org/foovocab#"
  xmlns:foaf="http://xmlns.com/foaf/0.1/">
	<foaf:Person rdf:nodeID="alice">
	    <foaf:name>Alice</foaf:name>
	    <foaf:knows rdf:nodeID="bob"/>
	    <foaf:knows rdf:nodeID="ted"/>
	    <eg:acquaintance rdf:nodeID="bob"/>
	    <eg:acquaintance rdf:nodeID="ted"/>
	    <eg:archNemesis rdf:nodeID="bob"/>
	    <eg:archNemesis rdf:nodeID="ted"/>
	</foaf:Person>
	<foaf:Person rdf:nodeID="bob">
	    <foaf:knows rdf:nodeID="alice"/>
	    <foaf:knows rdf:nodeID="ted"/>
	    <foaf:name>Bob</foaf:name>
	    <eg:secretlyStalking rdf:nodeID="alice"/>
	    <eg:acquaintance rdf:nodeID="alice"/>
	    <eg:archNemesis rdf:nodeID="ted"/>
	</foaf:Person>
	<foaf:Person rdf:nodeID="carol">
	    <foaf:name>Carol</foaf:name>
	    <foaf:knows rdf:nodeID="alice"/>
	    <foaf:knows rdf:nodeID="ted"/>
	    <eg:secretlyStalking rdf:nodeID="bob"/>
	    <eg:acquaintance rdf:nodeID="alice"/>
	    <eg:acquaintance rdf:nodeID="ted"/>
	</foaf:Person>
	<foaf:Person rdf:nodeID="ted">
	    <foaf:name>Ted</foaf:name>
	    <eg:acquaintance rdf:nodeID="carol"/>
	    <eg:acquaintance rdf:nodeID="alice"/>
	    <eg:archNemesis rdf:nodeID="bob"/>
	</foaf:Person>
</rdf:RDF>
Now execute the program:
$ python foafwk.py ,ex2.rdf
Carol is secretlyStalking Bob!
You can fiddle around with the ex2.rdf file and the python example to learn what more can be done.
Comments
Post a comment






* if you do not leave a valid e-mail or URL your comment may be deleted *







Navigation

Recent Entries

America and Europe: Vive la différence?
Server changes afoot
Diet behavior mod
Googling for sensitive info
Outlook 2003 and IMAP, a marriage made in Hell
Bike to Work Day, May 7th
Speakeasy rocks
Zippo USB?
When geographic data is nowhere 'near' correct
Local campaign contributions

User comments
Trackbacks

Contact

send me an e-mail E-mail
chat with me using MS messenger MSN Messenger
chat with me via AIM America Online
chat with me on ICQ ICQ
chat with me on Yahoo! Yahoo
Add my vCard to your electronic addressbook vCard
Friend of a Friend FoaF

Syndication

XML  RDF  CDF

Comments

XFML

Extra Stuff

foaf
vCard
pgp info
Linked In
Powered by
Movable Type 2.64