0

I am having difficulty in printing the SKOS broader and narrower concepts against my URIRef (i.e. the output of the SPAQRL query). I want to print the Broader and Narrowers concepts against the captured URI REF (i.e. Biomass). The file which i am parsing does not contains the Broader and Narrowers concepts. I dont know whether i need to manually add them in file before i run queries on them.

I have already seen the similar questions like skos broader and narrow inverse not working but couldnt find the solution.

import rdflib
g = rdflib.Graph()
result = g.parse("C://Users/support/Documents/Re.txt", format=("turtle"))
qres = g.query(
    """
    prefix skos: <http://www.w3.org/2004/02/skos/core#> 
    
    SELECT  *
    WHERE { ?s  skos:prefLabel  "Biomass"} 
                     
    """)
for row in qres: print(row)

The output of the query is

for row in qres: print(row)
(rdflib.term.URIRef('http://aims.fao.org/aos/agrovoc/c_926'),) 

I have tried by nesting the SELECT Queries but it is not working.

My Query

qres = g.query(
    """
    prefix skos: <http://www.w3.org/2004/02/skos/core#> 
        
    SELECT *
    WHERE { ?s  skos:broader  ?o . { 
           
     
    SELECT  ?s
    WHERE { ?s  skos:prefLabel  "Biomass" .}  
 
          }     
                     
    """)
             
for row in qres: print(row)
Beginner
  • 37
  • 8
  • *"I dont know whether i need to manually add them in file before i run queries on them."* - seriously? I mean, how would you be able to query for data that does **not** exist in your dataset, neither explicitly nor implicitly? – UninformedUser Jan 11 '21 at 16:19
  • 1
    naming your file `Re.txt` is also bad practice. Also it's not a `URIRef` but a URI. URIRef is just how the class is named in the Python API, but in the RDF/SPARQL world it's simply a URI – UninformedUser Jan 11 '21 at 16:20
  • and your last query would be invalid SPARQL syntax anyways. That would be two queries but there is nothing nested. Or where do you see the nesting here? – UninformedUser Jan 11 '21 at 16:21
  • 1
    so, add the data, then run the query. And in your case "merge" both queries from your last attempt. – UninformedUser Jan 11 '21 at 18:04
  • Thank you for your replies. The thing is that, i am using Agrovoc Ontology (i.e. http://aims.fao.org/aos/agrovoc/c_926.html). It contains its broader and narrower concepts by default. Do i need to define these concepts again in my script before making the queries ? – Beginner Jan 13 '21 at 14:40
  • 1
    @Beginner what do you mean by "define these concepts again in my script"? – ChrisW Jan 19 '21 at 18:22
  • I mean, these concepts are defined online using the this website. http://aims.fao.org/aos/agrovoc/c_926.html , I am trying to fetch the subconcepts related to it. – Beginner Jan 20 '21 at 15:57

1 Answers1

1

If you're just struggling with the query, then I think you're overcomplicating it. This should work

qres = g.query(
    """
    prefix skos: <http://www.w3.org/2004/02/skos/core#> 
        
    SELECT * WHERE { 
        ?s  skos:broader  ?o ; skos:prefLabel  "Biomass" . }   
                     
    """)
ChrisW
  • 4,970
  • 7
  • 55
  • 92