user305883
user305883

Reputation: 1741

Retrieve linked wikidata entities having a wikipedia page

I want to query wikidata by free text or by category, to return entities who has a corresponding wikipedia page.

For each page (or for a selected page) I want to fetch all the linked wikidata entities who have a corresponding wikipedia article.

Note that:

(e.g. a page in French History is available in multiple languages; I may have linked pages in French only as well as others multiple languages).

I cannot figure out which wikidata APIs to corresponding ones in wikipedia, to query linked articles, and how to query linked pages that exist even beyond a selected language.

I looked at:

https://www.mediawiki.org/wiki/Wikidata_Query_Service/User_Manual/MWAPI

https://stackoverflow.com/a/57983365/305883

https://www.mediawiki.org/wiki/API:Links

For example I may start with this sparql query:

SELECT ?item ?type ?itemLabel ?typeLabel WHERE {
 {
   SELECT ?item WHERE {
    SERVICE wikibase:mwapi {
      bd:serviceParam wikibase:endpoint "en.wikipedia.org" .
      bd:serviceParam wikibase:api "Generator" .
      bd:serviceParam mwapi:generator "search" .
      bd:serviceParam mwapi:gsrsearch "artificial intelligence" .
      bd:serviceParam mwapi:gsrlimit "max" .
      ?item wikibase:apiOutputItem mwapi:item .
    }
  } LIMIT 100
 }
 hint:Prior hint:runFirst "true".
 ?item wdt:P31|wdt:P279 ?type .
 SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
} LIMIT 100

Could you show examples to expand or adapt this query ?

Could you suggest other references than https://www.mediawiki.org/wiki/Wikidata_Query_Service/User_Manual/MWAPI , for an extensive use to callout to Mediawiki API from SPARQL (so that I can exploit wikidata and wikipedia) ?

Upvotes: 3

Views: 1260

Answers (1)

madbob
madbob

Reputation: 600

A way to obtain a Wikidata entity starting from a Wikipedia link is to perform a SPARQL query like:

SELECT ?item WHERE {
  <https://en.wikipedia.org/wiki/Albert_Einstein> schema:about ?item
}

Once you have found that Wikidata ID for Albert Einstein is wd:Q937, you can run another SPARQL query as:

SELECT ?article WHERE {
  ?article schema:about wd:Q937 .
  ?article schema:isPartOf / wikibase:wikiGroup "wikipedia" .
}

to retrieve all Wikipedia articles for that entity.

Upvotes: 0

Related Questions