From a9ed5e4311f91b94414b2f1e2275586103993ad5 Mon Sep 17 00:00:00 2001 From: Santiago21112001 Date: Fri, 26 Apr 2024 22:31:56 +0200 Subject: [PATCH] Arreglado bug respuestas repetidas --- questionservice/question-service.js | 3 +- questionservice/question-service.test.js | 8 ++++- questionservice/wikiUtils/wikiQuery.js | 38 ++++++++++++++++++++++-- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/questionservice/question-service.js b/questionservice/question-service.js index 3f11511..292ba7e 100644 --- a/questionservice/question-service.js +++ b/questionservice/question-service.js @@ -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); diff --git a/questionservice/question-service.test.js b/questionservice/question-service.test.js index 9473b04..bd4f9e6 100644 --- a/questionservice/question-service.test.js +++ b/questionservice/question-service.test.js @@ -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 @@ -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; }) })); diff --git a/questionservice/wikiUtils/wikiQuery.js b/questionservice/wikiUtils/wikiQuery.js index 3049081..b004485 100644 --- a/questionservice/wikiUtils/wikiQuery.js +++ b/questionservice/wikiUtils/wikiQuery.js @@ -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)' @@ -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 @@ -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 } @@ -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 \ No newline at end of file