Skip to content

Commit

Permalink
Merge pull request #294 from Arquisoft/feature/musicAuthorQuestion
Browse files Browse the repository at this point in the history
Feat: new question type
  • Loading branch information
Toto-hitori authored Apr 26, 2024
2 parents 967b3a0 + 26c1238 commit 8436f27
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
5 changes: 5 additions & 0 deletions questiongenerator/src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public static void main(String[] args) {
new BasketballVenueQuestion("es");
}

if (GeneralRepositoryStorer.doesntExist(AnswerCategory.SONG)) {
new MusicAuthorQuestion("en");
new MusicAuthorQuestion("es");
}


// IMAGES
if(GeneralRepositoryStorer.doesntExist(AnswerCategory.STADIUM)) {
Expand Down
95 changes: 95 additions & 0 deletions questiongenerator/src/main/java/templates/MusicAuthorQuestion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package templates;

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

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

public class MusicAuthorQuestion extends QuestionTemplate {


List<String> authorLabels;

private static final String[] spanishStringsIni = {"¿Quién es el autor de '", "¿Quién canta '", "¿Quién es el cantante de '", "¿Quién es el intérprete de '"};
private static final String[] englishStringsIni= {"Who is the author of '", "Who sings '", "Who is the singer of '", "Who is the performer of '"};

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


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

@Override
public void setQuery() {
this.sparqlQuery = "SELECT DISTINCT ?songLabel ?performerLabel ?awardLabel " +
"WHERE { " +
" ?song wdt:P31 wd:Q134556; " +
" wdt:P175 ?performer; " +
" p:P166 ?statement. " +
" ?statement ps:P166 ?award. " +
" SERVICE wikibase:label { " +
" bd:serviceParam wikibase:language \"en, es\"." +
" } " +
"}" +
"LIMIT 100";
}

@Override
public void processResults() {
authorLabels = 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 songLabel = "";
String performerLabel = "";

try {
JSONObject songObject = result.getJSONObject("songLabel");
songLabel = songObject.getString("value");

JSONObject performerLabelObject = result.getJSONObject("performerLabel");
performerLabel = performerLabelObject.getString("value");
} catch (Exception e) {
continue;
}

if (needToSkip(songLabel, performerLabel))
continue;

Answer a = new Answer(performerLabel, AnswerCategory.SONG, langCode);
answers.add(a);

String questionString = "";

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

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

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

private boolean needToSkip(String musicLabel, String genreLabel) {
if (authorLabels.contains(musicLabel)) {
return true;
}
authorLabels.add(musicLabel);

if (musicLabel.equals("") || genreLabel.equals(""))
return true;
if (QGHelper.isEntityName(musicLabel) || QGHelper.isEntityName(genreLabel))
return true;

return false;
}
}

0 comments on commit 8436f27

Please sign in to comment.