Search code examples
sparqldbpediavirtuoso

Fetching the abstract/comment for a person's label


I'm trying to build a python function which given a surface form (label) of a person's name (eg. 'Shah Abbas I') will retrieve the abstract and the comment associated with it.

As the label could have variations, I am trying to first find all objects that have that surface as a label, using a Virtuoso BIF verb. Here is the python string I am building interpolating "surface":

query = f"""
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX bif: <http://www.openlinksw.com/schemas/bif#>

SELECT ?subject ?label
WHERE {{
    ?subject rdfs:label ?label .
    ?label bif:contains "'{surface}'" .
}}
"""

this query would return something like this in results["results"]["bindings"] :

[0] -> {'subject': {'type': 'uri', 'value': 'http://dbpedia.org/resource/Shah_Abbas_I'}, 'label': {'type': 'literal', 'xml:lang': 'en', 'value': 'Shah Abbas I'}}
[1] -> {'subject': {'type': 'uri', 'value': 'http://dbpedia.org/resource/Mausoleum_of_Shah_Abbas_I'}, 'label': {'type': 'literal', 'xml:lang': 'en', 'value': 'Mausoleum of Shah Abbas I'}}
[2] -> {'subject': {'type': 'uri', 'value': 'http://dbpedia.org/resource/Shah_Abbas_I_the_Great'}, 'label': {'type': 'literal', 'xml:lang': 'en', 'value': 'Shah Abbas I the Great'}}
[3] -> {'subject': {'type': 'uri', 'value': "http://dbpedia.org/resource/Shah_Abbas_I's_invasions_of_Georgia"}, 'label': {'type': 'literal', 'xml:lang': 'en', 'value': "Shah Abbas I's invasions of Georgia"}} 

and then I would attempt to look into each of these to see if they have a http://dbpedia.org/ontology/wikiPageRedirects subject and if so return the object URI and in that try and find a comment/abstract.

For example amongst the triples found with:

SELECT ?property ?value
WHERE {
  <http://dbpedia.org/resource/Shah_Abbas_I> ?property ?value .
}

I can see:

http://dbpedia.org/ontology/wikiPageRedirects   http://dbpedia.org/resource/Abbas_the_Great

and so finally from this last page I could get and obtain abstract/comment.

I am quite lost :( Is the logic wrong? Is there a simpler way? Any Virtuoso dependent speedup very much appreciated.


Solution

  • The query suggested by @UninformedUser appears likely to deliver what you want, with reasonable speed. Full query (with some extra optional whitespace) presented here, because it's much clearer than in the comment —

    SELECT ?subject ?label ?abstract 
    WHERE
      { ?subject  rdfs:label                           ?label . 
        ?label    bif:contains                         "'Shah_Abbas'" . 
        ?subject  dbo:wikiPageRedirects?/dbo:abstract  ?abstract 
                    FILTER ( LANGMATCHES ( LANG ( ?abstract ), "en" ) )
      }
    

    Also see the query in the DBpedia SPARQL input form, and the results thereof.