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

Added 2 types of questions #76

Merged
merged 6 commits into from
Apr 7, 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
34 changes: 27 additions & 7 deletions game/qgservice/QGController.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { generateQuestionPopulation, generateQuestionCapital } = require('./generatorLogic/questiongenerator');
const { generateQuestionPopulation, generateQuestionCapital, generateQuestionChemical, generateQuestionMonument } = require('./generatorLogic/questiongenerator');
const Question4Answers = require('./Question4Answers');
const { executeSparqlQuery } = require('./generatorLogic/SparqlQuery')
const { bindCapitalsResults, bindPopulationResults } = require('./generatorLogic/BindResults')
const { spainCapitalQuery, worldCapitalQuery, worldPopulationQuery } = require('./generatorLogic/queries')
const { bindCapitalsResults, bindPopulationResults, bindChemicalResults, bindMonumentResults } = require('./generatorLogic/BindResults')
const { spainCapitalQuery, worldCapitalQuery, worldPopulationQuery, chemicalElementQuery, monumentQuery } = require('./generatorLogic/queries')
const { createMathQuestions } = require('./generatorLogic/MathQuestions')

let QGController = {
Expand All @@ -12,7 +12,7 @@ let QGController = {
const questions = [];

// spain capitals
nQuestions = 2;
nQuestions = 3;
const spainQueryResult = await executeSparqlQuery(spainCapitalQuery);
const spainCapitals = bindCapitalsResults(spainQueryResult)

Expand All @@ -22,7 +22,7 @@ let QGController = {
}

// world capitals
nQuestions = 3;
nQuestions = 2;
const worldQueryResult = await executeSparqlQuery(worldCapitalQuery);
const worldCapitals = bindCapitalsResults(worldQueryResult)

Expand All @@ -32,7 +32,7 @@ let QGController = {
}

// world population
nQuestions = 5;
nQuestions = 2;
const worldPopulationResult = await executeSparqlQuery(worldPopulationQuery);
const worldPopulation = bindPopulationResults(worldPopulationResult)

Expand All @@ -41,8 +41,28 @@ let QGController = {
questions.push(question);
}

// chemical elements
nQuestions = 2;
const chemicalResult = await executeSparqlQuery(chemicalElementQuery);
const chemicalElement = bindChemicalResults(chemicalResult)

for (let i = 0; i < nQuestions; i++) {
const question = generateQuestionChemical(chemicalElement);
questions.push(question);
}

// monuments
nQuestions = 2;
const monumentResult = await executeSparqlQuery(monumentQuery);
const monument = bindMonumentResults(monumentResult)

for (let i = 0; i < nQuestions; i++) {
const question = generateQuestionMonument(monument);
questions.push(question);
}

// math questions
const mathquestions = await createMathQuestions(5)
const mathquestions = await createMathQuestions(3)
questions.push(...mathquestions)

res.json(questions);
Expand Down
28 changes: 27 additions & 1 deletion game/qgservice/generatorLogic/BindResults.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,33 @@ function bindPopulationResults(queryResult){
return populationMap
}

function bindChemicalResults(queryResult){
const chemicalElementMap = new Map();

queryResult.results.bindings.forEach(entry => {
const elementLabel = entry.elementLabel.value;
const symbol = entry.symbol.value;
chemicalElementMap.set(symbol, elementLabel);
});

return chemicalElementMap
}

function bindMonumentResults(queryResult){
const monumentMap = new Map();

queryResult.results.bindings.forEach(entry => {
const monumentLabel = entry.monumentLabel.value;
const countryLabel = entry.countryLabel.value;
monumentMap.set(monumentLabel, countryLabel);
});

return monumentMap
}

module.exports = {
bindCapitalsResults,
bindPopulationResults
bindPopulationResults,
bindChemicalResults,
bindMonumentResults
}
18 changes: 17 additions & 1 deletion game/qgservice/generatorLogic/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ SELECT ?country ?countryLabel ?capital ?capitalLabel WHERE {
}
`

const chemicalElementQuery = `SELECT ?element ?elementLabel ?symbol WHERE {
?element wdt:P31 wd:Q11344; # Instance of chemical element
wdt:P246 ?symbol. # Chemical symbol
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
`

const monumentQuery = `SELECT ?monument ?monumentLabel ?country ?countryLabel WHERE {
?monument wdt:P31 wd:Q4989906; # Instance of historical monument
wdt:P17 ?country. # Country of the monument
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
LIMIT 1000
`


const chatgptPrompt = `
Expand All @@ -58,5 +72,7 @@ module.exports = {
spainPopulationQuery,
//chatgptPrompt,
spainCapitalQuery,
worldCapitalQuery
worldCapitalQuery,
chemicalElementQuery,
monumentQuery
};
79 changes: 78 additions & 1 deletion game/qgservice/generatorLogic/questiongenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,43 @@ function generateQuestionCapital(countryCapitalMap) {
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;
}

function generateQuestionChemical(chemicalElementMap) {
const chemicalElementArray = Array.from(chemicalElementMap);

const randomIndex = Math.floor(Math.random() * chemicalElementArray.length);
const [ symbol, chemical] = chemicalElementArray[randomIndex];

const incorrectAnswers = [];
while (incorrectAnswers.length < 3) {
const randomElement = chemicalElementArray[Math.floor(Math.random() * chemicalElementArray.length)];
const [randomSymbol, randomElementName] = randomElement;
if (randomElementName !== chemical && !incorrectAnswers.includes(randomSymbol)) {
incorrectAnswers.push(randomSymbol);
}
}

// Create the question object
const question = {
uuid: uuid.v4(),
question: `What is the symbol of ${chemical}?`,
correctAnswer: symbol,
incorrectAnswer1: incorrectAnswers[0],
incorrectAnswer2: incorrectAnswers[1],
incorrectAnswer3: incorrectAnswers[2],
};

// Save the question to MongoDB
const newQuestion = new Question4Answers(question);
Expand All @@ -78,7 +115,47 @@ function generateQuestionCapital(countryCapitalMap) {
return question;
}

function generateQuestionMonument(monumentMap) {
const monumentArray = Array.from(monumentMap);

const randomIndex = Math.floor(Math.random() * monumentArray.length);
const [ monumentLabel, countryLabel] = monumentArray[randomIndex];

const incorrectAnswers = [];
while (incorrectAnswers.length < 3) {
const randomMonument = monumentArray[Math.floor(Math.random() * monumentArray.length)];
const [randomMonumentLabel, randomCountry] = randomMonument;
if (randomMonumentLabel !== monumentLabel && !incorrectAnswers.includes(randomCountry)) {
incorrectAnswers.push(randomCountry);
}
}

// Create the question object
const question = {
uuid: uuid.v4(),
question: `Where is ${monumentLabel}?`,
correctAnswer: countryLabel,
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,
generateQuestionCapital
generateQuestionCapital,
generateQuestionChemical,
generateQuestionMonument
};
Loading