Skip to content

Commit

Permalink
Merge pull request #23 from WSE-research/inputDataExplanation
Browse files Browse the repository at this point in the history
Input data explanation
  • Loading branch information
dschiese authored Feb 18, 2024
2 parents 5ca54a1 + 14b8f05 commit 9072df2
Show file tree
Hide file tree
Showing 16 changed files with 95 additions and 1,129 deletions.
1,125 changes: 1 addition & 1,124 deletions client/src/json2.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>
<groupId>com.wse</groupId>
<artifactId>qanary-explanation-service</artifactId>
<version>2.2.0</version>
<version>2.3.0</version>
<name>Qanary explanation service</name>
<description>Webservice for rule-based explanation of QA-Systems as well as specific components</description>
<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@ControllerAdvice
public class ExplanationController {
Expand Down Expand Up @@ -49,4 +51,16 @@ public ResponseEntity<?> getExplanations(
}
}

@CrossOrigin
@GetMapping(value = {"/inputdata/{graphURI}/{componentURI}"}, produces = {
"application/rdf+xml",
"text/turtle",
"application/ld+json",
"*/*"})
public ResponseEntity<?> getInputExplanation(
@PathVariable String graphURI,
@PathVariable(required = false) String componentURI) throws IOException {
return new ResponseEntity<>(this.explanationService.createInputExplanation(graphURI,componentURI), HttpStatus.OK);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ protected void setWebClient(WebClient webClient) {

public void setSparqlEndpoint(URL sparqlEndpoint) {
this.sparqlEndpoint = sparqlEndpoint;
this.rdfConnection = RDFConnection.connect(sparqlEndpoint.toString());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ public String createTestWorkflow(AutomatedTestRequestBody requestBody, boolean d
AutomatedTest test;

while (jsonArray.length() < requestBody.getRuns()) {
logger.info("CURRENT RUN: {}", jsonArray.length()+1);
test = createTest(requestBody); // null if not successful
if (test != null) {
if (doGptCall) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.net.URL;
import java.nio.file.Files;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
Expand All @@ -33,6 +35,7 @@ public class ExplanationService {
// Query files
private static final String QUESTION_QUERY = "/queries/question_query.rq";
private static final String ANNOTATIONS_QUERY = "/queries/queries_for_annotation_types/fetch_all_annotation_types.rq";
private static final String INPUT_DATA_QUERY = "/queries/explanations/input_data/inputDataSelect.rq";
private static final String TEMPLATE_PLACEHOLDER_PREFIX = "${";
private static final String TEMPLATE_PLACEHOLDER_SUFFIX = "}";
private static final String OUTER_TEMPLATE_PLACEHOLDER_PREFIX = "&{";
Expand Down Expand Up @@ -90,10 +93,10 @@ private String getResult(String componentURI, String lang, List<String> explanat
String result = null;
logger.info("Explanations {}", explanations.size());
if (Objects.equals(lang, "en")) {
result = "The component " + componentURI + " has added " + (explanations.size() == 10 ? "at least " : "") + explanations.size() + " annotation(s) to the graph"
result = "The component " + componentURI + " has added " + (explanations.size() == 5 ? "at least " : "") + explanations.size() + " annotation(s) to the graph"
+ prefix + ": " + StringUtils.join(explanations, " ");
} else if (Objects.equals(lang, "de")) {
result = "Die Komponente " + componentURI + " hat " + (explanations.size() == 10 ? "mindestens " : "") + explanations.size() + " Annotation(en) zum Graph hinzugefügt"
result = "Die Komponente " + componentURI + " hat " + (explanations.size() == 5 ? "mindestens " : "") + explanations.size() + " Annotation(en) zum Graph hinzugefügt"
+ prefix + ": " + StringUtils.join(explanations, " ");
}
return result;
Expand Down Expand Up @@ -465,4 +468,57 @@ public String createTextualExplanation(String graphURI, String componentURI, Str
return getResult(componentURI, lang, explanations, createdExplanations.get(0));
}

public String createInputExplanation(String graph, String component) throws IOException {
QuerySolutionMap bindings = new QuerySolutionMap();
bindings.add("graph",ResourceFactory.createResource(graph));
bindings.add("usedComponent", ResourceFactory.createResource(component));
String query = QanaryTripleStoreConnector.readFileFromResourcesWithMap(INPUT_DATA_QUERY,bindings);

// set RDFConnection to URL for Input-data triplestore @see https://github.com/dschiese/input-data-storage
this.explanationSparqlRepository.setSparqlEndpoint(new URL("http://localhost:8891/sparql"));
ResultSet resultSet = this.explanationSparqlRepository.executeSparqlQueryWithResultSet(query);
int resultSetSize = 0;
List<String> explanationsForQueries = new ArrayList<>();

while(resultSet.hasNext()) {
QuerySolution currentSolution = resultSet.nextSolution();
resultSetSize++;
explanationsForQueries.add(createExplanationForQuery(currentSolution, graph, component));
}

// create Prefix
String prefix = getStringFromFile("/explanations/input_data/prefixes/en"); // adopt for any other languages
prefix = prefix.replace("${numberOfQueries}",String.valueOf(resultSetSize) +
(resultSetSize == 1 ? " SPARQL query" : " SPARQL queries")
);
prefix = prefix.replace("${component}",component).replace("${graph}",graph);
// Zusammenbauen

logger.info("Items: {}", explanationsForQueries.toString());
logger.info("PREFIX: {}", prefix);

return composeExplanation(explanationsForQueries,prefix);
}

public String createExplanationForQuery(QuerySolution currentSolution, String graph, String component) throws IOException {
String annotationType = currentSolution.get("annotationType").toString();
Map<String,String> items = new HashMap<>() {{
put("graph",graph);
put("component",component);
put("annotationType", annotationType);
}};
// for each language
String listItem = getStringFromFile("/explanations/input_data/" + annotationType + "/english");
return StringSubstitutor.replace(listItem,items,TEMPLATE_PLACEHOLDER_PREFIX, TEMPLATE_PLACEHOLDER_SUFFIX);
}

public String composeExplanation(List<String> listItems, String prefix) {
StringBuilder finalExplanation = new StringBuilder();
finalExplanation.append(prefix);
for(int i = 0; i < listItems.size(); i++) {
finalExplanation.append("\n").append(i+1).append(". ").append(listItems.get(i));
}
return finalExplanation.toString();
}

}
2 changes: 1 addition & 1 deletion src/main/resources/config/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ qanary.components.annotationofanswersparql=SINA,PlatypusQueryBuilder,QAnswerQuer
qanary.components.annotationofquestionlanguage=LD-Shuyo
qanary.components.annotationofquestiontranslation=
qanary.components.annotationofrelation=FalconRelComponent-dbpedia,DiambiguationProperty
chatgpt.api.key=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
chatgpt.api.key=
1 change: 0 additions & 1 deletion src/main/resources/example.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TEST_CLASS
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Die Komponente ${component} hat mit einer SPARQL-Anfrage auf den Graphen ${graph} alle Annotationen vom Typ ${typ} angefragt.
Dabei umfasst eine Annotation sowohl das Datum als auch die erzeugende Komponente, die URI der Ursprungsfrage, eine Resource als Body
und die dazugehörige Start- bzw. Endposition der korrelierenden Entität in der Ursprungsfrage. Sofern vorhanden wird auch die Bewertung für
die Resource übergeben.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
All Annotations of the type AnnotationOfInstance has been requested, which inherits the date of its creation, the URI for the origin question, a knowledge-graph resource as well as the correlating start and end position for the found entity. If provided, a score is fetched too.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Die Komponenten ${component} hat mit einer SPARQL Query
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test-English
1 change: 1 addition & 0 deletions src/main/resources/explanations/input_data/prefixes/en
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
On the graph ${graph} the component ${component} has used the following ${numberOfQueries} to compute new data:
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The component ${component} has used ${numberOfQueries} input-queries:\n
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
PREFIX ex: <http://example#>

SELECT *
FROM ?graph
WHERE {
?usedComponent ex:hasAnnotationType ?annotationType .
?annotationType ex:hasInputQuery ?inputQuery .
}

0 comments on commit 9072df2

Please sign in to comment.