Skip to content

Commit

Permalink
Merge pull request #107 from Arquisoft/question_generator
Browse files Browse the repository at this point in the history
Automatic update of the question generation data
  • Loading branch information
uo289097 authored Mar 12, 2024
2 parents 64452aa + 97823ae commit 027d2a2
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 111 deletions.
88 changes: 45 additions & 43 deletions question_generator/geography/cities/citiesQuestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,58 +11,60 @@ class CitiesQuestions{
this.cities={};
}
async loadData(){
if (Object.keys(this.cities).length === 0) {//Se obtienen 100 ciudades relevantes
const query=`
SELECT ?city ?cityLabel ?population ?countryLabel ?elevation_above_sea_level
WITH{
SELECT ?city ?cityLabel
WHERE{
?city wdt:P31 wd:Q515
let newResults={};
const query=`
SELECT ?city ?cityLabel ?population ?countryLabel ?elevation_above_sea_level
WITH{
SELECT ?city ?cityLabel
WHERE{
?city wdt:P31 wd:Q515
}
LIMIT 1000
} AS %i
WHERE {
INCLUDE %i
OPTIONAL{
?city wdt:P1082 ?population.
?city wdt:P17 ?country.
?city wdt:P2044 ?elevation_above_sea_level
}
LIMIT 1000
} AS %i
WHERE {
INCLUDE %i
OPTIONAL{
FILTER EXISTS{
?city wdt:P1082 ?population.
?city wdt:P17 ?country.
?city wdt:P2044 ?elevation_above_sea_level
}
FILTER EXISTS{
?city wdt:P1082 ?population.
?city wdt:P17 ?country.
?city wdt:P2044 ?elevation_above_sea_level
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
ORDER BY DESC(?population)
LIMIT 100
`
let cities = await queryExecutor.execute(query);
cities.forEach(city => {
const cityId = city.city.value;
const cityName = city.cityLabel.value;
const population = city.population.value;
const country = city.countryLabel.value;
const elevationAboveSeaLevel = city.elevation_above_sea_level.value;

if (!this.cities[cityId]) {
this.cities[cityId] = {
cityId: cityId,
cityName: cityName,
population: population,
country: country,
elevation_above_sea_level: []
};
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
ORDER BY DESC(?population)
LIMIT 100
`
let cities = await queryExecutor.execute(query);
cities.forEach(city => {
const cityId = city.city.value;
const cityName = city.cityLabel.value;
const population = city.population.value;
const country = city.countryLabel.value;
const elevationAboveSeaLevel = city.elevation_above_sea_level.value;

this.cities[cityId].elevation_above_sea_level.push(parseFloat(elevationAboveSeaLevel));
});
if (!newResults[cityId]) {
newResults[cityId] = {
cityId: cityId,
cityName: cityName,
population: population,
country: country,
elevation_above_sea_level: []
};
}

}
newResults[cityId].elevation_above_sea_level.push(parseFloat(elevationAboveSeaLevel));
});
this.cities=newResults;

}
async getRandomCities(numberOfCities){
await this.loadData();
if(Object.keys(this.cities).length==0){
await this.loadData();
}
const citiesArray = Object.values(this.cities);
const randomResults = citiesArray.sort(() => Math.random() - 0.5).slice(0, numberOfCities);
return randomResults
Expand Down
14 changes: 4 additions & 10 deletions question_generator/locales/en.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
{

"Which city is higher above sea level?": "Which city is higher above sea level?",
"Which city has more population?": "Which city has more population?",
"Which city is in?": "Which city is in %s?",



"Which planet is bigger?": "Which planet is bigger?",




"Who has more Grand Slams?": "Who has more Grand Slams?"
"Which planet is bigger?": "Which planet is bigger?",
"Who has more Grand Slams?": "Who has more Grand Slams?",
"Who has more followers?": "Who has more followers?",
"Who has more looses?": "Who has more looses?"
}
42 changes: 22 additions & 20 deletions question_generator/planets/planetsQuestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,44 @@ class PlanetsQuestions{
this.planets={}
}
async loadData(){
if(Object.keys(this.planets).length==0){
const query= `
SELECT ?planet ?planetLabel (SAMPLE(?radius) AS ?radius)
WHERE {
?categ wdt:P361 wd:Q337768.
?planet wdt:P31 ?categ.
OPTIONAL {
?planet wdt:P2120 ?radius.
}
FILTER EXISTS {
?planet wdt:P2120 ?radius.
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
GROUP BY ?planet ?planetLabel
let newResults={};
const query= `
SELECT ?planet ?planetLabel (SAMPLE(?radius) AS ?radius)
WHERE {
?categ wdt:P361 wd:Q337768.
?planet wdt:P31 ?categ.
OPTIONAL {
?planet wdt:P2120 ?radius.
}
FILTER EXISTS {
?planet wdt:P2120 ?radius.
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
GROUP BY ?planet ?planetLabel
`;
const results=await queryExecutor.execute(query)
results.forEach(planet => {
const planetId = planet.planet.value;
const planetName = planet.planetLabel.value;
const radius = planet.radius.value;

if (!this.planets[planetId]) {
this.planets[planetId] = {
if (!newResults[planetId]) {
newResults[planetId] = {
planetId: planetId,
planetName: planetName,
radius: []
};
}

this.planets[planetId].radius.push(parseFloat(radius));
newResults[planetId].radius.push(parseFloat(radius));
});
}
this.planets=newResults;
}
async getRandomPlanets(number) {
await this.loadData();
if(Object.keys(this.planets).length==0){
await this.loadData();
}
const array = Object.values(this.planets);
const randomResults = array.sort(() => Math.random() - 0.5).slice(0, number);
return randomResults
Expand Down
7 changes: 6 additions & 1 deletion question_generator/questionGenerationService.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ app.get('/api/questions/create', async (req, res) => {
});


generalTemplate.loadData();
function loadData() {
generalTemplate.loadData();
}

// Ejecuta loadData cada hora (60 minutos * 60 segundos * 1000 milisegundos)
setInterval(loadData, 60 * 60 * 1000);

const server = app.listen(port, () => {
console.log(`Listening at http://localhost:${port}`);
Expand Down
2 changes: 2 additions & 0 deletions question_generator/questionGenerationService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ const request = require('supertest');
let app;

beforeAll(async () => {
jest.useFakeTimers();
app = require('./questionGenerationService');
});

afterAll(async () => {
app.close();
jest.useRealTimers();
});

describe('Question generation service', () => {
Expand Down
77 changes: 40 additions & 37 deletions question_generator/sports/tennis/tennisQuestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,48 +11,51 @@ class TennisQuestions{
this.players={}
}
async loadData(){
if (Object.keys(this.players).length === 0) {//Se obtienen 100 ciudades relevantes
const query=`
SELECT DISTINCT ?tenista ?tenistaLabel ?pais ?paisLabel ?victorias ?followers
WHERE {
?tenista wdt:P106 wd:Q10833314.
let newResults={};
const query=`
SELECT DISTINCT ?tenista ?tenistaLabel ?pais ?paisLabel ?victorias ?followers
WHERE {
?tenista wdt:P106 wd:Q10833314.
OPTIONAL { ?tenista wdt:P1532 ?pais. }
OPTIONAL { ?tenista wdt:P564 ?victorias. }
OPTIONAL { ?tenista wdt:P8687 ?followers. }
FILTER (BOUND(?victorias) && BOUND(?pais) && BOUND(?followers))
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
ORDER BY DESC(?followers)
LIMIT 200
`
let players = await queryExecutor.execute(query);
players.forEach(tenista => {
const playerId = tenista.tenista.value;
const playerName = tenista.tenistaLabel.value;
const followers = tenista.followers.value;
const country = tenista.paisLabel.value;
const record = tenista.victorias.value;
OPTIONAL { ?tenista wdt:P1532 ?pais. }
OPTIONAL { ?tenista wdt:P564 ?victorias. }
OPTIONAL { ?tenista wdt:P8687 ?followers. }
FILTER (BOUND(?victorias) && BOUND(?pais) && BOUND(?followers))
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
ORDER BY DESC(?followers)
LIMIT 200
`
let players = await queryExecutor.execute(query);
players.forEach(tenista => {
const playerId = tenista.tenista.value;
const playerName = tenista.tenistaLabel.value;
const followers = tenista.followers.value;
const country = tenista.paisLabel.value;
const record = tenista.victorias.value;

const recordAux = record ? record.split("-") : ['', ''];
const wins = recordAux[0];
const looses = recordAux[1];
const recordAux = record ? record.split("-") : ['', ''];
const wins = recordAux[0];
const looses = recordAux[1];

if (!this.players[playerId]) {
this.players[playerId] = {
playerId: playerId,
playerName: playerName,
followers: followers,
country: country,
wins: wins,
looses: looses
};
}
});
}
if (!newResults[playerId]) {
newResults[playerId] = {
playerId: playerId,
playerName: playerName,
followers: followers,
country: country,
wins: wins,
looses: looses
};
}
});
this.players=newResults;

}
async getRandomPlayers(numberOfPlayers){
await this.loadData();
if(Object.keys(this.players).length==0){
await this.loadData();
}
const array = Object.values(this.players);
const randomResults = array.sort(() => Math.random() - 0.5).slice(0, numberOfPlayers);
return randomResults
Expand Down

0 comments on commit 027d2a2

Please sign in to comment.