Skip to content

Commit

Permalink
Arreglado bug respuestas repetidas
Browse files Browse the repository at this point in the history
  • Loading branch information
Santiago21112001 committed Apr 26, 2024
1 parent 0c494a5 commit a9ed5e4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
3 changes: 2 additions & 1 deletion questionservice/question-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ function selectRandomTemplatesKeys(templateKeys, sampleSize) {
}

async function generateQuestions() {
let backupAnswers = await WikiQuery.getBackupAnswers();
const templateKeys = Object.keys(templates);
const randomTemplateKeys = selectRandomTemplatesKeys(templateKeys, 5);
const randomTemplates = randomTemplateKeys.map(key => templates[key]);

let newQuestions = [];
for (let template of randomTemplates) {
let wikiQuestions = await WikiQuery.getQuestions(template, 20)
let wikiQuestions = await WikiQuery.getQuestions(template, 20, backupAnswers.slice())
newQuestions.push(...wikiQuestions);
}
await Question.insertMany(newQuestions);
Expand Down
8 changes: 7 additions & 1 deletion questionservice/question-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const Question = require('./question-model');

// Importamos y mockeamos el módulo wikiQuery.js
jest.mock('./wikiUtils/wikiQuery.js', () => ({
getQuestions: jest.fn().mockImplementation(async (template, count) => {
getQuestions: jest.fn().mockImplementation(async (template, count, backupAnswers) => {
const questions = [];

// Simula generar documentos de pregunta
Expand All @@ -22,6 +22,12 @@ jest.mock('./wikiUtils/wikiQuery.js', () => ({
}

return questions;
}),
getBackupAnswers: jest.fn().mockImplementation(async () => {
let backupAnswers = [{ "itemLabel": "Morris the Cat" },
{ "itemLabel": "Sockington" },
{ "itemLabel": "Misuke" }]
return backupAnswers;
})
}));

Expand Down
38 changes: 36 additions & 2 deletions questionservice/wikiUtils/wikiQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ class WikiQuery {
* Obtiene preguntas de wikidata a partir de una plantilla.
* @param {Object} template - La plantilla sacada de templates.json.
* @param {Number} limitValue - El límite de preguntas a obtener (número entero). Debe ser mayor a 3.
* @returns preguntas con el formato de api_questionservice.txt
* @returns preguntas con el formato de question-model
*/
static async getQuestions(template, limitValue) {
static async getQuestions(template, limitValue, backupAnswers) {
let backupAnswerIndex = 0;
let queryAnswerVar = '?answerLabel'
if (template.year===true){
queryAnswerVar = '(YEAR(?answer) AS ?answerLabel)'
Expand All @@ -33,6 +34,7 @@ class WikiQuery {
if (answerVar.startsWith('http')) {
answerVar = 'No hay';
}

let answers = [{ answer: answerVar, correct: true }];
let copy_results = results.slice(); // copia para no modificar la lista original
copy_results.splice(i, 1); // Eliminar la fila que lleva la pregunta correcta
Expand All @@ -42,6 +44,21 @@ class WikiQuery {
if (distractorAnswerVar.startsWith('http')) {
distractorAnswerVar = 'No hay';
}

// Comprobar si es una respuesta repetida
let repeated = answers.some(function(a) {
return a.answer === distractorAnswerVar;
});
// Si lo es, cambiarla por una de las de resguardo
if (repeated) {
distractorAnswerVar = backupAnswers[backupAnswerIndex]['itemLabel'].value;
backupAnswerIndex++;
// Reiniciar indice de la lista de resguardo para que no se salga
if (backupAnswerIndex >= backupAnswers.length) {
backupAnswerIndex = 0;
}
}

answers.push({ answer: distractorAnswerVar, correct: false })
copy_results.splice(randomIndex, 1); // Eliminar la fila elegida para que no vuelva a salir
}
Expand All @@ -64,6 +81,23 @@ class WikiQuery {
}
return array;
}

/**
*
* @returns una lista de 100 objetos con una propiedad 'itemLabel'
*/
static async getBackupAnswers() {
// Nombres de gatos
const query = `SELECT distinct ?itemLabel
WHERE
{
?item wdt:P31 wd:Q146.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
LIMIT 100`
const results = await wikiCall(query)
return results
}
}

module.exports = WikiQuery

0 comments on commit a9ed5e4

Please sign in to comment.