Skip to content

Commit

Permalink
Merge branch 'develop' into multiplayer-bugs-fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
RicardoDiNu authored Apr 26, 2024
2 parents 183e050 + 88ccb95 commit 025fecd
Show file tree
Hide file tree
Showing 66 changed files with 1,544 additions and 648 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/uniovi/WiqEs04bApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class WiqEs04bApplication {
public static void main(String[] args) {
SpringApplication.run(WiqEs04bApplication.class, args);
Expand Down
24 changes: 0 additions & 24 deletions src/main/java/com/uniovi/components/MultipleQuestionGenerator.java

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.uniovi.components.generators;

import com.fasterxml.jackson.databind.JsonNode;
import com.uniovi.entities.Category;
import com.uniovi.entities.Question;
import org.springframework.stereotype.Component;
Expand All @@ -9,11 +10,7 @@

@Component
public interface QuestionGenerator {
List<Question> getQuestions(String language) throws IOException, InterruptedException;

String getQuery();
List<Question> getQuestions() throws InterruptedException, IOException;

Category getCategory();


List<Question> getQuestions(String language, JsonNode question, Category cat) throws IOException, InterruptedException;
}
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;
}
}

This file was deleted.

Loading

0 comments on commit 025fecd

Please sign in to comment.