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 50298f7a..9e8154dc 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 + CAPITAL_CITY, COUNTRY, SONG, STADIUM, BALLON_DOR, GAMES_PUBLISHER, PAINTING } diff --git a/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionCategory.java b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionCategory.java index 147ed65d..d97db494 100644 --- a/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionCategory.java +++ b/api/src/main/java/lab/en2b/quizapi/questions/question/QuestionCategory.java @@ -2,5 +2,5 @@ public enum QuestionCategory { //HISTORY, GEOGRAPHY, SCIENCE, MATH, LITERATURE, ART, SPORTS, MUSIC, MOVIES, TV, POLITICS, OTHER - GEOGRAPHY, SPORTS, MUSIC + GEOGRAPHY, SPORTS, MUSIC, ART, VIDEOGAMES } diff --git a/questiongenerator/src/main/java/Main.java b/questiongenerator/src/main/java/Main.java index 3ce4b779..8c0b5755 100644 --- a/questiongenerator/src/main/java/Main.java +++ b/questiongenerator/src/main/java/Main.java @@ -2,30 +2,48 @@ import repositories.GeneralRepositoryStorer; import templates.BallonDOrQuestion; import templates.CountryCapitalQuestion; +import templates.VideogamesPublisherQuestion; public class Main { public static void main(String[] args) { + // TEXT if(GeneralRepositoryStorer.doesntExist(AnswerCategory.CAPITAL_CITY)) { new CountryCapitalQuestion("en"); new CountryCapitalQuestion("es"); } - /* - if(GeneralRepositoryStorer.doesntExist(AnswerCategory.SONG.toString())) { - new SongQuestion("en"); - new SongQuestion("es"); + if (GeneralRepositoryStorer.doesntExist(AnswerCategory.BALLON_DOR)) { + new BallonDOrQuestion(""); // No need to specify language code as it is not used } + if (GeneralRepositoryStorer.doesntExist(AnswerCategory.GAMES_PUBLISHER)) { + new VideogamesPublisherQuestion("en"); + new VideogamesPublisherQuestion("es"); + } + + + /* + // IMAGES + if(GeneralRepositoryStorer.doesntExist(AnswerCategory.STADIUM.toString())) { new StadiumQuestion("en"); new StadiumQuestion("es"); } - */ - if (GeneralRepositoryStorer.doesntExist(AnswerCategory.BALLON_DOR)) { - new BallonDOrQuestion(""); // No need to specify language code as it is not used + if (GeneralRepositoryStorer.doesntExist(AnswerCategory.PAINTING)) { + new PaintingQuestion("en"); + new PaintingQuestion("es"); } + */ + + /* + // VIDEOS + if(GeneralRepositoryStorer.doesntExist(AnswerCategory.SONG.toString())) { + new SongQuestion("en"); + new SongQuestion("es"); + } + */ } } \ No newline at end of file diff --git a/questiongenerator/src/main/java/model/AnswerCategory.java b/questiongenerator/src/main/java/model/AnswerCategory.java index 1fc9197b..9af4f704 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 + CAPITAL_CITY, COUNTRY, SONG, STADIUM, BALLON_DOR, GAMES_PUBLISHER, PAINTING } diff --git a/questiongenerator/src/main/java/model/QuestionCategory.java b/questiongenerator/src/main/java/model/QuestionCategory.java index 47f0782c..d39d26dc 100644 --- a/questiongenerator/src/main/java/model/QuestionCategory.java +++ b/questiongenerator/src/main/java/model/QuestionCategory.java @@ -2,5 +2,5 @@ public enum QuestionCategory { //HISTORY, GEOGRAPHY, SCIENCE, MATH, LITERATURE, ART, SPORTS, MUSIC, MOVIES, TV, POLITICS, OTHER - GEOGRAPHY, SPORTS, MUSIC + GEOGRAPHY, SPORTS, MUSIC, ART, VIDEOGAMES } diff --git a/questiongenerator/src/main/java/templates/PaintingQuestion.java b/questiongenerator/src/main/java/templates/PaintingQuestion.java new file mode 100644 index 00000000..5281284a --- /dev/null +++ b/questiongenerator/src/main/java/templates/PaintingQuestion.java @@ -0,0 +1,94 @@ +package templates; + +import model.*; +import org.json.JSONObject; + +import java.util.ArrayList; +import java.util.List; + +public class PaintingQuestion extends QuestionTemplate { + + List paintingLabels; + + public PaintingQuestion(String langCode) { + super(langCode); + } + + @Override + public void setQuery() { + this.sparqlQuery = + "SELECT DISTINCT ?paintingLabel ?authorLabel ?image " + + "WHERE { " + + " ?painting wdt:P31 wd:Q3305213; " + + " wdt:P170 ?author; " + + " wdt:P18 ?image; " + + " wdt:P1343 wd:Q66362718. " + + " ?author wdt:P106 wd:Q1028181. " + + " SERVICE wikibase:label { bd:serviceParam wikibase:language \"" + langCode + "\". } " + + "} " + + "LIMIT 100"; + } + + @Override + public void processResults() { + paintingLabels = new ArrayList<>(); + List questions = new ArrayList<>(); + List answers = new ArrayList<>(); + + for (int i = 0; i < results.length(); i++) { + + JSONObject result = results.getJSONObject(i); + + + JSONObject paintingLabelObject = result.getJSONObject("paintingLabel"); + String paintingLabel = paintingLabelObject.getString("value"); + + JSONObject authorLabelObject = result.getJSONObject("authorLabel"); + String authorLabel = authorLabelObject.getString("value"); + + JSONObject imageObject = result.getJSONObject("image"); + String imageLink = imageObject.getString("value"); + + if (needToSkip(paintingLabel)) + continue; + + String answerText = ""; + if (langCode.equals("es")) + answerText = paintingLabel + " de " + authorLabel; + else + answerText = paintingLabel + " by " + authorLabel; + + Answer a = new Answer(answerText, AnswerCategory.PAINTING, langCode); + answers.add(a); + + if (langCode.equals("es")) + questions.add(new Question(a, "¿Cuál es este cuadro? " + imageLink, QuestionCategory.ART, QuestionType.IMAGE)); + else + questions.add(new Question(a, "Which painting is this? " + imageLink, QuestionCategory.ART, QuestionType.IMAGE)); + } + + repository.saveAll(new ArrayList<>(answers)); + repository.saveAll(new ArrayList<>(questions)); + } + + private boolean needToSkip(String paintingLabel) { + if (paintingLabels.contains(paintingLabel)) { + return true; + } + paintingLabels.add(paintingLabel); + + boolean isEntityName = true; // Check if it is like Q232334 + if (paintingLabel.startsWith("Q") ){ + for (int i=1; i videoGameLabels; + + public VideogamesPublisherQuestion(String langCode) { + super(langCode); + } + + @Override + public void setQuery() { + this.sparqlQuery = + "SELECT ?gameLabel (MAX(?unitsSoldValue) as ?maxUnitsSold) (SAMPLE(?publisherLabel) as ?publisher) " + + "WHERE { " + + " ?game wdt:P31 wd:Q7889; " + + " wdt:P2664 ?unitsSoldValue. " + + " OPTIONAL { " + + " ?game wdt:P123 ?publisher. " + + " ?publisher rdfs:label ?publisherLabel. " + + " FILTER(LANG(?publisherLabel) IN (\"en\", \"es\")) " + + " } " + + " SERVICE wikibase:label { bd:serviceParam wikibase:language \"" + langCode + "\". } " + + "} " + + "GROUP BY ?game ?gameLabel " + + "ORDER BY DESC(?maxUnitsSold) " + + "LIMIT 150"; + } + + @Override + public void processResults() { + videoGameLabels = new ArrayList<>(); + List questions = new ArrayList<>(); + List answers = new ArrayList<>(); + + for (int i = 0; i < results.length()-10; i++) { + + JSONObject result = results.getJSONObject(i); + + String videoGameLabel = ""; + String publisherLabel = ""; + + try { + JSONObject videoGameLabelObject = result.getJSONObject("gameLabel"); + videoGameLabel = videoGameLabelObject.getString("value"); + + JSONObject publisherLabelObject = result.getJSONObject("publisher"); + publisherLabel = publisherLabelObject.getString("value"); + } catch (Exception e) { + continue; + } + + if (needToSkip(videoGameLabel, publisherLabel)) + continue; + + Answer a = new Answer(publisherLabel, AnswerCategory.GAMES_PUBLISHER, langCode); + answers.add(a); + + if (langCode.equals("es")) + questions.add(new Question(a, "¿Qué compañía publicó " + videoGameLabel + "?", QuestionCategory.VIDEOGAMES, QuestionType.TEXT)); + else + questions.add(new Question(a, "Who published " + videoGameLabel + "?", QuestionCategory.VIDEOGAMES, QuestionType.TEXT)); + } + + repository.saveAll(new ArrayList<>(answers)); + repository.saveAll(new ArrayList<>(questions)); + } + + private boolean needToSkip(String videoGameLabel, String publisherLabel) { + if (videoGameLabels.contains(videoGameLabel)) { + return true; + } + videoGameLabels.add(videoGameLabel); + + boolean isEntityName = isEntityName(videoGameLabel); + if (isEntityName){ + return true; + } + isEntityName = isEntityName(publisherLabel); + if (isEntityName){ + return true; + } + return false; + } + + private boolean isEntityName(String label){ + boolean isEntityName = true; // Check if it is like Q232334 + if (label.startsWith("Q") ){ + for (int i=1; i