Skip to content

Commit

Permalink
Merge pull request #267 from Arquisoft/feat/questionAddChanges
Browse files Browse the repository at this point in the history
More questions
  • Loading branch information
GOLASOOO authored Apr 21, 2024
2 parents 6313d61 + c472b76 commit aa257f3
Show file tree
Hide file tree
Showing 15 changed files with 489 additions and 69 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package lab.en2b.quizapi.questions.answer;

public enum AnswerCategory {
CAPITAL_CITY, COUNTRY, SONG, STADIUM, BALLON_DOR, GAMES_PUBLISHER, PAINTING
CAPITAL_CITY, COUNTRY, SONG, STADIUM, BALLON_DOR, GAMES_PUBLISHER, PAINTING, WTPOKEMON, GAMES_COUNTRY, GAMES_GENRE, BASKETBALL_VENUE
}

25 changes: 21 additions & 4 deletions questiongenerator/src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

public class Main {



public static void main(String[] args) {

//Edit constraints of the DB
GeneralRepositoryStorer.editConstraints();

// TEXT
Expand All @@ -25,6 +23,21 @@ public static void main(String[] args) {
new VideogamesPublisherQuestion("es");
}

if (GeneralRepositoryStorer.doesntExist(AnswerCategory.GAMES_GENRE)) {
new VideogamesGenreQuestion("en");
new VideogamesGenreQuestion("es");
}

if (GeneralRepositoryStorer.doesntExist(AnswerCategory.GAMES_COUNTRY)) {
new VideogamesCountryQuestion("en");
new VideogamesCountryQuestion("es");
}

if (GeneralRepositoryStorer.doesntExist(AnswerCategory.BASKETBALL_VENUE)) {
new BasketballVenueQuestion("en");
new BasketballVenueQuestion("es");
}


// IMAGES
if(GeneralRepositoryStorer.doesntExist(AnswerCategory.STADIUM)) {
Expand All @@ -37,9 +50,13 @@ public static void main(String[] args) {
new PaintingQuestion("es");
}

if (GeneralRepositoryStorer.doesntExist(AnswerCategory.WTPOKEMON)) {
new WhosThatPokemonQuestion("en");
new WhosThatPokemonQuestion("es");
}

/*
// VIDEOS
// VIDEOS not yet supported
if(GeneralRepositoryStorer.doesntExist(AnswerCategory.SONG.toString())) {
new SongQuestion("en");
new SongQuestion("es");
Expand Down
2 changes: 1 addition & 1 deletion questiongenerator/src/main/java/model/AnswerCategory.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package model;

public enum AnswerCategory {
CAPITAL_CITY, COUNTRY, SONG, STADIUM, BALLON_DOR, GAMES_PUBLISHER, PAINTING
CAPITAL_CITY, COUNTRY, SONG, STADIUM, BALLON_DOR, GAMES_PUBLISHER, PAINTING, WTPOKEMON, GAMES_COUNTRY, GAMES_GENRE, BASKETBALL_VENUE
}

Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
*/
public class GeneralRepositoryStorer {

public static final String LINKCONCAT = "#* &%";

public void saveAll(List<Storable> storableList) {
EntityManagerFactory emf = Jpa.getEntityManagerFactory();
EntityManager entityManager = emf.createEntityManager();
Expand Down
12 changes: 10 additions & 2 deletions questiongenerator/src/main/java/templates/BallonDOrQuestion.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

public class BallonDOrQuestion extends QuestionTemplate {

private static final String[] spanishStringsIni = {"¿Quién ganó el Balón de Oro en ", "¿Quién fue el ganador del Balón de Oro en ", "¿Quién obtuvo el Balón de Oro en ", "¿Quién se llevó el Balón de Oro en "};
private static final String[] englishStringsIni= {"Who won the Ballon d'Or in ", "Who was the winner of the Ballon d'Or in ", "Who got the Ballon d'Or in ", "Who was given the Ballon d'Or in "};

private static final String[] spanishStringsFin = {"?", "?", "?", "?"};
private static final String[] englishStringsFin = {"?", "?", "?", "?"};

/**
* It is not necessary to specify the language code for this question
* @param langCode IGNORED, spanish and english languages are generated at the same time
Expand Down Expand Up @@ -41,17 +47,19 @@ protected void processResults() {
if (needToSkip(playerLabel, year))
continue;


// EXCEPTION FOR THIS TYPE OF QUESTION
// English
Answer a = new Answer(playerLabel, AnswerCategory.BALLON_DOR, "en");
answers.add(a);
questions.add(new Question(a, "Who won the Ballon d'Or in " + year + "?", QuestionCategory.SPORTS, QuestionType.TEXT));

questions.add(new Question(a, englishStringsIni[i%4] + year + englishStringsFin[i%4], QuestionCategory.SPORTS, QuestionType.TEXT));

// Spanish
a = new Answer(playerLabel, AnswerCategory.BALLON_DOR, "es");
answers.add(a);
questions.add(new Question(a, "¿Quién ganó el Balón de Oro en " + year + "?", QuestionCategory.SPORTS, QuestionType.TEXT));

questions.add(new Question(a, spanishStringsIni[i%4] + year + spanishStringsFin[i%4], QuestionCategory.SPORTS, QuestionType.TEXT));
}

repository.saveAll(new ArrayList<>(answers));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package templates;

import model.*;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class BasketballVenueQuestion extends QuestionTemplate {

private List<String> teamLabels;

private static final String[] spanishStringsIni = {"¿Cómo se llama el estadio de ", "¿Dónde juega el equipo ", "¿Cuál es el estadio del equipo ", "¿Dónde se juegan los partidos del equipo "};
private static final String[] englishStringsIni= {"What is the venue of ", "What is the stadium of ", "Which name receives the venue of ", "Where does "};

private static final String[] spanishStringsFin = {"?", "?", "?", "?"};
private static final String[] englishStringsFin = {"?", "?", "?", " play?"};

public BasketballVenueQuestion(String langCode) {
super(langCode);
}

@Override
public void setQuery() {
this.sparqlQuery = "SELECT ?teamLabel ?venueLabel " +
"WHERE { " +
" ?team wdt:P31 wd:Q13393265; " +
" wdt:P118 ?league; " +
" wdt:P115 ?venue. " +
" VALUES ?league {wd:Q1126104 wd:Q155223} " +
" SERVICE wikibase:label { bd:serviceParam wikibase:language \"" + langCode + "\". } " +
"} ";
}

@Override
public void processResults() {
teamLabels = new ArrayList<>();
List<Question> questions = new ArrayList<>();
List<Answer> answers = new ArrayList<>();

for (int i = 0; i < results.length(); i++) {
JSONObject result = results.getJSONObject(i);
String teamLabel = result.getJSONObject("teamLabel").getString("value");
String venueLabel = result.getJSONObject("venueLabel").getString("value");

if (needToSkip(teamLabel, venueLabel)) {
continue;
}

Answer a = new Answer(venueLabel, AnswerCategory.BASKETBALL_VENUE, langCode);
answers.add(a);

String questionString = "";

if (langCode.equals("es"))
questionString = spanishStringsIni[i%4] + teamLabel + spanishStringsFin[i%4];
else
questionString = englishStringsIni[i%4] + teamLabel + englishStringsFin[i%4];

questions.add(new Question(a, questionString, QuestionCategory.SPORTS, QuestionType.TEXT));
}
repository.saveAll(new ArrayList<>(answers));
repository.saveAll(new ArrayList<>(questions));
}

private boolean needToSkip(String teamLabel, String venueLabel){
if (teamLabels.contains(teamLabel)) {
return true;
}
teamLabels.add(teamLabel);

if (QGHelper.isEntityName(teamLabel) || QGHelper.isEntityName(venueLabel))
return true;

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import model.AnswerCategory;
import model.Question;
import org.json.JSONObject;
import repositories.Storable;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -16,6 +15,12 @@
*/
public class CountryCapitalQuestion extends QuestionTemplate {

private static final String[] spanishStringsIni = {"¿Cuál es la capital de ", "¿Dónde se encuentra la capital de ", "¿Qué ciudad es la capital de ", "¿Cuál es la ciudad capital de "};
private static final String[] englishStringsIni= {"What is the capital of ", "Where is the capital of ", "Which city is the capital of ", "What is the capital city of "};

private static final String[] spanishStringsFin = {"?", "?", "?", "?"};
private static final String[] englishStringsFin = {"?", "?", "?", "?"};

/**
* Only need to invoke the constructor, and it will automatically do the HTTP request and the response recovery.
* @param langCode representation for the language to be used for the question. ("en" for English, "es" for Spanish)
Expand Down Expand Up @@ -57,11 +62,16 @@ protected void processResults() {
Answer a = new Answer(capitalLabel, AnswerCategory.CAPITAL_CITY, langCode);
answers.add(a);

String questionString = "";


//Saving the question
if (langCode.equals("es"))
questions.add(new Question(a, "¿Cuál es la capital de " + countryLabel + "?", QuestionCategory.GEOGRAPHY, QuestionType.TEXT));
questionString = spanishStringsIni[i%4] + countryLabel + spanishStringsFin[i%4];
else
questions.add(new Question(a, "What is the capital of " + countryLabel + "?", QuestionCategory.GEOGRAPHY, QuestionType.TEXT));
questionString = englishStringsIni[i%4] + countryLabel + englishStringsFin[i%4];

questions.add(new Question(a, questionString, QuestionCategory.GEOGRAPHY, QuestionType.TEXT));
}

repository.saveAll(new ArrayList<>(answers));
Expand Down
29 changes: 14 additions & 15 deletions questiongenerator/src/main/java/templates/PaintingQuestion.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import model.*;
import org.json.JSONObject;
import repositories.GeneralRepositoryStorer;

import java.util.ArrayList;
import java.util.List;

public class PaintingQuestion extends QuestionTemplate {

private static final String[] spanishStringsIni = {"¿Cómo se llama este cuadro?", "¿Cuál es este cuadro?", "¿Qué pintura es esta?", "¿Cuál es el nombre de este cuadro?"};
private static final String[] englishStringsIni= {"What is the name of this painting?", "Which painting is this?", "What painting is this?", "How's this painting called?"};

List<String> paintingLabels;

public PaintingQuestion(String langCode) {
Expand Down Expand Up @@ -50,7 +52,7 @@ public void processResults() {
JSONObject imageObject = result.getJSONObject("image");
String imageLink = imageObject.getString("value");

if (needToSkip(paintingLabel))
if (needToSkip(paintingLabel, imageLink))
continue;

String answerText = "";
Expand All @@ -62,32 +64,29 @@ public void processResults() {
Answer a = new Answer(answerText, AnswerCategory.PAINTING, langCode);
answers.add(a);

imageLink = imageLink.replace("http://", "https://");
if (langCode.equals("es"))
questions.add(new Question(a, "¿Cuál es este cuadro?" + GeneralRepositoryStorer.LINKCONCAT + imageLink, QuestionCategory.ART, QuestionType.IMAGE));
questions.add(new Question(a, spanishStringsIni[i%4] + QGHelper.LINKCONCAT + imageLink, QuestionCategory.ART, QuestionType.IMAGE));
else
questions.add(new Question(a, "Which painting is this?" + GeneralRepositoryStorer.LINKCONCAT + imageLink, QuestionCategory.ART, QuestionType.IMAGE));
questions.add(new Question(a, englishStringsIni[i%4] + QGHelper.LINKCONCAT + imageLink, QuestionCategory.ART, QuestionType.IMAGE));
}

repository.saveAll(new ArrayList<>(answers));
repository.saveAll(new ArrayList<>(questions));
}

private boolean needToSkip(String paintingLabel) {
private boolean needToSkip(String paintingLabel, String imageLink) {
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<paintingLabel.length(); i++){
if (!Character.isDigit(paintingLabel.charAt(i))){
isEntityName = false;
}
}
if (isEntityName){
return true;
}
if (QGHelper.isEntityName(paintingLabel)){
return true;
}

if (QGHelper.notAllowedExtension(imageLink)){
return true;
}

return false;
Expand Down
33 changes: 33 additions & 0 deletions questiongenerator/src/main/java/templates/QGHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package templates;

public class QGHelper {
public final static String LINKCONCAT = "#* &%";
public final static String[] allowedExtensions = {"png", "jpg", "jpeg", "gif", "svg"};

private QGHelper() {
}

public static boolean isEntityName(String label){
boolean isEntityName = true; // Check if it is like Q232334
if (label.startsWith("Q") ){
for (int i=1; i<label.length(); i++){
if (!Character.isDigit(label.charAt(i))){
isEntityName = false;
}
}
if (isEntityName){
return true;
}
}
return false;
}

public static boolean notAllowedExtension(String link){
for (String s : allowedExtensions){
if (link.endsWith(s)){
return false;
}
}
return true;
}
}
11 changes: 9 additions & 2 deletions questiongenerator/src/main/java/templates/SongQuestion.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

public class SongQuestion extends QuestionTemplate {

private static final String[] spanishStringsIni = {"¿Cómo se llama esta canción?", "¿Cuál es esta canción?", "¿Qué canción es esta?", "¿Cómo se llama esta canción?"};
private static final String[] englishStringsIni= {"What is the name of this song?", "Which song is this?", "What song is this?", "What is the name of this song?"};

List<String> songLabels;

public SongQuestion(String langCode) {
Expand Down Expand Up @@ -59,9 +62,9 @@ public void processResults() {
answers.add(a);

if (langCode.equals("es"))
questions.add(new Question(a, "¿Cuál es esta canción? " + musicVideoLink, QuestionCategory.MUSIC, QuestionType.AUDIO));
questions.add(new Question(a, spanishStringsIni[i%4] + QGHelper.LINKCONCAT + musicVideoLink, QuestionCategory.MUSIC, QuestionType.AUDIO));
else
questions.add(new Question(a, "Which song is this? " + musicVideoLink, QuestionCategory.MUSIC, QuestionType.AUDIO));
questions.add(new Question(a, englishStringsIni[i%4] + QGHelper.LINKCONCAT + musicVideoLink, QuestionCategory.MUSIC, QuestionType.AUDIO));
}

repository.saveAll(new ArrayList<>(answers));
Expand All @@ -74,6 +77,10 @@ private boolean needToSkip(String songLabel) {
}
songLabels.add(songLabel);

if (QGHelper.isEntityName(songLabel)){
return true;
}

return false;
}
}
Loading

0 comments on commit aa257f3

Please sign in to comment.