Skip to content

Commit

Permalink
Parameterized query and shuffling correct answer
Browse files Browse the repository at this point in the history
Automatic Wikidata queries and suffling the correct answer
  • Loading branch information
UO289930 authored Mar 11, 2024
2 parents 16ee107 + 5ed2b2e commit 8775398
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
20 changes: 18 additions & 2 deletions wikidata_service/WikiDataTest/Controllers/WikiDataController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public WikiDataController(ILogger<WikiDataController> logger,
_questionService = questionService;
}

[HttpGet("GetQuestions")]
public async Task<IActionResult> GetQuestions()
[HttpGet("GetCapitalsQuestions")]
public async Task<IActionResult> GetCapitalsQuestions()
{
RootObject jsonQuestions = await _wikiDataService.GetQuestions(_queryService.GetCapitalsQuery());

Expand All @@ -37,6 +37,22 @@ public async Task<IActionResult> GetQuestions()
}

return Ok(questions);
}

[HttpGet("GetQuestionsMovieQuestions")]
public async Task<IActionResult> GetQuestionsMovieDirectorQuestions()
{
var jsonQuestions = await _wikiDataService.GetQuestions(_queryService.GetMovieDirectorQuery());

return Ok(jsonQuestions);
}

[HttpGet("GetElementSymbolQuestions")]
public async Task<IActionResult> GetElementSymbolQuestions()
{
var jsonQuestions = await _wikiDataService.GetQuestions(_queryService.GetElementSymbolQuery());

return Ok(jsonQuestions);
}
}

Expand Down
15 changes: 15 additions & 0 deletions wikidata_service/WikiDataTest/Models/Question.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ public Question(string text, string[] answers, int correctAnswer)
this.text = text;
this.answers = answers;
this.correctAnswer = correctAnswer;
suffleAnswers();
}

private void suffleAnswers()
{
Random rand = new Random();
int newIndex = rand.Next(answers.Length);

// Swap correct answer with the answer at the new index
string temp = answers[newIndex];
answers[newIndex] = answers[correctAnswer];
answers[correctAnswer] = temp;

// Update correctAnswer to point to the new index
correctAnswer = newIndex;
}

public string ToString()
Expand Down
20 changes: 20 additions & 0 deletions wikidata_service/WikiDataTest/Services/QueryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,25 @@ public string GetCapitalsQuery()
{
return "SELECT ?capitalLabel ?countryLabel WHERE { ?capital wdt:P1376 ?country. ?capital wdt:P31 wd:Q5119. ?country wdt:P31 wd:Q3624078. SERVICE wikibase:label { bd:serviceParam wikibase:language \"[AUTO_LANGUAGE],en\". }} LIMIT 400";
}

public string GetMovieDirectorQuery(){
return GenerateSparqlQuery("Q11424", "P57");
}

public string GetElementSymbolQuery(){
return GenerateSparqlQuery("Q11344", "P246");
}

private string GenerateSparqlQuery(string themeId, string attributeId, int limit = 10)
{
return $@"
SELECT ?themeLabel ?attributeLabel WHERE {{
?theme wdt:P31 wd:{themeId};
wdt:{attributeId} ?attribute.
SERVICE wikibase:label {{ bd:serviceParam wikibase:language ""[AUTO_LANGUAGE],en"". }}
}}
LIMIT {limit}
";
}
}
}

0 comments on commit 8775398

Please sign in to comment.