Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question generator #33

Merged
merged 7 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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