HHS / meshrdf

Code and documentation for the release of MeSH in RDF format
https://hhs.github.io/meshrdf/
75 stars 19 forks source link

How can I execute the sample RDF queries from a python env using SPARQLWrapper (or anything else) #180

Closed yazoo178 closed 2 years ago

yazoo178 commented 3 years ago

I'm having a difficult time trying to run the SPAQL query examples outside of the demo environment provided on the webpage.

I suspect the has something to do with RDFS inference? flag == True, as I am unable to select this option when running the queries in a python environment.

image

The result set is empty in my case, but for the same online example the result set contains over 50 results.

danizen commented 3 years ago

Although rdflib does not support inference, inference is done on our end, and is requested through the "inference" parameter. In this case, the sparqlwrapper package only uses rdflib for parsing the results. If you need inference, then after you instantiate your object, you can use the method addCustomParameter as follows:

from SPARQLWrapper import SPARQLWrapper, JSON
q = """
... your query ...
"""

sparql = SPARQLWrapper('https://id.nlm.nih.gov/mesh/sparql')
sparql.addCustomParameter('inference', 'true')
sparql.setQuery(q)
sparql.setReturnFormat(JSON)
results = sparql.query().results()
print(results)

PS - it will be helpful if in future you use Github embedded code blocks (as I do above), rather than embedding an image from a Jupyter Notebook. If you still have trouble, I will need your query as text rather than an image to help :)

danizen commented 2 years ago

Given your thumbs up - I will close.