Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Added country flag questions #278

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}