Difference between revisions of "QAWiki:Guide/Queries"

From QAWiki
Jump to navigation Jump to search
Tag: Manual revert
 
(One intermediate revision by the same user not shown)
Line 5: Line 5:
The following are some tips to ensure high-quality and more uniform queries.
The following are some tips to ensure high-quality and more uniform queries.


== Return only what is asked for ==
=== Return what is asked for ===


If the query asks for a count, you can simply return a number. If the query rather asks for an entity or a list of entities, return their IDs/URLs on Wikidata (applications can then decide what data about entities to return, such as labels, images, etc.). Return distinct results. Though it might be tempting to return more background, this can be done automatically, or otherwise, it can form a different question.
If the query asks for a count, you can simply return a number. If the query rather asks for an entity or a list of entities, return their IDs/URLs on Wikidata (applications can then decide what data about entities to return, such as labels, images, etc.). Return distinct results. Though it might be tempting to return more background, this can be done automatically, or otherwise, it can form a different question.


== Try to keep queries general / Use elemental entities ==
=== Try to keep queries general / Use elemental entities ===


The best queries work for a wide selection of entities. For example, consider the query for the question "[[Item:Q68|''Who is the president of Poland?'']]". One option would be to query for the value of the Wikidata property [https://www.wikidata.org/wiki/Property:P35 P35 (''head of state'')] for [https://www.wikidata.org/wiki/Q36 Poland], which will give us the correct answer. But if we swap Poland with the United Kingdom, we would get the answer of '''Charles III''' as the head of state of that country is a king, not a president. We could alternatively look for the value for [https://www.wikidata.org/wiki/Property:P1308 P1308 (''officeholder'')] on the [https://www.wikidata.org/wiki/Q1054799 President of the Republic of Poland], but this query is difficult to re-use for other entities due to the compound nature of this entity; rather we'd like to build the query from simpler, more elemental entities like [https://www.wikidata.org/wiki/Q30461 president] and [https://www.wikidata.org/wiki/Q36 Poland] that are more easily interchanged to generate new queries. Hence the most general form of the query looks for [https://query.wikidata.org/#SELECT%20DISTINCT%20%3Fobj%20WHERE%20%7B%20%3Fsbj%20wdt%3AP31%2a%2Fwdt%3AP279%2a%20wd%3AQ30461%20.%20%3Fsbj%20wdt%3AP1001%20wd%3AQ36%20.%20%3Fsbj%20wdt%3AP1308%20%3Fobj%20%7D officeholders of positions that are instance or sub-class of president, and whose jurisdiction is Poland.] Though a bit more complex, this query avoids conflating monarchs with presidents, generalises to other positions such as ministers, and generalises also to other countries.
The best queries work for a wide selection of entities. For example, consider the query for the question "[[Item:Q68|''Who is the president of Poland?'']]". One option would be to query for the value of the Wikidata property [https://www.wikidata.org/wiki/Property:P35 P35 (''head of state'')] for [https://www.wikidata.org/wiki/Q36 Poland], which will give us the correct answer. But if we swap Poland with the United Kingdom, we would get the answer of '''Charles III''' as the head of state of that country is a king, not a president. We could alternatively look for the value for [https://www.wikidata.org/wiki/Property:P1308 P1308 (''officeholder'')] on the [https://www.wikidata.org/wiki/Q1054799 President of the Republic of Poland], but this query is difficult to re-use for other entities due to the compound nature of this entity; rather we'd like to build the query from simpler, more elemental entities like [https://www.wikidata.org/wiki/Q30461 president] and [https://www.wikidata.org/wiki/Q36 Poland] that are more easily interchanged to generate new queries. Hence the most general form of the query looks for [https://query.wikidata.org/#SELECT%20DISTINCT%20%3Fobj%20WHERE%20%7B%20%3Fsbj%20wdt%3AP31%2a%2Fwdt%3AP279%2a%20wd%3AQ30461%20.%20%3Fsbj%20wdt%3AP1001%20wd%3AQ36%20.%20%3Fsbj%20wdt%3AP1308%20%3Fobj%20%7D officeholders of positions that are instance or sub-class of president, and whose jurisdiction is Poland.] Though a bit more complex, this query avoids conflating monarchs with presidents, generalises to other positions such as ministers, and generalises also to other countries.


== Write the best query possible (even if not perfect) ==
=== Write the best query possible (even if not perfect) ===


Sometimes a query might provide incomplete results. Or if a query features negation, it might return incorrect results due to incomplete data. We recommend adding the query in any case, and hopefully when Wikidata becomes more complete/correct, the query will provide good results. For such cases, QAWiki also provides the qualifier/property [[Property:P36|P36 (''issue'')]], which can be used to indicate [https://tinyurl.com/2klcg89g a variety of such issues].
Sometimes a query might provide incomplete results. Or if a query features negation, it might return incorrect results due to incomplete data. We recommend adding the query in any case, and hopefully when Wikidata becomes more complete/correct, the query will provide good results. For such cases, QAWiki also provides the qualifier/property [[Property:P36|P36 (''issue'')]], which can be used to indicate [https://tinyurl.com/2klcg89g a variety of such issues].
<!-- SELECT DISTINCT ?o ?oLabel WHERE { ?s wdt:P36|pq:P36 ?o . SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } } -->
<!-- SELECT DISTINCT ?o ?oLabel WHERE { ?s wdt:P36|pq:P36 ?o . SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } } -->

Latest revision as of 05:50, 6 December 2022

Guide to creating queries to answer questions[edit | edit source]

The current focus is on generating SPARQL queries to answer questions over the Wikidata Query Service. You can write the query such that copying and pasting it into the service, it will generate the desired response; thus, for example, you can assume that Wikidata prefixes are defined, that features specific to the Wikidata Query Service are available, etc.

The following are some tips to ensure high-quality and more uniform queries.

Return what is asked for[edit | edit source]

If the query asks for a count, you can simply return a number. If the query rather asks for an entity or a list of entities, return their IDs/URLs on Wikidata (applications can then decide what data about entities to return, such as labels, images, etc.). Return distinct results. Though it might be tempting to return more background, this can be done automatically, or otherwise, it can form a different question.

Try to keep queries general / Use elemental entities[edit | edit source]

The best queries work for a wide selection of entities. For example, consider the query for the question "Who is the president of Poland?". One option would be to query for the value of the Wikidata property P35 (head of state) for Poland, which will give us the correct answer. But if we swap Poland with the United Kingdom, we would get the answer of Charles III as the head of state of that country is a king, not a president. We could alternatively look for the value for P1308 (officeholder) on the President of the Republic of Poland, but this query is difficult to re-use for other entities due to the compound nature of this entity; rather we'd like to build the query from simpler, more elemental entities like president and Poland that are more easily interchanged to generate new queries. Hence the most general form of the query looks for officeholders of positions that are instance or sub-class of president, and whose jurisdiction is Poland. Though a bit more complex, this query avoids conflating monarchs with presidents, generalises to other positions such as ministers, and generalises also to other countries.

Write the best query possible (even if not perfect)[edit | edit source]

Sometimes a query might provide incomplete results. Or if a query features negation, it might return incorrect results due to incomplete data. We recommend adding the query in any case, and hopefully when Wikidata becomes more complete/correct, the query will provide good results. For such cases, QAWiki also provides the qualifier/property P36 (issue), which can be used to indicate a variety of such issues.