Skip to content

Commit

Permalink
Merge pull request #67 from Arquisoft/Question_Generation_General
Browse files Browse the repository at this point in the history
Question generation general
  • Loading branch information
Mister-Mario authored Apr 2, 2024
2 parents 61051fb + 8607b0a commit 9b68950
Show file tree
Hide file tree
Showing 10 changed files with 755 additions and 47 deletions.
610 changes: 609 additions & 1 deletion package-lock.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.wikidata.wdtk.wikibaseapi.apierrors.MediaWikiApiErrorException;

import main.java.questionGenerator.question.Question;
import main.java.questionGenerator.question.QuestionType;

public abstract class AbstractGenerator {

Expand All @@ -26,10 +27,13 @@ public abstract class AbstractGenerator {
private static Map<String, ItemDocumentImpl> alreadyProcessedEntities = new HashMap<>();

private String propertyId = "";
private QuestionType type;

private static final String MESSAGES_PATH = "messages";
public AbstractGenerator(String propertyId) {

public AbstractGenerator(String propertyId, QuestionType type) {
this.propertyId = propertyId;
this.type = type;
}

/**
Expand Down Expand Up @@ -71,7 +75,7 @@ public Question generate(String id) {
answers.add(0, rightAnswer);
//create and return the question

return new Question(question, answers);
return new Question(question, answers, language, type);
}

protected String getName(Map<String, MonolingualTextValue> names) {
Expand All @@ -87,11 +91,11 @@ public String getPropertyId() {
return propertyId;
}

public static Map<String, ItemDocumentImpl> getAlreadyProcessedEntities() {
return new HashMap<>(alreadyProcessedEntities);
public static ItemDocumentImpl getAlreadyProcessedEntity(String id) {
return alreadyProcessedEntities.get(id);
}

public static void addItem(String entity, ItemDocumentImpl item) {
public static void addProcessedEntity(String entity, ItemDocumentImpl item) {
alreadyProcessedEntities.put(entity, item);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,94 @@
package main.java.questionGenerator.generator;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.wikidata.wdtk.datamodel.implementation.ItemDocumentImpl;
import org.wikidata.wdtk.datamodel.interfaces.Statement;
import org.wikidata.wdtk.datamodel.interfaces.Value;
import org.wikidata.wdtk.wikibaseapi.apierrors.MediaWikiApiErrorException;

import main.java.questionGenerator.question.QuestionType;

public abstract class RightAnswerIsEntity extends AbstractGenerator {

public RightAnswerIsEntity(String propertyId) {
super(propertyId);
public RightAnswerIsEntity(String propertyId, QuestionType type) {
super(propertyId, type);
}

/**
* This method acts as a wrapper because in some cases this is enough, but not in all of them,
* so the rest are in charge of overriding it and modifying what they need
*/
@Override
protected String getRightAnswer(Map<String, List<Statement>> claims) {
Value v = claims.get(super.getPropertyId()).get(0).getValue();
String entity = getRightAnswerEntity(v.toString());
return processRightAnswer(claims.get(super.getPropertyId()).get(0));
}

protected String processRightAnswer(Statement st) {
String entity = getRightAnswerEntity(st.getValue().toString());
String answer = "";
try {
ItemDocumentImpl idi = getAlreadyProcessedEntities().get(entity);
ItemDocumentImpl idi = getAlreadyProcessedEntity(entity);
if(idi==null) {
idi = (ItemDocumentImpl) wbdf.getEntityDocument(entity);
answer = getName(idi.getLabels());
addItem(entity, idi);
addProcessedEntity(entity, idi);
}
else
answer = getName(idi.getLabels());
} catch (MediaWikiApiErrorException | IOException e) {
return null;

}
return answer;
}

private String getRightAnswerEntity(String url) {
protected String getAnswer(String id){
ItemDocumentImpl idi = getAlreadyProcessedEntity(id);

if(idi==null) {
try {
idi = (ItemDocumentImpl) wbdf.getEntityDocument(id);
addProcessedEntity(id, idi);
} catch (MediaWikiApiErrorException | IOException e) {
/*
* * @throws MediaWikiApiErrorException
* if the API returns an error
* @throws IOException
* if we encounter network issues or HTTP 500 errors from Wikibase
*/
return null;
}
}
return getRightAnswer(idi.getJsonClaims());
}

protected String getRightAnswerEntity(String url) {
String[] split1 = url.split(" ");
String[] split2 = split1[0].split("/");
return split2[split2.length-1];
}

@Override
protected List<String> getWrongAnswers(String rightAnswer) {
Random rnd = new Random();
String[] entities = {"Q142", "Q183", "Q16", "Q142", "Q30", "Q408", "Q668", "Q17", "Q38", "Q159",
"Q79", "Q155", "Q884", "Q414", "Q41", "Q258", "Q96", "Q843", "Q148", "Q20"};
List<String> result = new ArrayList<>();
List<Integer> used = new ArrayList<>();
for(int i = 0; i < 3; i++){
int rndnum = rnd.nextInt(entities.length);
String wrong = getAnswer(entities[rndnum]);
if(wrong.equals(rightAnswer) || used.contains(rndnum))
i--;
else{
result.add(wrong);
used.add(rndnum);
}
}
return result;
}

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package main.java.questionGenerator.generator.specificGenerators;

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

import org.wikidata.wdtk.datamodel.interfaces.Snak;
import org.wikidata.wdtk.datamodel.interfaces.SnakGroup;
import org.wikidata.wdtk.datamodel.interfaces.Statement;

import main.java.questionGenerator.generator.RightAnswerIsEntity;
import main.java.questionGenerator.question.QuestionType;

public class CapitalGenerator extends RightAnswerIsEntity {

private final static String PROPERTY = "P36";

public CapitalGenerator(){
super(PROPERTY);
super(PROPERTY, QuestionType.CAPITAL);
}

@Override
Expand All @@ -20,13 +25,25 @@ protected String getQuestion(String name) {
}

@Override
protected List<String> getWrongAnswers(String rightAnswer) {
// TODO Auto-generated method stub
List<String> result = new ArrayList<>();
result.add("a");
result.add("b");
result.add("c");
return result;
protected String getRightAnswer(Map<String, List<Statement>> claims) {
for(Statement st : claims.get(super.getPropertyId())) {
boolean valid = true;
for(SnakGroup sg : st.getQualifiers()) {
for(Snak s : sg.getSnaks()) {
String value = getRightAnswerEntity(s.getPropertyId().toString());
if(value.equals("P582")) {
valid = false;
break;
}
}
if(!valid)
break;
}
if(valid) {
return processRightAnswer(st);
}
}
return null;
}

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package main.java.questionGenerator.generator.specificGenerators;

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

import main.java.questionGenerator.generator.RightAnswerIsEntity;
import main.java.questionGenerator.question.QuestionType;

public class LanguageGenerator extends RightAnswerIsEntity {

private final static String PROPERTY = "P37";

public LanguageGenerator(){
super(PROPERTY);
super(PROPERTY, QuestionType.LANGUAGE);
}

@Override
Expand All @@ -19,14 +17,4 @@ protected String getQuestion(String name) {
return String.format(q, name);
}

@Override
protected List<String> getWrongAnswers(String rightAnswer) {
// TODO Auto-generated method stub
List<String> result = new ArrayList<>();
result.add("a");
result.add("b");
result.add("c");
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
import org.wikidata.wdtk.datamodel.interfaces.Value;

import main.java.questionGenerator.generator.AbstractGenerator;
import main.java.questionGenerator.question.QuestionType;

public class PopulationGenerator extends AbstractGenerator {

private final static String PROPERTY = "P1082";

public PopulationGenerator(){
super(PROPERTY);
super(PROPERTY, QuestionType.POPULATION);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.wikidata.wdtk.datamodel.interfaces.Statement;
import org.wikidata.wdtk.datamodel.interfaces.Value;

import main.java.questionGenerator.generator.AbstractGenerator;
import main.java.questionGenerator.question.QuestionType;

public class SizeGenerator extends AbstractGenerator {

private final static String PROPERTY = "P2046";

public SizeGenerator(){
super(PROPERTY);
super(PROPERTY, QuestionType.SIZE);
}

@Override
Expand All @@ -31,11 +33,28 @@ protected String getRightAnswer(Map<String, List<Statement>> claims) {

@Override
protected List<String> getWrongAnswers(String rightAnswer) {
// TODO Auto-generated method stub
float number = 0;
// Check if it is a float
try {
number = Float.parseFloat(rightAnswer);
} catch(NumberFormatException e) {
//throw exception or maybe return null
}

List<String> result = new ArrayList<>();
result.add("a");
result.add("b");
result.add("c");
Random rnd = new Random();

// Gives values depending on parameter with percentage
// Example: If parameter is 50 value range is number*.5 and number*1.5
int parameter = 50;
for(int i = 0; i < 3; i++){
float wrong = (number * (100 - parameter + rnd.nextInt(parameter * 2 + 1)) / 100);
// Checking if it creates the same answer
if(wrong == number)
i--;
else
result.add(String.valueOf(wrong));
}
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ public class Question {
private String question;
private List<String> answers;
private int number = -1;
private String language;
private QuestionType type;

public Question(String question, List<String> answers){
public Question(String question, List<String> answers, String language, QuestionType type){
this.question = question;
this.answers = new ArrayList<>(answers);
this.language = language;
this.type = type;
}

public Question() {
Expand Down Expand Up @@ -50,6 +54,8 @@ public JSONObject getJSON() {
for(String s : answers)
json.accumulate("answers", s);
if(number != -1) json.accumulate("number", number); //Para que los tests pasen
json.accumulate("language", language);
json.accumulate("type", type);
return json;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ void testGenerateQuestionsEnglish() {
} catch (JSONException e) {
fail("Exception occurred while parsing JSON: " + e.getMessage());
}

assertTrue(json.has("language"));
assertEquals(json.get("language"), "en");

assertTrue(json.has("type"));
assertEquals(json.get("type"), t.toString());
}
}

Expand Down Expand Up @@ -133,6 +139,12 @@ void testGenerateQuestionsSpanish() {
} catch (JSONException e) {
fail("Exception occurred while parsing JSON: " + e.getMessage());
}

assertTrue(json.has("language"));
assertEquals(json.get("language"), "es");

assertTrue(json.has("type"));
assertEquals(json.get("type"), t.toString());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.junit.jupiter.api.Test;

import main.java.questionGenerator.question.Question;
import main.java.questionGenerator.question.QuestionType;

public class QuestionTests {

Expand All @@ -21,7 +22,7 @@ public class QuestionTests {
@BeforeEach
void setUp() {
List<String> answers = Arrays.asList("A", "B", "C");
question = new Question("What is the capital of France?", answers);
question = new Question("What is the capital of France?", answers, "en", QuestionType.CAPITAL);
}

@Test
Expand Down Expand Up @@ -60,7 +61,9 @@ void testGetJSON() {
try {
JSONObject expectedJson = new JSONObject()
.put("question", "What is the capital of France?")
.put("answers", new JSONArray().put("A").put("B").put("C"));
.put("answers", new JSONArray().put("A").put("B").put("C"))
.put("language", "en")
.put("type", "CAPITAL");
assertEquals(expectedJson.toString(), question.getJSON().toString());
} catch (JSONException e) {
fail("JSONException occurred: " + e.getMessage());
Expand Down

0 comments on commit 9b68950

Please sign in to comment.