generated from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #223 from Arquisoft/feat/newQuestions
New question type
- Loading branch information
Showing
7 changed files
with
228 additions
and
11 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
api/src/main/java/lab/en2b/quizapi/questions/answer/AnswerCategory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
questiongenerator/src/main/java/templates/PaintingQuestion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
questiongenerator/src/main/java/templates/VideogamesPublisherQuestion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |