Skip to content

Commit

Permalink
Merge pull request #16 from WSE-research/358_explanation_annotationOf…
Browse files Browse the repository at this point in the history
…QuestionLanguage

358 explanation annotation of question language
  • Loading branch information
dschiese authored Sep 21, 2023
2 parents bbc130f + 861cb77 commit 9bf6e6c
Show file tree
Hide file tree
Showing 144 changed files with 1,654 additions and 16 deletions.
1 change: 1 addition & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ The supported ones are the following:
. AnnotationOfAnswerSPARQL
. AnnotationOfAnswerJSON
. AnnotationOfRelation
. AnnotationOfQuestionLanguage

You can check the output annotation type for existing components in the component's README at https://github.com/WDAqua/Qanary-question-answering-component .

Expand Down
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>1.4.0</version>
<version>1.5.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 @@ -41,9 +41,6 @@ public class ExplanationService {
private static final String ANNOTATIONS_QUERY = "/queries/queries_for_annotation_types/fetch_all_annotation_types.rq";
private static final String TEMPLATE_PLACEHOLDER_PREFIX = "${";
private static final String TEMPLATE_PLACEHOLDER_SUFFIX = "}";
private final String OUTER_TEMPLATE_PLACEHOLDER_PREFIX = "&{";
private final String OUTER_TEMPLATE_PLACEHOLDER_SUFFIX = "}&";
private final String OUTER_TEMPLATE_REGEX = "\\&\\{.*\\}\\&";
// Mappings
private static final Map<String, String> headerFormatMap = new HashMap<>() {{
put("application/rdf+xml", "RDFXML");
Expand All @@ -58,18 +55,21 @@ public class ExplanationService {
put("annotationofanswersparql", "/queries/queries_for_annotation_types/annotations_of_answer_sparql.rq");
put("annotationofrelation", "/queries/queries_for_annotation_types/annotations_of_relation_query.rq");
put("annotationofanswerjson", "/queries/queries_for_annotation_types/annotations_of_answer_json_query.rq");
put("annotationofquestionlanguage", "/queries/queries_for_annotation_types/annotations_of_question_language_query.rq");
}};

// Holds explanation templates for the declared annotation types
private static final Map<String, String> annotationTypeExplanationTemplate = new HashMap<>() {{
put("annotationofspotinstance", "/explanations/annotation_of_spot_instance/");
put("annotationofinstance", "/explanations/annotation_of_instance/");
put("annotationofanswersparql", "/explanations/annotation_of_answer_sparql/");
put("annotationofrelation", "/explanations/annotation_of_relation/");
put("annotationofanswerjson", "/explanations/annotation_of_answer_json/");
put("annotationofquestionlanguage", "/explanations/annotation_of_question_language/");
}};

final String EXPLANATION_NAMESPACE = "urn:qanary:explanations#";
private final String OUTER_TEMPLATE_PLACEHOLDER_PREFIX = "&{";
private final String OUTER_TEMPLATE_PLACEHOLDER_SUFFIX = "}&";
private final String OUTER_TEMPLATE_REGEX = "\\&\\{.*\\}\\&";
private final ObjectMapper objectMapper;
Logger logger = LoggerFactory.getLogger(ExplanationService.class);

Expand Down Expand Up @@ -437,7 +437,7 @@ public List<String> fetchAllAnnotations(String graphURI, String componentURI) th
RDFNode type = result.get("annotationType");
String typeLocalName = type.asResource().getLocalName();
logger.info("Annotation-Type found: {}", typeLocalName);
if(!Objects.equals(typeLocalName, "AnswerJson"))
if (!Objects.equals(typeLocalName, "AnswerJson"))
types.add(typeLocalName.toLowerCase());
}

Expand Down Expand Up @@ -524,16 +524,15 @@ public String checkAndReplaceOuterPlaceholder(String template) {
Pattern pattern = Pattern.compile(OUTER_TEMPLATE_REGEX);
Matcher matcher = pattern.matcher(template);

while(matcher.find()) {
while (matcher.find()) {
String a = matcher.group();
if(a.contains(TEMPLATE_PLACEHOLDER_PREFIX)) {
if (a.contains(TEMPLATE_PLACEHOLDER_PREFIX)) {
template = template.replace(a, "");
}
else
} else
template = template.replace(
a,
a.replace(OUTER_TEMPLATE_PLACEHOLDER_PREFIX, "")
.replace(OUTER_TEMPLATE_PLACEHOLDER_SUFFIX,""));
.replace(OUTER_TEMPLATE_PLACEHOLDER_SUFFIX, ""));
}

return template;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
am ${createdAt} für die Frage mit der URI '${hasTarget}' die Sprache '${body}'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
und hat für die folgende(n) Frage(n) die Sprachen bestimmt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
at ${createdAt} for the question with the URI '${hasTarget}' the language '${body}'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
and has found the languages for the following question(s)
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
PREFIX qa: <http://www.wdaqua.eu/qa#>
PREFIX oa: <http://www.w3.org/ns/openannotation/core/>
PREFIX questionID: <http://195.90.200.248:8090/question/stored-question__text_>

SELECT *
FROM ?graphURI
WHERE {
?annotationId a qa:AnnotationOfQuestionLanguage .
?annotationId oa:hasTarget ?hasTarget ;
oa:hasBody ?body ;
oa:annotatedBy ?createdBy ;
oa:annotatedAt ?createdAt .
}
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ class ComponentExplanationTests {
put("annotationofanswersparql", "/explanations/annotation_of_answer_sparql/");
put("annotationofrelation", "/explanations/annotation_of_relation/");
put("annotationofanswerjson", "/explanations/annotation_of_answer_json/");
put("annotationofquestionlanguage", "/explanations/annotation_of_question_language/");
}};
private ServiceDataForTests serviceDataForTests;
@Autowired
Expand Down Expand Up @@ -290,7 +291,15 @@ Given a set of (key, value) the result should be the template without any more p
TODO: adding a test-case with the corresponding template
*/
@ParameterizedTest
@ValueSource(strings = {"annotationofinstance", "annotationofspotinstance", "annotationofanswersparql", "annotationofanswerjson", "annotationofrelation"})
@ValueSource(
strings = {
"annotationofinstance",
"annotationofspotinstance",
"annotationofanswersparql",
"annotationofanswerjson",
"annotationofrelation",
"annotationofquestionlanguage"
})
public void replacePropertiesTest(String type) {

Map<String, String> convertedMap = serviceDataForTests.getConvertedMapWithStringValues();
Expand All @@ -317,8 +326,15 @@ public void replacePropertiesTest(String type) {
// Paramterized ? // Create .ttl-files parse them into a model, set RDFConnection, execute w/ repository
// just several maps with different values -> increased testability for other tests
@ParameterizedTest
@ValueSource(strings = {"annotationofinstance", "annotationofspotinstance", "annotationofanswersparql", "annotationofanswerjson", "annotationofrelation"})

@ValueSource(
strings = {
"annotationofinstance",
"annotationofspotinstance",
"annotationofanswersparql",
"annotationofanswerjson",
"annotationofrelation",
"annotationofquestionlanguage"
})
public void addingExplanationsTest(String type) throws IOException {
List<QuerySolutionMap> querySolutionMapList = serviceDataForTests.getQuerySolutionMapList();
ResultSet resultSet = serviceDataForTests.createResultSet(querySolutionMapList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class ServiceDataForTests {
put("start", ResourceFactory.createTypedLiteral("1", XSDDatatype.XSDinteger));
put("end", ResourceFactory.createTypedLiteral("3", XSDDatatype.XSDinteger));
put("answerJson", ResourceFactory.createTypedLiteral("{example: {data:json, type:json}}"));
put("hasTarget", ResourceFactory.createResource("questionID:981554csdf314kl2"));
}};
private final Map<String, String> convertedMapWithStringValues = new HashMap<>() {{
put("body", "http://dbpedia.org/resource/example");
Expand All @@ -38,7 +39,8 @@ public class ServiceDataForTests {
put("score", "0.33");
put("start", "1");
put("end", "3");
put("answerJson","{example: {data:json, type:json}}");
put("answerJson", "{example: {data:json, type:json}}");
put("hasTarget", "questionID:981554csdf314kl2");
}};
private Logger logger = LoggerFactory.getLogger(ServiceDataForTests.class);
private List<QuerySolutionMap> querySolutionMapList;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
am 2023-09-13T06:38:13.114944Z für die Frage mit der URI 'questionID:981554csdf314kl2' die Sprache 'http://dbpedia.org/resource/example'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
at 2023-09-13T06:38:13.114944Z for the question with the URI 'questionID:981554csdf314kl2' the language 'http://dbpedia.org/resource/example'
8 changes: 8 additions & 0 deletions target/classes/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
spring.application.name=Qanary explanation service
server.port=4000
sparqlEndpoint=http://195.90.200.248:8090/sparql
# swagger-ui custom path
springdoc.swagger-ui.path=/swagger-ui.html
# api-docs custom path
springdoc.api-docs.path=/api-docs
springdoc.version=2.1.0
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
am ${createdAt} eine SPARQL-query ausgeführt und folgende JSON zurückgegeben ' ${answerJson} ' &{mit einer Konfidenz von ${score}}&
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
on ${createdAt} a SPARQL-query was executed and returned the following JSON ' ${answerJson} ' &{with a confidence of ${score}}&
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
am ${createdAt} mit einer Konfidenz von ${score} die SPARQL-query ' ${body} '
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
on ${createdAt} with a confidence of ${score} the SPARQL-query ' ${body} '
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
am ${createdAt} &{mit einer Konfidenz von ${score}}& die Resource ${body}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
on ${createdAt} &{with a confidence of ${score}}& and the resource ${body}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
am ${createdAt} für die Frage mit der URI '${hasTarget}' die Sprache '${body}'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
und hat für die folgende(n) Frage(n) die Sprachen bestimmt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
at ${createdAt} for the question with the URI '${hasTarget}' the language '${body}'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
and has found the languages for the following question(s)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
' ${body} ' am ${createdAt} &{beginnend bei ${start} und endend bei ${end}}& &{mit einer Konfidenz von ${score}}&
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
und dabei wurden folgende Relationen aus der Ursprungsfrage gefunden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
' ${body} ' at ${createdAt} &{starting at ${start} and ending at ${end}}& &{with a confidence of ${score}}&
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
and the following relations were found withing the origin question
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
am ${createdAt} beginnend bei ${start} und endend an Position ${end}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
und dabei wurden folgende Entitäten aus der Ursprungsfrage gefunden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
at ${createdAt} starting from position ${start} and ending at position ${end}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
and each annotation from type AnnotationOfSpotInstance found the following entities from the origin question
15 changes: 15 additions & 0 deletions target/classes/queries/annotations_sparql_query.rq
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX oa: <http://www.w3.org/ns/openannotation/core/>
PREFIX qa: <http://www.wdaqua.eu/qa#>
SELECT *
FROM ?graphURI
WHERE {
?annotationId rdf:type ?type.
?annotationId oa:hasBody ?body.
?annotationId oa:hasTarget ?target.
?annotationId oa:annotatedBy $createdBy .
?annotationId oa:annotatedAt $createdAt .
OPTIONAL {
?annotationId qa:score ?score .
}
}
8 changes: 8 additions & 0 deletions target/classes/queries/components_sparql_query.rq
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX oa: <http://www.w3.org/ns/openannotation/core/>
PREFIX qa: <http://www.wdaqua.eu/qa#>
SELECT DISTINCT ?component
FROM ?graphURI
WHERE {
?s oa:annotatedBy ?component .
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
PREFIX dbr: <http://dbpedia.org/resource/>
PREFIX oa: <http://www.w3.org/ns/openannotation/core/>
PREFIX qa: <http://www.wdaqua.eu/qa#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

SELECT *
FROM ?graphURI
WHERE {
?annotationId
a qa:AnnotationOfAnswerJson ;
oa:hasTarget ?question ;
oa:hasBody ?answer ;
oa:annotatedBy ?componentURI ;
oa:annotatedAt ?createdAt .
OPTIONAL {
?annotationId qa:score ?score .
}
?answer
a qa:AnswerJson ;
rdf:value ?answerJson .
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
PREFIX dbr: <http://dbpedia.org/resource/>
PREFIX oa: <http://www.w3.org/ns/openannotation/core/>
PREFIX qa: <http://www.wdaqua.eu/qa#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

SELECT *
FROM ?graphURI
WHERE {
?newAnnotation rdf:type qa:AnnotationOfAnswerSPARQL .
?newAnnotation oa:hasTarget ?targetQuestion .
?newAnnotation oa:hasBody ?body .
OPTIONAL {
?newAnnotation qa:score ?score .
}
?newAnnotation oa:annotatedAt ?createdAt .
?newAnnotation oa:annotatedBy ?componentURI .
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX oa: <http://www.w3.org/ns/openannotation/core/>
PREFIX qa: <http://www.wdaqua.eu/qa#>

SELECT *
FROM ?graphURI
WHERE {
?annotationId rdf:type qa:AnnotationOfInstance .
?annotationId oa:hasTarget [
a oa:SpecificResource;
oa:hasSource ?source;
oa:hasSelector [
a oa:TextPositionSelector;
oa:start ?start;
oa:end ?end;
]
].
?annotationId oa:hasBody ?body ;
oa:annotatedBy ?componentURI ;
oa:annotatedAt ?createdAt .
OPTIONAL {
?annotationId qa:score ?score .
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
PREFIX qa: <http://www.wdaqua.eu/qa#>
PREFIX oa: <http://www.w3.org/ns/openannotation/core/>
PREFIX questionID: <http://195.90.200.248:8090/question/stored-question__text_>

SELECT *
FROM ?graphURI
WHERE {
?annotationId a qa:AnnotationOfQuestionLanguage .
?annotationId oa:hasTarget ?hasTarget ;
oa:hasBody ?body ;
oa:annotatedBy ?createdBy ;
oa:annotatedAt ?createdAt .
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
PREFIX qa: <http://www.wdaqua.eu/qa#>
PREFIX oa: <http://www.w3.org/ns/openannotation/core/>

SELECT *
FROM ?graphURI
WHERE {
?annotationId a qa:AnnotationOfRelation .
?annotationId oa:hasTarget [
a oa:SpecificResource;
oa:hasSource ?hasSource
] .
OPTIONAL {
?annotationId oa:hasTarget [
oa:hasSelector [
a oa:TextPositionSelector ;
oa:start ?start ;
oa:end ?end
]
] .
}
OPTIONAL {
?annotationId qa:score ?score .
}
?annotationId oa:hasBody ?body ;
oa:annotatedBy ?createdBy ;
oa:annotatedAt ?createdAt .
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX oa: <http://www.w3.org/ns/openannotation/core/>
PREFIX qa: <http://www.wdaqua.eu/qa#>

SELECT *
FROM ?graphURI
WHERE {
?annotationId rdf:type qa:AnnotationOfSpotInstance.
?annotationId oa:hasTarget [
a oa:SpecificResource;
oa:hasSource ?source;
oa:hasSelector [
a oa:TextPositionSelector;
oa:start ?start;
oa:end ?end;
]
].
?annotationId oa:annotatedAt ?createdAt ;
oa:annotatedBy ?componentURI .
}

Loading

0 comments on commit 9bf6e6c

Please sign in to comment.