Skip to content

Commit

Permalink
Added questions of spain capitals and countries capitals
Browse files Browse the repository at this point in the history
  • Loading branch information
UO277274 committed Feb 27, 2024
1 parent f24bd60 commit 9b11145
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
36 changes: 36 additions & 0 deletions game/qgservice/qg-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,42 @@ app.get('/spainPopulation', async (req, res) => {
}
});

app.get('/worldCapitals', async (req, res) => {
try {
const sparqlResult = await executeSparqlQuery(worldCapitalQuery);
const countryCapitals = new Map();

sparqlResult.results.bindings.forEach(entry => {
const countryLabel = entry.countryLabel.value;
const capital = entry.capitalLabel.value;
countryCapitals.set(countryLabel, capital);
});

const question = generateQuestionCapital(countryCapitals);
res.json(question);
} catch (error) {
res.status(500).json({ error: 'Internal Server Error' });
}
});

app.get('/spainCapitals', async (req, res) => {
try {
const sparqlResult = await executeSparqlQuery(spainCapitalQuery);
const countryCapitals = new Map();

sparqlResult.results.bindings.forEach(entry => {
const countryLabel = entry.countryLabel.value;
const capital = entry.capitalLabel.value;
countryCapitals.set(countryLabel, capital);
});

const question = generateQuestionCapital(countryCapitals);
res.json(question);
} catch (error) {
res.status(500).json({ error: 'Internal Server Error' });
}
});

app.get('/', (req, res) => {
res.json({
"hi": "question generator"
Expand Down
20 changes: 20 additions & 0 deletions game/qgservice/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,26 @@ WHERE {
}
`

const worldCapitalQuery = `
SELECT ?country ?countryLabel ?capital ?capitalLabel WHERE {
?country wdt:P31 wd:Q6256;
wdt:P36 ?capital.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
`

// Utilizar country en vez de comunidad para poder reaprovechar codigo
const spainCapitalQuery = `
SELECT ?country ?countryLabel ?capital ?capitalLabel WHERE {
?country wdt:P31 wd:Q10742;
wdt:P36 ?capital;
wdt:P17 wd:Q29.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
`



const chatgptPrompt = `
Hi, I want you to generate trivia questions, with 1 correct answer and 3 incorrect. Please format them as:
question: *question*
Expand Down
37 changes: 37 additions & 0 deletions game/qgservice/questiongenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,43 @@ function generateQuestionPopulation(cityPopulationMap) {
return question;
}

function generateQuestionCapital(countryCapitalMap) {
const countryCapitalArray = Array.from(countryCapitalMap);

const randomIndex = Math.floor(Math.random() * countryCapitalArray.length);
const [country, capital] = countryCapitalArray[randomIndex];

const incorrectAnswers = [];
while (incorrectAnswers.length < 3) {
const randomCountry = countryCapitalArray[Math.floor(Math.random() * countryCapitalArray.length)];
const [randomCountryName, randomCountryCapital] = randomCountry;
if (randomCountryName !== country && !incorrectAnswers.includes(randomCountryCapital)) {
incorrectAnswers.push(randomCountryCapital);
}
}

// Create the question object
const question = {
question: `What is the capital of ${country}?`,
correctAnswer: capital,
incorrectAnswer1: incorrectAnswers[0],
incorrectAnswer2: incorrectAnswers[1],
incorrectAnswer3: incorrectAnswers[2],
};

// Save the question to MongoDB
const newQuestion = new Question4Answers(question);
newQuestion.save()
.then(savedQuestion => {
console.log('Question saved to MongoDB:', savedQuestion);
})
.catch(error => {
console.error('Error saving question to MongoDB:', error.message);
});

return question;
}

module.exports = {
generateQuestionPopulation,
};

0 comments on commit 9b11145

Please sign in to comment.