Skip to content

Commit

Permalink
Merge pull request #309 from Arquisoft/feat/questionPublisherDate
Browse files Browse the repository at this point in the history
Feat/question publisher date
  • Loading branch information
Toto-hitori authored Apr 28, 2024
2 parents 4015426 + 5d00180 commit 5de5a30
Show file tree
Hide file tree
Showing 4 changed files with 103 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, 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;
}
}

0 comments on commit 5de5a30

Please sign in to comment.