From c0320925a4aa3e69782994009463eaa2d5632e92 Mon Sep 17 00:00:00 2001 From: Paul Heinze Date: Thu, 16 May 2024 10:23:07 +0200 Subject: [PATCH] Extract QAnswer queries to files (#347) * create SPARQL insert queries using commons methods * create SPARQL insert queries using commons methods * increment versions * change handling of QAnswer API response * look for question transltaion in the triplestore * add QB DeepPavlovWrapper to component reactor pom * add ability to retrieve answer sparql by index QAnswer provides a confidence score, but that has no relevance for selecting the best query candidate. Instead, only order is relevant for this Service. AnnotationOfAnswerSPARQL now also contains an index, s.t. the nth query candidate can be retrieved from the triple store. * use new naming for answer data type annotation * use existing question translation from triplestore * add index to generated insert query * add index for annotated answer SPARQL * increment versions * use predefined datatype URIs --- .gitignore | 5 +- pom.xml | 2 + qanary-component-QB-BirthDataWikidata/pom.xml | 2 +- .../wikidata/BirthDataQueryBuilder.java | 1 + .../pom.xml | 2 +- .../qb/QueryBuilderDateOfDeathDBpedia.java | 13 +- qanary-component-QB-DeepPavlovWrapper/pom.xml | 2 +- .../qb/DeepPavlovWrapper.java | 6 +- qanary-component-QB-GAnswerWrapper/pom.xml | 2 +- .../qb/GAnswerQueryBuilder.java | 6 +- qanary-component-QB-PlatypusWrapper/pom.xml | 2 +- .../qb/PlatypusQueryBuilder.java | 6 +- qanary-component-QB-QAnswer/pom.xml | 2 +- ...werQueryBuilderAndSparqlResultFetcher.java | 82 ++--- .../qanswer/qb/messages/QAnswerResult.java | 24 +- qanary-component-QB-RuBQWrapper/pom.xml | 2 +- .../rubqwrapper/qb/RuBQQueryBuilder.java | 6 +- qanary-component-QB-TeBaQaWrapper/pom.xml | 2 +- .../tebaqawrapper/qb/TeBaQAQueryBuilder.java | 6 +- qanary-component-QBE-QAnswer/pom.xml | 2 +- .../qbe/QAnswerQueryBuilderAndExecutor.java | 296 +++++++++--------- .../qanswer/qbe/messages/QAnswerResult.java | 9 + qanary-component-QE-SparqlExecuter/pom.xml | 2 +- .../qanary/sparqlexecuter/SparqlExecuter.java | 3 +- 24 files changed, 262 insertions(+), 223 deletions(-) diff --git a/.gitignore b/.gitignore index ed57ff84a..1c1100aeb 100644 --- a/.gitignore +++ b/.gitignore @@ -148,5 +148,6 @@ dmypy.json .factorypath -# LOCAL APPLEICATION PROPERTIES -application-local.properties \ No newline at end of file +# LOCAL FILES +application-local.properties +.run_tests diff --git a/pom.xml b/pom.xml index b462c70ac..097424f14 100644 --- a/pom.xml +++ b/pom.xml @@ -154,6 +154,8 @@ qanary-component-KG2KG-TranslateAnnotationsOfInstance qanary-component-QB-DateOfDeathDBpedia + + qanary-component-QB-DeepPavlovWrapper diff --git a/qanary-component-QB-BirthDataWikidata/pom.xml b/qanary-component-QB-BirthDataWikidata/pom.xml index 5759a1f14..f8fc742ae 100644 --- a/qanary-component-QB-BirthDataWikidata/pom.xml +++ b/qanary-component-QB-BirthDataWikidata/pom.xml @@ -5,7 +5,7 @@ 4.0.0 eu.wdaqua.qanary.component qanary-component-QB-BirthDataWikidata - 3.4.0 + 3.4.1 eu.wdaqua.qanary qa.qanarycomponent-parent diff --git a/qanary-component-QB-BirthDataWikidata/src/main/java/eu/wdaqua/component/qb/birthdata/wikidata/BirthDataQueryBuilder.java b/qanary-component-QB-BirthDataWikidata/src/main/java/eu/wdaqua/component/qb/birthdata/wikidata/BirthDataQueryBuilder.java index aa42fdd60..d620ea866 100644 --- a/qanary-component-QB-BirthDataWikidata/src/main/java/eu/wdaqua/component/qb/birthdata/wikidata/BirthDataQueryBuilder.java +++ b/qanary-component-QB-BirthDataWikidata/src/main/java/eu/wdaqua/component/qb/birthdata/wikidata/BirthDataQueryBuilder.java @@ -243,6 +243,7 @@ private QanaryMessage processForFirstNameAndLastName(QanaryMessage myQanaryMessa // confidence // is // expressed + bindings.add("index", ResourceFactory.createTypedLiteral(Integer.toString(i), XSDDatatype.XSDint)); bindings.add("application", ResourceFactory.createResource("urn:qanary:" + this.applicationName)); // get the template of the INSERT query diff --git a/qanary-component-QB-DateOfDeathDBpedia/pom.xml b/qanary-component-QB-DateOfDeathDBpedia/pom.xml index 245a36d96..2820abac9 100644 --- a/qanary-component-QB-DateOfDeathDBpedia/pom.xml +++ b/qanary-component-QB-DateOfDeathDBpedia/pom.xml @@ -5,7 +5,7 @@ 4.0.0 eu.wdaqua.qanary.component qanary-component-QB-DateOfDeathDBpedia - 0.1.3 + 0.1.4 eu.wdaqua.qanary qa.qanarycomponent-parent diff --git a/qanary-component-QB-DateOfDeathDBpedia/src/main/java/eu/wdaqua/qanary/component/dateofdeathrealpersons/qb/QueryBuilderDateOfDeathDBpedia.java b/qanary-component-QB-DateOfDeathDBpedia/src/main/java/eu/wdaqua/qanary/component/dateofdeathrealpersons/qb/QueryBuilderDateOfDeathDBpedia.java index f5917e42b..852204374 100644 --- a/qanary-component-QB-DateOfDeathDBpedia/src/main/java/eu/wdaqua/qanary/component/dateofdeathrealpersons/qb/QueryBuilderDateOfDeathDBpedia.java +++ b/qanary-component-QB-DateOfDeathDBpedia/src/main/java/eu/wdaqua/qanary/component/dateofdeathrealpersons/qb/QueryBuilderDateOfDeathDBpedia.java @@ -49,7 +49,6 @@ public class QueryBuilderDateOfDeathDBpedia extends QanaryComponent { private static final String QUERY_FILE_DBPEDIA_QUERY = "/queries/dbpedia_query.rq"; // used from the Qanary commons queries, query to create a new annotation and store the computed query - private static final String QUERY_FILE_STORE_COMPUTED_ANNOTATIONS = "/queries/insert_one_AnnotationOfAnswerSPARQL.rq"; private static final Logger logger = LoggerFactory.getLogger(QueryBuilderDateOfDeathDBpedia.class); private final String SUPPORTED_PREFIX = "What is the date of death of "; private final String applicationName; @@ -60,7 +59,6 @@ public QueryBuilderDateOfDeathDBpedia(@Value("${spring.application.name}") final // here if the files are available and do contain content // do files exist? QanaryTripleStoreConnector.guardNonEmptyFileFromResources(QUERY_FILE_FETCH_REQUIRED_ANNOTATIONS); QanaryTripleStoreConnector.guardNonEmptyFileFromResources(QUERY_FILE_DBPEDIA_QUERY); - QanaryTripleStoreConnector.guardNonEmptyFileFromResources(QUERY_FILE_STORE_COMPUTED_ANNOTATIONS); } /** @@ -155,9 +153,9 @@ public List fetchEntitiesAndCreateQueries(QanaryQuestion qanaryQuestion, */ public List createQueries(QanaryQuestion qanaryQuestion, List queries) throws Exception { List createdQueries = new ArrayList<>(); - for (String entity : queries - ) { - createdQueries.add(getInsertQuery(qanaryQuestion, entity)); + + for (int i = 0; i > queries.size(); i++) { + createdQueries.add(getInsertQuery(qanaryQuestion, queries.get(i), i)); } return createdQueries; } @@ -195,7 +193,7 @@ public String getDbpediaQuery(String dbpediaResource) throws Exception { } // binds query variables with concrete values and returns the Insert-query - public String getInsertQuery(QanaryQuestion myQanaryQuestion, String createdDbpediaQuery) + public String getInsertQuery(QanaryQuestion myQanaryQuestion, String createdDbpediaQuery, int index) throws SparqlQueryFailed, URISyntaxException, QanaryExceptionNoOrMultipleQuestions, IOException { QuerySolutionMap bindingsForInsert = new QuerySolutionMap(); @@ -204,8 +202,9 @@ public String getInsertQuery(QanaryQuestion myQanaryQuestion, String cre bindingsForInsert.add("application", ResourceFactory.createResource("urn:qanary:" + this.applicationName)); bindingsForInsert.add("selectQueryThatShouldComputeTheAnswer", ResourceFactory.createTypedLiteral(createdDbpediaQuery, XSDDatatype.XSDdate)); bindingsForInsert.add("score", ResourceFactory.createTypedLiteral("1.0", XSDDatatype.XSDfloat)); + bindingsForInsert.add("index", ResourceFactory.createTypedLiteral(Integer.toString(index), XSDDatatype.XSDint)); - return QanaryTripleStoreConnector.readFileFromResourcesWithMap(QUERY_FILE_STORE_COMPUTED_ANNOTATIONS, bindingsForInsert); + return QanaryTripleStoreConnector.insertAnnotationOfAnswerSPARQL(bindingsForInsert); } } diff --git a/qanary-component-QB-DeepPavlovWrapper/pom.xml b/qanary-component-QB-DeepPavlovWrapper/pom.xml index 2a313352f..b798fc9be 100644 --- a/qanary-component-QB-DeepPavlovWrapper/pom.xml +++ b/qanary-component-QB-DeepPavlovWrapper/pom.xml @@ -5,7 +5,7 @@ 4.0.0 eu.wdaqua.qanary.component qanary-component-QB-DeepPavlovWrapper - 0.1.1 + 0.2.0 eu.wdaqua.qanary diff --git a/qanary-component-QB-DeepPavlovWrapper/src/main/java/eu/wdaqua/qanary/component/deeppavlovwrapper/qb/DeepPavlovWrapper.java b/qanary-component-QB-DeepPavlovWrapper/src/main/java/eu/wdaqua/qanary/component/deeppavlovwrapper/qb/DeepPavlovWrapper.java index 71459d14c..8f32fe4f8 100644 --- a/qanary-component-QB-DeepPavlovWrapper/src/main/java/eu/wdaqua/qanary/component/deeppavlovwrapper/qb/DeepPavlovWrapper.java +++ b/qanary-component-QB-DeepPavlovWrapper/src/main/java/eu/wdaqua/qanary/component/deeppavlovwrapper/qb/DeepPavlovWrapper.java @@ -159,7 +159,8 @@ public QanaryMessage process(QanaryMessage myQanaryMessage) { // STEP 3: store computed knowledge about the given question into the Qanary triplestore // (the global process memory) // -------------------------------------------------------------------- - String sparql = getSparqlInsertQuery(myQanaryQuestion, result); + int index = 0; // only one query expected + String sparql = getSparqlInsertQuery(myQanaryQuestion, result, index); myQanaryUtils.getQanaryTripleStoreConnector().update(sparql); } } catch (Exception e) { @@ -239,7 +240,7 @@ protected String prepareResultQueryForSparqlInsert(String resultQuery) { * @throws SparqlQueryFailed * @throws IOException */ - protected String getSparqlInsertQuery(QanaryQuestion myQanaryQuestion, DeepPavlovResult result) throws QanaryExceptionNoOrMultipleQuestions, URISyntaxException, SparqlQueryFailed, IOException { + protected String getSparqlInsertQuery(QanaryQuestion myQanaryQuestion, DeepPavlovResult result, int index) throws QanaryExceptionNoOrMultipleQuestions, URISyntaxException, SparqlQueryFailed, IOException { String query = prepareResultQueryForSparqlInsert(result.getSparql()); @@ -250,6 +251,7 @@ protected String getSparqlInsertQuery(QanaryQuestion myQanaryQuestion, D bindings.add("targetQuestion", ResourceFactory.createResource(myQanaryQuestion.getUri().toASCIIString())); bindings.add("selectQueryThatShouldComputeTheAnswer", ResourceFactory.createStringLiteral(query)); bindings.add("confidence", ResourceFactory.createTypedLiteral(result.getConfidence())); + bindings.add("index", ResourceFactory.createTypedLiteral(index)); bindings.add("application", ResourceFactory.createResource("urn:qanary:" + this.applicationName)); // get the template of the INSERT query diff --git a/qanary-component-QB-GAnswerWrapper/pom.xml b/qanary-component-QB-GAnswerWrapper/pom.xml index d97ec715b..95f507223 100644 --- a/qanary-component-QB-GAnswerWrapper/pom.xml +++ b/qanary-component-QB-GAnswerWrapper/pom.xml @@ -5,7 +5,7 @@ 4.0.0 eu.wdaqua.qanary.component qanary-component-QB-GAnswerWrapper - 3.2.2 + 3.2.3 eu.wdaqua.qanary diff --git a/qanary-component-QB-GAnswerWrapper/src/main/java/eu/wdaqua/qanary/component/ganswerwrapper/qb/GAnswerQueryBuilder.java b/qanary-component-QB-GAnswerWrapper/src/main/java/eu/wdaqua/qanary/component/ganswerwrapper/qb/GAnswerQueryBuilder.java index 8839fc54b..64915b215 100644 --- a/qanary-component-QB-GAnswerWrapper/src/main/java/eu/wdaqua/qanary/component/ganswerwrapper/qb/GAnswerQueryBuilder.java +++ b/qanary-component-QB-GAnswerWrapper/src/main/java/eu/wdaqua/qanary/component/ganswerwrapper/qb/GAnswerQueryBuilder.java @@ -138,7 +138,8 @@ public QanaryMessage process(QanaryMessage myQanaryMessage) throws Exception { } // STEP 3: add information to Qanary triplestore - String sparql = getSparqlInsertQuery(myQanaryQuestion, result); + int index = 0; // only one query expected + String sparql = getSparqlInsertQuery(myQanaryQuestion, result, index); myQanaryUtils.getQanaryTripleStoreConnector().update(sparql); return myQanaryMessage; @@ -197,7 +198,7 @@ private String cleanStringForSparqlQuery(String myString) { * @throws SparqlQueryFailed * @throws IOException */ - protected String getSparqlInsertQuery(QanaryQuestion myQanaryQuestion, GAnswerResult result) throws QanaryExceptionNoOrMultipleQuestions, URISyntaxException, SparqlQueryFailed, IOException { + protected String getSparqlInsertQuery(QanaryQuestion myQanaryQuestion, GAnswerResult result, int index) throws QanaryExceptionNoOrMultipleQuestions, URISyntaxException, SparqlQueryFailed, IOException { String answerSparql = cleanStringForSparqlQuery(result.getSparql()); @@ -208,6 +209,7 @@ protected String getSparqlInsertQuery(QanaryQuestion myQanaryQuestion, G bindings.add("targetQuestion", ResourceFactory.createResource(myQanaryQuestion.getUri().toASCIIString())); bindings.add("selectQueryThatShouldComputeTheAnswer", ResourceFactory.createStringLiteral(answerSparql)); bindings.add("confidence", ResourceFactory.createTypedLiteral(result.getConfidence())); + bindings.add("index", ResourceFactory.createTypedLiteral(index)); bindings.add("application", ResourceFactory.createResource("urn:qanary:" + this.applicationName)); // get the template of the INSERT query diff --git a/qanary-component-QB-PlatypusWrapper/pom.xml b/qanary-component-QB-PlatypusWrapper/pom.xml index a93ca3d8d..9fe29e952 100644 --- a/qanary-component-QB-PlatypusWrapper/pom.xml +++ b/qanary-component-QB-PlatypusWrapper/pom.xml @@ -5,7 +5,7 @@ 4.0.0 eu.wdaqua.qanary.component qanary-component-QB-PlatypusWrapper - 3.3.4 + 3.3.5 eu.wdaqua.qanary diff --git a/qanary-component-QB-PlatypusWrapper/src/main/java/eu/wdaqua/qanary/component/platypuswrapper/qb/PlatypusQueryBuilder.java b/qanary-component-QB-PlatypusWrapper/src/main/java/eu/wdaqua/qanary/component/platypuswrapper/qb/PlatypusQueryBuilder.java index 44276f5e1..b8695228d 100644 --- a/qanary-component-QB-PlatypusWrapper/src/main/java/eu/wdaqua/qanary/component/platypuswrapper/qb/PlatypusQueryBuilder.java +++ b/qanary-component-QB-PlatypusWrapper/src/main/java/eu/wdaqua/qanary/component/platypuswrapper/qb/PlatypusQueryBuilder.java @@ -150,7 +150,8 @@ public QanaryMessage process(QanaryMessage myQanaryMessage) throws Exception { // STEP 3: add information to Qanary triplestore //String sparql = getSparqlInsertQuery(myQanaryQuestion, result); //myQanaryUtils.getQanaryTripleStoreConnector().update(sparql); - QuerySolutionMap bindings = populateBindings(myQanaryQuestion, result); + int index = 0; // only one query expected + QuerySolutionMap bindings = populateBindings(myQanaryQuestion, result, index); String insertAnswerSPARQL = getSparqlInsertQueryForAnswerSPARQL(bindings); String insertTypedLiteral = getSparqlInsertQueryForTypedLiteral(bindings); myQanaryUtils.getQanaryTripleStoreConnector().update(insertAnswerSPARQL); @@ -257,7 +258,7 @@ protected String getSparqlInsertQueryForTypedLiteral(QuerySolutionMap bindings) return sparql; } - protected QuerySolutionMap populateBindings(QanaryQuestion myQanaryQuestion, PlatypusResult result) throws QanaryExceptionNoOrMultipleQuestions, URISyntaxException, SparqlQueryFailed { + protected QuerySolutionMap populateBindings(QanaryQuestion myQanaryQuestion, PlatypusResult result, int index) throws QanaryExceptionNoOrMultipleQuestions, URISyntaxException, SparqlQueryFailed { String answerSparql = cleanStringForSparqlQuery(result.getSparql()); @@ -268,6 +269,7 @@ protected QuerySolutionMap populateBindings(QanaryQuestion myQanaryQuest bindings.add("targetQuestion", ResourceFactory.createResource(myQanaryQuestion.getUri().toASCIIString())); bindings.add("selectQueryThatShouldComputeTheAnswer", ResourceFactory.createStringLiteral(answerSparql)); bindings.add("confidence", ResourceFactory.createTypedLiteral(result.getConfidence())); + bindings.add("index", ResourceFactory.createTypedLiteral(index)); bindings.add("application", ResourceFactory.createResource("urn:qanary:" + this.applicationName)); // TODO: add result value with correct datatype // TODO: how does this handle a *list* of values? diff --git a/qanary-component-QB-QAnswer/pom.xml b/qanary-component-QB-QAnswer/pom.xml index 9d3f5fd80..841a4fe8f 100644 --- a/qanary-component-QB-QAnswer/pom.xml +++ b/qanary-component-QB-QAnswer/pom.xml @@ -4,7 +4,7 @@ 4.0.0 eu.wdaqua.qanary.component qanary-component-QB-QAnswer - 4.1.1 + 4.2.0 eu.wdaqua.qanary diff --git a/qanary-component-QB-QAnswer/src/main/java/eu/wdaqua/qanary/component/qanswer/qb/QAnswerQueryBuilderAndSparqlResultFetcher.java b/qanary-component-QB-QAnswer/src/main/java/eu/wdaqua/qanary/component/qanswer/qb/QAnswerQueryBuilderAndSparqlResultFetcher.java index 585ecd8f8..b5a25fa3d 100644 --- a/qanary-component-QB-QAnswer/src/main/java/eu/wdaqua/qanary/component/qanswer/qb/QAnswerQueryBuilderAndSparqlResultFetcher.java +++ b/qanary-component-QB-QAnswer/src/main/java/eu/wdaqua/qanary/component/qanswer/qb/QAnswerQueryBuilderAndSparqlResultFetcher.java @@ -18,13 +18,16 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; -import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; import org.springframework.web.util.UriComponentsBuilder; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; + import eu.wdaqua.qanary.commons.QanaryExceptionNoOrMultipleQuestions; import eu.wdaqua.qanary.commons.QanaryMessage; import eu.wdaqua.qanary.commons.QanaryQuestion; @@ -35,7 +38,6 @@ import eu.wdaqua.qanary.component.qanswer.qb.messages.QAnswerResult; import eu.wdaqua.qanary.component.qanswer.qb.messages.QAnswerResult.QAnswerQueryCandidate; import eu.wdaqua.qanary.exceptions.SparqlQueryFailed; -import net.minidev.json.JSONObject; @Component /** @@ -135,14 +137,31 @@ public QanaryMessage process(QanaryMessage myQanaryMessage) throws Exception { // STEP 1: get the required data from the Qanary triplestore (the global process // memory) QanaryQuestion myQanaryQuestion = this.getQanaryQuestion(myQanaryMessage); - String questionString = myQanaryQuestion.getTextualRepresentation(); - List retrievedNamedEntities = getNamedEntitiesOfQuestion(myQanaryQuestion, - myQanaryQuestion.getInGraph()); + String questionString = ""; + try { + questionString = myQanaryQuestion.getTextualRepresentation(lang); + logger.info("Using specific textual representation for language {}: {}", lang, questionString); + } catch (Exception e) { + logger.warn("Could not retrieve specific textual representation for language {}:\n{}", e.getMessage()); + } + // only if no language-specific text could be found + if (questionString.length() == 0){ + try { + questionString = myQanaryQuestion.getTextualRepresentation(); + logger.info("Using default textual representation {}", questionString); + } catch (Exception e) { + logger.warn("Could not retrieve textual representation:\n{}", e.getMessage()); + // stop processing of the question, as it will not work without a question text + return myQanaryMessage; + } + } // STEP 2: compute new information about the question // enriching of query, based on recognized resources + List retrievedNamedEntities = getNamedEntitiesOfQuestion(myQanaryQuestion, + myQanaryQuestion.getInGraph()); String questionStringWithResources = computeQuestionStringWithReplacedResources( questionString, retrievedNamedEntities, threshold); // fetching SPARQL query candidates from the QAnswer API @@ -185,11 +204,13 @@ protected QAnswerResult requestQAnswerWebService(URI qanaryApiUri, String questi .queryParam("user", "{user}") // .encode().toUriString(); - HttpEntity response = myRestTemplate.getForEntity(urlTemplate, JSONObject.class, parameters); - logger.info("QAnswer JSON result for question '{}': {}", questionString, - response.getBody().getAsString("question")); + ResponseEntity stringResponse = myRestTemplate.getForEntity(urlTemplate, String.class, parameters); + logger.info("QAnswer String result for question '{}': {}", questionString, + stringResponse.getBody()); + + JsonObject jsonResponse = JsonParser.parseString(stringResponse.getBody()).getAsJsonObject(); - return new QAnswerResult(response.getBody(), questionString, qanaryApiUri, lang, knowledgeBaseId, user); + return new QAnswerResult(jsonResponse, questionString, qanaryApiUri, lang, knowledgeBaseId, user); } /** @@ -320,6 +341,7 @@ public List getSparqlInsertQueriesForQueryCandidates( bindings.add("targetQuestion", ResourceFactory.createResource(questionUri.toASCIIString())); bindings.add("selectQueryThatShouldComputeTheAnswer", ResourceFactory.createStringLiteral(queryCandidate.getQueryString())); bindings.add("confidence", ResourceFactory.createTypedLiteral(queryCandidate.getScore())); + bindings.add("index", ResourceFactory.createTypedLiteral(queryCandidate.getIndex())); bindings.add("application", ResourceFactory.createResource("urn:qanary:" + this.applicationName)); // get the template of the INSERT query @@ -333,41 +355,21 @@ public List getSparqlInsertQueriesForQueryCandidates( } public String getSparqlInsertQueryForImprovedQuestion( - URI graph, URI questionUri, QAnswerResult result) throws QanaryExceptionNoOrMultipleQuestions, URISyntaxException, SparqlQueryFailed { + URI graph, URI questionUri, QAnswerResult result) throws QanaryExceptionNoOrMultipleQuestions, URISyntaxException, SparqlQueryFailed, IOException { + logger.warn("get query for Improved Question"); // the computed answer's SPARQL query needs to be cleaned String improvedQuestion = cleanStringForSparqlQuery(result.getQuestion()); - String sparql = "" // - + "PREFIX qa: \n" // - + "PREFIX oa: \n" // - + "PREFIX xsd: \n" // - + "PREFIX rdf: \n" // - + "INSERT { \n" // - + "GRAPH ?graph { \n" // - // improved question - + " ?annotationImprovedQuestion a qa:AnnotationOfImprovedQuestion ; \n" // - + " oa:hasTarget ?question ; \n" // - + " oa:hasBody ?improvedQuestion ; \n" // - + " oa:annotatedBy ?service ; \n" // - + " oa:annotatedAt ?time ; \n" // - + " qa:score ?score . \n" // - // - + " ?improvedQuestion a qa:ImprovedQuestion ; \n " // - + " rdf:value ?improvedQuestionText . \n " // - + " }\n" // end: GRAPH - + "}\n" // end: insert - + "WHERE { \n" // - + " BIND (IRI(str(RAND())) AS ?annotationImprovedQuestion) . \n" // - + " BIND (IRI(str(RAND())) AS ?improvedQuestion) . \n" // - // - + " BIND (now() AS ?time) . \n" // - + " BIND (<" + graph.toASCIIString() + "> AS ?graph) . \n" // - + " BIND (<" + questionUri.toASCIIString() + "> AS ?question) . \n" // - + " BIND ( AS ?service ) . \n" // - + " BIND (\"\"\"" + improvedQuestion + "\"\"\"^^xsd:string AS ?improvedQuestionText ) . \n" // - // - + "} \n"; // end: where + // bind: graph, question, service, improvedQuestionText + QuerySolutionMap bindings = new QuerySolutionMap(); + bindings.add("graph", ResourceFactory.createResource(graph.toASCIIString())); + bindings.add("question", ResourceFactory.createResource(questionUri.toASCIIString())); + bindings.add("application", ResourceFactory.createResource("urn:qanary:" + this.applicationName)); + bindings.add("improvedQuestionText", ResourceFactory.createStringLiteral(improvedQuestion)); + + String sparql = QanaryTripleStoreConnector.insertAnnotationOfImprovedQuestion(bindings); + logger.warn("sparql: {}", sparql); return sparql; } diff --git a/qanary-component-QB-QAnswer/src/main/java/eu/wdaqua/qanary/component/qanswer/qb/messages/QAnswerResult.java b/qanary-component-QB-QAnswer/src/main/java/eu/wdaqua/qanary/component/qanswer/qb/messages/QAnswerResult.java index b2e0c01c9..2adad6f7a 100644 --- a/qanary-component-QB-QAnswer/src/main/java/eu/wdaqua/qanary/component/qanswer/qb/messages/QAnswerResult.java +++ b/qanary-component-QB-QAnswer/src/main/java/eu/wdaqua/qanary/component/qanswer/qb/messages/QAnswerResult.java @@ -11,10 +11,8 @@ import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; -import com.google.gson.JsonParser; import io.swagger.v3.oas.annotations.Hidden; -import net.minidev.json.JSONObject; public class QAnswerResult { private static final Logger logger = LoggerFactory.getLogger(QAnswerResult.class); @@ -27,13 +25,9 @@ public class QAnswerResult { private String question; private List queryCandidates; - public QAnswerResult(JSONObject json, String question, URI endpoint, String language, String knowledgebaseId, String user) + public QAnswerResult(JsonObject json, String question, URI endpoint, String language, String knowledgebaseId, String user) throws URISyntaxException { - logger.debug("result: {}", json.toJSONString()); - - JsonObject parsedJsonObject = JsonParser.parseString(json.toJSONString()).getAsJsonObject(); - this.question = question; this.language = language; this.knowledgebaseId = knowledgebaseId; @@ -41,7 +35,7 @@ public QAnswerResult(JSONObject json, String question, URI endpoint, String lang this.endpoint = endpoint; this.queryCandidates = new LinkedList(); - initData(parsedJsonObject); + initData(json); } /** @@ -54,11 +48,13 @@ private void initData(JsonObject parsedJsonObject) throws URISyntaxException { JsonArray queryCandidatesArray = parsedJsonObject.getAsJsonArray("queries").getAsJsonArray(); - for (JsonElement queryCandidate : queryCandidatesArray) { + for (int i = 0; i < queryCandidatesArray.size(); i++) { + JsonElement queryCandidate = queryCandidatesArray.get(i); JsonObject queryCandidateObject = queryCandidate.getAsJsonObject(); String query = queryCandidateObject.get("query").getAsString(); float score = queryCandidateObject.get("confidence").getAsFloat(); - QAnswerQueryCandidate candidate = new QAnswerQueryCandidate(query, score); + int index = i; + QAnswerQueryCandidate candidate = new QAnswerQueryCandidate(query, index, score); queryCandidates.add(candidate); } @@ -92,10 +88,12 @@ public String getQuestion() { public class QAnswerQueryCandidate { private String query; private float score; + private int index; - public QAnswerQueryCandidate(String query, float score) { + public QAnswerQueryCandidate(String query, int index, float score) { this.query = query; this.score = score; + this.index = index; } public String getQueryString() { @@ -105,6 +103,10 @@ public String getQueryString() { public float getScore() { return this.score; } + + public int getIndex() { + return this.index; + } } } diff --git a/qanary-component-QB-RuBQWrapper/pom.xml b/qanary-component-QB-RuBQWrapper/pom.xml index 24aff1cdb..043da1c71 100644 --- a/qanary-component-QB-RuBQWrapper/pom.xml +++ b/qanary-component-QB-RuBQWrapper/pom.xml @@ -5,7 +5,7 @@ 4.0.0 eu.wdaqua.qanary.component qanary-component-QB-RuBQWrapper - 3.2.4 + 3.2.5 eu.wdaqua.qanary diff --git a/qanary-component-QB-RuBQWrapper/src/main/java/eu/wdaqua/qanary/component/rubqwrapper/qb/RuBQQueryBuilder.java b/qanary-component-QB-RuBQWrapper/src/main/java/eu/wdaqua/qanary/component/rubqwrapper/qb/RuBQQueryBuilder.java index 753521779..9518df11e 100644 --- a/qanary-component-QB-RuBQWrapper/src/main/java/eu/wdaqua/qanary/component/rubqwrapper/qb/RuBQQueryBuilder.java +++ b/qanary-component-QB-RuBQWrapper/src/main/java/eu/wdaqua/qanary/component/rubqwrapper/qb/RuBQQueryBuilder.java @@ -138,7 +138,8 @@ public QanaryMessage process(QanaryMessage myQanaryMessage) throws Exception { } // STEP 3: add information to Qanary triplestore - String sparql = getSparqlInsertQuery(myQanaryQuestion, result); + int index = 0; // only one query expected + String sparql = getSparqlInsertQuery(myQanaryQuestion, result, index); myQanaryUtils.getQanaryTripleStoreConnector().update(sparql); return myQanaryMessage; @@ -197,7 +198,7 @@ private String cleanStringForSparqlQuery(String myString) { * @throws SparqlQueryFailed * @throws IOException */ - protected String getSparqlInsertQuery(QanaryQuestion myQanaryQuestion, RuBQResult result) throws QanaryExceptionNoOrMultipleQuestions, URISyntaxException, SparqlQueryFailed, IOException { + protected String getSparqlInsertQuery(QanaryQuestion myQanaryQuestion, RuBQResult result, int index) throws QanaryExceptionNoOrMultipleQuestions, URISyntaxException, SparqlQueryFailed, IOException { String answerSparql = cleanStringForSparqlQuery(result.getSparql()); @@ -208,6 +209,7 @@ protected String getSparqlInsertQuery(QanaryQuestion myQanaryQuestion, R bindings.add("targetQuestion", ResourceFactory.createResource(myQanaryQuestion.getUri().toASCIIString())); bindings.add("selectQueryThatShouldComputeTheAnswer", ResourceFactory.createStringLiteral(answerSparql)); bindings.add("confidence", ResourceFactory.createTypedLiteral(result.getConfidence())); + bindings.add("index", ResourceFactory.createTypedLiteral(index)); bindings.add("application", ResourceFactory.createResource("urn:qanary:" + this.applicationName)); // get the template of the INSERT query diff --git a/qanary-component-QB-TeBaQaWrapper/pom.xml b/qanary-component-QB-TeBaQaWrapper/pom.xml index 5640ba63b..6feb9daf9 100644 --- a/qanary-component-QB-TeBaQaWrapper/pom.xml +++ b/qanary-component-QB-TeBaQaWrapper/pom.xml @@ -5,7 +5,7 @@ 4.0.0 eu.wdaqua.qanary.component qanary-component-QB-TeBaQAWrapper - 3.2.5 + 3.2.6 eu.wdaqua.qanary diff --git a/qanary-component-QB-TeBaQaWrapper/src/main/java/eu/wdaqua/qanary/component/tebaqawrapper/qb/TeBaQAQueryBuilder.java b/qanary-component-QB-TeBaQaWrapper/src/main/java/eu/wdaqua/qanary/component/tebaqawrapper/qb/TeBaQAQueryBuilder.java index 99d55b3d8..a697b14b9 100644 --- a/qanary-component-QB-TeBaQaWrapper/src/main/java/eu/wdaqua/qanary/component/tebaqawrapper/qb/TeBaQAQueryBuilder.java +++ b/qanary-component-QB-TeBaQaWrapper/src/main/java/eu/wdaqua/qanary/component/tebaqawrapper/qb/TeBaQAQueryBuilder.java @@ -136,7 +136,8 @@ public QanaryMessage process(QanaryMessage myQanaryMessage) throws Exception { } // STEP 3: add information to Qanary triplestore - String sparql = getSparqlInsertQuery(myQanaryQuestion, result); + int index = 0; // only one query expected + String sparql = getSparqlInsertQuery(myQanaryQuestion, result, index); myQanaryUtils.getQanaryTripleStoreConnector().update(sparql); return myQanaryMessage; @@ -195,7 +196,7 @@ private String cleanStringForSparqlQuery(String myString) { * @throws SparqlQueryFailed * @throws IOException */ - protected String getSparqlInsertQuery(QanaryQuestion myQanaryQuestion, TeBaQAResult result) throws QanaryExceptionNoOrMultipleQuestions, URISyntaxException, SparqlQueryFailed, IOException { + protected String getSparqlInsertQuery(QanaryQuestion myQanaryQuestion, TeBaQAResult result, int index) throws QanaryExceptionNoOrMultipleQuestions, URISyntaxException, SparqlQueryFailed, IOException { String answerSparql = cleanStringForSparqlQuery(result.getSparql()); @@ -206,6 +207,7 @@ protected String getSparqlInsertQuery(QanaryQuestion myQanaryQuestion, T bindings.add("targetQuestion", ResourceFactory.createResource(myQanaryQuestion.getUri().toASCIIString())); bindings.add("selectQueryThatShouldComputeTheAnswer", ResourceFactory.createStringLiteral(answerSparql)); bindings.add("confidence", ResourceFactory.createTypedLiteral(result.getConfidence())); + bindings.add("index", ResourceFactory.createTypedLiteral(index)); bindings.add("application", ResourceFactory.createResource("urn:qanary:" + this.applicationName)); // get the template of the INSERT query diff --git a/qanary-component-QBE-QAnswer/pom.xml b/qanary-component-QBE-QAnswer/pom.xml index 8600b747a..b54ce792d 100644 --- a/qanary-component-QBE-QAnswer/pom.xml +++ b/qanary-component-QBE-QAnswer/pom.xml @@ -5,7 +5,7 @@ 4.0.0 eu.wdaqua.qanary.component qanary-component-QBE-QAnswer - 3.4.1 + 3.5.0 eu.wdaqua.qanary qa.qanarycomponent-parent diff --git a/qanary-component-QBE-QAnswer/src/main/java/eu/wdaqua/qanary/component/qanswer/qbe/QAnswerQueryBuilderAndExecutor.java b/qanary-component-QBE-QAnswer/src/main/java/eu/wdaqua/qanary/component/qanswer/qbe/QAnswerQueryBuilderAndExecutor.java index 568fa8fca..66b9f8c17 100644 --- a/qanary-component-QBE-QAnswer/src/main/java/eu/wdaqua/qanary/component/qanswer/qbe/QAnswerQueryBuilderAndExecutor.java +++ b/qanary-component-QBE-QAnswer/src/main/java/eu/wdaqua/qanary/component/qanswer/qbe/QAnswerQueryBuilderAndExecutor.java @@ -1,5 +1,6 @@ package eu.wdaqua.qanary.component.qanswer.qbe; +import java.io.IOException; import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; @@ -151,18 +152,59 @@ public QanaryMessage process(QanaryMessage myQanaryMessage) throws Exception { // STEP 1: get the required data from the Qanary triplestore (the global process // memory) QanaryQuestion myQanaryQuestion = this.getQanaryQuestion(myQanaryMessage); - String questionString = myQanaryQuestion.getTextualRepresentation(); - List retrievedNamedEntities = getNamedEntitiesOfQuestion(myQanaryQuestion, - myQanaryQuestion.getInGraph()); + + String questionString = ""; + try { + questionString = myQanaryQuestion.getTextualRepresentation(lang); + logger.info("Using specific textual representation for language {}: {}", lang, questionString); + } catch (Exception e) { + logger.warn("Could not retrieve specific textual representation for language {}:\n{}", e.getMessage()); + } + // only if no language-specific text could be found + if (questionString.length() == 0){ + try { + questionString = myQanaryQuestion.getTextualRepresentation(); + logger.info("Using default textual representation {}", questionString); + } catch (Exception e) { + logger.warn("Could not retrieve textual representation:\n{}", e.getMessage()); + // stop processing of the question, as it will not work without a question text + return myQanaryMessage; + } + } // STEP 2: enriching of query and fetching data from the QAnswer API + List retrievedNamedEntities = getNamedEntitiesOfQuestion(myQanaryQuestion, + myQanaryQuestion.getInGraph()); String questionStringWithResources = computeQuestionStringWithReplacedResources(questionString, retrievedNamedEntities, threshold); QAnswerResult result = requestQAnswerWebService(this.getQanswerEndpoint(), questionStringWithResources, lang, knowledgeBaseId, user); // STEP 3: add information to Qanary triplestore - String sparql = getSparqlInsertQuery(myQanaryQuestion, result, knowledgeBaseId); - myQanaryUtils.getQanaryTripleStoreConnector().update(sparql); + URI graph = myQanaryQuestion.getOutGraph(); + URI questionUri = myQanaryQuestion.getUri(); + + String sparqlImprovedQuestion = getSparqlInsertQueryForImprovedQuestion(graph, questionUri, result); + logger.debug("created SPARQL query for improved question: {}", sparqlImprovedQuestion); + myQanaryUtils.getQanaryTripleStoreConnector().update(sparqlImprovedQuestion); + + int index = 0; // only one query expected + String sparqlQueryCandidate = getSparqlInsertQueryForQueryCandidate(graph, questionUri, result, index); + logger.debug("created SPARQL query for query candidate: {}", sparqlQueryCandidate); + myQanaryUtils.getQanaryTripleStoreConnector().update(sparqlQueryCandidate); + + String sparqlAnswerJson = getSparqlInsertQueryForAnswerJson(graph, questionUri, result); + logger.debug("created SPARQL query for answer json: {}", sparqlAnswerJson); + myQanaryUtils.getQanaryTripleStoreConnector().update(sparqlAnswerJson); + + String sparqlAnswerType = getSparqlInsertQueryForAnswerType(graph, questionUri, result); + logger.debug("created SPARQL query for answer type: {}", sparqlAnswerType); + myQanaryUtils.getQanaryTripleStoreConnector().update(sparqlAnswerType); + + + // TODO: result json and type + +// String sparql = getSparqlInsertQuery(myQanaryQuestion, result, knowledgeBaseId); +// myQanaryUtils.getQanaryTripleStoreConnector().update(sparql); return myQanaryMessage; } @@ -210,7 +252,8 @@ protected QAnswerResult requestQAnswerWebService(URI uri, String questionString, logger.info("post to endpoint not successful: {}", e); } - return new QAnswerResult(response.getBody(), questionString, uri, lang, knowledgeBaseId, user); + return new QAnswerResult(response.getBody(), + questionString, uri, lang, knowledgeBaseId, user); } /** @@ -319,153 +362,124 @@ private String cleanStringForSparqlQuery(String myString) { } /** - * creates the SPARQL query for inserting the data into Qanary triplestore - *

- * data can be retrieved via SPARQL 1.1 from the Qanary triplestore using: - * - *

+     * creates the SPARQL query for inserting the improved question into Qanary triplestore
      *
-     * SELECT * FROM  WHERE {
-     * ?s ?p ?o ;
-     * a ?type.
-     * VALUES ?t {
-     * qa:AnnotationOfAnswerSPARQL qa:SparqlQuery
-     * qa:AnnotationOfImprovedQuestion qa:ImprovedQuestion
-     * qa:AnnotationAnswer qa:Answer
-     * qa:AnnotationOfAnswerType qa:AnswerType
-     * }
-     * }
-     * ORDER BY ?type
-     * 
- * - * @param myQanaryQuestion + * @param graph + * @param questionUri * @param result * @return * @throws QanaryExceptionNoOrMultipleQuestions * @throws URISyntaxException * @throws SparqlQueryFailed + * @throws IOException */ - private String getSparqlInsertQuery(QanaryQuestion myQanaryQuestion, QAnswerResult result, String usedKnowledgeGraph) - throws QanaryExceptionNoOrMultipleQuestions, URISyntaxException, SparqlQueryFailed { + public String getSparqlInsertQueryForImprovedQuestion( + URI graph, URI questionUri, QAnswerResult result) throws QanaryExceptionNoOrMultipleQuestions, URISyntaxException, SparqlQueryFailed, IOException { + logger.warn("get query for Improved Question"); // the computed answer's SPARQL query needs to be cleaned - String createdAnswerSparqlQuery = cleanStringForSparqlQuery(result.getSparql()); String improvedQuestion = cleanStringForSparqlQuery(result.getQuestion()); - String answerValuesAsListFormat = ""; - int counter = 1; // starts at 1 - for (String answer : result.getValues()) { - // only one consistent type for all answers is expected - if (result.isAnswerOfResourceType()) { - answerValuesAsListFormat += String.format("; rdf:_%d <%s> ", counter, answer); - } else { - answerValuesAsListFormat += String.format("; rdf:_%d \"%s\"^^<%s> ", counter, answer, - result.getDatatype()); - } - counter++; - } + // bind: graph, question, service, improvedQuestionText + QuerySolutionMap bindings = new QuerySolutionMap(); + bindings.add("graph", ResourceFactory.createResource(graph.toASCIIString())); + bindings.add("question", ResourceFactory.createResource(questionUri.toASCIIString())); + bindings.add("application", ResourceFactory.createResource("urn:qanary:" + this.applicationName)); + bindings.add("improvedQuestionText", ResourceFactory.createStringLiteral(improvedQuestion)); + + String sparql = QanaryTripleStoreConnector.insertAnnotationOfImprovedQuestion(bindings); + logger.warn("sparql: {}", sparql); + + return sparql; + } + + /** + * creates the SPARQL query for inserting the query candidate into Qanary triplestore + * + * @param graph + * @param questionUri + * @param result + * @return + * @throws QanaryExceptionNoOrMultipleQuestions + * @throws URISyntaxException + * @throws SparqlQueryFailed + * @throws IOException + */ + public String getSparqlInsertQueryForQueryCandidate( + URI graph, URI questionUri, QAnswerResult result, int index) throws QanaryExceptionNoOrMultipleQuestions, URISyntaxException, SparqlQueryFailed, IOException { + + QuerySolutionMap bindings = new QuerySolutionMap(); + // use the variable names defined in method insertAnnotationOfAnswerSPARQL + bindings.add("graph", ResourceFactory.createResource(graph.toASCIIString())); + bindings.add("targetQuestion", ResourceFactory.createResource(questionUri.toASCIIString())); + bindings.add("selectQueryThatShouldComputeTheAnswer", ResourceFactory.createStringLiteral(result.getSparql())); + bindings.add("confidence", ResourceFactory.createTypedLiteral(result.getConfidence())); + bindings.add("index", ResourceFactory.createTypedLiteral(index)); // TODO: currently, only one SPARQL is annotated, so index is always 0 + bindings.add("application", ResourceFactory.createResource("urn:qanary:" + this.applicationName)); + + // get the template of the INSERT query + String sparql = QanaryTripleStoreConnector.insertAnnotationOfAnswerSPARQL(bindings); + logger.info("SPARQL insert for adding data to Qanary triplestore: {}", sparql); + + return sparql; + } + + /** + * creates the SPARQL query for inserting the answer json into Qanary triplestore + * + * @param graph + * @param questionUri + * @param result + * @return + * @throws QanaryExceptionNoOrMultipleQuestions + * @throws URISyntaxException + * @throws SparqlQueryFailed + * @throws IOException + */ + public String getSparqlInsertQueryForAnswerJson( + URI graph, URI questionUri, QAnswerResult result) throws QanaryExceptionNoOrMultipleQuestions, URISyntaxException, SparqlQueryFailed, IOException { + + QuerySolutionMap bindings = new QuerySolutionMap(); + // use the variable names defined in method insertAnnotationOfAnswerSPARQL + bindings.add("graph", ResourceFactory.createResource(graph.toASCIIString())); + bindings.add("targetQuestion", ResourceFactory.createResource(questionUri.toASCIIString())); + // TODO: check content of answer json + bindings.add("jsonAnswer", ResourceFactory.createStringLiteral(result.getAnswerJsonString())); + bindings.add("application", ResourceFactory.createResource("urn:qanary:" + this.applicationName)); + + // get the template of the INSERT query + String sparql = QanaryTripleStoreConnector.insertAnnotationOfAnswerJson(bindings); + logger.info("SPARQL insert for adding data to Qanary triplestore: {}", sparql); + + return sparql; + } + + /** + * creates the SPARQL query for inserting the answer type into Qanary triplestore + * + * @param graph + * @param questionUri + * @param result + * @return + * @throws QanaryExceptionNoOrMultipleQuestions + * @throws URISyntaxException + * @throws SparqlQueryFailed + * @throws IOException + */ + public String getSparqlInsertQueryForAnswerType( + URI graph, URI questionUri, QAnswerResult result) throws QanaryExceptionNoOrMultipleQuestions, URISyntaxException, SparqlQueryFailed, IOException { + + QuerySolutionMap bindings = new QuerySolutionMap(); + // use the variable names defined in method insertAnnotationOfAnswerSPARQL + bindings.add("graph", ResourceFactory.createResource(graph.toASCIIString())); + bindings.add("targetQuestion", ResourceFactory.createResource(questionUri.toASCIIString())); + bindings.add("answerDataType", ResourceFactory.createResource(result.getDatatype().toString())); + bindings.add("application", ResourceFactory.createResource("urn:qanary:" + this.applicationName)); + + // get the template of the INSERT query + String sparql = QanaryTripleStoreConnector.insertAnnotationOfAnswerDataType(bindings); + logger.info("SPARQL insert for adding data to Qanary triplestore: {}", sparql); - QuerySolutionMap bindingsForInsert = new QuerySolutionMap(); - bindingsForInsert.add("service", ResourceFactory.createResource(this.applicationName)); - bindingsForInsert.add("question", ResourceFactory.createStringLiteral(myQanaryQuestion.getUri().toASCIIString())); - bindingsForInsert.add("score", ResourceFactory.createTypedLiteral(String.valueOf(result.getConfidence()), XSDDatatype.XSDdouble)); - bindingsForInsert.add("improvedQuestionText", ResourceFactory.createStringLiteral(improvedQuestion)); - bindingsForInsert.add("sparqlQueryString", ResourceFactory.createStringLiteral(createdAnswerSparqlQuery.replace("\"", "\\\"") + "\"") ); - bindingsForInsert.add("answerDataType", ResourceFactory.createResource(String.valueOf(result.getDatatype()))); - bindingsForInsert.add("knowledgeGraph", ResourceFactory.createResource(String.valueOf(knowledgeGraphEndpoints.get(usedKnowledgeGraph)))); - bindingsForInsert.add("json", ResourceFactory.createStringLiteral( - result.getJsonString().replace("\"", "\\\"").replace("\\\\", "\\\\\\").replace("\\n", "").replace("\\t", "").replace("\\/", "/") - )); - - - // TODO: Move to qanary.commons and use template queries - String sparql = "" // - + "PREFIX qa: \n" // - + "PREFIX oa: \n" // - + "PREFIX xsd: \n" // - + "PREFIX rdf: \n" // - + "INSERT { \n" // - + "GRAPH <" + myQanaryQuestion.getOutGraph() + "> { \n" // - // used SPARQL query - + " ?annotationSPARQL a qa:AnnotationOfAnswerSPARQL ; \n" // - + " oa:hasTarget ?question ; \n" // - + " oa:hasBody ?sparql ; \n" // - + " oa:annotatedBy ?service ; \n" // - + " oa:annotatedAt ?time ; \n" // - + " qa:score ?score ; \n" // - + " qa:overKnowledgeGraph ?knowledgeGraph . \n" // - // - + " ?sparql a qa:SparqlQuery ; \n" // - + " rdf:value ?sparqlQueryString . \n" // - // improved question - + " ?annotationImprovedQuestion a qa:AnnotationOfImprovedQuestion ; \n" // - + " oa:hasTarget ?question ; \n" // - + " oa:hasBody ?improvedQuestion ; \n" // - + " oa:annotatedBy ?service ; \n" // - + " oa:annotatedAt ?time ; \n" // - + " qa:score ?score . \n" // - // - + " ?improvedQuestion a qa:ImprovedQuestion ; \n " // - + " rdf:value ?improvedQuestionText . \n " // - // answer - + " ?annotationAnswer a qa:AnnotationAnswer ; \n" // - + " oa:hasTarget ?question ; \n" // - + " oa:hasBody ?answer ; \n" // - + " oa:annotatedBy ?service ; \n" // - + " oa:annotatedAt ?time ; \n" // - + " qa:score ?score . \n" // - // - + " ?answer a qa:Answer ; \n" // - + " rdf:value [ a rdf:Seq " + answerValuesAsListFormat + " ] . \n" // - // answer type - + " ?annotationAnswerType a qa:AnnotationOfAnswerType ; \n" // - + " oa:hasTarget ?question ; \n" // - + " oa:hasBody ?annotationOfAnswerType ; \n" // - + " oa:annotatedBy ?service ; \n" // - + " oa:annotatedAt ?time ; \n" // - + " qa:score ?score . \n" // - // - + " ?answerType a qa:AnswerType ; \n" // - + " rdf:value ?answerDataType . \n" // - // JSON answer (GERBIL) - + " ?annotationAnswerJson a qa:AnnotationOfAnswerJson ; \n" // - + " oa:hasTarget ?question ; \n" // - + " oa:hasBody ?answerJson ; \n " // - + " oa:annotatedBy ?service ; \n" // - + " oa:annotatedAt ?time ; \n" // - + " qa:score ?score . \n" // - // - + " ?answerJson rdf:value ?json . \n " // - + " }\n" // end: graph - + "}\n" // end: insert - + "WHERE { \n" // - + " BIND (IRI(str(RAND())) AS ?annotationSPARQL) . \n" // - + " BIND (IRI(str(RAND())) AS ?sparql) . \n" // - // - + " BIND (IRI(str(RAND())) AS ?annotationOfAnswerType) . \n" // - + " BIND (IRI(str(RAND())) AS ?answerType) . \n" // - // - + " BIND (IRI(str(RAND())) AS ?annotationAnswer) . \n" // - + " BIND (IRI(str(RAND())) AS ?answer) . \n" // - // - + " BIND (IRI(str(RAND())) AS ?annotationAnswerJson) . \n" // - + " BIND (IRI(str(RAND())) AS ?answerJson) . \n" // - // - + " BIND (IRI(str(RAND())) AS ?annotationImprovedQuestion) . \n" // - + " BIND (IRI(str(RAND())) AS ?improvedQuestion) . \n" // - // - + " BIND (now() AS ?time) . \n" // - + " BIND (<" + myQanaryQuestion.getUri().toASCIIString() + "> AS ?question) . \n" // - + " BIND (\"" + result.getConfidence() + "\"^^xsd:double AS ?score) . \n" // - + " BIND ( AS ?service ) . \n" // - + " BIND (\"" + createdAnswerSparqlQuery.replace("\"", "\\\"") + "\"^^xsd:string AS ?sparqlQueryString ) . \n" // - + " BIND (\"" + improvedQuestion + "\"^^xsd:string AS ?improvedQuestionText ) . \n" // - + " BIND ( <" + result.getDatatype() + "> AS ?answerDataType) . \n" // - + " BIND ( <" + knowledgeGraphEndpoints.get(usedKnowledgeGraph) + "> AS ?knowledgeGraph) . \n" // - + " BIND (\"" + result.getJsonString().replace("\"", "\\\"").replace("\\\\", "\\\\\\").replace("\\n", "").replace("\\t", "").replace("\\/", "/") + "\"^^xsd:string AS ?json ). \n " // - + "}\n"; - - logger.info("SPARQL INSERT for adding data to the Qanary triplestore: ", sparql); return sparql; } diff --git a/qanary-component-QBE-QAnswer/src/main/java/eu/wdaqua/qanary/component/qanswer/qbe/messages/QAnswerResult.java b/qanary-component-QBE-QAnswer/src/main/java/eu/wdaqua/qanary/component/qanswer/qbe/messages/QAnswerResult.java index 039d4f240..e0fc3d26c 100644 --- a/qanary-component-QBE-QAnswer/src/main/java/eu/wdaqua/qanary/component/qanswer/qbe/messages/QAnswerResult.java +++ b/qanary-component-QBE-QAnswer/src/main/java/eu/wdaqua/qanary/component/qanswer/qbe/messages/QAnswerResult.java @@ -29,6 +29,7 @@ public class QAnswerResult { private String question; private String sparql; private List values; + private String answerJsonString; private String type; private URI datatype; private double confidence; @@ -98,13 +99,17 @@ private void initData(JsonArray answers) throws URISyntaxException, NoLiteralFie JsonArray languages = questionData.get("language").getAsJsonArray(); logger.debug("responseQuestion->language: {}", languages.toString()); + String answerJson = questionData.get("answers").getAsString(); + JsonObject language = languages.get(0).getAsJsonObject(); logger.debug("0. language: {}", language.toString()); logger.debug("0. sparql: {}", language.get("SPARQL").getAsString()); + logger.debug("0. answer json: {}", answerJson); logger.debug("0. confidence: {}", language.get("confidence").getAsDouble()); this.confidence = language.get("confidence").getAsDouble(); this.sparql = language.get("SPARQL").getAsString(); + this.answerJsonString = answerJson; } @@ -303,4 +308,8 @@ public String getSparql() { public String getJsonString() { return jsonString; } + + public String getAnswerJsonString(){ + return answerJsonString; + } } diff --git a/qanary-component-QE-SparqlExecuter/pom.xml b/qanary-component-QE-SparqlExecuter/pom.xml index f78571506..6a170fb7e 100644 --- a/qanary-component-QE-SparqlExecuter/pom.xml +++ b/qanary-component-QE-SparqlExecuter/pom.xml @@ -5,7 +5,7 @@ 4.0.0 eu.wdaqua.qanary.component qanary-component-QE-SparqlExecuter - 3.2.3 + 3.3.0 org.springframework.boot diff --git a/qanary-component-QE-SparqlExecuter/src/main/java/eu/wdaqua/qanary/sparqlexecuter/SparqlExecuter.java b/qanary-component-QE-SparqlExecuter/src/main/java/eu/wdaqua/qanary/sparqlexecuter/SparqlExecuter.java index 7ff9e950d..b154fd7b5 100644 --- a/qanary-component-QE-SparqlExecuter/src/main/java/eu/wdaqua/qanary/sparqlexecuter/SparqlExecuter.java +++ b/qanary-component-QE-SparqlExecuter/src/main/java/eu/wdaqua/qanary/sparqlexecuter/SparqlExecuter.java @@ -125,8 +125,7 @@ public String selectKnowledgeGraphEnpdoint(String sparqlQuery){ } public String getResultSparqlQuery(QanaryUtils myQanaryUtils, QanaryQuestion myQanaryQuestion) throws SparqlQueryFailed, IOException{ - // TODO: this was changed (no longer using QanaryMessage.getInGraph()) - ResultSet resultset = myQanaryUtils.getQanaryTripleStoreConnector().select(QanaryTripleStoreConnector.getHighestScoreAnnotationOfAnswerInGraph(myQanaryQuestion.getOutGraph())); + ResultSet resultset = myQanaryUtils.getQanaryTripleStoreConnector().select(QanaryTripleStoreConnector.getLowestIndexAnnotationOfAnswerInGraph(myQanaryQuestion.getOutGraph())); String sparqlQuery = ""; while (resultset.hasNext()) { sparqlQuery = resultset.next().get("selectQueryThatShouldComputeTheAnswer").toString().replace("\\\"", "\"").replace("\\n", "\n");