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

Templates added for planets and geography categories #126

Merged
merged 3 commits into from
Mar 29, 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
1 change: 0 additions & 1 deletion docs/images/deploymentDiagramWIQ.svg

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const queryExecutor=require("../../queryExecutor");
const QuestionsUtils = require("../../questions-utils");
class CAQuestions{
static getInstance(){
if (!this.questions) {
this.questions = new CAQuestions();
}
return this.questions;
}
constructor(){
this.data={};
}
async loadValues(){
let result={};
const queries=[
`
SELECT ?community ?communityLabel
WHERE {
?community wdt:P31 wd:Q10742;
SERVICE wikibase:label { bd:serviceParam wikibase:language "es". }
}
`

];
for(let i = 0; i <queries.length; i++) {
let query = queries[i];
let cas = await queryExecutor.execute(query);
cas.forEach(ca=>{
const caId = ca.community.value.match(/Q\d+/)[0];
const caName = ca.communityLabel.value;
if (!result[caId]) {
result[caId] = {
caId: caId,
name: caName,
}
}
});
}
return result;

}
async loadData(){
let newResults = await this.loadValues();
const propertiesToLoad=[
{
name:'capital',
id: 'P36'
},
{
name:'highest_point',
id: 'P610'
},
{
name:'population',
id: 'P1082'
},
{
name: 'area',
id: 'P2046'
},
{
name: 'head_of_government',
id: 'P6'
}
]
for(let i = 0; i <Object.keys(newResults).length; i++) {
let id = Object.keys(newResults)[i];
let r= await queryExecutor.executeQueryForEntityAndProperty(id, propertiesToLoad);
if(r.length>0){
for(let j=0;j<propertiesToLoad.length;j++){
if(r[0][propertiesToLoad[j].name]!==undefined){
newResults[id][propertiesToLoad[j].name] = r[0][propertiesToLoad[j].name].value;
}
}
}
}
this.data=newResults;

}
async doQuestion(property,nValues){
if(Object.keys(this.data).length==0){
await this.loadData();
}
return QuestionsUtils.getValuesFromDataAndProperty(this.data, property, nValues);
}
}
module.exports=CAQuestions;
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const instance=require('./autonomousCommunitiesSpainQuestions');
const Query=instance.getInstance();
function loadData(){
Query.loadData();
}
const templates=[
async ()=>
{
const results= await Query.doQuestion('area',4);
return{
"question":"Which autonomous community of Spain has an area of?",
"question_param":results.question_param,
"correct":results.correct,
"incorrects":results.incorrects
}
},
async ()=>
{
const results= await Query.doQuestion('population',4);
return{
"question":"Which autonomous community of Spain has a population of?",
"question_param":results.question_param,
"correct":results.correct,
"incorrects":results.incorrects
}
},
async ()=>
{
const results= await Query.doQuestion('capital',4);
return{
"question":"Which autonomous community of Spain has a capital called?",
"question_param":results.question_param,
"correct":results.correct,
"incorrects":results.incorrects
}
},
async ()=>
{
const results= await Query.doQuestion('head_of_government',4);
return{
"question":"Which autonomous community of Spain has a president called?",
"question_param":results.question_param,
"correct":results.correct,
"incorrects":results.incorrects
}
},
async ()=>
{
const results= await Query.doQuestion('highest_point',4);
return{
"question":"Which autonomous community of Spain has x as the highest point?",
"question_param":results.question_param,
"correct":results.correct,
"incorrects":results.incorrects
}
}
]
module.exports.getRandomQuestion = () => templates[Math.floor(Math.random()*templates.length)]();
module.exports.loadData = () =>loadData();
5 changes: 4 additions & 1 deletion question_generator/geography/geographyTemplate.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const citiesTemplate=require('./cities/citiesTemplates')
const autonomousCommunitiesSpainTemplate=require('./autonomous_communities_spain/autonomousCommunitiesSpainTemplates')
function loadData(){
citiesTemplate.loadData();
autonomousCommunitiesSpainTemplate.loadData();
}
const templates=[
citiesTemplate.getRandomQuestion
citiesTemplate.getRandomQuestion,
autonomousCommunitiesSpainTemplate.getRandomQuestion
]
module.exports.getRandomQuestion = () => templates[Math.floor(Math.random()*templates.length)]();
module.exports.loadData = () =>loadData();
24 changes: 20 additions & 4 deletions question_generator/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,31 @@
"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 city has an area of?": "Which city has an area od %s km2?",
"Which city has an area of?": "Which city has an area of %s km2?",
"Which city is governed by?": "Which city is governed by %s?",
"Which city is in the time zone?": "Which city is in the time zone %s?",

"Which autonomous community of Spain has an area of?": "Which autonomous community of Spain has an area of %s km2?",
"Which autonomous community of Spain has a population of?": "Which autonomous community of Spain has a population of %s?",
"Which autonomous community of Spain has a capital called?": "Which autonomous community of Spain has a capital called %s?",
"Which autonomous community of Spain has a president called?": "Which autonomous community of Spain has a president called %s?",
"Which autonomous community of Spain has x as the highest point?" : "Which autonomous community of Spain has %s as the highest point?",




"Which planet is bigger?": "Which planet is bigger?",
"Which planet has a mass of?": "Which planet has a mass of %s kg?",
"Which planet has a radius of?": "Which planet has a radius of %s km?",
"Which planet has an orbital period of?" : "Which planet has an orbital period of %s days?",
"Which planet has x as the lowest point?" : "Which planet has %s as the lowest point?",
"Which planet has x as the highest point?" : "Which planet has %s as the highest point?",
"Which planet has an area of x?" : "Which planet has an area of %s km2?",
"Which planet has a density of x?" : "Which planet has a density of %s kg/m3?",

"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?",
"Who has more wins?": "Who has more wins?",
"Who has more followers? (Tennis)": "Who has more followers? (Tennis)",
"Who has more looses? (Tennis)": "Who has more looses? (Tennis)",
"Who has more wins? (Tennis)": "Who has more wins? (Tennis)",
"Which tennis player is from?" : "Which tennis player is from %s?"
}
20 changes: 17 additions & 3 deletions question_generator/locales/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,26 @@
"Which city is governed by?": "¿Qué ciudad es gobernada por %s?",
"Which city is in the time zone?": "¿Qué ciudad está en la zona horaria %s?",

"Which autonomous community of Spain has an area of?": "¿Qué comunidad autónoma de España tiene un área de %s km2?",
"Which autonomous community of Spain has a population of?": "¿Qué comunidad autónoma de España tiene una población de %s?",
"Which autonomous community of Spain has a capital called?": "¿Qué comunidad autónoma de España tiene una capital llamada %s?",
"Which autonomous community of Spain has a president called?": "¿Qué comunidad autónoma de España tiene un presidente llamado %s?",
"Which autonomous community of Spain has x as the highest point?" : "¿Qué comunidad autónoma de España tiene %s como el punto más alto?",


"Which planet is bigger?": "¿Qué planeta es más grande?",
"Which planet has a mass of?": "¿Qué planeta tiene una masa de %s kg?",
"Which planet has a radius of?": "¿Qué planeta tiene un radio de %s km?",
"Which planet has an orbital period of?" : "¿Qué planeta tiene un período orbital de %s días?",
"Which planet has x as the lowest point?" : "¿Qué planeta tiene %s como el punto más bajo?",
"Which planet has x as the highest point?" : "¿Qué planeta tiene %s como el punto más alto?",
"Which planet has an area of x?" : "¿Qué planeta tiene un área de %s km2?",
"Which planet has a density of x?" : "¿Qué planeta tiene una densidad de %s kg/m3?",

"Who has more Grand Slams?": "¿Quién tiene más Grand Slams?",
"Who has more followers?": "¿Quién tiene más seguidores?",
"Who has more looses?": "¿Quién tiene más derrotas?",
"Who has more wins?": "¿Quién tiene más victorias?",
"Who has more followers?": "¿Quién tiene más seguidores? (Tenis)",
"Who has more looses?": "¿Quién tiene más derrotas? (Tenis)",
"Who has more wins?": "¿Quién tiene más victorias? (Tenis)",
"Which tennis player is from?" : "¿Qué jugador es de %s?"

}
123 changes: 89 additions & 34 deletions question_generator/planets/planetsQuestions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const queryExecutor=require("../queryExecutor")
const QuestionsUtils = require("../questions-utils");
class PlanetsQuestions{
#planetsQuestions=null;
static getInstance(){
Expand All @@ -10,39 +11,86 @@ class PlanetsQuestions{
constructor(){
this.planets={}
}
async loadData(){
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". }
async loadPlanets(){
let result={};
const planetsQueries=[
`
SELECT ?planet ?planetLabel
WHERE {
?categ wdt:P361 wd:Q337768.
?planet wdt:P31 ?categ.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
`,
`
SELECT ?planet ?planetLabel
WHERE {
?planet wdt:P31 wd:Q3504248;
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
`

];
for(let i = 0; i <planetsQueries.length; i++) {
let query = planetsQueries[i];
let planets = await queryExecutor.execute(query);
planets.forEach(planet=>{
const planetId = planet.planet.value.match(/Q\d+/)[0];
const planetName = planet.planetLabel.value;
if (!result[planetId]) {
result[planetId] = {
planetId: planetId,
name: planetName
}
}
});
}
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;
return result;

if (!newResults[planetId]) {
newResults[planetId] = {
planetId: planetId,
planetName: planetName,
radius: []
};
}
async loadData(){
let newResults = await this.loadPlanets();
const propertiesToLoad=[
{
name:'radius',
id: 'P2120'
},
{
name:'density',
id: 'P2054'
},
{
name:'area',
id: 'P2046'
},
{
name:'mass',
id: 'P2067'
},
{
name:'orbital_period',
id: 'P2047'
},
{
name:'highest_point',
id: 'P610'
},
{
name:'lowest_point',
id: 'P1589'
}

newResults[planetId].radius.push(parseFloat(radius));
});
]
for(let i = 0; i <Object.keys(newResults).length; i++) {
let id = Object.keys(newResults)[i];
let r= await queryExecutor.executeQueryForEntityAndProperty(id, propertiesToLoad);
if(r.length>0){
for(let j=0;j<propertiesToLoad.length;j++){
if(r[0][propertiesToLoad[j].name]!==undefined){
newResults[id][propertiesToLoad[j].name] = r[0][propertiesToLoad[j].name].value;
}
}
}
}
this.planets=newResults;
}
async getRandomPlanets(number) {
Expand All @@ -53,19 +101,26 @@ class PlanetsQuestions{
const randomResults = array.sort(() => Math.random() - 0.5).slice(0, number);
return randomResults
}
async doQuestion(property,nValues){
if(Object.keys(this.planets).length==0){
await this.loadData();
}
return QuestionsUtils.getValuesFromDataAndProperty(this.planets, property, nValues);
}
async getBiggestPlanet(){
const results=await this.getRandomPlanets(4);
const formattedResults = await results.sort((a, b) => b.radius[0] - a.radius[0]);
var finalResults={
let formattedResults = results.filter(planet => planet.radius !== undefined);
formattedResults = await formattedResults.sort((a, b) => parseFloat(b.radius) - parseFloat(a.radius));
let finalResults={
correct: null,
incorrects:[]
}
for(let i = 0; i < Math.min(formattedResults.length,4); i++) {
if(i==0){
finalResults.correct=formattedResults[i].planetName;
finalResults.correct=formattedResults[i].name;
}
else{
finalResults.incorrects.push(formattedResults[i].planetName);
finalResults.incorrects.push(formattedResults[i].name);
}
}
return finalResults;
Expand Down
Loading