Skip to content

Commit

Permalink
Merge pull request #134 from Arquisoft/133-internacionalizar-los-dato…
Browse files Browse the repository at this point in the history
…s-recabados-de-wikidata

Datos internacionalizados
  • Loading branch information
CANCI0 authored Apr 23, 2024
2 parents d08a580 + dc23a2b commit d82787d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
4 changes: 2 additions & 2 deletions gatewayservice/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ paths:
name: tematica
schema:
type: string
enum: [paises, literatura, cine, arte, programacion]
enum: [paises, literatura, cine, arte, programacion, "futbolistas", "clubes", "baloncestistas", "politica", "videojuegos"]
description: Category of questions to retrieve
- in: query
name: n
Expand Down Expand Up @@ -468,7 +468,7 @@ paths:
properties:
tematicas:
type: string
enum: [paises, literatura, cine, arte, programacion]
enum: [paises, literatura, cine, arte, programacion, "futbolistas", "clubes", "baloncestistas", "politica", "videojuegos"]
n:
type: integer
locale:
Expand Down
50 changes: 31 additions & 19 deletions questionservice/questionGen/GenericGenerator.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
const axios = require("axios");

// El generador hace una query a Wikidata por idioma
const LANGUAGES = ["es", "en"];

class GenericGenerator {
constructor(entity, props, types, preguntas) {
this.data = {};
for (let i = 0; i < LANGUAGES.length; i++) {
this.data[LANGUAGES[i]] = {};
}

this.entity = entity;
this.props = props;
this.types = types;
Expand Down Expand Up @@ -62,40 +70,44 @@ class GenericGenerator {

// Función para realizar la consulta SPARQL y obtener los datos de Wikidata
async getData() {
const sparqlQuery = `
for (let i = 0; i < LANGUAGES.length; i++) {
const sparqlQuery = `
SELECT DISTINCT ?entityLabel ${this.#generateLabels(
this.props
).join(" ")}
WHERE {
?entity ${this.entity};
${this.#generateProps(this.props)} .
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],es" }
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],${
LANGUAGES[i]
}" }
}
LIMIT 10000
`;

const url = `https://query.wikidata.org/sparql?query=${encodeURIComponent(
sparqlQuery
)}&format=json`;

await axios
.get(url)
.then((response) => {
const data = response.data;
this.data = data.results.bindings.groupByEntity();
})
.catch((error) => {
console.error("Error fetching data: " + error.message);
});

var url = `https://query.wikidata.org/sparql?query=${encodeURIComponent(
sparqlQuery
)}&format=json`;

await axios
.get(url)
.then((response) => {
const data = response.data;
this.data[LANGUAGES[i]] = data.results.bindings.groupByEntity();
})
.catch((error) => {
console.error("Error fetching data: " + error.message);
});
}
}

generateRandomQuestion(locale) {
// Elegir aleatoriamente una entidad del array
var entidades = Object.keys(this.data);
var entidades = Object.keys(this.data[locale]);
const entidadLabel =
entidades[Math.floor(Math.random() * entidades.length)];

const entidad = this.data[entidadLabel];
const entidad = this.data[locale][entidadLabel];

// Elegir aleatoriamente una propiedad de la entidad para hacer la pregunta
const propiedades = this.propLabels;
Expand All @@ -121,7 +133,7 @@ class GenericGenerator {
while (questionObj.respuestas.length < 4) {
const otroPaisLabel =
entidades[Math.floor(Math.random() * entidades.length)];
const otroPais = this.data[otroPaisLabel];
const otroPais = this.data[locale][otroPaisLabel];
let prop = otroPais[propiedadPregunta][0];

// Si no está en las propiedades de la entidad de la pregunta
Expand Down

0 comments on commit d82787d

Please sign in to comment.