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

Feat/question publisher date #309

Merged
merged 4 commits into from
Apr 28, 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, COUNTRY_FLAG
CAPITAL_CITY, COUNTRY, SONG, STADIUM, BALLON_DOR, GAMES_PUBLISHER, PAINTING, WTPOKEMON, GAMES_COUNTRY, GAMES_GENRE, BASKETBALL_VENUE, COUNTRY_FLAG, GAMES_RELEASE
}

7 changes: 7 additions & 0 deletions questiongenerator/src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public static void main(String[] args) {
new MusicAuthorQuestion("es");
}

if (GeneralRepositoryStorer.doesntExist(AnswerCategory.GAMES_RELEASE)) {
new VideogamesReleaseDate("en");
new VideogamesReleaseDate("es");
}


// IMAGES
if(GeneralRepositoryStorer.doesntExist(AnswerCategory.STADIUM)) {
Expand All @@ -65,6 +70,8 @@ public static void main(String[] args) {
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, COUNTRY_FLAG
CAPITAL_CITY, COUNTRY, SONG, STADIUM, BALLON_DOR, GAMES_PUBLISHER, PAINTING, WTPOKEMON, GAMES_COUNTRY, GAMES_GENRE, BASKETBALL_VENUE, COUNTRY_FLAG, GAMES_RELEASE
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package templates;

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

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

public class VideogamesReleaseDate extends QuestionTemplate {
List<String> videoGameLabels;

private static final String[] spanishStringsIni = {"¿Cuándo se publicó ", "¿En qué fecha fue lanzado ", "¿Cuándo fue lanzado ", "¿En qué fecha fue publicado "};
private static final String[] englishStringsIni= {"When was ", "On what date was ", "When was ", "On what date was "};

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

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

@Override
public void setQuery() {
this.sparqlQuery = "SELECT ?gameLabel (MAX(?unitsSoldValue) as ?maxUnitsSold) (MIN(?publicationDate) as ?oldestPublicationDate)\n" +
"WHERE { " +
" ?game wdt:P31 wd:Q7889; " +
" wdt:P2664 ?unitsSoldValue. " +
" ?game wdt:P577 ?publicationDate. " +
" 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<Question> questions = new ArrayList<>();
List<Answer> answers = new ArrayList<>();

for (int i = 0; i < results.length()-10; i++) {

JSONObject result = results.getJSONObject(i);

String videoGameLabel = "";
String publishDateLabel = "";

try {
JSONObject videoGameLabelObject = result.getJSONObject("gameLabel");
videoGameLabel = videoGameLabelObject.getString("value");

JSONObject publishDateLabelObject = result.getJSONObject("oldestPublicationDate");
publishDateLabel = publishDateLabelObject.getString("value");

publishDateLabel = publishDateLabel.substring(0, 4);

} catch (Exception e) {
continue;
}

if (needToSkip(videoGameLabel, publishDateLabel))
continue;

Answer a = new Answer(publishDateLabel, AnswerCategory.GAMES_RELEASE, langCode);
answers.add(a);

String questionString = "";

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

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

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

private boolean needToSkip(String videoGameLabel, String countryLabel) {
if (videoGameLabels.contains(videoGameLabel)) {
return true;
}
videoGameLabels.add(videoGameLabel);

if (QGHelper.isEntityName(videoGameLabel) || QGHelper.isEntityName(countryLabel))
return true;

return false;
}
}