generated from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into multiplayer-bugs-fixing
- Loading branch information
Showing
66 changed files
with
1,544 additions
and
648 deletions.
There are no files selected for viewing
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
24 changes: 0 additions & 24 deletions
24
src/main/java/com/uniovi/components/MultipleQuestionGenerator.java
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
src/main/java/com/uniovi/components/QuestionGeneratorTestController.java
This file was deleted.
Oops, something went wrong.
87 changes: 0 additions & 87 deletions
87
src/main/java/com/uniovi/components/generators/AbstractQuestionGenerator.java
This file was deleted.
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
163 changes: 163 additions & 0 deletions
163
src/main/java/com/uniovi/components/generators/QuestionGeneratorV2.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 |
---|---|---|
@@ -0,0 +1,163 @@ | ||
package com.uniovi.components.generators; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.uniovi.dto.AnswerDto; | ||
import com.uniovi.dto.CategoryDto; | ||
import com.uniovi.dto.QuestionDto; | ||
import com.uniovi.entities.Answer; | ||
import com.uniovi.entities.Category; | ||
import com.uniovi.entities.Question; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.URLEncoder; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.nio.charset.StandardCharsets; | ||
import java.security.SecureRandom; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Random; | ||
|
||
public class QuestionGeneratorV2 implements QuestionGenerator{ | ||
|
||
private JsonNode jsonNode; | ||
private String language_placeholder; | ||
private String question_placeholder; | ||
private String answer_placeholder; | ||
private String language; | ||
|
||
private Random random = new SecureRandom(); | ||
|
||
public QuestionGeneratorV2(JsonNode jsonNode) { | ||
this.jsonNode = jsonNode; | ||
this.language_placeholder = jsonNode.get("language_placeholder").textValue(); | ||
this.question_placeholder = jsonNode.get("question_placeholder").textValue(); | ||
this.answer_placeholder = jsonNode.get("answer_placeholder").textValue(); | ||
} | ||
|
||
@Override | ||
public List<Question> getQuestions(String language) throws IOException, InterruptedException { | ||
this.language = language; | ||
List<Question> questions = new ArrayList<>(); | ||
JsonNode categories = jsonNode.findValue("categories"); | ||
for(JsonNode category : categories){ | ||
String categoryName = category.get("name").textValue(); | ||
Category cat = new Category(categoryName); | ||
JsonNode questionsNode = category.findValue("questions"); | ||
for(JsonNode question : questionsNode){ | ||
questions.addAll(this.generateQuestion(question, cat)); | ||
} | ||
} | ||
return questions; | ||
} | ||
|
||
@Override | ||
public List<Question> getQuestions(String language, JsonNode question, Category cat) throws IOException, InterruptedException { | ||
this.language = language; | ||
return this.generateQuestion(question, cat); | ||
} | ||
|
||
private List<Question> generateQuestion(JsonNode question, Category cat) throws IOException, InterruptedException { | ||
// Get the SPARQL query from the JSON | ||
String query = question.get("sparqlQuery").textValue(); | ||
|
||
// Get the question and answer words from the JSON | ||
String questionLabel = question.get("question").textValue(); | ||
String answerLabel= question.get("answer").textValue(); | ||
|
||
// Replace the placeholders in the query with the actual values | ||
query = query.replace(language_placeholder, language). | ||
replace(question_placeholder, questionLabel). | ||
replace(answer_placeholder, answerLabel); | ||
|
||
// Execute the query and get the results | ||
JsonNode results = getQueryResult(query); | ||
List<Question> questions = new ArrayList<>(); | ||
|
||
// Prepare the statement base based on the language | ||
String statement = this.prepareStatement(question); | ||
|
||
for (JsonNode result : results) { | ||
// Generate the correct answer | ||
String correctAnswer = result.path(answerLabel).path("value").asText(); | ||
Answer correct = new Answer(correctAnswer, true); | ||
|
||
// Generate the options | ||
List<Answer> options = this.generateOptions(results, correctAnswer, answerLabel); | ||
options.add(correct); | ||
|
||
if (statement != null) { | ||
// Generate the question statement | ||
String questionStatement = statement.replace(question_placeholder, result.path(questionLabel).path("value").asText()); | ||
|
||
// Generate the question | ||
Question q = new Question(questionStatement, options, correct, cat, language); | ||
|
||
// Add the question to the list | ||
questions.add(q); | ||
} | ||
} | ||
return questions; | ||
} | ||
|
||
private List<Answer> generateOptions(JsonNode results, String correctAnswer, String answerLabel) { | ||
List<Answer> options = new ArrayList<>(); | ||
List<String> usedOptions = new ArrayList<>(); | ||
int size = results.size(); | ||
int tries = 0; | ||
|
||
while (options.size() < 3 && tries < 10) { | ||
int random = (int) (this.random.nextFloat() * size); | ||
String option = results.get(random).path(answerLabel).path("value").asText(); | ||
if (!option.equals(correctAnswer) && !usedOptions.contains(option) ) { | ||
usedOptions.add(option); | ||
options.add(new Answer(option, false)); | ||
} | ||
tries++; | ||
} | ||
return options; | ||
} | ||
|
||
/** | ||
* Generates a statement based on the language of the question | ||
* @param question The question node | ||
* @return The statement in the language of the question or null if the language is not found | ||
*/ | ||
private String prepareStatement(JsonNode question) { | ||
JsonNode statementNode = question.findValue("statements"); | ||
for (JsonNode statement : statementNode) { | ||
if (statement.get("language").textValue().equals(language)) { | ||
return statement.get("statement").textValue(); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private JsonNode getQueryResult(String query) throws IOException, InterruptedException { | ||
|
||
System.out.println("Query: " + query); | ||
HttpClient client = HttpClient.newHttpClient(); | ||
JsonNode resultsNode; | ||
String endpointUrl = "https://query.wikidata.org/sparql?query=" + | ||
URLEncoder.encode(query, StandardCharsets.UTF_8) + | ||
"&format=json"; | ||
|
||
HttpRequest request = HttpRequest.newBuilder() | ||
.uri(URI.create(endpointUrl)) | ||
.header("Accept", "application/json") | ||
.build(); | ||
|
||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); | ||
|
||
// Process the JSON response using Jackson ObjectMapper | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
JsonNode jsonResponse = objectMapper.readTree(response.body()); | ||
|
||
// Access the data from the JSON response | ||
resultsNode = jsonResponse.path("results").path("bindings"); | ||
return resultsNode; | ||
} | ||
} |
17 changes: 0 additions & 17 deletions
17
src/main/java/com/uniovi/components/generators/geography/AbstractGeographyGenerator.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.