Skip to content

Commit

Permalink
Merge pull request #70 from Arquisoft/Question_Generation_General
Browse files Browse the repository at this point in the history
Updated question generator
  • Loading branch information
Mister-Mario authored Apr 7, 2024
2 parents 0259ad4 + 904d0dd commit ee85720
Show file tree
Hide file tree
Showing 14 changed files with 472 additions and 217 deletions.
14 changes: 14 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
{
"configurations": [
{
"type": "java",
"name": "MainMock",
"request": "launch",
"mainClass": "main.java.MainMock",
"projectName": "questionGenerator"
},
{
"type": "java",
"name": "MainRandomizer",
"request": "launch",
"mainClass": "main.java.MainRandomizer",
"projectName": "questionGenerator"
},
{
"type": "java",
"name": "Main",
Expand Down
58 changes: 21 additions & 37 deletions questionGenerator/src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package main.java;

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

import main.java.questionGenerator.QuestionGenerator;
Expand All @@ -10,43 +9,28 @@

public class Main {

private static QuestionType[] types = {QuestionType.POPULATION, QuestionType.CAPITAL, QuestionType.SIZE,
QuestionType.LANGUAGE};

public static void main(String[] args) {
System.out.println("Ahora en Espa�ol");
QuestionGenerator qg = new QuestionGenerator("es");
for(QuestionType t : types) {
run(qg, t);
System.out.println();
}
System.out.println("Now English");
qg = new QuestionGenerator("en");
for(QuestionType t : types) {
run(qg, t);
System.out.println();
}
System.out.println("Now in english but with bad language code");
qg = new QuestionGenerator("ep");
for(QuestionType t : types) {
run(qg, t);
System.out.println();
}

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

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

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

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

run(qg, QuestionType.SIZE, 3);
}

private static void run(QuestionGenerator qg, QuestionType type) {
List<String> questionJSONList = new ArrayList<>();

//Populate JSON list here
for(int i=0; i<3; i++) {
Question question = qg.generateQuestion(type);
question.setNumber(i);
questionJSONList.add(question.getJSON().toString());
System.out.println(question.getJSON().toString());
}

QuestionRepository.getInstance().insert(questionJSONList);
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());
}
QuestionRepository.getInstance().insert(questions.stream().map(q -> q.getJSON().toString()).toList());
}

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

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

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

public class MainMock {

private static QuestionType[] types = {QuestionType.POPULATION, QuestionType.CAPITAL, QuestionType.SIZE,
QuestionType.LANGUAGE};

public static void main(String[] args) {
System.out.println("Ahora en Espa�ol");
QuestionGeneratorMock qg = new QuestionGeneratorMock("es");
for(QuestionType t : types) {
run(qg, t);
System.out.println();
}
System.out.println("Now English");
qg = new QuestionGeneratorMock("en");
for(QuestionType t : types) {
run(qg, t);
System.out.println();
}
System.out.println("Now in english but with bad language code");
qg = new QuestionGeneratorMock("ep");
for(QuestionType t : types) {
run(qg, t);
System.out.println();
}

}

private static void run(QuestionGeneratorMock qg, QuestionType type) {
List<String> questionJSONList = new ArrayList<>();

//Populate JSON list here
for(int i=0; i<3; i++) {
Question question = qg.generateQuestion(type);
question.setNumber(i);
questionJSONList.add(question.getJSON().toString());
System.out.println(question.getJSON().toString());
}

QuestionRepository.getInstance().insert(questionJSONList);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package main.java.questionGenerator;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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;
Expand All @@ -8,67 +14,62 @@
import main.java.questionGenerator.question.Question;
import main.java.questionGenerator.question.QuestionType;



public class QuestionGenerator {

private AbstractGenerator generator;
private String id;

private AbstractGenerator generator;
private String languageCode;

private static String[] POP_ENTITIES = {"Q14317", "Q12273", "Q14649"};
private static String[] CAP_ENTITIES = {"Q3934", "Q29", "Q43"};
private static String[] SIZE_ENTITIES = {"Q29", "Q12273", "Q3934"};
private static String[] LANG_ENTITIES = {"Q29", "Q43", "Q3934"};

public QuestionGenerator(String languageCode){
this.languageCode = languageCode;
}

public Question generateQuestion(QuestionType type) {
generatorFactory(type);
public List<Question> generateQuestions(QuestionType type, int amount){
setGenerator(type);
generator.setLocalization(languageCode);
return generator.generate(id);
}
List<Question> questions = new ArrayList<>();
List<String> entites = new ArrayList<>();
try {
entites = EntityGenerator.getEntities(type, 100);
} catch (IOException e) {
e.printStackTrace();
}
Random rnd = new Random();
List<String> chosen = new ArrayList<>();
int size = entites.size();
int number = 0;
while(number<amount) {
int index = rnd.nextInt(size);
String entity = entites.get(index);
if(!chosen.contains(entity)) {
chosen.add(entity);
Question q = generator.generate(entity);
questions.add(q);
number++;
}
}
return questions;
}

private void generatorFactory(QuestionType type) {
private void setGenerator(QuestionType type) {
switch (type) {
case POPULATION: {
id = POP_ENTITIES[0];
generator = new PopulationGenerator();
moveUp(POP_ENTITIES);
POP_ENTITIES[POP_ENTITIES.length-1] = id;
break;
}
case CAPITAL: {
id = CAP_ENTITIES[0];
generator = new CapitalGenerator();
moveUp(CAP_ENTITIES);
CAP_ENTITIES[CAP_ENTITIES.length-1] = id;
break;

}
case SIZE: {
id = SIZE_ENTITIES[0];
generator = new SizeGenerator();
moveUp(SIZE_ENTITIES);
SIZE_ENTITIES[SIZE_ENTITIES.length-1] = id;
break;
}
case LANGUAGE: {
id = LANG_ENTITIES[0];
generator = new LanguageGenerator();
moveUp(LANG_ENTITIES);
LANG_ENTITIES[LANG_ENTITIES.length-1] = id;
break;
}
}
}

private void moveUp(String[] list) {
for(int i=0; i<list.length-1; i++) {
list[i] = list[i+1];
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package main.java.questionGenerator;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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.question.Question;
import main.java.questionGenerator.question.QuestionType;

public class QuestionGeneratorMock {

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

private static String[] POP_ENTITIES = {"Q14317", "Q12273", "Q14649"};
private static String[] CAP_ENTITIES = {"Q3934", "Q29", "Q43"};
private static String[] SIZE_ENTITIES = {"Q29", "Q12273", "Q3934"};
private static String[] LANG_ENTITIES = {"Q29", "Q43", "Q3934"};

public QuestionGeneratorMock(String languageCode){
this.languageCode = languageCode;
}

public Question generateQuestion(QuestionType type) {
generatorFactory(type);
generator.setLocalization(languageCode);
return generator.generate(id);
}

public List<Question> generateQuestions(QuestionType type, int amount){
setGenerator(type);
generator.setLocalization(languageCode);
List<Question> questions = new ArrayList<>();
List<String> entites = new ArrayList<>();
try {
entites = EntityGenerator.getEntities(type, 100);
} catch (IOException e) {
e.printStackTrace();
}
Random rnd = new Random();
List<String> chosen = new ArrayList<>();
int size = entites.size();
int number = 0;
while(number<amount) {
int index = rnd.nextInt(size);
String entity = entites.get(index);
if(!chosen.contains(entity)) {
chosen.add(entity);
Question q = generator.generate(entity);
questions.add(q);
number++;
}
}
return questions;
}

private void setGenerator(QuestionType type) {
switch (type) {
case POPULATION: {
generator = new PopulationGenerator();
break;
}
case CAPITAL: {
generator = new CapitalGenerator();
break;

}
case SIZE: {
generator = new SizeGenerator();
break;
}
case LANGUAGE: {
generator = new LanguageGenerator();
break;
}
}
}

private void generatorFactory(QuestionType type) {
switch (type) {
case POPULATION: {
id = POP_ENTITIES[0];
generator = new PopulationGenerator();
moveUp(POP_ENTITIES);
POP_ENTITIES[POP_ENTITIES.length-1] = id;
break;
}
case CAPITAL: {
id = CAP_ENTITIES[0];
generator = new CapitalGenerator();
moveUp(CAP_ENTITIES);
CAP_ENTITIES[CAP_ENTITIES.length-1] = id;
break;

}
case SIZE: {
id = SIZE_ENTITIES[0];
generator = new SizeGenerator();
moveUp(SIZE_ENTITIES);
SIZE_ENTITIES[SIZE_ENTITIES.length-1] = id;
break;
}
case LANGUAGE: {
id = LANG_ENTITIES[0];
generator = new LanguageGenerator();
moveUp(LANG_ENTITIES);
LANG_ENTITIES[LANG_ENTITIES.length-1] = id;
break;
}
}
}

private void moveUp(String[] list) {
for(int i=0; i<list.length-1; i++) {
list[i] = list[i+1];
}
}

}
Loading

0 comments on commit ee85720

Please sign in to comment.