Skip to content

Construct a KnowledgeBase from a Wikidata Query#548

Open
cthoyt wants to merge 1 commit intoallenai:mainfrom
cthoyt:wikidata-kb
Open

Construct a KnowledgeBase from a Wikidata Query#548
cthoyt wants to merge 1 commit intoallenai:mainfrom
cthoyt:wikidata-kb

Conversation

@cthoyt
Copy link
Contributor

@cthoyt cthoyt commented Aug 29, 2025

This PR is a spin-off from #542 that addresses #346.

This PR adds a utility function for constructing a knowledge base from a SPARQL query to Wikidata (requires pip install wikidata-client) with the following:

from scispacy.linking_utils import KnowledgeBase

# this SPARQL query gets named cats
sparql = """
SELECT ?item ?itemLabel ?itemDescription ?itemAltLabel
WHERE {
    ?item wdt:P31 wd:Q146. # Must be a cat
    SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],mul,en". }
}
"""

kb = KnowledgeBase.from_wikidata(sparql, "item")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments