generated from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 2
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 #67 from Arquisoft/Question_Generation_General
Question generation general
- Loading branch information
Showing
10 changed files
with
755 additions
and
47 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
68 changes: 59 additions & 9 deletions
68
questionGenerator/src/main/java/questionGenerator/generator/RightAnswerIsEntity.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,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; | ||
} | ||
|
||
} |
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
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
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