Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question generation general #103

Merged
merged 71 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
3d98d3f
Started the answers decoration
UO289845 Apr 8, 2024
0c789b8
Added the head of government question type
UO289845 Apr 14, 2024
8672521
Added the persistent main
UO289845 Apr 14, 2024
9a4b1d4
Added the code to remove entites from the collection before adding th…
UO289845 Apr 14, 2024
289d78b
Modified the main to comply with the new deisgn
UO289845 Apr 15, 2024
504648f
Included the decorator classes and interface
UO289845 Apr 15, 2024
f1eba34
Removed use of previous decorator attempt
UO289845 Apr 15, 2024
56cd284
The generator now uses the correct decorator for the answers
UO289845 Apr 15, 2024
dd87dbe
Merge branch 'master' into Question_Generation_General
UO289845 Apr 17, 2024
e7389da
Fixed the problems with the merges
UO289845 Apr 17, 2024
b112212
Fixed the duplicate code for generating number distractors
UO289845 Apr 17, 2024
25435dd
Added the movie director generator
UO289845 Apr 17, 2024
fbb89a2
Improved the messages for the director questions
UO289845 Apr 17, 2024
d338c84
Modified the answers are entites hierarchy
UO289845 Apr 17, 2024
9ed9719
Fixed small code repetition
UO289845 Apr 17, 2024
6229686
Ensured that the director generator only generated film directors que…
UO289845 Apr 17, 2024
fa035a9
Added an interface to the generator hierarchy
UO289845 Apr 18, 2024
9238de6
Improved the generator hierarchy usage
UO289845 Apr 18, 2024
2aef92a
Fixed the problem with the director generator
UO289845 Apr 18, 2024
e807bf1
Removed old code remaining of the film director
UO289845 Apr 18, 2024
5281b9c
Improved the way of collecting the properties for generating entites
UO289845 Apr 18, 2024
b59fe08
Aumented the number of entites generated to reduce the number of errors
UO289845 Apr 18, 2024
d65ca31
Restructured the generator package hierarchy
UO289845 Apr 18, 2024
bc4df3b
Added the Director question type to the persistent main
UO289845 Apr 18, 2024
2f4e5d8
Added extra parameter to the getRightAnswer method
UO289845 Apr 18, 2024
b5f6596
The new parameter is now properly used
UO289845 Apr 18, 2024
31ec0e3
Added the videogame developer generator
UO289845 Apr 18, 2024
55f709e
Removed commented code
UO289845 Apr 18, 2024
a1985fa
Improved the questions for the videogame developer
UO289845 Apr 18, 2024
f9418a1
Added the videogame publisher generator
UO289845 Apr 18, 2024
c414f65
Improved the questions of the videogames generators
UO289845 Apr 18, 2024
463ec80
Added the videogame genre generator
UO289845 Apr 18, 2024
636661b
The persistent main now can generate questions for the videogames types
UO289845 Apr 18, 2024
75f6977
Improved the persistent main
UO289845 Apr 18, 2024
e412d51
Introduced the transaction logic in the persistent main and repository
UO289845 Apr 18, 2024
fa5d1e7
Added the new Videogame country generator
UO289845 Apr 19, 2024
bcd23c5
Added the videogame country to the persistent main
UO289845 Apr 19, 2024
97edb71
The Question Generator is now a singleton
UO289845 Apr 19, 2024
b646455
Parametrized the number of entites generated
UO289845 Apr 19, 2024
16146c0
Improved the question message in the videogame genre question
UO289845 Apr 19, 2024
248b029
Improved the usage of the decorator pattern
UO289845 Apr 19, 2024
3dbe449
Removed redundancies in the messages
UO289845 Apr 19, 2024
f5f8a0e
The specific generators now work as singletons
UO289845 Apr 19, 2024
859cc6f
Created the head of state test file and added a few test cases
UO289845 Apr 19, 2024
e4f9e20
Improved the tests for the head of state generator
UO289845 Apr 20, 2024
1795b9a
The test now run in order
UO289845 Apr 20, 2024
5e58811
Added tests for the director generator
UO289845 Apr 20, 2024
4763bf7
The order of the tests of the director generator now works as intended
UO289845 Apr 20, 2024
637459a
Added the test of the videogame developer generator
UO289845 Apr 20, 2024
fa786df
Added new test cases for the director and head of government tests
UO289845 Apr 20, 2024
dc2bd7e
Fixed the name of the head of government test
UO289845 Apr 20, 2024
235e3b8
Fixed the problem in the videogame developer generator test name
UO289845 Apr 20, 2024
549c267
Added test for the videogame publisher generator
UO289845 Apr 20, 2024
4435dd3
Added tests for the videogame genre generator
UO289845 Apr 20, 2024
f884e11
Added test for the videogame country generator
UO289845 Apr 20, 2024
fe2438f
Merge pull request #111 from Arquisoft/Question_Generator_Tests
uo289267 Apr 20, 2024
8ace842
new file before translation
ErdemYabaci Apr 20, 2024
3a78d92
turkish translation for the questions
ErdemYabaci Apr 20, 2024
029327e
better translation for: king->president, makers->developers
ErdemYabaci Apr 20, 2024
6cdfaaf
added turkish to the abstractgenerator
ErdemYabaci Apr 20, 2024
0a813d1
renaming of turkish messages file
ErdemYabaci Apr 20, 2024
81634ed
removal of special characters
ErdemYabaci Apr 20, 2024
2cb63eb
Added tests for the capital generator
UO289845 Apr 20, 2024
9ae1b4c
Added tests for the population generator
UO289845 Apr 20, 2024
dcecd2c
Added tests for the language generator
UO289845 Apr 20, 2024
0619860
Added tests for the size generator
UO289845 Apr 20, 2024
3fdd2e9
Improved the test case that checked that the question was as expected
UO289845 Apr 20, 2024
6b16f91
adding turkish to persistentmain.java
ErdemYabaci Apr 21, 2024
bad36e2
removal of unused commented code
ErdemYabaci Apr 21, 2024
30f56e9
Updated the main naming nomenclature
UO289845 Apr 22, 2024
867fc58
Removed transaction idea
Mister-Mario Apr 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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