0

I am using the following sparql query in wikidata query editor:

SELECT ?s ?p WHERE {?s ?p wd:Q22673982 .}

Link to the query editor: https://w.wiki/5E7

I am getting 40 records for the above query.

However, when I try to do the same in python using SPARQLWrapper I get 0 records. My code is as follows.

import pandas as pd
from SPARQLWrapper import SPARQLWrapper, JSON

sparqlwd = SPARQLWrapper("https://query.wikidata.org/sparql")

myid = "wd:Q22673982"
sparqlwd.setQuery(f"SELECT ?s ?p WHERE {{?s ?p \"{myid}\" .}}")
sparqlwd.setReturnFormat(JSON)
results = sparqlwd.query().convert()
print(results)
results_df = pd.io.json.json_normalize(results['results']['bindings'])
print(results_df)

I am just wondering why this mismatch happens. Is there a way to resolve this issue?

I am happy to provide more details if needed.

logi-kal
  • 7,107
  • 6
  • 31
  • 43
EmJ
  • 4,398
  • 9
  • 44
  • 105
  • 1
    For those kind of problems, please just print the query string or use a debugger - then you can compare the query generated by code with the one you used in the web UI. the error you made is obvious, isn't it? – UninformedUser Jun 24 '19 at 03:33
  • @AKSW Thanks a lot. Oh, I am such an stupid person, that I wasted all my morning time in figuring out where I am making things wrong. I should definitely use a debugger. I figured out where I have make things wrong. Thank you very much once again :) – EmJ Jun 24 '19 at 03:51
  • 1
    yeah, a debugger is really nice. especially for those kind of typos. I mean, indeed your could also just always print the query string to log the queries your code is executing. whatever you prefer, at least it helps to avoid those differences in query result – UninformedUser Jun 24 '19 at 03:52
  • 1
    that said, you created 3(?) questions the last two days, maybe you could add the corresponding answers and click also on "accept" such that those questions can be considered as closed? – UninformedUser Jun 24 '19 at 03:56
  • @AKSW I added the answers to all my questions this time :) except this one: https://stackoverflow.com/questions/56724431/how-to-remove-non-english-entries-from-wikidata? where I am struggling to remove general terms from my vocabulary. I thought the terms that have a lot of incoming links in English wikidata are the most general terms. That is why I followed this path. Honestly, I was thinking about for weeks and still did not find a good solution. If you have any suggestions in filtering general terms using wikidata/dbpedia/wikipedia please kindly let me know :) – EmJ Jun 24 '19 at 04:27
  • @AKSW I am happy to open a new question and post your suggestion as an answer :) Thank you very much for all your valuable comments once again :) – EmJ Jun 24 '19 at 04:28
  • 1
    the most incoming links in graphs are called usually called *authorities*. That has nothing to do with most general I'd think, but ok if this is your assumption. Nevertheless, I still don't understand why filtering out other languages would help here. And what about the the other incoming links? Have you had a look at all links? There are also those statement links, like `wds:Q47450549-9a42a039-42f9-5b51-aa83-5cb0303d6551 ps:P921`? I guess, you have to read a bit about the Wikidata data model in their docs to understand the meaning of those links. – UninformedUser Jun 24 '19 at 06:46
  • @AKSW yes, you are right. Now I am also feeling that considering authorities as the general terms is not a meaningful approach. Do you have any other suggestions (such as using properties/paths etc. in wikidata/dbpedia) to find potential general terms? It would be a great great assistance if you could help in this instance. Thank you very much :) – EmJ Jun 24 '19 at 07:42

1 Answers1

2

My code using SPARQLWRAPPER is almost right. However, I have made a typo when preparing the query using f-strings.

The corrected code is as follows, which solved my issue.

import pandas as pd
from SPARQLWrapper import SPARQLWrapper, JSON

sparqlwd = SPARQLWrapper("https://query.wikidata.org/sparql")
myid = "wd:Q22673982"
sparqlwd.setQuery(f"SELECT ?s ?p WHERE {{?s ?p {myid} .}}")
sparqlwd.setReturnFormat(JSON)
results = sparqlwd.query().convert()
print(results)
results_df = pd.io.json.json_normalize(results['results']['bindings'])
print(results_df)
EmJ
  • 4,398
  • 9
  • 44
  • 105