Skip to content

Commit

Permalink
Chore: Added country flag questions
Browse files Browse the repository at this point in the history
  • Loading branch information
UO283615 committed Apr 23, 2024
1 parent 783214d commit 296161e
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 2 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, 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
}

5 changes: 5 additions & 0 deletions questiongenerator/src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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())) {
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, 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
}

77 changes: 77 additions & 0 deletions questiongenerator/src/main/java/templates/CountryFlagQuestion.java
Original file line number Diff line number Diff line change
@@ -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<String> 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<Question> questions = new ArrayList<>();
List<Answer> 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;
}
}

0 comments on commit 296161e

Please sign in to comment.