Skip to content

Commit

Permalink
Merge pull request #33 from Arquisoft/question_generator
Browse files Browse the repository at this point in the history
Question generator
  • Loading branch information
UO287687 authored Feb 12, 2024
2 parents 92e5cbc + bcba025 commit ba8768b
Show file tree
Hide file tree
Showing 11 changed files with 1,698 additions and 0 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
134 changes: 134 additions & 0 deletions question_generator/cities/citiesQuestions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
const queryExecutor=require("../queryExecutor")
class CitiesQuestions{
#citiesQuestions=null;
static getInstance(){
if (!this.citiesQuestions) {
this.citiesQuestions = new CitiesQuestions();
}
return this.citiesQuestions;
}
constructor(){
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
}
LIMIT 1000
} AS %i
WHERE {
INCLUDE %i
OPTIONAL{
?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: []
};
}

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

}
}
async getRandomCities(numberOfCities){
await this.loadData();
const citiesArray = Object.values(this.cities);
const randomResults = citiesArray.sort(() => Math.random() - 0.5).slice(0, numberOfCities);
return randomResults
}
async getMostPopulatedCity(){
let numberOfCities=4
const results=await this.getRandomCities(numberOfCities);
const formattedResults = await results.map(result => {
return {
item: result.cityName,
value:parseFloat(result.population),
};
}).sort((a, b) => b.value - a.value);
const finalResults={
correct: null,
incorrects: []
}
for(let i=0;i<numberOfCities;i++){
if(i==0){
finalResults.correct=formattedResults[i].item
}
else{
finalResults.incorrects.push(formattedResults[i].item)
}
}
return finalResults
}
async getCityForCountry(){
let numberOfCities=4;
let result =(await this.getRandomCities(1))[0];
let country=result.country;

let correct = result.cityName;
let incorrects = []
let i=1;
while(i<numberOfCities){
let city=(await this.getRandomCities(1))[0];
if(city.country!=country){
incorrects.push(city.cityName);
i++;
}
}
return {
country:country,
correct:correct,
incorrects:incorrects
}
}
async getHigherCity(){
let numberOfCities=4;
let result =await this.getRandomCities(numberOfCities);
//Using first value in the array for elevation_above_sea_level
const formattedResults = await result.sort((a, b) => b.elevation_above_sea_level[0] - a.elevation_above_sea_level[0]);
const finalResults={
correct: null,
incorrects: []
}
for(let i=0;i<numberOfCities;i++){
if(i==0){
finalResults.correct=formattedResults[i].cityName
}
else{
finalResults.incorrects.push(formattedResults[i].cityName)
}
}
return finalResults;
}
}
module.exports=CitiesQuestions;
38 changes: 38 additions & 0 deletions question_generator/cities/citiesTemplates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const cities=require('./citiesQuestions');
const citiesQuery=cities.getInstance();
function loadData(){
citiesQuery.loadData();
}
const templates=[
async ()=>
{
const results= await citiesQuery.getMostPopulatedCity();
return{
"question":"Which city has more population?",
"correct":results.correct,
"incorrects":results.incorrects
}
},
async ()=>
{
const results= await citiesQuery.getCityForCountry();
return{
"question":"Which city is in "+results.country+"?",
"correct":results.correct,
"incorrects":results.incorrects
}
},
async ()=>
{
const results= await citiesQuery.getHigherCity();
return{
"question":"Which city is higher above sea level?",
"correct":results.correct,
"incorrects":results.incorrects
}
}


]
module.exports.getRandomQuestion = () => templates[Math.floor(Math.random()*templates.length)]();
module.exports.loadData = () =>loadData();
Loading

0 comments on commit ba8768b

Please sign in to comment.