Skip to content

Commit

Permalink
Merge pull request #103 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 25, 2024
2 parents 680c2d0 + 867fc58 commit fa773cd
Show file tree
Hide file tree
Showing 50 changed files with 2,058 additions and 328 deletions.
78 changes: 48 additions & 30 deletions questionGenerator/src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package main.java;

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

import main.java.questionGenerator.QuestionGenerator;
Expand All @@ -8,37 +9,54 @@
import main.java.questionGenerator.repository.QuestionRepository;

public class Main {

private static QuestionGenerator qg = QuestionGenerator.getInstance();

private static final long TIME_SKIP = 18000000; //5 hours
// private static final long TIME_SKIP = 43200000; //12 hours
//private static final long TIME_SKIP = 1000; //1 minute


private static String[] languages = {"en", "es", "tr"};

private static QuestionType[] types = {QuestionType.CAPITAL, QuestionType.LANGUAGE, QuestionType.POPULATION,
QuestionType.SIZE, QuestionType.HEAD_OF_GOVERMENT};
//, QuestionType.VIDEOGAME_DEVELOPER, QuestionType.VIDEOGAME_PUBLISHER, QuestionType.VIDEOGAME_GENRE, QuestionType.VIDEOGAME_COUNTRY};

public static void main(String[] args) {
QuestionGenerator qgEn = new QuestionGenerator("en");

run(qgEn, QuestionType.CAPITAL, 3);
System.out.println();

run(qgEn, QuestionType.LANGUAGE, 3);
System.out.println();

run(qgEn, QuestionType.POPULATION, 3);
System.out.println();

run(qgEn, QuestionType.SIZE, 3);

QuestionGenerator qgEs = new QuestionGenerator("es");

run(qgEs, QuestionType.CAPITAL, 3);
System.out.println();

run(qgEs, QuestionType.LANGUAGE, 3);
System.out.println();

run(qgEs, QuestionType.POPULATION, 3);
System.out.println();

run(qgEs, QuestionType.SIZE, 3);
//private static final int NUMBER_OF_QUESTIONS = 50;
private static final int NUMBER_OF_QUESTIONS = 100;
//private static final int NUMBER_OF_QUESTIONS = 3;
//private static final int NUMBER_OF_QUESTIONS = 1;

public static void main(String[] args) {
while(true) {
List<String> questions = generate().stream().map(q -> q.getJSON().toString()).toList();
QuestionRepository.getInstance().populate(questions);
try {
Thread.sleep(TIME_SKIP);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

private static List<Question> generate() {
List<Question> questions = new ArrayList<Question>();
for(String lang : languages) {
qg.setLanguageCode(lang);
for(QuestionType type: types)
questions.addAll(run(qg, type, NUMBER_OF_QUESTIONS));
}
return questions;
}

private static void run(QuestionGenerator qg, QuestionType type, int numberOfQuestions){
List<Question> questions = qg.generateQuestions(type, numberOfQuestions);
QuestionRepository.getInstance().insert(questions.stream().map(q -> q.getJSON().toString()).toList());
private static List<Question> run(QuestionGenerator qg, QuestionType type, int numberOfQuestions) {
List<Question> questions = qg.generateQuestions(type, numberOfQuestions);
for(int i=0; i<questions.size(); i++) {
Question question = questions.get(i);
question.setNumber(i);
}
return questions;
}
}

}
38 changes: 38 additions & 0 deletions questionGenerator/src/main/java/StaticMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main.java;

import java.util.List;

import main.java.questionGenerator.QuestionGenerator;
import main.java.questionGenerator.question.Question;
import main.java.questionGenerator.question.QuestionType;
import main.java.questionGenerator.repository.QuestionRepository;

public class StaticMain {

public static void main(String[] args) {
QuestionGenerator qg = QuestionGenerator.getInstance();

run(qg, QuestionType.CAPITAL, 3);
run(qg, QuestionType.LANGUAGE, 3);
run(qg, QuestionType.POPULATION, 3);
run(qg, QuestionType.SIZE, 3);
run(qg, QuestionType.HEAD_OF_GOVERMENT, 3);
run(qg, QuestionType.DIRECTOR, 3);
run(qg, QuestionType.VIDEOGAME_DEVELOPER, 3);
run(qg, QuestionType.VIDEOGAME_PUBLISHER, 3);
run(qg, QuestionType.VIDEOGAME_GENRE, 3);
run(qg, QuestionType.VIDEOGAME_COUNTRY, 3);

}

private static void run(QuestionGenerator qg, QuestionType type, int numberOfQuestions){
List<Question> questions = qg.generateQuestions(type, numberOfQuestions);
for(int i=0; i<questions.size(); i++) {
Question question = questions.get(i);
question.setNumber(i);
System.out.println(question.getJSON().toString());
}
System.out.println();
QuestionRepository.getInstance().insert(questions.stream().map(q -> q.getJSON().toString()).toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,46 @@
import java.util.Random;

import main.java.questionGenerator.entityGenerator.EntityGenerator;
import main.java.questionGenerator.generator.AbstractGenerator;
import main.java.questionGenerator.generator.specificGenerators.CapitalGenerator;
import main.java.questionGenerator.generator.specificGenerators.LanguageGenerator;
import main.java.questionGenerator.generator.specificGenerators.PopulationGenerator;
import main.java.questionGenerator.generator.specificGenerators.SizeGenerator;
import main.java.questionGenerator.generator.Generator;
import main.java.questionGenerator.generator.answersAreEntites.withSubProperties.CapitalGenerator;
import main.java.questionGenerator.generator.answersAreEntites.withSubProperties.HeadOfGovernmentGenerator;
import main.java.questionGenerator.generator.answersAreEntites.withSubProperties.LanguageGenerator;
import main.java.questionGenerator.generator.answersAreEntites.withSubProperties.videogames.VideogameCountryGenerator;
import main.java.questionGenerator.generator.answersAreEntites.withSubProperties.videogames.VideogameDeveloperGenerator;
import main.java.questionGenerator.generator.answersAreEntites.withSubProperties.videogames.VideogameGenreGenerator;
import main.java.questionGenerator.generator.answersAreEntites.withSubProperties.videogames.VideogamePublisherGenerator;
import main.java.questionGenerator.generator.answersAreEntites.withoutSubProperties.DirectorGenerator;
import main.java.questionGenerator.generator.answersAreNotEntites.PopulationGenerator;
import main.java.questionGenerator.generator.answersAreNotEntites.SizeGenerator;
import main.java.questionGenerator.question.Question;
import main.java.questionGenerator.question.QuestionType;

public class QuestionGenerator {

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

public QuestionGenerator(String languageCode){
this.languageCode = languageCode;
private static QuestionGenerator questionGenerator;

public static QuestionGenerator getInstance() {
if(questionGenerator==null)
questionGenerator = new QuestionGenerator();
return questionGenerator;
}

private QuestionGenerator() {

}

public List<Question> generateQuestions(QuestionType type, int amount){
setGenerator(type);
generator.setLocalization(languageCode);
long sampleSize = getSampleSize(amount);
generator.setSampleSize(sampleSize);
List<Question> questions = new ArrayList<>();
List<String> entites = new ArrayList<>();
try {
entites = EntityGenerator.getEntities(type, 100);
entites = EntityGenerator.getEntities(type, sampleSize, generator.getPropertyId());
} catch (IOException e) {
e.printStackTrace();
}
Expand All @@ -43,6 +59,13 @@ public List<Question> generateQuestions(QuestionType type, int amount){
try {
q = generator.generate(entity);
questions.add(q);
} catch(RuntimeException e) {
/*
* Sometimes not all the parameters for generating can be passed in the query, so this
* acts as failsave to avoid generating undesired questions, while not overloading the
* screen with error messages if not wanted
*/
//System.err.println(e.getMessage());
} catch (Exception e) {
//If there's any problem generating the question we jump to the next one
System.err.println(e.getMessage());
Expand All @@ -55,23 +78,61 @@ public List<Question> generateQuestions(QuestionType type, int amount){
private void setGenerator(QuestionType type) {
switch (type) {
case POPULATION: {
generator = new PopulationGenerator();
generator = PopulationGenerator.getInstance();
break;
}
case CAPITAL: {
generator = new CapitalGenerator();
generator = CapitalGenerator.getInstance();
break;

}
case SIZE: {
generator = new SizeGenerator();
generator = SizeGenerator.getInstance();
break;
}
case LANGUAGE: {
generator = new LanguageGenerator();
generator = LanguageGenerator.getInstance();
break;
}
case HEAD_OF_GOVERMENT: {
generator = HeadOfGovernmentGenerator.getInstance();
break;
}
case DIRECTOR: {
generator = DirectorGenerator.getInstance();
break;
}
case VIDEOGAME_DEVELOPER: {
generator = VideogameDeveloperGenerator.getInstance();
break;
}
case VIDEOGAME_PUBLISHER: {
generator = VideogamePublisherGenerator.getInstance();
break;
}
case VIDEOGAME_GENRE: {
generator = VideogameGenreGenerator.getInstance();
break;
}
case VIDEOGAME_COUNTRY: {
generator = VideogameCountryGenerator.getInstance();
break;
}
}
}

public String getLanguageCode() {
return languageCode;
}

public void setLanguageCode(String languageCode) {
this.languageCode = languageCode;
}

private long getSampleSize(int amount) {
long value = amount * 10;
if(value<1000)
return 1000;
return value;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ public class EntityGenerator {
private static final String PRE_URL = "https://www.wikidata.org/w/api.php?action=query&format=json&"
+ "list=backlinks&formatversion=2&";

private static String property = "";
private static final String PROPERTY_URL = "bltitle=Property%3AP";
private static final String PROPERTY_URL = "bltitle=Property%3A";

private static final String POST_URL = "&blnamespace=0&";

private static final String LIMIT = "bllimit=";

public static List<String> getEntities(QuestionType type, int size) throws IOException{
propertyFactory(type);
public static List<String> getEntities(QuestionType type, long size, String property) throws IOException{
URL url = new URL(PRE_URL+PROPERTY_URL+property+POST_URL+LIMIT+size);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
Expand All @@ -44,23 +42,6 @@ public static List<String> getEntities(QuestionType type, int size) throws IOExc
return result;
}

private static void propertyFactory(QuestionType type) {
switch (type) {
case CAPITAL:
property = "36";
break;
case LANGUAGE:
property = "37";
break;
case POPULATION:
property = "1082";
break;
case SIZE:
property = "2046";
break;
}
}

private static List<String> extractEntities(String json){
List<String> result = new ArrayList<>();
String[] entities = json.split("\"title\":");
Expand Down
Loading

0 comments on commit fa773cd

Please sign in to comment.