Skip to content

Commit

Permalink
Merge pull request #133 from Arquisoft/Question_Generation_General
Browse files Browse the repository at this point in the history
Fixed small problems with the generation
  • Loading branch information
Mister-Mario authored Apr 27, 2024
2 parents 40e18d3 + 8e70c74 commit 67c897f
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public AnswersAreEntitiesWithSubProperties(String propertyId, QuestionType type,
this.PROPERTY_TO_CHECK = propertyToCheck;
}

/* The value of the Snak is deprecated and no longer implemented or updated, but since we are simply
* checking whether is null or not is not important
*/
@SuppressWarnings("deprecation")
@Override
public String getRightAnswer(Map<String, List<Statement>> claims, String propertyId) throws Exception {
if(claims.get(propertyId)==null) {
Expand All @@ -31,6 +35,8 @@ public String getRightAnswer(Map<String, List<Statement>> claims, String propert
for(Snak s : sg.getSnaks()) {
String value = getIdFromLink(s.getPropertyId().toString());
if(value.equals(PROPERTY_TO_CHECK)) {
if(s.getValue()==null)
return processRightAnswer(st);
valid = false;
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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;
Expand Down Expand Up @@ -49,6 +50,24 @@ public List<String> getWrongAnswers(String rightAnswer) {
for(String s : original) {
result.add(String.valueOf((int) Float.parseFloat(s)));
}
return ensureAllAnswersAreDiferent(result);
}

private List<String> ensureAllAnswersAreDiferent(List<String> answers){
List<String> result = new ArrayList<>();
for(String answer : answers) {
if(!result.contains(answer))
result.add(answer);
else {
Random rn = new Random();
while(result.contains(answer)) {
int realAnswer = Integer.parseInt(answer);
int half = realAnswer /2;
int newValue = rn.nextInt(half, realAnswer+half);
answer = String.valueOf(newValue);
}
}
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import main.java.questionGenerator.question.answers.formatAnswers.AddUnitsFormater;
import main.java.questionGenerator.question.answers.formatAnswers.EmbellishNumbersFormater;
import main.java.questionGenerator.question.answers.formatAnswers.RemoveEFromNumber;
import main.java.questionGenerator.question.answers.formatAnswers.SetAmountOfDecimals;

public class SizeGenerator extends AnswersAreNotEntites {

Expand Down Expand Up @@ -45,7 +46,7 @@ private String getRightAnswerEntity(String url) {
@Override
public List<String> decorateAnswers(List<String> answers) {
AnswerFormater formater = new RemoveEFromNumber(new EmbellishNumbersFormater(
new AddUnitsFormater(null, "km^2")));
new SetAmountOfDecimals(new AddUnitsFormater(null, "km^2"), 2)));
return formater.format(answers);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main.java.questionGenerator.question.answers.formatAnswers;

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

import main.java.questionGenerator.question.answers.AbstractFormater;
import main.java.questionGenerator.question.answers.AnswerFormater;

public class SetAmountOfDecimals extends AbstractFormater {

private int amount;

public SetAmountOfDecimals(AnswerFormater formater, int amount) {
super(formater);
this.amount = amount;
}

@Override
public List<String> format(List<String> answers) {
List<String> result = new ArrayList<>();
for(String answer : answers) {
String[] splitted = split(answer);
if(splitted.length==0)
result.add(answer);
else {
String decimalPart = formatDecimalPart(splitted[1]);
result.add(splitted[0] + '.' + decimalPart);
}
}
return end(result);
}

private String[] split(String answer) {
String[] result = {"", ""};
int position = 0;
for(char c : answer.toCharArray()) {
if(c=='.')
position = 1;
else
result[position] += c;
}
return result;
}

private String formatDecimalPart(String decimalPart) {
if(decimalPart.length()<amount) {
int diference = amount - decimalPart.length();
for(int i=0; i<diference; i++) {
decimalPart += "0";
}
}
else if(decimalPart.length()>amount) {
char[] aux = decimalPart.toCharArray();
decimalPart = "";
for(int i=0; i<amount; i++) {
decimalPart += aux[i];
}
}
return decimalPart;
}

}

0 comments on commit 67c897f

Please sign in to comment.