質問

I am pulling out data from a user entered dbpedia page using RDFlib in python and am struggling to find a way to test to see of the page is about a person. I am sure this is easy and could use a test such as does the page have a resource for birthDate but that seems like a kludge.

Is there a straight forward way to do this preferably without using SPARQL

My code goes something like

from rdflib import Graph, URIRef
URI_ref = 'http://dbpedia.org/resource/Richard_Nixon'
g = Graph()
g.parse(URI_ref)

for info in g.subject_objects(URIRef("http://dbpedia.org/ontology/birthName"))
    print "This person ", str(info[0]), "was named ", str(info[1]), "at birth"

Not all records have birthName but I would still like to check that this was a person and not a fruit for example :)

New to this so any help appreciated.

役に立ちましたか?

解決

Without using SPARQL, you could find all of the type assertions for the resource and verify that dbpedia:Person (or whatever class you are considering to be a person) is one of them. The code would look like this.

from rdflib import Graph, URIRef, RDF
uri = URIRef('http://dbpedia.org/resource/Richard_Nixon')
person = URIRef('http://dbpedia.org/ontology/Person')

g = Graph()
g.parse(uri)

for obj in g.objects(subject=uri, predicate=RDF.type):
    if obj == person:
        print uri, "is a", person

You asked for an answer that doesn't use SPARQL but running a SPARQL query against the data fetched from DBpedia is quite a clean way way of doing this. Here is sample code for that approach:

from rdflib import Graph, URIRef

def is_person(uri):
    uri = URIRef(uri)
    person = URIRef('http://dbpedia.org/ontology/Person')
    g = Graph()
    g.parse(uri)
    resp = g.query(
        "ASK {?uri a ?person}",
        initBindings={'uri': uri, 'person': person}
    )
    print uri, "is a person?", resp.askAnswer
    return resp.askAnswer


uri = URIRef('http://dbpedia.org/resource/Richard_Nixon')
person = URIRef('http://dbpedia.org/ontology/Person')

nixon = 'http://dbpedia.org/resource/Richard_Nixon'
is_person(nixon)

pear = 'http://dbpedia.org/resource/Pear'
is_person(pear)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top