Skip to content

Commit

Permalink
Merge branch 'Question_Generation_General' into 69-making-the-webapp-…
Browse files Browse the repository at this point in the history
…use-the-record-service-through-the-gateway-service
  • Loading branch information
Mister-Mario committed Apr 7, 2024
2 parents 579bb79 + a30e334 commit 1af13d5
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 204 deletions.
51 changes: 0 additions & 51 deletions questionGenerator/src/main/java/MainMock.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import main.java.questionGenerator.question.QuestionType;

public class QuestionGenerator {

private AbstractGenerator generator;
private AbstractGenerator generator;
private String languageCode;

public QuestionGenerator(String languageCode){
Expand All @@ -33,19 +33,21 @@ public List<Question> generateQuestions(QuestionType type, int amount){
} catch (IOException e) {
e.printStackTrace();
}
Random rnd = new Random();
List<String> chosen = new ArrayList<>();
Random rnd = new Random();
int size = entites.size();
int number = 0;
while(number<amount) {
while(questions.size()<amount && size>0) {
int index = rnd.nextInt(size);
String entity = entites.get(index);
if(!chosen.contains(entity)) {
chosen.add(entity);
Question q = generator.generate(entity);
entites.remove(index);
Question q = null;
try {
q = generator.generate(entity);
questions.add(q);
number++;
} catch (Exception e) {
//If there's any problem generating the question we jump to the next one
System.err.println(e.getMessage());
}
size = entites.size();
}
return questions;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public abstract class AbstractGenerator {
private QuestionType type;

private static final String MESSAGES_PATH = "messages";

private String message;

public AbstractGenerator(String propertyId, QuestionType type, String message) {
Expand All @@ -43,8 +43,9 @@ public AbstractGenerator(String propertyId, QuestionType type, String message) {
*
* @param id
* @return question generated or null if wikidata gives an error
* @throws Exception
*/
public Question generate(String id) {
public Question generate(String id) throws Exception {
//get the wikidata entity using the id
ItemDocumentImpl idi = alreadyProcessedEntities.get(id);

Expand Down Expand Up @@ -86,20 +87,19 @@ protected String getName(Map<String, MonolingualTextValue> names) {
return mtv.getText();
}

// protected abstract String getQuestion(String name);
protected abstract String getRightAnswer(Map<String, List<Statement>> claims) throws Exception;
protected abstract List<String> getWrongAnswers(String rightAnswer) throws Exception;

protected String getQuestion(String name) {
String q = getMessages().getString(message);
return String.format(q, name);
}

protected abstract String getRightAnswer(Map<String, List<Statement>> claims);
protected abstract List<String> getWrongAnswers(String rightAnswer);


public String getPropertyId() {
return propertyId;
}

public static ItemDocumentImpl getAlreadyProcessedEntity(String id) {
return alreadyProcessedEntities.get(id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
import main.java.questionGenerator.question.QuestionType;

public abstract class AnswersAreEntities extends AbstractGenerator {

private final String PROPERTY_TO_CHECK;
private final QuestionType type;
private QuestionType type;

public AnswersAreEntities(String propertyId, QuestionType type, String propertyToCheck, String message) {
super(propertyId, type, message);
Expand All @@ -27,7 +27,11 @@ public AnswersAreEntities(String propertyId, QuestionType type, String propertyT
}

@Override
protected String getRightAnswer(Map<String, List<Statement>> claims) {
protected String getRightAnswer(Map<String, List<Statement>> claims) throws Exception {
if(claims.get(super.getPropertyId())==null) {
throw new Exception("Claims does not have the property " + super.getPropertyId());
}

for(Statement st : claims.get(super.getPropertyId())) {
boolean valid = true;
for(SnakGroup sg : st.getQualifiers()) {
Expand All @@ -48,7 +52,7 @@ protected String getRightAnswer(Map<String, List<Statement>> claims) {
return null;
}

protected String processRightAnswer(Statement st) {
private String processRightAnswer(Statement st) {
String entity = getIdFromLink(st.getValue().toString());
String answer = "";
try {
Expand All @@ -66,7 +70,7 @@ protected String processRightAnswer(Statement st) {
return answer;
}

protected String getAnswer(String id){
private String getAnswer(String id) throws Exception{
ItemDocumentImpl idi = getAlreadyProcessedEntity(id);

if(idi==null) {
Expand All @@ -93,7 +97,7 @@ protected String getIdFromLink(String url) {
}

@Override
protected List<String> getWrongAnswers(String rightAnswer) {
protected List<String> getWrongAnswers(String rightAnswer) throws Exception {
List<String> entites = new ArrayList<>();
try {
entites = EntityGenerator.getEntities(type, 100);
Expand All @@ -102,15 +106,13 @@ protected List<String> getWrongAnswers(String rightAnswer) {
}
Random rnd = new Random();
List<String> result = new ArrayList<>();
List<Integer> used = new ArrayList<>();
for(int i = 0; i < 3; i++){
int rndnum = rnd.nextInt(entites.size());
String wrong = getAnswer(entites.get(rndnum));
if(wrong == null || wrong.equals(rightAnswer) || used.contains(rndnum))
if(wrong == null || wrong.equals(rightAnswer) || result.contains(wrong))
i--;
else{
result.add(wrong);
used.add(rndnum);
}
}
return result;
Expand Down

0 comments on commit 1af13d5

Please sign in to comment.