hsh_triplify/vivordftest.py

96 lines
2.9 KiB
Python
Raw Permalink Normal View History

import csv
from pprint import pprint
import urllib
from rdflib import Namespace
from rdfalchemy import rdfSingle
from rdfalchemy.rdfSubject import rdfSubject
from rdflib import Literal, BNode, Namespace, URIRef
from rdflib import RDF, RDFS, Graph, OWL
from rdflib.namespace import XSD
foaf = Namespace('http://xmlns.com/foaf/0.1/')
core = Namespace('http://vivoweb.org/ontology/core#')
vitro = Namespace('http://vitro.mannlib.cornell.edu/ns/vitro/0.7')
local = Namespace('http://localhost/core/ontology/core-local#')
#Namespace for the vivo app
vivo_app_url = 'http://localhost/vivo/'
app = Namespace(vivo_app_url)
class Thing(rdfSubject):
rdf_type = OWL.Thing
label = rdfSingle(RDFS.label)
class Person(Thing):
rdf_type = foaf.Person
class FacultyMember(Person):
rdf_type = core.FacultyMember
firstname = rdfSingle(foaf.firstName)
middlename = rdfSingle(core.middleName)
lastname = rdfSingle(foaf.lastName)
work_email = rdfSingle(core.workEmail)
phone = rdfSingle(core.workPhone)
fax = rdfSingle(core.workFax)
research_overview = rdfSingle(core.researchOverview)
preferred_title = rdfSingle(core.preferredTitle)
moniker = rdfSingle(vitro.moniker)
people_id = rdfSingle(local.peopleID)
def get_graph():
"""
Helper for getting a graph and binding namespaces.
"""
g = rdfSubject.db
#Bind the namespaces
g.bind('foaf', foaf)
g.bind('core', core)
g.bind('vitro', vitro)
g.bind('local', local)
return g
g = get_graph()
#Open the sample VIVO people file.
csv_url = 'http://iweb.dl.sourceforge.net/project/vivo/Data%20Ingest/people.csv'
people_file = urllib.urlopen(csv_url)
for count, row in enumerate(csv.DictReader(people_file)):
#Create a URI for the person.
person_uri = URIRef("%sfaculty%s" % (app, count + 1))
fac = FacultyMember(person_uri)
fac.label = row.get('name').strip()
fac.people_id = row.get('person_ID')
fac.moniker = row.get('title')
fac.firstname = row.get('first')
middle_name = row.get('middle')
if middle_name is not None and middle_name != "":
fac.middlename = row.get('middle')
fac.lastname = row.get('last')
fac.work_email = row.get('email')
fac.phone = row.get('phone')
fac.fax = row.get('fax')
print g.serialize(format='n3')
g.close()
"""
#Load the n3 file as a rdfSubject db.
people_n3 = 'http://iweb.dl.sourceforge.net/project/vivo/Data%20Ingest/people.n3'
rdfSubject.db.load(people_n3, format='n3')
#Get all of the assistant professors in the graph.
asst_professors = FacultyMember.filter_by(moniker="Assistant Professor")
print '\n' + '=' * 20
print "Assistant Professors"
print '=' * 20 + '\n'
for fac in asst_professors:
#Print full name, email, and url to vivo profile.
print "%s\t%s\t%s" % (fac.label, fac.work_email, fac.resUri.toPython())
#Use get_by to retrieve a single faculty member
faculty = FacultyMember.get_by(hr_id='3958')
print faculty.label"""