Skip to content

Commit

Permalink
Merge pull request #223 from Arquisoft/feat/newQuestions
Browse files Browse the repository at this point in the history
New question type
  • Loading branch information
jjgancfer authored Apr 12, 2024
2 parents e697941 + fb0ffae commit 0168183
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 11 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
CAPITAL_CITY, COUNTRY, SONG, STADIUM, BALLON_DOR, GAMES_PUBLISHER, PAINTING
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

public enum QuestionCategory {
//HISTORY, GEOGRAPHY, SCIENCE, MATH, LITERATURE, ART, SPORTS, MUSIC, MOVIES, TV, POLITICS, OTHER
GEOGRAPHY, SPORTS, MUSIC
GEOGRAPHY, SPORTS, MUSIC, ART, VIDEOGAMES
}
32 changes: 25 additions & 7 deletions questiongenerator/src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,48 @@
import repositories.GeneralRepositoryStorer;
import templates.BallonDOrQuestion;
import templates.CountryCapitalQuestion;
import templates.VideogamesPublisherQuestion;

public class Main {
public static void main(String[] args) {

// TEXT
if(GeneralRepositoryStorer.doesntExist(AnswerCategory.CAPITAL_CITY)) {
new CountryCapitalQuestion("en");
new CountryCapitalQuestion("es");
}

/*
if(GeneralRepositoryStorer.doesntExist(AnswerCategory.SONG.toString())) {
new SongQuestion("en");
new SongQuestion("es");
if (GeneralRepositoryStorer.doesntExist(AnswerCategory.BALLON_DOR)) {
new BallonDOrQuestion(""); // No need to specify language code as it is not used
}

if (GeneralRepositoryStorer.doesntExist(AnswerCategory.GAMES_PUBLISHER)) {
new VideogamesPublisherQuestion("en");
new VideogamesPublisherQuestion("es");
}


/*
// IMAGES
if(GeneralRepositoryStorer.doesntExist(AnswerCategory.STADIUM.toString())) {
new StadiumQuestion("en");
new StadiumQuestion("es");
}
*/
if (GeneralRepositoryStorer.doesntExist(AnswerCategory.BALLON_DOR)) {
new BallonDOrQuestion(""); // No need to specify language code as it is not used
if (GeneralRepositoryStorer.doesntExist(AnswerCategory.PAINTING)) {
new PaintingQuestion("en");
new PaintingQuestion("es");
}
*/


/*
// VIDEOS
if(GeneralRepositoryStorer.doesntExist(AnswerCategory.SONG.toString())) {
new SongQuestion("en");
new SongQuestion("es");
}
*/
}
}
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
CAPITAL_CITY, COUNTRY, SONG, STADIUM, BALLON_DOR, GAMES_PUBLISHER, PAINTING
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

public enum QuestionCategory {
//HISTORY, GEOGRAPHY, SCIENCE, MATH, LITERATURE, ART, SPORTS, MUSIC, MOVIES, TV, POLITICS, OTHER
GEOGRAPHY, SPORTS, MUSIC
GEOGRAPHY, SPORTS, MUSIC, ART, VIDEOGAMES
}
94 changes: 94 additions & 0 deletions questiongenerator/src/main/java/templates/PaintingQuestion.java
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 PaintingQuestion extends QuestionTemplate {

List<String> paintingLabels;

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

@Override
public void setQuery() {
this.sparqlQuery =
"SELECT DISTINCT ?paintingLabel ?authorLabel ?image " +
"WHERE { " +
" ?painting wdt:P31 wd:Q3305213; " +
" wdt:P170 ?author; " +
" wdt:P18 ?image; " +
" wdt:P1343 wd:Q66362718. " +
" ?author wdt:P106 wd:Q1028181. " +
" SERVICE wikibase:label { bd:serviceParam wikibase:language \"" + langCode + "\". } " +
"} " +
"LIMIT 100";
}

@Override
public void processResults() {
paintingLabels = 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);


JSONObject paintingLabelObject = result.getJSONObject("paintingLabel");
String paintingLabel = paintingLabelObject.getString("value");

JSONObject authorLabelObject = result.getJSONObject("authorLabel");
String authorLabel = authorLabelObject.getString("value");

JSONObject imageObject = result.getJSONObject("image");
String imageLink = imageObject.getString("value");

if (needToSkip(paintingLabel))
continue;

String answerText = "";
if (langCode.equals("es"))
answerText = paintingLabel + " de " + authorLabel;
else
answerText = paintingLabel + " by " + authorLabel;

Answer a = new Answer(answerText, AnswerCategory.PAINTING, langCode);
answers.add(a);

if (langCode.equals("es"))
questions.add(new Question(a, "¿Cuál es este cuadro? " + imageLink, QuestionCategory.ART, QuestionType.IMAGE));
else
questions.add(new Question(a, "Which painting is this? " + imageLink, QuestionCategory.ART, QuestionType.IMAGE));
}

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

private boolean needToSkip(String paintingLabel) {
if (paintingLabels.contains(paintingLabel)) {
return true;
}
paintingLabels.add(paintingLabel);

boolean isEntityName = true; // Check if it is like Q232334
if (paintingLabel.startsWith("Q") ){
for (int i=1; i<paintingLabel.length(); i++){
if (!Character.isDigit(paintingLabel.charAt(i))){
isEntityName = false;
}
}
if (isEntityName){
return true;
}
}

return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package templates;

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

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

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

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

@Override
public void setQuery() {
this.sparqlQuery =
"SELECT ?gameLabel (MAX(?unitsSoldValue) as ?maxUnitsSold) (SAMPLE(?publisherLabel) as ?publisher) " +
"WHERE { " +
" ?game wdt:P31 wd:Q7889; " +
" wdt:P2664 ?unitsSoldValue. " +
" OPTIONAL { " +
" ?game wdt:P123 ?publisher. " +
" ?publisher rdfs:label ?publisherLabel. " +
" FILTER(LANG(?publisherLabel) IN (\"en\", \"es\")) " +
" } " +
" 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 publisherLabel = "";

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

JSONObject publisherLabelObject = result.getJSONObject("publisher");
publisherLabel = publisherLabelObject.getString("value");
} catch (Exception e) {
continue;
}

if (needToSkip(videoGameLabel, publisherLabel))
continue;

Answer a = new Answer(publisherLabel, AnswerCategory.GAMES_PUBLISHER, langCode);
answers.add(a);

if (langCode.equals("es"))
questions.add(new Question(a, "¿Qué compañía publicó " + videoGameLabel + "?", QuestionCategory.VIDEOGAMES, QuestionType.TEXT));
else
questions.add(new Question(a, "Who published " + videoGameLabel + "?", QuestionCategory.VIDEOGAMES, QuestionType.TEXT));
}

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

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

boolean isEntityName = isEntityName(videoGameLabel);
if (isEntityName){
return true;
}
isEntityName = isEntityName(publisherLabel);
if (isEntityName){
return true;
}
return false;
}

private boolean isEntityName(String label){
boolean isEntityName = true; // Check if it is like Q232334
if (label.startsWith("Q") ){
for (int i=1; i<label.length(); i++){
if (!Character.isDigit(label.charAt(i))){
isEntityName = false;
}
}
if (isEntityName){
return true;
}
}
return false;
}
}

0 comments on commit 0168183

Please sign in to comment.