Querying WikiData, difference between p and wdt default prefix
Asked Answered
S

3

21

I am new to wikidata and I can't figure out when I should use -->

wdt prefix (http://www.wikidata.org/prop/direct/)

and when I should use -->

p prefix (http://www.wikidata.org/prop/).

in my sparql queries. Can someone explain what each of these mean and what is the difference?

Selfreliant answered 15/3, 2016 at 22:3 Comment(1)
#46197332Phonon
H
14

Things in the p: namespace are used to select statements. Things in the wdt: namespace are used to select entites. Entity selection, with wdt:, allows you to simplify or summarize more complex queries involving statement selection.

When you see a p: you are usually going to see a ps: or pq: shortly following. This is because you rarely want a list of statements; you usually want to know something about those statements.

This example is a two-step process showing you all the graffiti in Wikidata:

SELECT ?graffiti ?graffitiLabel
WHERE
{
    ?graffiti p:P31 ?statement .  # entities that are statements
    ?statement ps:P31 wd:Q17514 . # which state something is graffiti
    SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}

Two different versions of the P31 property are used here, housed in different namespaces. Each version comes with different expectations about how it will connect to other items. Things in the p: namespace connect entities to statements, and things in the ps: namespace connect statements to values. In the example, p:P31 is used to select statements about an entity. The entity will be graffiti, but we do not specify that until the next line, where ps:P31 is used to select the values (subjects) of the statements, specifying that those values should be graffiti.

So, that's kind of complicated! The wdt: namespace is supposed to make this kind of query simper. The example could be rewritten as:

SELECT ?graffiti ?graffitiLabel
WHERE
{
    ?graffiti wdt:P31 wd:Q17514 . # entities that are graffiti
    SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}

This is now one line shorter because we are no longer looking for statements about graffiti, but for graffiti itself. The dual p: and ps: linkages are summarized with a wdt: version of the same P31 property. However, be aware:

  • This technique only works for statements that are true or false in nature, like, is a thing graffiti or not. (The "t" in wdt: stands for "truthy").
  • Information available to wdt: is just missing some facts, sometimes. Often in my experience a p: and ps: query will return a few more results than a wdt: query.
Hotchkiss answered 16/8, 2019 at 21:11 Comment(1)
"Things in the wdt: namespace are used to select entites." No. The wdt: prefix can also select literals like e.g. wdt:P1082 does.Reformism
R
8

If you go to the Wikidata item page for Barack Obama at https://www.wikidata.org/wiki/Q76 and scroll down, you see the entry for the "spouse" property P26: wikidata-spouse

Think of the p: prefix as a way to get to the entire white box on the right side of the image. In order to get to the information inside the white box, you need to dig deeper. In order to get to the main part of the information ("Michelle Obama"), you combine the p: prefix with the ps: prefix like this:

SELECT ?spouse WHERE {
  wd:Q76 p:P26 ?s .
  ?s ps:P26 ?spouse .
}

The variable ?s is an abstract statement node (aka the white box).

You can get the same information with only one triple in the body of the query by using wdt::

SELECT ?spouse WHERE {
  wd:Q76 wdt:P26 ?spouse .
}

So why would you ever use p:? You might have noticed that the white box also contains meta information ("start time" and "place of marriage"). In order to get to the meta information, you combine the p: prefix with the pq: prefix. The following example query returns all the information together with the statement node:

SELECT ?s ?spouse ?time ?place WHERE {
  wd:Q76 p:P26 ?s .
  ?s ps:P26 ?spouse .
  ?s pq:P580 ?time .
  ?s pq:P2842 ?place .
}
Reformism answered 10/10, 2021 at 21:31 Comment(0)
H
1

They're simply XML namespace prefixes, basically a shortcut for full URIs. So given wdt:Apples, the full URI is http://www.wikidata.org/prop/direct/Apples and given p:fruitType the URI is http://www.wikidata.org/prop/fruitType.

Prefixes/namespaces have no other meaning, they are simply ways to define the name of something with URL format. However conventions, such as defining properties in http://www.wikidata.org/prop/, are useful to separate the meanings of terms, so 'direct' is likely a sub-type of property as well (in this case having to do with wikipedia dumps).

For the specifics, you'd need to hope the authors have exposed some naming convention, or be caught in a loop of "was it p:P51 or p:P15 or maybe wdt:P51?". And may luck be with you because the "semantics" of semantic technology have been lost.

Hal answered 15/3, 2016 at 23:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.