LearnToGrow
LearnToGrow

Reputation: 1750

How to get the Wikidata ID (Q…) using just a label?

Is there any way to get the Wikidata Q identifier of an entity using just the label as a string?

For example, "Java":

Q251

I want to search only the English labels.

Upvotes: 1

Views: 970

Answers (1)

PREFIX wd: <http://www.wikidata.org/entity/>
SELECT DISTINCT ?qid
WHERE {

  # make input string into a language-tagged string
  BIND( STRLANG("Java", "en") AS ?label ) .

  # search all items that have this languaged-tagged string as label
  ?item rdfs:label ?label .

  # extract the last path segment of the URI
  BIND(STRAFTER(STR(?item), STR(wd:)) AS ?qid) .

}

If you can append @en to the input string ("Java"@en), you can use:

PREFIX wd: <http://www.wikidata.org/entity/>
SELECT DISTINCT ?qid
WHERE {

  ?item rdfs:label "Java"@en .

  BIND(STRAFTER(STR(?item), STR(wd:)) AS ?qid) .

}

If you are fine with the URI (e.g., http://www.wikidata.org/entity/Q251) instead of the QID (e.g., Q251) as result, you can use:

SELECT DISTINCT ?item
WHERE {

  ?item rdfs:label "Java"@en .

}

If you want to ignore case, you can use UCASE or LCASE in a FILTER.

Upvotes: 3

Related Questions