From 296161e9c5133134a176636c8db161fe9c6a2b89 Mon Sep 17 00:00:00 2001 From: Diego Villanueva Date: Wed, 24 Apr 2024 00:46:09 +0200 Subject: [PATCH] Chore: Added country flag questions --- .../questions/answer/AnswerCategory.java | 2 +- questiongenerator/src/main/java/Main.java | 5 ++ .../src/main/java/model/AnswerCategory.java | 2 +- .../java/templates/CountryFlagQuestion.java | 77 +++++++++++++++++++ 4 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 questiongenerator/src/main/java/templates/CountryFlagQuestion.java diff --git a/api/src/main/java/lab/en2b/quizapi/questions/answer/AnswerCategory.java b/api/src/main/java/lab/en2b/quizapi/questions/answer/AnswerCategory.java index cc8239b0..f74bd1b0 100644 --- a/api/src/main/java/lab/en2b/quizapi/questions/answer/AnswerCategory.java +++ b/api/src/main/java/lab/en2b/quizapi/questions/answer/AnswerCategory.java @@ -1,6 +1,6 @@ package lab.en2b.quizapi.questions.answer; public enum AnswerCategory { - CAPITAL_CITY, COUNTRY, SONG, STADIUM, BALLON_DOR, GAMES_PUBLISHER, PAINTING, WTPOKEMON, GAMES_COUNTRY, GAMES_GENRE, BASKETBALL_VENUE + CAPITAL_CITY, COUNTRY, SONG, STADIUM, BALLON_DOR, GAMES_PUBLISHER, PAINTING, WTPOKEMON, GAMES_COUNTRY, GAMES_GENRE, BASKETBALL_VENUE, COUNTRY_FLAG } diff --git a/questiongenerator/src/main/java/Main.java b/questiongenerator/src/main/java/Main.java index e9f76e02..3b7a256b 100644 --- a/questiongenerator/src/main/java/Main.java +++ b/questiongenerator/src/main/java/Main.java @@ -55,6 +55,11 @@ public static void main(String[] args) { new WhosThatPokemonQuestion("es"); } + if (GeneralRepositoryStorer.doesntExist(AnswerCategory.COUNTRY_FLAG)) { + new CountryFlagQuestion("en"); + new CountryFlagQuestion("es"); + } + /* // VIDEOS not yet supported if(GeneralRepositoryStorer.doesntExist(AnswerCategory.SONG.toString())) { diff --git a/questiongenerator/src/main/java/model/AnswerCategory.java b/questiongenerator/src/main/java/model/AnswerCategory.java index 7de719bb..c4b0f5a1 100644 --- a/questiongenerator/src/main/java/model/AnswerCategory.java +++ b/questiongenerator/src/main/java/model/AnswerCategory.java @@ -1,6 +1,6 @@ package model; public enum AnswerCategory { - CAPITAL_CITY, COUNTRY, SONG, STADIUM, BALLON_DOR, GAMES_PUBLISHER, PAINTING, WTPOKEMON, GAMES_COUNTRY, GAMES_GENRE, BASKETBALL_VENUE + CAPITAL_CITY, COUNTRY, SONG, STADIUM, BALLON_DOR, GAMES_PUBLISHER, PAINTING, WTPOKEMON, GAMES_COUNTRY, GAMES_GENRE, BASKETBALL_VENUE, COUNTRY_FLAG } diff --git a/questiongenerator/src/main/java/templates/CountryFlagQuestion.java b/questiongenerator/src/main/java/templates/CountryFlagQuestion.java new file mode 100644 index 00000000..3b254737 --- /dev/null +++ b/questiongenerator/src/main/java/templates/CountryFlagQuestion.java @@ -0,0 +1,77 @@ +package templates; + +import model.QuestionCategory; +import model.QuestionType; +import model.Answer; +import model.AnswerCategory; +import model.Question; +import org.json.JSONObject; + +import java.util.ArrayList; +import java.util.List; + +public class CountryFlagQuestion extends QuestionTemplate { + + private static final String[] spanishStringsIni = {"¿Que país tiene esta bandera? ", "¿A qué país pertenece esta bandera? ", "¿De qué país es esta bandera? ", "¿Cuál es el país de esta bandera? "}; + private static final String[] englishStringsIni= {"Which country has this flag? ", "To which country belongs this flag? ", "From which country is this flag? ", "What is the country represented by this flag? "}; + + List countryLabels; + + public CountryFlagQuestion(String langCode) { + super(langCode); + } + + @Override + public void setQuery() { + this.sparqlQuery = "SELECT ?countryLabel ?flagLabel\n" + + "WHERE " + + "{ " + + " ?country wdt:P31 wd:Q6256; " + + " wdt:P41 ?flag. " + + " SERVICE wikibase:label { bd:serviceParam wikibase:language \"" + langCode + "\". } " + + "}"; + } + + @Override + public void processResults() { + countryLabels = new ArrayList<>(); + List questions = new ArrayList<>(); + List answers = new ArrayList<>(); + + for (int i = 0; i < results.length(); i++) { + JSONObject result = results.getJSONObject(i); + String countryLabel = result.getJSONObject("countryLabel").getString("value"); + String flagLabel = result.getJSONObject("flagLabel").getString("value"); + + if (needToSkip(countryLabel, flagLabel)) { + continue; + } + + Answer a = new Answer(countryLabel, AnswerCategory.COUNTRY_FLAG, langCode); + answers.add(a); + + if (langCode.equals("es")){ + String questionString = spanishStringsIni[i%4] + QGHelper.LINKCONCAT + flagLabel; + questions.add(new Question(a, questionString, QuestionCategory.GEOGRAPHY, QuestionType.IMAGE)); + } else { + String questionString = englishStringsIni[i%4] + QGHelper.LINKCONCAT + flagLabel; + questions.add(new Question(a, questionString, QuestionCategory.GEOGRAPHY, QuestionType.IMAGE)); + } + } + repository.saveAll(new ArrayList<>(answers)); + repository.saveAll(new ArrayList<>(questions)); + } + + private boolean needToSkip(String countryLabel, String venueLabel){ + if (countryLabels.contains(countryLabel)) { + return true; + } + countryLabels.add(countryLabel); + + if (QGHelper.isEntityName(countryLabel) || QGHelper.isEntityName(venueLabel)) { + return true; + } + + return false; + } +}