Skip to content

Commit

Permalink
Filtering good questions
Browse files Browse the repository at this point in the history
  • Loading branch information
uo288347 committed Apr 27, 2024
1 parent 375dac7 commit 26c8c06
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 258 deletions.
3 changes: 2 additions & 1 deletion questions/services/generate-questions-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async function generateQuestions(n, language, questionCategory) {

// get data for selected entity
let pos = Math.floor(Math.random() * entity.properties.length);

//use only property of that required category
if(questionCategory) {
const filteredProperties = [];
Expand All @@ -33,6 +33,7 @@ async function generateQuestions(n, language, questionCategory) {
filteredProperties.push(property);
}
});

const randomPos = Math.floor(Math.random() * filteredProperties.length);
const propertyJson = filteredProperties[randomPos];
pos = entity.properties.findIndex(prop => prop === propertyJson);
Expand Down
11 changes: 7 additions & 4 deletions questions/services/wikidata-service.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const axios = require('axios');

async function getRandomEntity(entity, pos, language) {

const property = entity.properties[pos].property;
const filt = entity.properties[pos].filter;
var filter = '';
Expand All @@ -22,13 +23,12 @@ async function getRandomEntity(entity, pos, language) {
`;
// it is better to use the FILTER rather than SERVICE
//SERVICE wikibase:label { bd:serviceParam wikibase:language "es". }

const urlApiWikidata = 'https://query.wikidata.org/sparql';
const headers = {
'User-Agent': 'QuestionGeneration/1.0',
'Accept': 'application/json',
};

try {
response = await axios.get(urlApiWikidata, {
params: {
Expand All @@ -37,14 +37,16 @@ async function getRandomEntity(entity, pos, language) {
},
headers: headers,
});



const data = await response.data
const entities = data.results.bindings;

if (entities.length > 0) {
const randomEntity = entities[Math.floor(Math.random() * entities.length)];
const entityName = randomEntity.entityLabel.value;
const property = randomEntity.property.value;
console.log("ENTITY, PROPERTY: ",entityName, property);
return [entityName, property];
} else {
return null;
Expand Down Expand Up @@ -95,6 +97,7 @@ async function getProperties(property, language, filt) {
for(var i = 0; i < 3 ; i++) {
properties[i] = list[Math.floor(Math.random() * list.length)].property.value;
}
console.log("PROPERTIES: ",properties);
return properties;
}
return null;
Expand Down
Loading

0 comments on commit 26c8c06

Please sign in to comment.