From 4844b8d09aca3d9ab79af5d8a74e09355cdecf04 Mon Sep 17 00:00:00 2001 From: Paul Heinze Date: Sun, 25 Feb 2024 22:30:15 +0000 Subject: [PATCH] use query file from qa.commons (#340) Use a query template that is defined in the Qanary commons package, and remove the query template that was locally defined for this component. Change the binding variables to fit the new template. Add a unit test for setLanguageText() to test the creation of INSERT queries and updates to the triplestore. --- qanary-component-LD-Shuyo/pom.xml | 2 +- .../component/shuyo/ld/LanguageDetection.java | 42 ++++++++++--------- ...ert_one_annotation_of_question_language.rq | 17 -------- .../shuyo/ld/LanguageDetectionTest.java | 41 ++++++++++++++++-- 4 files changed, 60 insertions(+), 42 deletions(-) delete mode 100644 qanary-component-LD-Shuyo/src/main/resources/queries/insert_one_annotation_of_question_language.rq diff --git a/qanary-component-LD-Shuyo/pom.xml b/qanary-component-LD-Shuyo/pom.xml index 729e2e595..bc1097b82 100644 --- a/qanary-component-LD-Shuyo/pom.xml +++ b/qanary-component-LD-Shuyo/pom.xml @@ -5,7 +5,7 @@ 4.0.0 eu.wdaqua.qanary.component qanary-component-LD-Shuyo - 4.1.11 + 4.1.12 org.springframework.boot spring-boot-starter-parent diff --git a/qanary-component-LD-Shuyo/src/main/java/eu/wdaqua/qanary/component/shuyo/ld/LanguageDetection.java b/qanary-component-LD-Shuyo/src/main/java/eu/wdaqua/qanary/component/shuyo/ld/LanguageDetection.java index 8a7c7d7d9..440a02002 100644 --- a/qanary-component-LD-Shuyo/src/main/java/eu/wdaqua/qanary/component/shuyo/ld/LanguageDetection.java +++ b/qanary-component-LD-Shuyo/src/main/java/eu/wdaqua/qanary/component/shuyo/ld/LanguageDetection.java @@ -1,21 +1,5 @@ package eu.wdaqua.qanary.component.shuyo.ld; -import com.cybozu.labs.langdetect.Detector; -import com.cybozu.labs.langdetect.DetectorFactory; -import com.cybozu.labs.langdetect.LangDetectException; -import eu.wdaqua.qanary.commons.QanaryMessage; -import eu.wdaqua.qanary.commons.QanaryQuestion; -import eu.wdaqua.qanary.commons.QanaryUtils; -import eu.wdaqua.qanary.commons.triplestoreconnectors.QanaryTripleStoreConnector; -import eu.wdaqua.qanary.component.QanaryComponent; -import org.apache.commons.io.IOUtils; -import org.apache.jena.query.QuerySolutionMap; -import org.apache.jena.rdf.model.ResourceFactory; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Component; - import java.io.IOException; import java.io.InputStream; import java.net.JarURLConnection; @@ -27,6 +11,24 @@ import java.util.jar.JarEntry; import java.util.jar.JarFile; +import org.apache.commons.io.IOUtils; +import org.apache.jena.query.QuerySolutionMap; +import org.apache.jena.rdf.model.ResourceFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import com.cybozu.labs.langdetect.Detector; +import com.cybozu.labs.langdetect.DetectorFactory; +import com.cybozu.labs.langdetect.LangDetectException; + +import eu.wdaqua.qanary.commons.QanaryMessage; +import eu.wdaqua.qanary.commons.QanaryQuestion; +import eu.wdaqua.qanary.commons.QanaryUtils; +import eu.wdaqua.qanary.commons.triplestoreconnectors.QanaryTripleStoreConnector; +import eu.wdaqua.qanary.component.QanaryComponent; + /** * the component detects the languages of the current component * @@ -36,7 +38,7 @@ */ @Component public class LanguageDetection extends QanaryComponent { - private final String FILENAME_ANNOTATIONS_FILTERED = "/queries/insert_one_annotation_of_question_language.rq"; + private final String FILENAME_ANNOTATIONS_FILTERED = "/queries/insert_one_AnnotationOfQuestionLanguage.rq"; private static final Logger logger = LoggerFactory.getLogger(LanguageDetection.class); private static boolean languageProfileLoaded = false; @@ -150,9 +152,9 @@ public void setLanguageText(List languages, QanaryQuestion myQanaryQu QuerySolutionMap bindings = new QuerySolutionMap(); // use here the variable names defined in method insertAnnotationOfAnswerSPARQL bindings.add("graph", ResourceFactory.createResource(myQanaryQuestion.getOutGraph().toASCIIString())); - bindings.add("targetQuestion", ResourceFactory.createResource(myQanaryQuestion.getUri().toASCIIString())); - bindings.add("language", ResourceFactory.createStringLiteral(languages.get(i))); - bindings.add("application", ResourceFactory.createResource("urn:qanary:" + this.applicationName)); + bindings.add("hasTarget", ResourceFactory.createResource(myQanaryQuestion.getUri().toASCIIString())); + bindings.add("hasBody", ResourceFactory.createStringLiteral(languages.get(i))); + bindings.add("annotatedBy", ResourceFactory.createResource("urn:qanary:" + this.applicationName)); // get the template of the INSERT query String sparql = this.loadQueryFromFile(FILENAME_ANNOTATIONS_FILTERED, bindings); diff --git a/qanary-component-LD-Shuyo/src/main/resources/queries/insert_one_annotation_of_question_language.rq b/qanary-component-LD-Shuyo/src/main/resources/queries/insert_one_annotation_of_question_language.rq deleted file mode 100644 index 838c6766b..000000000 --- a/qanary-component-LD-Shuyo/src/main/resources/queries/insert_one_annotation_of_question_language.rq +++ /dev/null @@ -1,17 +0,0 @@ -PREFIX qa: -PREFIX oa: - -INSERT { - GRAPH ?graph { - ?a a qa:AnnotationOfQuestionLanguage . - ?a oa:hasBody ?language . - ?a oa:hasTarget ?targetQuestion ; - oa:annotatedBy ?application ; - oa:annotatedAt ?time - } -} - -WHERE { - BIND (IRI(str(RAND())) AS ?a) . - BIND (now() as ?time) . -} \ No newline at end of file diff --git a/qanary-component-LD-Shuyo/src/test/java/eu/wdaqua/qanary/component/shuyo/ld/LanguageDetectionTest.java b/qanary-component-LD-Shuyo/src/test/java/eu/wdaqua/qanary/component/shuyo/ld/LanguageDetectionTest.java index 78524aec1..1714cf97d 100644 --- a/qanary-component-LD-Shuyo/src/test/java/eu/wdaqua/qanary/component/shuyo/ld/LanguageDetectionTest.java +++ b/qanary-component-LD-Shuyo/src/test/java/eu/wdaqua/qanary/component/shuyo/ld/LanguageDetectionTest.java @@ -1,15 +1,25 @@ package eu.wdaqua.qanary.component.shuyo.ld; -import com.cybozu.labs.langdetect.LangDetectException; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.IOException; +import java.net.URI; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.IOException; -import java.util.ArrayList; +import com.cybozu.labs.langdetect.LangDetectException; + +import eu.wdaqua.qanary.commons.QanaryQuestion; +import eu.wdaqua.qanary.commons.QanaryUtils; +import eu.wdaqua.qanary.commons.triplestoreconnectors.QanaryTripleStoreConnector; -import static org.junit.jupiter.api.Assertions.assertTrue; public class LanguageDetectionTest { @@ -201,4 +211,27 @@ private Boolean checkDetectedLanguageForQuestion(String textualQuestion, String } } + @Test + void testSetLanguageText() throws Exception { + QanaryQuestion mockedQanaryQuestion = Mockito.mock(QanaryQuestion.class); + QanaryUtils mockedQanaryUtils = Mockito.mock(QanaryUtils.class); + QanaryTripleStoreConnector mockedQanaryTriplestoreConnector = Mockito.mock(QanaryTripleStoreConnector.class); + String questionUri = "urn:test:question"; + String graphUri = "urn:test:graph"; + + // not explicitly stubbing QanaryTriplestoreConnector.update(), as it is void + Mockito.when(mockedQanaryUtils.getQanaryTripleStoreConnector()).thenReturn(mockedQanaryTriplestoreConnector); + Mockito.when(mockedQanaryQuestion.getUri()).thenReturn(new URI(questionUri)); + Mockito.when(mockedQanaryQuestion.getOutGraph()).thenReturn(new URI(graphUri)); + + // given a list of identified languages + List languages = new ArrayList<>(Arrays.asList("en", "de")); + + // when setLanguageText() is called + myLanguageDetection.setLanguageText(languages, mockedQanaryQuestion, mockedQanaryUtils); + + // then a query string is created and the update() method is called for each language + Mockito.verify(mockedQanaryTriplestoreConnector, Mockito.times(languages.size())) + .update(Mockito.anyString()); + } }