-
Notifications
You must be signed in to change notification settings - Fork 57
#25 Issue: Wikidata URI Grounding #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aliiakbarkhan
wants to merge
4
commits into
dbpedia:master
Choose a base branch
from
aliiakbarkhan:aliiakbarkhan/issue25
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+284
−32
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bd4b34c
#25 Issue: Wikidata URI Grounding
aliiakbarkhan 3d7011b
Fix suggested by coderrabbtai
aliiakbarkhan ae9b7a9
fix : second KB lookup now blocks every request.
aliiakbarkhan 70850be
fix for accept the HTTPS forms of these resource URIs too.
aliiakbarkhan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| { | ||
| "java.configuration.updateBuildConfiguration": "interactive" | ||
| "java.configuration.updateBuildConfiguration": "interactive", | ||
| "java.compile.nullAnalysis.mode": "automatic" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| package chatbot.lib.api; | ||
|
|
||
| import chatbot.lib.Constants; | ||
| import chatbot.lib.Utility; | ||
| import chatbot.lib.request.TemplateType; | ||
| import chatbot.lib.response.ResponseData; | ||
| import chatbot.lib.response.ResponseType; | ||
| import org.apache.jena.query.*; | ||
| import org.apache.jena.sparql.engine.http.QueryEngineHTTP; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.util.*; | ||
|
|
||
| /** | ||
| * Service for querying the Wikidata SPARQL endpoint to retrieve entity | ||
| * information. | ||
| * Mirrors the interface of the DBpedia SPARQL class but targets Wikidata. | ||
| */ | ||
| public class WikidataSPARQL { | ||
| private static final Logger logger = LoggerFactory.getLogger(WikidataSPARQL.class); | ||
|
|
||
| private static final String ENDPOINT = "https://query.wikidata.org/sparql"; | ||
| private static final String PREFIXES = "PREFIX wd: <http://www.wikidata.org/entity/>\n" + | ||
| "PREFIX wdt: <http://www.wikidata.org/prop/direct/>\n" + | ||
| "PREFIX wikibase: <http://wikiba.se/ontology#>\n" + | ||
| "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" + | ||
| "PREFIX schema: <http://schema.org/>\n"; | ||
|
|
||
| public WikidataSPARQL() { | ||
| } | ||
|
|
||
| public String buildQuery(String query) { | ||
| return PREFIXES + query; | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves entity information from Wikidata for the given URI. | ||
| * Returns a ResponseData card with label, description, image, Wikipedia link, | ||
| * and Wikidata link. | ||
| */ | ||
| public ResponseData getEntityInformation(String uri) { | ||
| String entityId = Utility.extractWikidataEntityId(uri); | ||
| if (entityId == null) { | ||
| return null; | ||
| } | ||
|
|
||
| String query = buildQuery( | ||
| "SELECT ?label ?description ?image ?articleEN WHERE {\n" + | ||
| " wd:" + entityId + " rdfs:label ?label . FILTER(lang(?label) = 'en') .\n" + | ||
| " OPTIONAL { wd:" + entityId | ||
| + " schema:description ?description . FILTER(lang(?description) = 'en') }\n" + | ||
| " OPTIONAL { wd:" + entityId + " wdt:P18 ?image }\n" + | ||
| " OPTIONAL {\n" + | ||
| " ?articleEN schema:about wd:" + entityId + " ;\n" + | ||
| " schema:isPartOf <https://en.wikipedia.org/> .\n" + | ||
| " }\n" + | ||
| "} LIMIT 1"); | ||
|
|
||
| QueryExecution queryExecution = null; | ||
| ResponseData responseData = null; | ||
|
|
||
| try { | ||
| queryExecution = executeQuery(query); | ||
| Iterator<QuerySolution> results = queryExecution.execSelect(); | ||
| if (results.hasNext()) { | ||
| QuerySolution result = results.next(); | ||
| responseData = new ResponseData(); | ||
|
|
||
| // Label | ||
| String label = result.get("label").asLiteral().getString(); | ||
| responseData.setTitle(label); | ||
|
|
||
| // Description | ||
| if (result.get("description") != null) { | ||
| responseData.setText(result.get("description").asLiteral().getString()); | ||
| } | ||
|
|
||
| // Thumbnail / Image | ||
| if (result.get("image") != null) { | ||
| responseData.setImage(result.get("image").toString()); | ||
| } | ||
|
|
||
| // Wikipedia link | ||
| if (result.get("articleEN") != null) { | ||
| responseData.addButton(new ResponseData.Button("View in Wikipedia", ResponseType.BUTTON_LINK, | ||
| result.get("articleEN").toString())); | ||
| } | ||
|
|
||
| // Wikidata link | ||
| responseData.addButton(new ResponseData.Button("View in Wikidata", ResponseType.BUTTON_LINK, uri)); | ||
|
|
||
| // Learn More button (reuses the existing template mechanism) | ||
| responseData.addButton(new ResponseData.Button("Learn More", ResponseType.BUTTON_PARAM, | ||
| TemplateType.LEARN_MORE + Utility.STRING_SEPARATOR + uri + Utility.STRING_SEPARATOR + label)); | ||
| } | ||
| } catch (Exception e) { | ||
| logger.error("Error querying Wikidata for entity: " + uri, e); | ||
| } finally { | ||
| if (queryExecution != null) { | ||
| queryExecution.close(); | ||
| } | ||
| } | ||
| return responseData; | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves the English label for a Wikidata entity. | ||
| */ | ||
| public String getLabel(String uri) { | ||
| String entityId = Utility.extractWikidataEntityId(uri); | ||
| if (entityId == null) | ||
| return null; | ||
|
|
||
| String query = buildQuery( | ||
| "SELECT ?label WHERE {\n" + | ||
| " wd:" + entityId + " rdfs:label ?label . FILTER(lang(?label) = 'en') .\n" + | ||
| "} LIMIT 1"); | ||
|
|
||
| QueryExecution queryExecution = executeQuery(query); | ||
| String label = null; | ||
|
|
||
| try { | ||
| Iterator<QuerySolution> results = queryExecution.execSelect(); | ||
| if (results.hasNext()) { | ||
| label = results.next().get("label").asLiteral().getString(); | ||
| } | ||
| } | ||
| catch (Exception e) { | ||
| logger.error("Error querying Wikidata for label: " + uri, e); | ||
| } | ||
| finally { | ||
| if (queryExecution != null) { | ||
| queryExecution.close(); | ||
| } | ||
| } | ||
| return label; | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Executes a SPARQL query against the Wikidata endpoint. | ||
| */ | ||
| public QueryExecution executeQuery(String queryString) { | ||
| logger.info("Wikidata SPARQL Query is:\n" + queryString); | ||
| Query query = QueryFactory.create(queryString); | ||
| QueryEngineHTTP queryEngine = (QueryEngineHTTP) QueryExecutionFactory.sparqlService(ENDPOINT, query); | ||
| queryEngine.addParam("timeout", String.valueOf(Constants.API_TIMEOUT)); | ||
| // Wikidata requires a User-Agent header | ||
| queryEngine.addParam("format", "json"); | ||
aliiakbarkhan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return queryEngine; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.