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

Automatic Wikidata queries and suffling the correct answer #88

Merged
merged 3 commits into from
Mar 11, 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
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}
";
}
}
}