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 Some Question Templates #182

Merged
merged 5 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 1 addition & 2 deletions .env
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
teamname="wiq_en3b"
PORT=80
teamname="wiq_en3b"
100 changes: 100 additions & 0 deletions questionservice/src/models/template-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,106 @@ const generateSampleTest = () => {
typeName: 'science',
},
});

addQuestionTemplate({
questionTemplate: 'What is the official language of $$$?',
question_type: {
name: 'Language',
query: `SELECT DISTINCT ?templateLabel ?answerLabel
WHERE {
?template wdt:P31 wd:Q6256;
wdt:P1082 ?population.
?template wdt:P37 ?answer.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY UUID()
LIMIT 5
`,
entities: [],
typeName: 'geography',
},
});

addQuestionTemplate({
questionTemplate: 'Where did the Olympic Games of $$$ take place?',
question_type: {
name: 'Olympics',
query: `SELECT ?templateLabel ?answerLabel
WHERE {
?olympicGame wdt:P31 wd:Q159821; # Instances of Olympic Games
wdt:P276 ?answer;
wdt:P585 ?date.
BIND(YEAR(?date) AS ?templateLabel)
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY UUID()
LIMIT 5
`,
entities: [],
typeName: 'sports',
},
});

addQuestionTemplate({
questionTemplate: 'Where did the Winter Olympic Games of $$$ take place?',
question_type: {
name: 'Olympics',
query: `SELECT ?templateLabel ?answerLabel
WHERE {
?olympicGame wdt:P31 wd:Q82414; # Instances of Winter Olympic Games
wdt:P276 ?answer;
wdt:P585 ?date.
BIND(YEAR(?date) AS ?templateLabel)
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY UUID()
LIMIT 5
`,
entities: [],
typeName: 'sports',
},
});

addQuestionTemplate({
questionTemplate: 'What is the capacity of $$$?',
question_type: {
name: 'Stadiums',
query: `SELECT ?templateLabel ?answerLabel
WHERE {
?template wdt:P31 wd:Q483110; # Instances of stadiums
wdt:P1083 ?answer.
FILTER (?answer >= 40000)
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY UUID()
LIMIT 5

`,
entities: [],
typeName: 'sports',
},
});


addQuestionTemplate({
questionTemplate: 'Which stadium is this?',
question_type: {
name: 'Images_Stadiums',
query: `SELECT ?templateLabel ?answerLabel
WHERE {
?answer wdt:P31 wd:Q483110; # Instances of stadiums
wdt:P1083 ?capacity;
wdt:P18 ?template.
FILTER (?capacity >= 40000)
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY UUID()
LIMIT 5
`,
entities: [],
typeName: 'sports',
},
});
};

export { TemplateModel, generateSampleTest };
35 changes: 28 additions & 7 deletions questionservice/src/services/question-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,15 @@ const generateQuestionJson = async (
}

// Generate answers
let answersArray: object[] = getRandomResponses(
wikidataResponse,
randomIndexes
);
let answersArray: object[] = [];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ Getting worse: Primitive Obsession
The ratio of primitive types in function arguments increases from 46.43% to 46.67%, threshold = 30.0%

try {
answersArray = getRandomResponses(
wikidataResponse,
randomIndexes
);
} catch (error) {
return undefined;
}

// Randomizing answers order
shuffleArray(answersArray);
Expand Down Expand Up @@ -366,14 +371,30 @@ function getRandomResponses(
randomIndexes: number[]
): any {
let answersArray: object[] = [];
for (let i = 0; i < optionsNumber; i++) {
let answersIndex = 0;
let i = 0;
while (answersIndex < optionsNumber) {
let answer = wikidataResponse[randomIndexes[i]].answerLabel;
answersArray[i] = {
id: i + 1,
i++;
if (answersArrayContainsAnswer(answersArray, answer)) {
continue;
}
answersArray[answersIndex] = {
id: answersIndex + 1,
text: answer,
};
answersIndex++;
}
if (answersArray.length != optionsNumber) {
throw new Error('Not enough answers for the question could be found');
}

return answersArray;
}


function answersArrayContainsAnswer(answersArray: any, answer: string): boolean {
return !answersArray.every((answerObject: any) => answerObject.text !== answer);
}

export { generateQuestions };
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ sonar.coverage.exclusions=**/*.test.ts,**/*.test.js,**/*.test-utils.js,**/jest.c
sonar.sources=webapp/src/components,users/authservice,users/userservice,gatewayservice,users/utils,questionservice
sonar.sourceEncoding=UTF-8
sonar.exclusions=node_modules/**
sonar.cpd.exclusions=**/test/*
sonar.cpd.exclusions=**/test/*,**/template-model.ts
sonar.javascript.lcov.reportPaths=**/coverage/lcov.info