From 177e4a9989e2ab909925055cf284340dc35d04ba Mon Sep 17 00:00:00 2001 From: jorge Date: Fri, 1 Mar 2024 17:30:07 +0100 Subject: [PATCH 01/23] Created the question generator project --- .../QuestionGenerator/.vscode/settings.json | 7 +++++++ wikidataAPI/QuestionGenerator/README.md | 18 ++++++++++++++++++ .../bin/QuestionGenerator.class | Bin 0 -> 276 bytes .../src/QuestionGenerator.java | 5 +++++ 4 files changed, 30 insertions(+) create mode 100644 wikidataAPI/QuestionGenerator/.vscode/settings.json create mode 100644 wikidataAPI/QuestionGenerator/README.md create mode 100644 wikidataAPI/QuestionGenerator/bin/QuestionGenerator.class create mode 100644 wikidataAPI/QuestionGenerator/src/QuestionGenerator.java diff --git a/wikidataAPI/QuestionGenerator/.vscode/settings.json b/wikidataAPI/QuestionGenerator/.vscode/settings.json new file mode 100644 index 00000000..e112a702 --- /dev/null +++ b/wikidataAPI/QuestionGenerator/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "java.project.sourcePaths": ["src"], + "java.project.outputPath": "bin", + "java.project.referencedLibraries": [ + "lib/**/*.jar" + ] +} diff --git a/wikidataAPI/QuestionGenerator/README.md b/wikidataAPI/QuestionGenerator/README.md new file mode 100644 index 00000000..7c03a532 --- /dev/null +++ b/wikidataAPI/QuestionGenerator/README.md @@ -0,0 +1,18 @@ +## Getting Started + +Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code. + +## Folder Structure + +The workspace contains two folders by default, where: + +- `src`: the folder to maintain sources +- `lib`: the folder to maintain dependencies + +Meanwhile, the compiled output files will be generated in the `bin` folder by default. + +> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there. + +## Dependency Management + +The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies). diff --git a/wikidataAPI/QuestionGenerator/bin/QuestionGenerator.class b/wikidataAPI/QuestionGenerator/bin/QuestionGenerator.class new file mode 100644 index 0000000000000000000000000000000000000000..1e074fb156493cf19a284ef11a7f283a216a3d93 GIT binary patch literal 276 zcmZWk!A`|(7!CJro#2wPn=D)Z%ae%*KY))S%r@S* zyf^R7Oy11l`1=Ro3Bwc-;pVmBz1P;fuwkdXbtz)PaI3ylUMsW7SLK!~Pv|~sqy00X zb9cWc#B=+>89KP^BSDuiD74{avn$!XtFmUoZDA`_ua(n5|C_P@)O*6HI18Q;vNzkf ziWfSpkIyEi;VMiZH^>VRrjRX|ippCJBo9Bh_!c8VCK%dbv?tha>qPjf^{xdY3{K!` BI_v-d literal 0 HcmV?d00001 diff --git a/wikidataAPI/QuestionGenerator/src/QuestionGenerator.java b/wikidataAPI/QuestionGenerator/src/QuestionGenerator.java new file mode 100644 index 00000000..3a23a7a2 --- /dev/null +++ b/wikidataAPI/QuestionGenerator/src/QuestionGenerator.java @@ -0,0 +1,5 @@ +public class QuestionGenerator { + // public static void main(String[] args) throws Exception { + // System.out.println("Hello, World!"); + // } +} From dca5a1bf2a568fafe140dc225c38b398cb745086 Mon Sep 17 00:00:00 2001 From: jorge Date: Sat, 2 Mar 2024 11:36:27 +0100 Subject: [PATCH 02/23] Created and set up the generator, with some basic examples --- .vscode/launch.json | 7 ++ backend/wiq/pom.xml | 42 +++++++++++ .../wiq/services/questionGenerator/Main.java | 18 +++++ .../questionGenerator/QuestionGenerator.java | 37 +++++++++ .../generator/AbstractGenerator.java | 71 ++++++++++++++++++ .../generator/CapitalGenerator.java | 48 ++++++++++++ .../generator/PopulationGenerator.java | 31 ++++++++ .../questionGenerator/question/Question.java | 50 ++++++++++++ .../question/QuestionType.java | 7 ++ .../QuestionGenerator/.vscode/settings.json | 7 -- wikidataAPI/QuestionGenerator/README.md | 18 ----- .../bin/QuestionGenerator.class | Bin 276 -> 0 bytes .../src/QuestionGenerator.java | 5 -- 13 files changed, 311 insertions(+), 30 deletions(-) create mode 100644 backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/Main.java create mode 100644 backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/QuestionGenerator.java create mode 100644 backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java create mode 100644 backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/CapitalGenerator.java create mode 100644 backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/PopulationGenerator.java create mode 100644 backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/question/Question.java create mode 100644 backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/question/QuestionType.java delete mode 100644 wikidataAPI/QuestionGenerator/.vscode/settings.json delete mode 100644 wikidataAPI/QuestionGenerator/README.md delete mode 100644 wikidataAPI/QuestionGenerator/bin/QuestionGenerator.class delete mode 100644 wikidataAPI/QuestionGenerator/src/QuestionGenerator.java diff --git a/.vscode/launch.json b/.vscode/launch.json index e2a80236..4c2d1974 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -1,5 +1,12 @@ { "configurations": [ + { + "type": "java", + "name": "Main", + "request": "launch", + "mainClass": "com.wiq.wiq.services.questionGenerator.Main", + "projectName": "wiq" + }, { "type": "java", "name": "Spring Boot-WiqApplication", diff --git a/backend/wiq/pom.xml b/backend/wiq/pom.xml index b64bb95d..d141062a 100644 --- a/backend/wiq/pom.xml +++ b/backend/wiq/pom.xml @@ -15,6 +15,8 @@ Demo project for Spring Boot 17 + 1.7.10 + 0.11.0 @@ -32,6 +34,38 @@ javax.servlet jstl + + + org.wikidata.wdtk + wdtk-datamodel + ${wikidataToolkitVersion} + + + org.wikidata.wdtk + wdtk-dumpfiles + ${wikidataToolkitVersion} + + + org.wikidata.wdtk + wdtk-rdf + ${wikidataToolkitVersion} + + + org.wikidata.wdtk + wdtk-wikibaseapi + ${wikidataToolkitVersion} + + + org.slf4j + slf4j-log4j12 + ${slf4jVersion} + + + + org.json + json + 20230618 + @@ -40,6 +74,14 @@ org.springframework.boot spring-boot-maven-plugin + + + maven-compiler-plugin + 3.8.1 + + 17 + + diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/Main.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/Main.java new file mode 100644 index 00000000..ca54c962 --- /dev/null +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/Main.java @@ -0,0 +1,18 @@ +package com.wiq.wiq.services.questionGenerator; + +import java.io.IOException; + +import com.wiq.wiq.services.questionGenerator.question.QuestionType; + +public class Main { + + public static void main(String[] args) throws IOException { + QuestionGenerator qg = new QuestionGenerator(); + String json = ""; + json = qg.generateQuestion(QuestionType.POPULATION); + // json = qg.generateQuestion(QuestionType.CAPITAL); + System.out.println(json); + + } + +} diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/QuestionGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/QuestionGenerator.java new file mode 100644 index 00000000..fab81bb1 --- /dev/null +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/QuestionGenerator.java @@ -0,0 +1,37 @@ +package com.wiq.wiq.services.questionGenerator; + +import com.wiq.wiq.services.questionGenerator.generator.AbstractGenerator; +import com.wiq.wiq.services.questionGenerator.generator.CapitalGenerator; +import com.wiq.wiq.services.questionGenerator.generator.PopulationGenerator; +import com.wiq.wiq.services.questionGenerator.question.QuestionType; + + + +public class QuestionGenerator { + + private AbstractGenerator generator; + private String id; + + public String generateQuestion(QuestionType type) { + generatorFactory(type); + String answer = generator.generate(id).getJSON().toString(); + return answer; + } + + private void generatorFactory(QuestionType type) { + switch (type) { + case POPULATION: { + id = "Q14317"; + generator = new PopulationGenerator(); + break; + } + case CAPITAL: { + id = "Q3934"; + generator = new CapitalGenerator(); + break; + + } + } + } + +} diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java new file mode 100644 index 00000000..b5f2e09b --- /dev/null +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java @@ -0,0 +1,71 @@ +package com.wiq.wiq.services.questionGenerator.generator; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import org.wikidata.wdtk.datamodel.implementation.ItemDocumentImpl; +import org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue; +import org.wikidata.wdtk.datamodel.interfaces.Statement; +import org.wikidata.wdtk.wikibaseapi.WikibaseDataFetcher; +import org.wikidata.wdtk.wikibaseapi.apierrors.MediaWikiApiErrorException; + +import com.wiq.wiq.services.questionGenerator.question.Question; + +public abstract class AbstractGenerator { + + protected static final WikibaseDataFetcher wbdf = WikibaseDataFetcher.getWikidataDataFetcher(); + private static final String LANGUAGE = "en"; + + + /** + * + * @param id + * @return question generated or null if wikidata gives an error + */ + public Question generate(String id) { + //get the wikidata entity using the id + ItemDocumentImpl idi = null; + try { + idi = (ItemDocumentImpl) wbdf.getEntityDocument(id); + } catch (MediaWikiApiErrorException | IOException e) { + /* + * * @throws MediaWikiApiErrorException + * if the API returns an error + * @throws IOException + * if we encounter network issues or HTTP 500 errors from Wikibase + */ + return null; + } + String name = getName(idi.getLabels()); + + //get the question + String question = getQuestion(name); + + //get the right answer + String rightAnswer = getRightAnswer(idi.getJsonClaims()); + + //get the wrong answers +// List answers = getWrongAnswers(rightAnswer); + List answers = new ArrayList<>(); + answers.add("a"); + answers.add("b"); + answers.add("c"); + + answers.add(0, rightAnswer); + //create and return the question + + return new Question(question, answers); + } + + protected String getName(Map names) { + MonolingualTextValue mtv = names.get(LANGUAGE); + return mtv.getText(); + } + + protected abstract String getQuestion(String name); + protected abstract String getRightAnswer(Map> claims); + protected abstract List getWrongAnswers(String rightAnswer); + +} diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/CapitalGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/CapitalGenerator.java new file mode 100644 index 00000000..8d1b8420 --- /dev/null +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/CapitalGenerator.java @@ -0,0 +1,48 @@ +package com.wiq.wiq.services.questionGenerator.generator; + +import java.io.IOException; +import java.util.List; +import java.util.Map; + +import org.wikidata.wdtk.datamodel.implementation.ItemDocumentImpl; +import org.wikidata.wdtk.datamodel.interfaces.Statement; +import org.wikidata.wdtk.datamodel.interfaces.Value; +import org.wikidata.wdtk.wikibaseapi.apierrors.MediaWikiApiErrorException; + +public class CapitalGenerator extends AbstractGenerator { + + private final static String TEMPLATE = "What's the capital of %s?"; + private final static String PROPERTY = "P36"; + + @Override + protected String getQuestion(String name) { + return String.format(TEMPLATE, name); + } + + @Override + protected String getRightAnswer(Map> claims) { + List a =claims.get(PROPERTY); + Value v = a.get(0).getValue(); + String ent = getRightAnswerEntity(v.toString()); + String answer = ""; + try { + answer = getName(((ItemDocumentImpl) wbdf.getEntityDocument(ent)).getLabels()); + } catch (MediaWikiApiErrorException | IOException e) { + return null; + } + return answer; + } + + @Override + protected List getWrongAnswers(String rightAnswer) { + // TODO Auto-generated method stub + return null; + } + + private String getRightAnswerEntity(String url) { + String[] split1 = url.split(" "); + String[] split2 = split1[0].split("/"); + return split2[split2.length-1]; + } + +} diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/PopulationGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/PopulationGenerator.java new file mode 100644 index 00000000..17264797 --- /dev/null +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/PopulationGenerator.java @@ -0,0 +1,31 @@ +package com.wiq.wiq.services.questionGenerator.generator; + +import java.util.List; +import java.util.Map; + +import org.wikidata.wdtk.datamodel.interfaces.Statement; +import org.wikidata.wdtk.datamodel.interfaces.Value; + +public class PopulationGenerator extends AbstractGenerator { + + private final static String TEMPLATE = "What's the population of %s?"; + private final static String PROPERTY = "P1082"; + + @Override + protected String getQuestion(String name) { + return String.format(TEMPLATE, name); + } + + @Override + protected String getRightAnswer(Map> claims) { + Value v = claims.get(PROPERTY).get(0).getValue(); + return v.toString(); + } + + @Override + protected List getWrongAnswers(String rightAnswer) { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/question/Question.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/question/Question.java new file mode 100644 index 00000000..422dec56 --- /dev/null +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/question/Question.java @@ -0,0 +1,50 @@ +package com.wiq.wiq.services.questionGenerator.question; + +import java.util.ArrayList; +import java.util.List; + +import org.json.JSONObject; + +public class Question { + + private String question; + private List answers; + + public Question(String question, List answers){ + this.question = question; + this.answers = new ArrayList<>(answers); + } + + public Question() { + + } + + public String getQuestion() { + return question; + } + + public void setQuestion(String question) { + this.question = question; + } + + public List getAnswers() { + return new ArrayList<>(answers); + } + + public void setAnswers(List answers) { + this.answers = new ArrayList<>(answers); + } + + public void addRightAnswer(String answer) { + answers.add(0, answer); + } + + public JSONObject getJSON() { + JSONObject json = new JSONObject(); + json.accumulate("question", question); + for(String s : answers) + json.accumulate("answers", s); + return json; + } + +} diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/question/QuestionType.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/question/QuestionType.java new file mode 100644 index 00000000..2983a9f8 --- /dev/null +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/question/QuestionType.java @@ -0,0 +1,7 @@ +package com.wiq.wiq.services.questionGenerator.question; + +public enum QuestionType { + + POPULATION, CAPITAL; + +} diff --git a/wikidataAPI/QuestionGenerator/.vscode/settings.json b/wikidataAPI/QuestionGenerator/.vscode/settings.json deleted file mode 100644 index e112a702..00000000 --- a/wikidataAPI/QuestionGenerator/.vscode/settings.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "java.project.sourcePaths": ["src"], - "java.project.outputPath": "bin", - "java.project.referencedLibraries": [ - "lib/**/*.jar" - ] -} diff --git a/wikidataAPI/QuestionGenerator/README.md b/wikidataAPI/QuestionGenerator/README.md deleted file mode 100644 index 7c03a532..00000000 --- a/wikidataAPI/QuestionGenerator/README.md +++ /dev/null @@ -1,18 +0,0 @@ -## Getting Started - -Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code. - -## Folder Structure - -The workspace contains two folders by default, where: - -- `src`: the folder to maintain sources -- `lib`: the folder to maintain dependencies - -Meanwhile, the compiled output files will be generated in the `bin` folder by default. - -> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there. - -## Dependency Management - -The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies). diff --git a/wikidataAPI/QuestionGenerator/bin/QuestionGenerator.class b/wikidataAPI/QuestionGenerator/bin/QuestionGenerator.class deleted file mode 100644 index 1e074fb156493cf19a284ef11a7f283a216a3d93..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 276 zcmZWk!A`|(7!CJro#2wPn=D)Z%ae%*KY))S%r@S* zyf^R7Oy11l`1=Ro3Bwc-;pVmBz1P;fuwkdXbtz)PaI3ylUMsW7SLK!~Pv|~sqy00X zb9cWc#B=+>89KP^BSDuiD74{avn$!XtFmUoZDA`_ua(n5|C_P@)O*6HI18Q;vNzkf ziWfSpkIyEi;VMiZH^>VRrjRX|ippCJBo9Bh_!c8VCK%dbv?tha>qPjf^{xdY3{K!` BI_v-d diff --git a/wikidataAPI/QuestionGenerator/src/QuestionGenerator.java b/wikidataAPI/QuestionGenerator/src/QuestionGenerator.java deleted file mode 100644 index 3a23a7a2..00000000 --- a/wikidataAPI/QuestionGenerator/src/QuestionGenerator.java +++ /dev/null @@ -1,5 +0,0 @@ -public class QuestionGenerator { - // public static void main(String[] args) throws Exception { - // System.out.println("Hello, World!"); - // } -} From 3520d5f3fd1dfc52c2d39178bca38726a9a3c8a6 Mon Sep 17 00:00:00 2001 From: jorge Date: Sat, 2 Mar 2024 12:11:23 +0100 Subject: [PATCH 03/23] Make it so there are three examples per question type --- .../wiq/services/questionGenerator/Main.java | 19 +++++++++++++++---- .../questionGenerator/QuestionGenerator.java | 17 +++++++++++++++-- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/Main.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/Main.java index ca54c962..ee2b85f1 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/Main.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/Main.java @@ -8,11 +8,22 @@ public class Main { public static void main(String[] args) throws IOException { QuestionGenerator qg = new QuestionGenerator(); - String json = ""; - json = qg.generateQuestion(QuestionType.POPULATION); - // json = qg.generateQuestion(QuestionType.CAPITAL); - System.out.println(json); +// String json = ""; +//// json = qg.generateQuestion(QuestionType.POPULATION); +// json = qg.generateQuestion(QuestionType.CAPITAL); +// System.out.println(json); + run(qg); } + + private static void run(QuestionGenerator qg) { + for(int i=0; i<3; i++) { + System.out.println(qg.generateQuestion(QuestionType.POPULATION)); + } + System.out.println(); + for(int i=0; i<3; i++) { + System.out.println(qg.generateQuestion(QuestionType.CAPITAL)); + } + } } diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/QuestionGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/QuestionGenerator.java index fab81bb1..c020a093 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/QuestionGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/QuestionGenerator.java @@ -12,6 +12,9 @@ public class QuestionGenerator { private AbstractGenerator generator; private String id; + private static String[] POP_ENTITIES = {"Q14317", "Q12273", "Q14649"}; + private static String[] CAP_ENTITIES = {"Q3934", "Q29", "Q43"}; + public String generateQuestion(QuestionType type) { generatorFactory(type); String answer = generator.generate(id).getJSON().toString(); @@ -21,17 +24,27 @@ public String generateQuestion(QuestionType type) { private void generatorFactory(QuestionType type) { switch (type) { case POPULATION: { - id = "Q14317"; + id = POP_ENTITIES[0]; generator = new PopulationGenerator(); + moveUp(POP_ENTITIES); + POP_ENTITIES[POP_ENTITIES.length-1] = id; break; } case CAPITAL: { - id = "Q3934"; + id = CAP_ENTITIES[0]; generator = new CapitalGenerator(); + moveUp(CAP_ENTITIES); + CAP_ENTITIES[CAP_ENTITIES.length-1] = id; break; } } } + + private void moveUp(String[] list) { + for(int i=0; i Date: Sat, 2 Mar 2024 13:11:15 +0100 Subject: [PATCH 04/23] Added two extra cases: size and language --- .../wiq/services/questionGenerator/Main.java | 21 ++++++--- .../questionGenerator/QuestionGenerator.java | 18 +++++++ .../generator/LanguageGenerator.java | 47 +++++++++++++++++++ .../generator/SizeGenerator.java | 38 +++++++++++++++ .../question/QuestionType.java | 2 +- 5 files changed, 118 insertions(+), 8 deletions(-) create mode 100644 backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/LanguageGenerator.java create mode 100644 backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/SizeGenerator.java diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/Main.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/Main.java index ee2b85f1..7139c4e5 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/Main.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/Main.java @@ -1,28 +1,35 @@ package com.wiq.wiq.services.questionGenerator; -import java.io.IOException; - import com.wiq.wiq.services.questionGenerator.question.QuestionType; public class Main { - public static void main(String[] args) throws IOException { + private static QuestionType[] types = {QuestionType.POPULATION, QuestionType.CAPITAL, QuestionType.SIZE, + QuestionType.LANGUAGE}; + + public static void main(String[] args) { QuestionGenerator qg = new QuestionGenerator(); // String json = ""; //// json = qg.generateQuestion(QuestionType.POPULATION); // json = qg.generateQuestion(QuestionType.CAPITAL); // System.out.println(json); +// run(qg); + +// System.out.println(qg.generateQuestion(QuestionType.LANGUAGE)); run(qg); } private static void run(QuestionGenerator qg) { - for(int i=0; i<3; i++) { - System.out.println(qg.generateQuestion(QuestionType.POPULATION)); + for(QuestionType t : types) { + runTypes(qg, t); + System.out.println(); } - System.out.println(); + } + + private static void runTypes(QuestionGenerator qg, QuestionType type) { for(int i=0; i<3; i++) { - System.out.println(qg.generateQuestion(QuestionType.CAPITAL)); + System.out.println(qg.generateQuestion(type)); } } diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/QuestionGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/QuestionGenerator.java index c020a093..65285a3d 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/QuestionGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/QuestionGenerator.java @@ -2,7 +2,9 @@ import com.wiq.wiq.services.questionGenerator.generator.AbstractGenerator; import com.wiq.wiq.services.questionGenerator.generator.CapitalGenerator; +import com.wiq.wiq.services.questionGenerator.generator.LanguageGenerator; import com.wiq.wiq.services.questionGenerator.generator.PopulationGenerator; +import com.wiq.wiq.services.questionGenerator.generator.SizeGenerator; import com.wiq.wiq.services.questionGenerator.question.QuestionType; @@ -14,6 +16,8 @@ public class QuestionGenerator { private static String[] POP_ENTITIES = {"Q14317", "Q12273", "Q14649"}; private static String[] CAP_ENTITIES = {"Q3934", "Q29", "Q43"}; + private static String[] SIZE_ENTITIES = {"Q29", "Q12273", "Q3934"}; + private static String[] LANG_ENTITIES = {"Q29", "Q43", "Q3934"}; public String generateQuestion(QuestionType type) { generatorFactory(type); @@ -38,6 +42,20 @@ private void generatorFactory(QuestionType type) { break; } + case SIZE: { + id = SIZE_ENTITIES[0]; + generator = new SizeGenerator(); + moveUp(SIZE_ENTITIES); + SIZE_ENTITIES[SIZE_ENTITIES.length-1] = id; + break; + } + case LANGUAGE: { + id = LANG_ENTITIES[0]; + generator = new LanguageGenerator(); + moveUp(LANG_ENTITIES); + LANG_ENTITIES[LANG_ENTITIES.length-1] = id; + break; + } } } diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/LanguageGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/LanguageGenerator.java new file mode 100644 index 00000000..d6a34f91 --- /dev/null +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/LanguageGenerator.java @@ -0,0 +1,47 @@ +package com.wiq.wiq.services.questionGenerator.generator; + +import java.io.IOException; +import java.util.List; +import java.util.Map; + +import org.wikidata.wdtk.datamodel.implementation.ItemDocumentImpl; +import org.wikidata.wdtk.datamodel.interfaces.Statement; +import org.wikidata.wdtk.datamodel.interfaces.Value; +import org.wikidata.wdtk.wikibaseapi.apierrors.MediaWikiApiErrorException; + +public class LanguageGenerator extends AbstractGenerator { + + private static final String TEMPLATE = "What's the official language of %s?"; + private final static String PROPERTY = "P37"; + + @Override + protected String getQuestion(String name) { + return String.format(TEMPLATE, name); + } + + @Override + protected String getRightAnswer(Map> claims) { + Value v = claims.get(PROPERTY).get(0).getValue(); + String entity = getRightAnswerEntity(v.toString()); + String answer = ""; + try { + answer = getName(((ItemDocumentImpl) wbdf.getEntityDocument(entity)).getLabels()); + } catch (MediaWikiApiErrorException | IOException e) { + return null; + } + return answer; + } + + @Override + protected List getWrongAnswers(String rightAnswer) { + // TODO Auto-generated method stub + return null; + } + + private String getRightAnswerEntity(String url) { + String[] split1 = url.split(" "); + String[] split2 = split1[0].split("/"); + return split2[split2.length-1]; + } + +} diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/SizeGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/SizeGenerator.java new file mode 100644 index 00000000..30ec2b8a --- /dev/null +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/SizeGenerator.java @@ -0,0 +1,38 @@ +package com.wiq.wiq.services.questionGenerator.generator; + +import java.util.List; +import java.util.Map; + +import org.wikidata.wdtk.datamodel.interfaces.Statement; +import org.wikidata.wdtk.datamodel.interfaces.Value; + +public class SizeGenerator extends AbstractGenerator { + + private static final String TEMPLATE = "What's the size of %s?"; + private final static String PROPERTY = "P2046"; + + @Override + protected String getQuestion(String name) { + return String.format(TEMPLATE, name); + } + + @Override + protected String getRightAnswer(Map> claims) { + List a = claims.get(PROPERTY); + Value v = a.get(0).getValue(); + return getRightAnswerEntity(v.toString()); + } + + @Override + protected List getWrongAnswers(String rightAnswer) { + // TODO Auto-generated method stub + return null; + } + + private String getRightAnswerEntity(String url) { + String[] split1 = url.split(" "); + String[] split2 = split1[0].split("/"); + return split2[split2.length-1]; + } + +} diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/question/QuestionType.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/question/QuestionType.java index 2983a9f8..2b148f71 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/question/QuestionType.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/question/QuestionType.java @@ -2,6 +2,6 @@ public enum QuestionType { - POPULATION, CAPITAL; + POPULATION, CAPITAL, SIZE, LANGUAGE; } From a6fcc525634f52f94c597531d9ea050e2a4d8508 Mon Sep 17 00:00:00 2001 From: jorge Date: Sat, 2 Mar 2024 13:12:22 +0100 Subject: [PATCH 05/23] =?UTF-8?q?Changed=20the=20main=20a=20bit=20so=20it?= =?UTF-8?q?=C2=A1s=20more=20concise?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wiq/services/questionGenerator/Main.java | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/Main.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/Main.java index 7139c4e5..dd66e21c 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/Main.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/Main.java @@ -9,25 +9,14 @@ public class Main { public static void main(String[] args) { QuestionGenerator qg = new QuestionGenerator(); -// String json = ""; -//// json = qg.generateQuestion(QuestionType.POPULATION); -// json = qg.generateQuestion(QuestionType.CAPITAL); -// System.out.println(json); -// run(qg); - -// System.out.println(qg.generateQuestion(QuestionType.LANGUAGE)); - run(qg); - - } - - private static void run(QuestionGenerator qg) { for(QuestionType t : types) { - runTypes(qg, t); + run(qg, t); System.out.println(); } + } - private static void runTypes(QuestionGenerator qg, QuestionType type) { + private static void run(QuestionGenerator qg, QuestionType type) { for(int i=0; i<3; i++) { System.out.println(qg.generateQuestion(type)); } From 3552a4da5aa470025f0110b04b1ec0dbf112f426 Mon Sep 17 00:00:00 2001 From: jorge Date: Sat, 2 Mar 2024 14:59:58 +0100 Subject: [PATCH 06/23] Refactored code to an abstract class and also some to a common one. --- .../generator/AbstractGenerator.java | 23 +++++++++++- .../generator/CapitalGenerator.java | 37 +++---------------- .../generator/LanguageGenerator.java | 36 +++--------------- .../generator/PopulationGenerator.java | 7 ++-- .../generator/RightAnswerIsEntity.java | 33 +++++++++++++++++ .../generator/SizeGenerator.java | 10 ++--- 6 files changed, 72 insertions(+), 74 deletions(-) create mode 100644 backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/RightAnswerIsEntity.java diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java index b5f2e09b..9036dce8 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java @@ -18,6 +18,8 @@ public abstract class AbstractGenerator { protected static final WikibaseDataFetcher wbdf = WikibaseDataFetcher.getWikidataDataFetcher(); private static final String LANGUAGE = "en"; + private String propertyId = ""; + private String template = ""; /** * @@ -64,8 +66,27 @@ protected String getName(Map names) { return mtv.getText(); } - protected abstract String getQuestion(String name); + protected String getQuestion(String name) { + return String.format(template, name); + } + protected abstract String getRightAnswer(Map> claims); protected abstract List getWrongAnswers(String rightAnswer); + public String getPropertyId() { + return propertyId; + } + + public void setPropertyId(String propertyId) { + this.propertyId = propertyId; + } + + public String getTemplate() { + return template; + } + + public void setTemplate(String template) { + this.template = template; + } + } diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/CapitalGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/CapitalGenerator.java index 8d1b8420..106f5a09 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/CapitalGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/CapitalGenerator.java @@ -1,36 +1,15 @@ package com.wiq.wiq.services.questionGenerator.generator; -import java.io.IOException; import java.util.List; -import java.util.Map; -import org.wikidata.wdtk.datamodel.implementation.ItemDocumentImpl; -import org.wikidata.wdtk.datamodel.interfaces.Statement; -import org.wikidata.wdtk.datamodel.interfaces.Value; -import org.wikidata.wdtk.wikibaseapi.apierrors.MediaWikiApiErrorException; - -public class CapitalGenerator extends AbstractGenerator { +public class CapitalGenerator extends RightAnswerIsEntity { private final static String TEMPLATE = "What's the capital of %s?"; private final static String PROPERTY = "P36"; - - @Override - protected String getQuestion(String name) { - return String.format(TEMPLATE, name); - } - - @Override - protected String getRightAnswer(Map> claims) { - List a =claims.get(PROPERTY); - Value v = a.get(0).getValue(); - String ent = getRightAnswerEntity(v.toString()); - String answer = ""; - try { - answer = getName(((ItemDocumentImpl) wbdf.getEntityDocument(ent)).getLabels()); - } catch (MediaWikiApiErrorException | IOException e) { - return null; - } - return answer; + + public CapitalGenerator() { + super.setPropertyId(PROPERTY); + super.setTemplate(TEMPLATE); } @Override @@ -38,11 +17,5 @@ protected List getWrongAnswers(String rightAnswer) { // TODO Auto-generated method stub return null; } - - private String getRightAnswerEntity(String url) { - String[] split1 = url.split(" "); - String[] split2 = split1[0].split("/"); - return split2[split2.length-1]; - } } diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/LanguageGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/LanguageGenerator.java index d6a34f91..fa474dfa 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/LanguageGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/LanguageGenerator.java @@ -1,35 +1,15 @@ package com.wiq.wiq.services.questionGenerator.generator; -import java.io.IOException; import java.util.List; -import java.util.Map; -import org.wikidata.wdtk.datamodel.implementation.ItemDocumentImpl; -import org.wikidata.wdtk.datamodel.interfaces.Statement; -import org.wikidata.wdtk.datamodel.interfaces.Value; -import org.wikidata.wdtk.wikibaseapi.apierrors.MediaWikiApiErrorException; - -public class LanguageGenerator extends AbstractGenerator { +public class LanguageGenerator extends RightAnswerIsEntity { private static final String TEMPLATE = "What's the official language of %s?"; private final static String PROPERTY = "P37"; - - @Override - protected String getQuestion(String name) { - return String.format(TEMPLATE, name); - } - - @Override - protected String getRightAnswer(Map> claims) { - Value v = claims.get(PROPERTY).get(0).getValue(); - String entity = getRightAnswerEntity(v.toString()); - String answer = ""; - try { - answer = getName(((ItemDocumentImpl) wbdf.getEntityDocument(entity)).getLabels()); - } catch (MediaWikiApiErrorException | IOException e) { - return null; - } - return answer; + + public LanguageGenerator() { + super.setPropertyId(PROPERTY); + super.setTemplate(TEMPLATE); } @Override @@ -37,11 +17,5 @@ protected List getWrongAnswers(String rightAnswer) { // TODO Auto-generated method stub return null; } - - private String getRightAnswerEntity(String url) { - String[] split1 = url.split(" "); - String[] split2 = split1[0].split("/"); - return split2[split2.length-1]; - } } diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/PopulationGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/PopulationGenerator.java index 17264797..0caf0e08 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/PopulationGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/PopulationGenerator.java @@ -10,10 +10,9 @@ public class PopulationGenerator extends AbstractGenerator { private final static String TEMPLATE = "What's the population of %s?"; private final static String PROPERTY = "P1082"; - - @Override - protected String getQuestion(String name) { - return String.format(TEMPLATE, name); + + public PopulationGenerator() { + super.setTemplate(TEMPLATE); } @Override diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/RightAnswerIsEntity.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/RightAnswerIsEntity.java new file mode 100644 index 00000000..c059eb92 --- /dev/null +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/RightAnswerIsEntity.java @@ -0,0 +1,33 @@ +package com.wiq.wiq.services.questionGenerator.generator; + +import java.io.IOException; +import java.util.List; +import java.util.Map; + +import org.wikidata.wdtk.datamodel.implementation.ItemDocumentImpl; +import org.wikidata.wdtk.datamodel.interfaces.Statement; +import org.wikidata.wdtk.datamodel.interfaces.Value; +import org.wikidata.wdtk.wikibaseapi.apierrors.MediaWikiApiErrorException; + +public abstract class RightAnswerIsEntity extends AbstractGenerator { + + @Override + protected String getRightAnswer(Map> claims) { + Value v = claims.get(super.getPropertyId()).get(0).getValue(); + String entity = getRightAnswerEntity(v.toString()); + String answer = ""; + try { + answer = getName(((ItemDocumentImpl) wbdf.getEntityDocument(entity)).getLabels()); + } catch (MediaWikiApiErrorException | IOException e) { + return null; + } + return answer; + } + + private String getRightAnswerEntity(String url) { + String[] split1 = url.split(" "); + String[] split2 = split1[0].split("/"); + return split2[split2.length-1]; + } + +} diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/SizeGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/SizeGenerator.java index 30ec2b8a..55e326cd 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/SizeGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/SizeGenerator.java @@ -10,16 +10,14 @@ public class SizeGenerator extends AbstractGenerator { private static final String TEMPLATE = "What's the size of %s?"; private final static String PROPERTY = "P2046"; - - @Override - protected String getQuestion(String name) { - return String.format(TEMPLATE, name); + + public SizeGenerator() { + super.setTemplate(TEMPLATE); } @Override protected String getRightAnswer(Map> claims) { - List a = claims.get(PROPERTY); - Value v = a.get(0).getValue(); + Value v = claims.get(PROPERTY).get(0).getValue(); return getRightAnswerEntity(v.toString()); } From 59bfcd12ad5806eeafbb936db401f37eb1a124ed Mon Sep 17 00:00:00 2001 From: ErdemYabaci Date: Sat, 2 Mar 2024 18:10:33 +0100 Subject: [PATCH 07/23] Getting the wrong answers for population questions. --- .../generator/PopulationGenerator.java | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/PopulationGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/PopulationGenerator.java index 0caf0e08..dbef8c00 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/PopulationGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/PopulationGenerator.java @@ -2,6 +2,9 @@ import java.util.List; import java.util.Map; +import java.util.Math; +import java.util.Random; +import java.util.ArrayList; import org.wikidata.wdtk.datamodel.interfaces.Statement; import org.wikidata.wdtk.datamodel.interfaces.Value; @@ -23,8 +26,31 @@ protected String getRightAnswer(Map> claims) { @Override protected List getWrongAnswers(String rightAnswer) { - // TODO Auto-generated method stub - return null; + int number = 0; + // Check if it is a integer + if (rightAnswer.matches("\\d+")) { + // Convert the string to an integer + number = Integer.parseInt(rightAnswer); + } else { + //throw new NotAnInteger(); + } + + List wrongAnswers = new ArrayList(); + + Random rnd = new Random(); + + // Gives values depending on parameter with percentage + // Example: If parameter is 50 value range is number*.5 and number*1.5 + int parameter = 50; + for(int i = 0; i < 3; i++){ + int wrong = Math.round(number * (100 - parameter + rnd.nextInt(parameter * 2 + 1)) / 100); + // Checking if it creates the same answer + if(wrong == number) + i--; + else + wrongAnswers.add(String.valueOf(wrong)); + } + return wrongAnswers; } } From 8dc32ff81ed3475533954753ef045188cce10416 Mon Sep 17 00:00:00 2001 From: jorge Date: Sat, 2 Mar 2024 18:36:52 +0100 Subject: [PATCH 08/23] Updated with the changes and fixed small import problem --- .../questionGenerator/generator/PopulationGenerator.java | 1 - 1 file changed, 1 deletion(-) diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/PopulationGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/PopulationGenerator.java index dbef8c00..01aac329 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/PopulationGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/PopulationGenerator.java @@ -2,7 +2,6 @@ import java.util.List; import java.util.Map; -import java.util.Math; import java.util.Random; import java.util.ArrayList; From 293ad7ce6bc917f437b1909ef5130c0a085c7aac Mon Sep 17 00:00:00 2001 From: jorge Date: Sat, 2 Mar 2024 18:39:50 +0100 Subject: [PATCH 09/23] Improved package distribution --- .../wiq/services/questionGenerator/QuestionGenerator.java | 8 ++++---- .../{ => specificGenerators}/CapitalGenerator.java | 4 +++- .../{ => specificGenerators}/LanguageGenerator.java | 4 +++- .../{ => specificGenerators}/PopulationGenerator.java | 4 +++- .../generator/{ => specificGenerators}/SizeGenerator.java | 4 +++- 5 files changed, 16 insertions(+), 8 deletions(-) rename backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/{ => specificGenerators}/CapitalGenerator.java (73%) rename backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/{ => specificGenerators}/LanguageGenerator.java (74%) rename backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/{ => specificGenerators}/PopulationGenerator.java (90%) rename backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/{ => specificGenerators}/SizeGenerator.java (85%) diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/QuestionGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/QuestionGenerator.java index 65285a3d..4c2ac846 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/QuestionGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/QuestionGenerator.java @@ -1,10 +1,10 @@ package com.wiq.wiq.services.questionGenerator; import com.wiq.wiq.services.questionGenerator.generator.AbstractGenerator; -import com.wiq.wiq.services.questionGenerator.generator.CapitalGenerator; -import com.wiq.wiq.services.questionGenerator.generator.LanguageGenerator; -import com.wiq.wiq.services.questionGenerator.generator.PopulationGenerator; -import com.wiq.wiq.services.questionGenerator.generator.SizeGenerator; +import com.wiq.wiq.services.questionGenerator.generator.specificGenerators.CapitalGenerator; +import com.wiq.wiq.services.questionGenerator.generator.specificGenerators.LanguageGenerator; +import com.wiq.wiq.services.questionGenerator.generator.specificGenerators.PopulationGenerator; +import com.wiq.wiq.services.questionGenerator.generator.specificGenerators.SizeGenerator; import com.wiq.wiq.services.questionGenerator.question.QuestionType; diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/CapitalGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/CapitalGenerator.java similarity index 73% rename from backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/CapitalGenerator.java rename to backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/CapitalGenerator.java index 106f5a09..8fff0916 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/CapitalGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/CapitalGenerator.java @@ -1,7 +1,9 @@ -package com.wiq.wiq.services.questionGenerator.generator; +package com.wiq.wiq.services.questionGenerator.generator.specificGenerators; import java.util.List; +import com.wiq.wiq.services.questionGenerator.generator.RightAnswerIsEntity; + public class CapitalGenerator extends RightAnswerIsEntity { private final static String TEMPLATE = "What's the capital of %s?"; diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/LanguageGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/LanguageGenerator.java similarity index 74% rename from backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/LanguageGenerator.java rename to backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/LanguageGenerator.java index fa474dfa..d9817ba8 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/LanguageGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/LanguageGenerator.java @@ -1,7 +1,9 @@ -package com.wiq.wiq.services.questionGenerator.generator; +package com.wiq.wiq.services.questionGenerator.generator.specificGenerators; import java.util.List; +import com.wiq.wiq.services.questionGenerator.generator.RightAnswerIsEntity; + public class LanguageGenerator extends RightAnswerIsEntity { private static final String TEMPLATE = "What's the official language of %s?"; diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/PopulationGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/PopulationGenerator.java similarity index 90% rename from backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/PopulationGenerator.java rename to backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/PopulationGenerator.java index 01aac329..279fdb1e 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/PopulationGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/PopulationGenerator.java @@ -1,4 +1,4 @@ -package com.wiq.wiq.services.questionGenerator.generator; +package com.wiq.wiq.services.questionGenerator.generator.specificGenerators; import java.util.List; import java.util.Map; @@ -8,6 +8,8 @@ import org.wikidata.wdtk.datamodel.interfaces.Statement; import org.wikidata.wdtk.datamodel.interfaces.Value; +import com.wiq.wiq.services.questionGenerator.generator.AbstractGenerator; + public class PopulationGenerator extends AbstractGenerator { private final static String TEMPLATE = "What's the population of %s?"; diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/SizeGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/SizeGenerator.java similarity index 85% rename from backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/SizeGenerator.java rename to backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/SizeGenerator.java index 55e326cd..5d3b6348 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/SizeGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/SizeGenerator.java @@ -1,4 +1,4 @@ -package com.wiq.wiq.services.questionGenerator.generator; +package com.wiq.wiq.services.questionGenerator.generator.specificGenerators; import java.util.List; import java.util.Map; @@ -6,6 +6,8 @@ import org.wikidata.wdtk.datamodel.interfaces.Statement; import org.wikidata.wdtk.datamodel.interfaces.Value; +import com.wiq.wiq.services.questionGenerator.generator.AbstractGenerator; + public class SizeGenerator extends AbstractGenerator { private static final String TEMPLATE = "What's the size of %s?"; From 7c0a4c37948afa4c3176de9823e674fbacdae512 Mon Sep 17 00:00:00 2001 From: jorge Date: Sat, 2 Mar 2024 18:40:26 +0100 Subject: [PATCH 10/23] Added a way to store already searched entites --- .../generator/AbstractGenerator.java | 31 ++++++++++++------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java index 9036dce8..7dab35cf 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java @@ -2,6 +2,7 @@ import java.io.IOException; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -17,6 +18,8 @@ public abstract class AbstractGenerator { protected static final WikibaseDataFetcher wbdf = WikibaseDataFetcher.getWikidataDataFetcher(); private static final String LANGUAGE = "en"; + + private static Map alreadyProcessedEntities = new HashMap<>(); private String propertyId = ""; private String template = ""; @@ -28,18 +31,24 @@ public abstract class AbstractGenerator { */ public Question generate(String id) { //get the wikidata entity using the id - ItemDocumentImpl idi = null; - try { - idi = (ItemDocumentImpl) wbdf.getEntityDocument(id); - } catch (MediaWikiApiErrorException | IOException e) { - /* - * * @throws MediaWikiApiErrorException - * if the API returns an error - * @throws IOException - * if we encounter network issues or HTTP 500 errors from Wikibase - */ - return null; + ItemDocumentImpl idi = alreadyProcessedEntities.get(id); + + if(idi==null) { + try { + idi = (ItemDocumentImpl) wbdf.getEntityDocument(id); + alreadyProcessedEntities.put(id, idi); + } catch (MediaWikiApiErrorException | IOException e) { + /* + * * @throws MediaWikiApiErrorException + * if the API returns an error + * @throws IOException + * if we encounter network issues or HTTP 500 errors from Wikibase + */ + return null; + } } + + String name = getName(idi.getLabels()); //get the question From 7f72711fdb0863a122555fbe6965dae33673b9dc Mon Sep 17 00:00:00 2001 From: jorge Date: Sat, 2 Mar 2024 18:46:54 +0100 Subject: [PATCH 11/23] The properties taht are also entities now are also stored to avoid searching them more than once --- .../questionGenerator/generator/AbstractGenerator.java | 8 ++++++++ .../questionGenerator/generator/RightAnswerIsEntity.java | 9 ++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java index 7dab35cf..09dd5c0f 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java @@ -98,4 +98,12 @@ public void setTemplate(String template) { this.template = template; } + public static Map getAlreadyProcessedEntities() { + return new HashMap<>(alreadyProcessedEntities); + } + + public static void addItem(String entity, ItemDocumentImpl item) { + alreadyProcessedEntities.put(entity, item); + } + } diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/RightAnswerIsEntity.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/RightAnswerIsEntity.java index c059eb92..b5bc7daa 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/RightAnswerIsEntity.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/RightAnswerIsEntity.java @@ -17,7 +17,14 @@ protected String getRightAnswer(Map> claims) { String entity = getRightAnswerEntity(v.toString()); String answer = ""; try { - answer = getName(((ItemDocumentImpl) wbdf.getEntityDocument(entity)).getLabels()); + ItemDocumentImpl idi = getAlreadyProcessedEntities().get(entity); + if(idi==null) { + idi = (ItemDocumentImpl) wbdf.getEntityDocument(entity); + answer = getName(idi.getLabels()); + addItem(entity, idi); + } + else + answer = getName(idi.getLabels()); } catch (MediaWikiApiErrorException | IOException e) { return null; } From b467de09ce9b75d32b674333996c7916211d6224 Mon Sep 17 00:00:00 2001 From: jorge Date: Sun, 3 Mar 2024 21:23:37 +0100 Subject: [PATCH 12/23] Modified the classes so that they pass the template and property id to the parent by the constructor instead of by setters --- .../questionGenerator/generator/AbstractGenerator.java | 5 +++++ .../questionGenerator/generator/RightAnswerIsEntity.java | 4 ++++ .../generator/specificGenerators/CapitalGenerator.java | 3 +-- .../generator/specificGenerators/LanguageGenerator.java | 4 ++-- .../generator/specificGenerators/PopulationGenerator.java | 2 +- .../generator/specificGenerators/SizeGenerator.java | 2 +- 6 files changed, 14 insertions(+), 6 deletions(-) diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java index 09dd5c0f..fbf26546 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java @@ -23,6 +23,11 @@ public abstract class AbstractGenerator { private String propertyId = ""; private String template = ""; + + public AbstractGenerator(String propertyId, String template) { + this.propertyId = propertyId; + this.template = template; + } /** * diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/RightAnswerIsEntity.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/RightAnswerIsEntity.java index b5bc7daa..ee315451 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/RightAnswerIsEntity.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/RightAnswerIsEntity.java @@ -11,6 +11,10 @@ public abstract class RightAnswerIsEntity extends AbstractGenerator { + public RightAnswerIsEntity(String propertyId, String template) { + super(propertyId, template); + } + @Override protected String getRightAnswer(Map> claims) { Value v = claims.get(super.getPropertyId()).get(0).getValue(); diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/CapitalGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/CapitalGenerator.java index 8fff0916..6f22605c 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/CapitalGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/CapitalGenerator.java @@ -10,8 +10,7 @@ public class CapitalGenerator extends RightAnswerIsEntity { private final static String PROPERTY = "P36"; public CapitalGenerator() { - super.setPropertyId(PROPERTY); - super.setTemplate(TEMPLATE); + super(PROPERTY, TEMPLATE); } @Override diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/LanguageGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/LanguageGenerator.java index d9817ba8..8c2dc628 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/LanguageGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/LanguageGenerator.java @@ -10,10 +10,10 @@ public class LanguageGenerator extends RightAnswerIsEntity { private final static String PROPERTY = "P37"; public LanguageGenerator() { - super.setPropertyId(PROPERTY); - super.setTemplate(TEMPLATE); + super(PROPERTY, TEMPLATE); } + @Override protected List getWrongAnswers(String rightAnswer) { // TODO Auto-generated method stub diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/PopulationGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/PopulationGenerator.java index 279fdb1e..2e50b1b5 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/PopulationGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/PopulationGenerator.java @@ -16,7 +16,7 @@ public class PopulationGenerator extends AbstractGenerator { private final static String PROPERTY = "P1082"; public PopulationGenerator() { - super.setTemplate(TEMPLATE); + super(PROPERTY, TEMPLATE); } @Override diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/SizeGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/SizeGenerator.java index 5d3b6348..12305a70 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/SizeGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/SizeGenerator.java @@ -14,7 +14,7 @@ public class SizeGenerator extends AbstractGenerator { private final static String PROPERTY = "P2046"; public SizeGenerator() { - super.setTemplate(TEMPLATE); + super(PROPERTY, TEMPLATE); } @Override From 4c99d7d4188352f03f8d6ffceff861731c524608 Mon Sep 17 00:00:00 2001 From: jorge Date: Sun, 3 Mar 2024 21:27:36 +0100 Subject: [PATCH 13/23] Removed the sample data now that there's one question with incorrect answers --- .../questionGenerator/generator/AbstractGenerator.java | 7 +------ .../generator/specificGenerators/CapitalGenerator.java | 3 ++- .../generator/specificGenerators/LanguageGenerator.java | 3 ++- .../generator/specificGenerators/SizeGenerator.java | 3 ++- 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java index fbf26546..b51d5d37 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java @@ -1,7 +1,6 @@ package com.wiq.wiq.services.questionGenerator.generator; import java.io.IOException; -import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -63,11 +62,7 @@ public Question generate(String id) { String rightAnswer = getRightAnswer(idi.getJsonClaims()); //get the wrong answers -// List answers = getWrongAnswers(rightAnswer); - List answers = new ArrayList<>(); - answers.add("a"); - answers.add("b"); - answers.add("c"); + List answers = getWrongAnswers(rightAnswer); answers.add(0, rightAnswer); //create and return the question diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/CapitalGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/CapitalGenerator.java index 6f22605c..ac618cc7 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/CapitalGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/CapitalGenerator.java @@ -1,5 +1,6 @@ package com.wiq.wiq.services.questionGenerator.generator.specificGenerators; +import java.util.ArrayList; import java.util.List; import com.wiq.wiq.services.questionGenerator.generator.RightAnswerIsEntity; @@ -16,7 +17,7 @@ public CapitalGenerator() { @Override protected List getWrongAnswers(String rightAnswer) { // TODO Auto-generated method stub - return null; + return new ArrayList(); } } diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/LanguageGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/LanguageGenerator.java index 8c2dc628..0dfe5e0a 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/LanguageGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/LanguageGenerator.java @@ -1,5 +1,6 @@ package com.wiq.wiq.services.questionGenerator.generator.specificGenerators; +import java.util.ArrayList; import java.util.List; import com.wiq.wiq.services.questionGenerator.generator.RightAnswerIsEntity; @@ -17,7 +18,7 @@ public LanguageGenerator() { @Override protected List getWrongAnswers(String rightAnswer) { // TODO Auto-generated method stub - return null; + return new ArrayList(); } } diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/SizeGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/SizeGenerator.java index 12305a70..e24e6f78 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/SizeGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/SizeGenerator.java @@ -1,5 +1,6 @@ package com.wiq.wiq.services.questionGenerator.generator.specificGenerators; +import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -26,7 +27,7 @@ protected String getRightAnswer(Map> claims) { @Override protected List getWrongAnswers(String rightAnswer) { // TODO Auto-generated method stub - return null; + return new ArrayList(); } private String getRightAnswerEntity(String url) { From 232e34394e91acb932bd62df33d61509eb5cbe50 Mon Sep 17 00:00:00 2001 From: UO289845 Date: Mon, 4 Mar 2024 11:22:32 +0100 Subject: [PATCH 14/23] Added examples of wrong answers to all questions --- .../generator/specificGenerators/CapitalGenerator.java | 6 +++++- .../generator/specificGenerators/LanguageGenerator.java | 6 +++++- .../generator/specificGenerators/SizeGenerator.java | 6 +++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/CapitalGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/CapitalGenerator.java index ac618cc7..ce7b16f8 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/CapitalGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/CapitalGenerator.java @@ -17,7 +17,11 @@ public CapitalGenerator() { @Override protected List getWrongAnswers(String rightAnswer) { // TODO Auto-generated method stub - return new ArrayList(); + List result = new ArrayList<>(); + result.add("a"); + result.add("b"); + result.add("c"); + return result; } } diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/LanguageGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/LanguageGenerator.java index 0dfe5e0a..6e0b155c 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/LanguageGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/LanguageGenerator.java @@ -18,7 +18,11 @@ public LanguageGenerator() { @Override protected List getWrongAnswers(String rightAnswer) { // TODO Auto-generated method stub - return new ArrayList(); + List result = new ArrayList<>(); + result.add("a"); + result.add("b"); + result.add("c"); + return result; } } diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/SizeGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/SizeGenerator.java index e24e6f78..38553b1d 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/SizeGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/SizeGenerator.java @@ -27,7 +27,11 @@ protected String getRightAnswer(Map> claims) { @Override protected List getWrongAnswers(String rightAnswer) { // TODO Auto-generated method stub - return new ArrayList(); + List result = new ArrayList<>(); + result.add("a"); + result.add("b"); + result.add("c"); + return result; } private String getRightAnswerEntity(String url) { From a0f404482f49486bc7d6d4a84e22cf9b4691b044 Mon Sep 17 00:00:00 2001 From: jorge Date: Mon, 4 Mar 2024 14:15:15 +0100 Subject: [PATCH 15/23] Updated the condition of the wrong answers --- .../specificGenerators/PopulationGenerator.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/PopulationGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/PopulationGenerator.java index 2e50b1b5..a86cb642 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/PopulationGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/PopulationGenerator.java @@ -29,12 +29,11 @@ protected String getRightAnswer(Map> claims) { protected List getWrongAnswers(String rightAnswer) { int number = 0; // Check if it is a integer - if (rightAnswer.matches("\\d+")) { - // Convert the string to an integer - number = Integer.parseInt(rightAnswer); - } else { - //throw new NotAnInteger(); - } + try { + number = Integer.parseInt(rightAnswer); + } catch(NumberFormatException e) { + //throw exception or maybe return null + } List wrongAnswers = new ArrayList(); From 1474946e07d69e1656a9793964e5f149de37a8b7 Mon Sep 17 00:00:00 2001 From: jorge Date: Tue, 5 Mar 2024 16:31:08 +0100 Subject: [PATCH 16/23] Added internaztionalization support for english and spanish --- .../generator/AbstractGenerator.java | 36 +++++++++---------- .../generator/RightAnswerIsEntity.java | 4 +-- .../generator/rsc/messages.properties | 4 +++ .../generator/rsc/messages_en.properties | 4 +++ .../generator/rsc/messages_es.properties | 4 +++ .../specificGenerators/CapitalGenerator.java | 10 ++++-- .../specificGenerators/LanguageGenerator.java | 9 +++-- .../PopulationGenerator.java | 10 ++++-- .../specificGenerators/SizeGenerator.java | 10 ++++-- 9 files changed, 62 insertions(+), 29 deletions(-) create mode 100644 backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/rsc/messages.properties create mode 100644 backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/rsc/messages_en.properties create mode 100644 backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/rsc/messages_es.properties diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java index b51d5d37..a2b052dc 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java @@ -3,7 +3,9 @@ import java.io.IOException; import java.util.HashMap; import java.util.List; +import java.util.Locale; import java.util.Map; +import java.util.ResourceBundle; import org.wikidata.wdtk.datamodel.implementation.ItemDocumentImpl; import org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue; @@ -18,14 +20,17 @@ public abstract class AbstractGenerator { protected static final WikibaseDataFetcher wbdf = WikibaseDataFetcher.getWikidataDataFetcher(); private static final String LANGUAGE = "en"; + private Locale localization = Locale.getDefault(); + private ResourceBundle messages; + private static Map alreadyProcessedEntities = new HashMap<>(); private String propertyId = ""; - private String template = ""; - public AbstractGenerator(String propertyId, String template) { + public AbstractGenerator(String propertyId) { this.propertyId = propertyId; - this.template = template; + localization = Locale.ENGLISH; + this.messages = ResourceBundle.getBundle("rsc/messages", localization); } /** @@ -75,10 +80,7 @@ protected String getName(Map names) { return mtv.getText(); } - protected String getQuestion(String name) { - return String.format(template, name); - } - + protected abstract String getQuestion(String name); protected abstract String getRightAnswer(Map> claims); protected abstract List getWrongAnswers(String rightAnswer); @@ -86,18 +88,6 @@ public String getPropertyId() { return propertyId; } - public void setPropertyId(String propertyId) { - this.propertyId = propertyId; - } - - public String getTemplate() { - return template; - } - - public void setTemplate(String template) { - this.template = template; - } - public static Map getAlreadyProcessedEntities() { return new HashMap<>(alreadyProcessedEntities); } @@ -106,4 +96,12 @@ public static void addItem(String entity, ItemDocumentImpl item) { alreadyProcessedEntities.put(entity, item); } + public static String getLanguage() { + return LANGUAGE; + } + + public ResourceBundle getMessages() { + return messages; + } + } diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/RightAnswerIsEntity.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/RightAnswerIsEntity.java index ee315451..b69067fb 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/RightAnswerIsEntity.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/RightAnswerIsEntity.java @@ -11,8 +11,8 @@ public abstract class RightAnswerIsEntity extends AbstractGenerator { - public RightAnswerIsEntity(String propertyId, String template) { - super(propertyId, template); + public RightAnswerIsEntity(String propertyId) { + super(propertyId); } @Override diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/rsc/messages.properties b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/rsc/messages.properties new file mode 100644 index 00000000..780a92bd --- /dev/null +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/rsc/messages.properties @@ -0,0 +1,4 @@ +question.population = What's the population of %s? +question.language = What's the official language of %s? +question.capital = What's the capital of %s? +question.size = What's the size of %s? \ No newline at end of file diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/rsc/messages_en.properties b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/rsc/messages_en.properties new file mode 100644 index 00000000..780a92bd --- /dev/null +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/rsc/messages_en.properties @@ -0,0 +1,4 @@ +question.population = What's the population of %s? +question.language = What's the official language of %s? +question.capital = What's the capital of %s? +question.size = What's the size of %s? \ No newline at end of file diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/rsc/messages_es.properties b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/rsc/messages_es.properties new file mode 100644 index 00000000..839ae10d --- /dev/null +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/rsc/messages_es.properties @@ -0,0 +1,4 @@ +question.population = ¿Cuál es la población de %s? +question.language = ¿Cuál es el idioma oficial de %s? +question.capital = ¿Cuál es la capital de %s? +question.size = ¿Cuál es el área (km cuadrados) de %s? \ No newline at end of file diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/CapitalGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/CapitalGenerator.java index ce7b16f8..f79dd0fd 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/CapitalGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/CapitalGenerator.java @@ -7,11 +7,17 @@ public class CapitalGenerator extends RightAnswerIsEntity { - private final static String TEMPLATE = "What's the capital of %s?"; + // private final static String TEMPLATE = "What's the capital of %s?"; private final static String PROPERTY = "P36"; public CapitalGenerator() { - super(PROPERTY, TEMPLATE); + super(PROPERTY); + } + + @Override + protected String getQuestion(String name) { + String q = getMessages().getString("question.capital"); + return String.format(q, name); } @Override diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/LanguageGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/LanguageGenerator.java index 6e0b155c..9c6efd40 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/LanguageGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/LanguageGenerator.java @@ -7,13 +7,18 @@ public class LanguageGenerator extends RightAnswerIsEntity { - private static final String TEMPLATE = "What's the official language of %s?"; + // private static final String TEMPLATE = "What's the official language of %s?"; private final static String PROPERTY = "P37"; public LanguageGenerator() { - super(PROPERTY, TEMPLATE); + super(PROPERTY); } + @Override + protected String getQuestion(String name) { + String q = getMessages().getString("question.language"); + return String.format(q, name); + } @Override protected List getWrongAnswers(String rightAnswer) { diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/PopulationGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/PopulationGenerator.java index a86cb642..7cb2e6d3 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/PopulationGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/PopulationGenerator.java @@ -12,11 +12,17 @@ public class PopulationGenerator extends AbstractGenerator { - private final static String TEMPLATE = "What's the population of %s?"; + // private final static String TEMPLATE = "What's the population of %s?"; private final static String PROPERTY = "P1082"; public PopulationGenerator() { - super(PROPERTY, TEMPLATE); + super(PROPERTY); + } + + @Override + protected String getQuestion(String name) { + String q = getMessages().getString("question.population"); + return String.format(q, name); } @Override diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/SizeGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/SizeGenerator.java index 38553b1d..b3c08dee 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/SizeGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/SizeGenerator.java @@ -11,11 +11,17 @@ public class SizeGenerator extends AbstractGenerator { - private static final String TEMPLATE = "What's the size of %s?"; + // private static final String TEMPLATE = "What's the size of %s?"; private final static String PROPERTY = "P2046"; public SizeGenerator() { - super(PROPERTY, TEMPLATE); + super(PROPERTY); + } + + @Override + protected String getQuestion(String name) { + String q = getMessages().getString("question.size"); + return String.format(q, name); } @Override From 7bbfee85407d51986980c2766e309060237119c4 Mon Sep 17 00:00:00 2001 From: jorge Date: Tue, 5 Mar 2024 18:13:26 +0100 Subject: [PATCH 17/23] Turned the specific generators into singletons --- .../services/questionGenerator/QuestionGenerator.java | 8 ++++---- .../specificGenerators/CapitalGenerator.java | 10 +++++++++- .../specificGenerators/LanguageGenerator.java | 10 +++++++++- .../specificGenerators/PopulationGenerator.java | 10 +++++++++- .../generator/specificGenerators/SizeGenerator.java | 11 ++++++++++- 5 files changed, 41 insertions(+), 8 deletions(-) diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/QuestionGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/QuestionGenerator.java index 4c2ac846..88e191f6 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/QuestionGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/QuestionGenerator.java @@ -29,14 +29,14 @@ private void generatorFactory(QuestionType type) { switch (type) { case POPULATION: { id = POP_ENTITIES[0]; - generator = new PopulationGenerator(); + generator = PopulationGenerator.getInstance(); moveUp(POP_ENTITIES); POP_ENTITIES[POP_ENTITIES.length-1] = id; break; } case CAPITAL: { id = CAP_ENTITIES[0]; - generator = new CapitalGenerator(); + generator = CapitalGenerator.getInstance(); moveUp(CAP_ENTITIES); CAP_ENTITIES[CAP_ENTITIES.length-1] = id; break; @@ -44,14 +44,14 @@ private void generatorFactory(QuestionType type) { } case SIZE: { id = SIZE_ENTITIES[0]; - generator = new SizeGenerator(); + generator = SizeGenerator.getInstance(); moveUp(SIZE_ENTITIES); SIZE_ENTITIES[SIZE_ENTITIES.length-1] = id; break; } case LANGUAGE: { id = LANG_ENTITIES[0]; - generator = new LanguageGenerator(); + generator = LanguageGenerator.getInstance(); moveUp(LANG_ENTITIES); LANG_ENTITIES[LANG_ENTITIES.length-1] = id; break; diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/CapitalGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/CapitalGenerator.java index f79dd0fd..44b64846 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/CapitalGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/CapitalGenerator.java @@ -10,7 +10,15 @@ public class CapitalGenerator extends RightAnswerIsEntity { // private final static String TEMPLATE = "What's the capital of %s?"; private final static String PROPERTY = "P36"; - public CapitalGenerator() { + private static CapitalGenerator capitalGenerator = null; + + public static CapitalGenerator getInstance() { + if(capitalGenerator == null) + capitalGenerator = new CapitalGenerator(); + return capitalGenerator; + } + + private CapitalGenerator() { super(PROPERTY); } diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/LanguageGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/LanguageGenerator.java index 9c6efd40..c7f74367 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/LanguageGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/LanguageGenerator.java @@ -10,7 +10,15 @@ public class LanguageGenerator extends RightAnswerIsEntity { // private static final String TEMPLATE = "What's the official language of %s?"; private final static String PROPERTY = "P37"; - public LanguageGenerator() { + private static LanguageGenerator languageGenerator = null; + + public static LanguageGenerator getInstance() { + if(languageGenerator == null) + languageGenerator = new LanguageGenerator(); + return languageGenerator; + } + + private LanguageGenerator() { super(PROPERTY); } diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/PopulationGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/PopulationGenerator.java index 7cb2e6d3..e7fb2067 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/PopulationGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/PopulationGenerator.java @@ -15,7 +15,15 @@ public class PopulationGenerator extends AbstractGenerator { // private final static String TEMPLATE = "What's the population of %s?"; private final static String PROPERTY = "P1082"; - public PopulationGenerator() { + private static PopulationGenerator populationGenerator = null; + + public static PopulationGenerator getInstance() { + if(populationGenerator == null) + populationGenerator = new PopulationGenerator(); + return populationGenerator; + } + + private PopulationGenerator() { super(PROPERTY); } diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/SizeGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/SizeGenerator.java index b3c08dee..6ec4c1e7 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/SizeGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/SizeGenerator.java @@ -14,7 +14,16 @@ public class SizeGenerator extends AbstractGenerator { // private static final String TEMPLATE = "What's the size of %s?"; private final static String PROPERTY = "P2046"; - public SizeGenerator() { + private static SizeGenerator sizeGenerator = null; + + public static SizeGenerator getInstance() { + if(sizeGenerator==null) + sizeGenerator = new SizeGenerator(); + return sizeGenerator; + + } + + private SizeGenerator() { super(PROPERTY); } From aec32189f8b4caa9939d6a9682681a37be425730 Mon Sep 17 00:00:00 2001 From: jorge Date: Tue, 5 Mar 2024 18:21:14 +0100 Subject: [PATCH 18/23] Fixed an issue with the path to the messages The path was wrong and it gave an error whenever it try access the messages files --- .../questionGenerator/generator/AbstractGenerator.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java index a2b052dc..50be4ae4 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java @@ -30,7 +30,8 @@ public abstract class AbstractGenerator { public AbstractGenerator(String propertyId) { this.propertyId = propertyId; localization = Locale.ENGLISH; - this.messages = ResourceBundle.getBundle("rsc/messages", localization); + this.messages = ResourceBundle.getBundle("com/wiq/wiq/services/questionGenerator/"+ + "generator/rsc/messages", localization); } /** From 29b573e19ffa3e757e00e4f8c0aad30ee70b2dfa Mon Sep 17 00:00:00 2001 From: jorge Date: Tue, 5 Mar 2024 19:32:35 +0100 Subject: [PATCH 19/23] Allowed for changing dinamically the language --- .../wiq/services/questionGenerator/Main.java | 2 +- .../questionGenerator/QuestionGenerator.java | 6 +++ .../generator/AbstractGenerator.java | 43 +++++++++++++++---- 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/Main.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/Main.java index dd66e21c..6b6db3bf 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/Main.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/Main.java @@ -8,7 +8,7 @@ public class Main { QuestionType.LANGUAGE}; public static void main(String[] args) { - QuestionGenerator qg = new QuestionGenerator(); + QuestionGenerator qg = new QuestionGenerator("en"); for(QuestionType t : types) { run(qg, t); System.out.println(); diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/QuestionGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/QuestionGenerator.java index 88e191f6..da1f5ba5 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/QuestionGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/QuestionGenerator.java @@ -13,14 +13,20 @@ public class QuestionGenerator { private AbstractGenerator generator; private String id; + private String languageCode; private static String[] POP_ENTITIES = {"Q14317", "Q12273", "Q14649"}; private static String[] CAP_ENTITIES = {"Q3934", "Q29", "Q43"}; private static String[] SIZE_ENTITIES = {"Q29", "Q12273", "Q3934"}; private static String[] LANG_ENTITIES = {"Q29", "Q43", "Q3934"}; + + public QuestionGenerator(String languageCode){ + this.languageCode = languageCode; + } public String generateQuestion(QuestionType type) { generatorFactory(type); + generator.setLocalization(languageCode); String answer = generator.generate(id).getJSON().toString(); return answer; } diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java index 50be4ae4..dca0041f 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java @@ -18,7 +18,7 @@ public abstract class AbstractGenerator { protected static final WikibaseDataFetcher wbdf = WikibaseDataFetcher.getWikidataDataFetcher(); - private static final String LANGUAGE = "en"; + private String language = "en"; private Locale localization = Locale.getDefault(); private ResourceBundle messages; @@ -27,11 +27,14 @@ public abstract class AbstractGenerator { private String propertyId = ""; + private static final String MESSAGES_PATH = "com/wiq/wiq/services/questionGenerator/"+ + "generator/rsc/messages"; + public AbstractGenerator(String propertyId) { this.propertyId = propertyId; - localization = Locale.ENGLISH; - this.messages = ResourceBundle.getBundle("com/wiq/wiq/services/questionGenerator/"+ - "generator/rsc/messages", localization); + // localization = Locale.ENGLISH; + // this.messages = ResourceBundle.getBundle("com/wiq/wiq/services/questionGenerator/"+ + // "generator/rsc/messages", localization); } /** @@ -77,7 +80,7 @@ public Question generate(String id) { } protected String getName(Map names) { - MonolingualTextValue mtv = names.get(LANGUAGE); + MonolingualTextValue mtv = names.get(language); return mtv.getText(); } @@ -97,12 +100,36 @@ public static void addItem(String entity, ItemDocumentImpl item) { alreadyProcessedEntities.put(entity, item); } - public static String getLanguage() { - return LANGUAGE; - } + // public String getLanguage() { + // return language; + // } public ResourceBundle getMessages() { return messages; } + public void setLocalization(String languageCode) { + languageCode = languageCode.toLowerCase(); + switch (languageCode) { + case "en":{ + this.language = "en"; + this.localization = Locale.ENGLISH; + this.messages = ResourceBundle.getBundle(MESSAGES_PATH, localization); + break; + } + case "es":{ + this.language = "es"; + this.localization = new Locale("es"); + this.messages = ResourceBundle.getBundle(MESSAGES_PATH, localization); + break; + } + default:{ + this.language = "en"; + this.localization = Locale.ENGLISH; + this.messages = ResourceBundle.getBundle(MESSAGES_PATH, localization); + break; + } + } + } + } From 842018276969860a0a486cec651f120f4f38206a Mon Sep 17 00:00:00 2001 From: jorge Date: Tue, 5 Mar 2024 21:13:17 +0100 Subject: [PATCH 20/23] Removed the singleton functionality This is to simplify the way it works. The actual code has not been removed, but rather commented, so taht it can be easilly reimplemented (or simply deleted) after a discussion with the team. --- .../questionGenerator/QuestionGenerator.java | 8 ++++---- .../specificGenerators/CapitalGenerator.java | 19 ++++++++++++------- .../specificGenerators/LanguageGenerator.java | 19 ++++++++++++------- .../PopulationGenerator.java | 19 ++++++++++++------- .../specificGenerators/SizeGenerator.java | 19 ++++++++++++------- 5 files changed, 52 insertions(+), 32 deletions(-) diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/QuestionGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/QuestionGenerator.java index da1f5ba5..f5a9b011 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/QuestionGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/QuestionGenerator.java @@ -35,14 +35,14 @@ private void generatorFactory(QuestionType type) { switch (type) { case POPULATION: { id = POP_ENTITIES[0]; - generator = PopulationGenerator.getInstance(); + generator = new PopulationGenerator(); moveUp(POP_ENTITIES); POP_ENTITIES[POP_ENTITIES.length-1] = id; break; } case CAPITAL: { id = CAP_ENTITIES[0]; - generator = CapitalGenerator.getInstance(); + generator = new CapitalGenerator(); moveUp(CAP_ENTITIES); CAP_ENTITIES[CAP_ENTITIES.length-1] = id; break; @@ -50,14 +50,14 @@ private void generatorFactory(QuestionType type) { } case SIZE: { id = SIZE_ENTITIES[0]; - generator = SizeGenerator.getInstance(); + generator = new SizeGenerator(); moveUp(SIZE_ENTITIES); SIZE_ENTITIES[SIZE_ENTITIES.length-1] = id; break; } case LANGUAGE: { id = LANG_ENTITIES[0]; - generator = LanguageGenerator.getInstance(); + generator = new LanguageGenerator(); moveUp(LANG_ENTITIES); LANG_ENTITIES[LANG_ENTITIES.length-1] = id; break; diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/CapitalGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/CapitalGenerator.java index 44b64846..cedf90bb 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/CapitalGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/CapitalGenerator.java @@ -10,15 +10,20 @@ public class CapitalGenerator extends RightAnswerIsEntity { // private final static String TEMPLATE = "What's the capital of %s?"; private final static String PROPERTY = "P36"; - private static CapitalGenerator capitalGenerator = null; + //This is for using as a singleton + // private static CapitalGenerator capitalGenerator = null; - public static CapitalGenerator getInstance() { - if(capitalGenerator == null) - capitalGenerator = new CapitalGenerator(); - return capitalGenerator; - } + // public static CapitalGenerator getInstance() { + // if(capitalGenerator == null) + // capitalGenerator = new CapitalGenerator(); + // return capitalGenerator; + // } - private CapitalGenerator() { + // private CapitalGenerator() { + // super(PROPERTY); + // } + + public CapitalGenerator(){ super(PROPERTY); } diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/LanguageGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/LanguageGenerator.java index c7f74367..fdd70488 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/LanguageGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/LanguageGenerator.java @@ -10,15 +10,20 @@ public class LanguageGenerator extends RightAnswerIsEntity { // private static final String TEMPLATE = "What's the official language of %s?"; private final static String PROPERTY = "P37"; - private static LanguageGenerator languageGenerator = null; + //This is for using as a singleton + // private static LanguageGenerator languageGenerator = null; - public static LanguageGenerator getInstance() { - if(languageGenerator == null) - languageGenerator = new LanguageGenerator(); - return languageGenerator; - } + // public static LanguageGenerator getInstance() { + // if(languageGenerator == null) + // languageGenerator = new LanguageGenerator(); + // return languageGenerator; + // } - private LanguageGenerator() { + // private LanguageGenerator() { + // super(PROPERTY); + // } + + public LanguageGenerator(){ super(PROPERTY); } diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/PopulationGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/PopulationGenerator.java index e7fb2067..ed44788e 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/PopulationGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/PopulationGenerator.java @@ -15,15 +15,20 @@ public class PopulationGenerator extends AbstractGenerator { // private final static String TEMPLATE = "What's the population of %s?"; private final static String PROPERTY = "P1082"; - private static PopulationGenerator populationGenerator = null; + //This is for using as a singleton + // private static PopulationGenerator populationGenerator = null; - public static PopulationGenerator getInstance() { - if(populationGenerator == null) - populationGenerator = new PopulationGenerator(); - return populationGenerator; - } + // public static PopulationGenerator getInstance() { + // if(populationGenerator == null) + // populationGenerator = new PopulationGenerator(); + // return populationGenerator; + // } - private PopulationGenerator() { + // private PopulationGenerator() { + // super(PROPERTY); + // } + + public PopulationGenerator(){ super(PROPERTY); } diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/SizeGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/SizeGenerator.java index 6ec4c1e7..83f40dfe 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/SizeGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/SizeGenerator.java @@ -14,16 +14,21 @@ public class SizeGenerator extends AbstractGenerator { // private static final String TEMPLATE = "What's the size of %s?"; private final static String PROPERTY = "P2046"; - private static SizeGenerator sizeGenerator = null; + //This is for using as a singleton + // private static SizeGenerator sizeGenerator = null; - public static SizeGenerator getInstance() { - if(sizeGenerator==null) - sizeGenerator = new SizeGenerator(); - return sizeGenerator; + // public static SizeGenerator getInstance() { + // if(sizeGenerator==null) + // sizeGenerator = new SizeGenerator(); + // return sizeGenerator; - } + // } - private SizeGenerator() { + // private SizeGenerator() { + // super(PROPERTY); + // } + + public SizeGenerator(){ super(PROPERTY); } From e26e32d952f9565c5e88ebcaf845b0538840206e Mon Sep 17 00:00:00 2001 From: jorge Date: Tue, 5 Mar 2024 21:17:24 +0100 Subject: [PATCH 21/23] Improved the localization algorithm to avoid code repetition --- .../generator/AbstractGenerator.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java index dca0041f..d021cb0a 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java @@ -112,24 +112,24 @@ public void setLocalization(String languageCode) { languageCode = languageCode.toLowerCase(); switch (languageCode) { case "en":{ - this.language = "en"; - this.localization = Locale.ENGLISH; - this.messages = ResourceBundle.getBundle(MESSAGES_PATH, localization); + localize(languageCode); break; } case "es":{ - this.language = "es"; - this.localization = new Locale("es"); - this.messages = ResourceBundle.getBundle(MESSAGES_PATH, localization); + localize(languageCode); break; } default:{ - this.language = "en"; - this.localization = Locale.ENGLISH; - this.messages = ResourceBundle.getBundle(MESSAGES_PATH, localization); + localize("en"); break; } } } + + private void localize(String languageCode) { + this.language = languageCode; + this.localization = new Locale(languageCode); + this.messages = ResourceBundle.getBundle(MESSAGES_PATH, localization); + } } From 04d3a2d8e3d676b4cc71ae867f1b56985316ab6b Mon Sep 17 00:00:00 2001 From: jorge Date: Wed, 6 Mar 2024 16:05:37 +0100 Subject: [PATCH 22/23] Made the localization method more robust --- .../questionGenerator/generator/AbstractGenerator.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java index d021cb0a..2b380468 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java @@ -109,7 +109,10 @@ public ResourceBundle getMessages() { } public void setLocalization(String languageCode) { - languageCode = languageCode.toLowerCase(); + if(languageCode==null) + languageCode = "en"; + else + languageCode = languageCode.toLowerCase(); switch (languageCode) { case "en":{ localize(languageCode); From df147eab35211f4367d039a0a37c45b68cc98778 Mon Sep 17 00:00:00 2001 From: jorge Date: Wed, 6 Mar 2024 16:06:59 +0100 Subject: [PATCH 23/23] Removed commented code --- .../generator/AbstractGenerator.java | 7 ------- .../specificGenerators/CapitalGenerator.java | 14 -------------- .../specificGenerators/LanguageGenerator.java | 14 -------------- .../specificGenerators/PopulationGenerator.java | 14 -------------- .../specificGenerators/SizeGenerator.java | 15 --------------- 5 files changed, 64 deletions(-) diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java index 2b380468..1774027a 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/AbstractGenerator.java @@ -32,9 +32,6 @@ public abstract class AbstractGenerator { public AbstractGenerator(String propertyId) { this.propertyId = propertyId; - // localization = Locale.ENGLISH; - // this.messages = ResourceBundle.getBundle("com/wiq/wiq/services/questionGenerator/"+ - // "generator/rsc/messages", localization); } /** @@ -100,10 +97,6 @@ public static void addItem(String entity, ItemDocumentImpl item) { alreadyProcessedEntities.put(entity, item); } - // public String getLanguage() { - // return language; - // } - public ResourceBundle getMessages() { return messages; } diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/CapitalGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/CapitalGenerator.java index cedf90bb..1d39cafc 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/CapitalGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/CapitalGenerator.java @@ -7,21 +7,7 @@ public class CapitalGenerator extends RightAnswerIsEntity { - // private final static String TEMPLATE = "What's the capital of %s?"; private final static String PROPERTY = "P36"; - - //This is for using as a singleton - // private static CapitalGenerator capitalGenerator = null; - - // public static CapitalGenerator getInstance() { - // if(capitalGenerator == null) - // capitalGenerator = new CapitalGenerator(); - // return capitalGenerator; - // } - - // private CapitalGenerator() { - // super(PROPERTY); - // } public CapitalGenerator(){ super(PROPERTY); diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/LanguageGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/LanguageGenerator.java index fdd70488..70aa0732 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/LanguageGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/LanguageGenerator.java @@ -7,21 +7,7 @@ public class LanguageGenerator extends RightAnswerIsEntity { - // private static final String TEMPLATE = "What's the official language of %s?"; private final static String PROPERTY = "P37"; - - //This is for using as a singleton - // private static LanguageGenerator languageGenerator = null; - - // public static LanguageGenerator getInstance() { - // if(languageGenerator == null) - // languageGenerator = new LanguageGenerator(); - // return languageGenerator; - // } - - // private LanguageGenerator() { - // super(PROPERTY); - // } public LanguageGenerator(){ super(PROPERTY); diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/PopulationGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/PopulationGenerator.java index ed44788e..098dee8a 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/PopulationGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/PopulationGenerator.java @@ -12,21 +12,7 @@ public class PopulationGenerator extends AbstractGenerator { - // private final static String TEMPLATE = "What's the population of %s?"; private final static String PROPERTY = "P1082"; - - //This is for using as a singleton - // private static PopulationGenerator populationGenerator = null; - - // public static PopulationGenerator getInstance() { - // if(populationGenerator == null) - // populationGenerator = new PopulationGenerator(); - // return populationGenerator; - // } - - // private PopulationGenerator() { - // super(PROPERTY); - // } public PopulationGenerator(){ super(PROPERTY); diff --git a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/SizeGenerator.java b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/SizeGenerator.java index 83f40dfe..94dc5ac1 100644 --- a/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/SizeGenerator.java +++ b/backend/wiq/src/main/java/com/wiq/wiq/services/questionGenerator/generator/specificGenerators/SizeGenerator.java @@ -11,22 +11,7 @@ public class SizeGenerator extends AbstractGenerator { - // private static final String TEMPLATE = "What's the size of %s?"; private final static String PROPERTY = "P2046"; - - //This is for using as a singleton - // private static SizeGenerator sizeGenerator = null; - - // public static SizeGenerator getInstance() { - // if(sizeGenerator==null) - // sizeGenerator = new SizeGenerator(); - // return sizeGenerator; - - // } - - // private SizeGenerator() { - // super(PROPERTY); - // } public SizeGenerator(){ super(PROPERTY);