generated from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from Arquisoft/wikidata_sergiollende
WikidataAPI improved and working
- Loading branch information
Showing
12 changed files
with
1,007 additions
and
120 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace WikiDataTest.Models | ||
{ | ||
|
||
public class CapitalCountry | ||
{ | ||
public Label capitalLabel { get; set; } | ||
public Label countryLabel { get; set; } | ||
|
||
public string ToString() | ||
{ | ||
return countryLabel.value + ": " + capitalLabel.value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace WikiDataTest.Models | ||
{ | ||
public class Label | ||
{ | ||
public string value { get; set; } | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
namespace WikiDataTest.Models | ||
{ | ||
public class Question | ||
{ | ||
public string text { get; set; } | ||
public string[] answers { get; set; } | ||
public int correctAnswer { get; set; } | ||
|
||
public Question(string text, string[] answers, int correctAnswer) | ||
{ | ||
this.text = text; | ||
this.answers = answers; | ||
this.correctAnswer = correctAnswer; | ||
} | ||
|
||
public string ToString() | ||
{ | ||
string aux = "text: " + text; | ||
aux += "\ncorrectAnswer: " + answers[0]; | ||
aux += "\nAnswer: " + answers[1]; | ||
aux += "\nAnswer: " + answers[2]; | ||
aux += "\nAnswer: " + answers[3]; | ||
return aux; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace WikiDataTest.Models | ||
{ | ||
public class Results | ||
{ | ||
public List<CapitalCountry> bindings { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace WikiDataTest.Models | ||
{ | ||
public class RootObject | ||
{ | ||
public Results results { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace WikiDataTest.Services | ||
{ | ||
public class QueryService | ||
{ | ||
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"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using System; | ||
using WikiDataTest.Controllers; | ||
using WikiDataTest.Models; | ||
|
||
namespace WikiDataTest.Services | ||
{ | ||
public class QuestionService | ||
{ | ||
/// <summary> | ||
/// Generate the questions from the capital/countries passed as parameter | ||
/// </summary> | ||
/// <param name="capitalCountries"> all the countries obtained </param> | ||
/// <param name="numberQuestions"> number of questions to generate </param> | ||
/// <returns></returns> | ||
public Question[] GenerateQuestionsCapitalsOf(CapitalCountry[] capitalCountries, int numberQuestions = 10) | ||
{ | ||
Question[] questions = new Question[numberQuestions]; | ||
|
||
ISet<int> ids = new HashSet<int>(); | ||
Random r = new Random(); | ||
|
||
while (ids.Count < numberQuestions) | ||
{ | ||
ids.Add(r.Next(capitalCountries.Length)); | ||
} | ||
|
||
int[] idsList = new int[numberQuestions]; | ||
int i = 0; | ||
foreach (int id in ids) | ||
{ | ||
idsList[i++] = id; | ||
} | ||
|
||
// Generate questions | ||
for (int j = 0; j < idsList.Length; j++) | ||
{ | ||
// Generate the questions | ||
string countryName = capitalCountries[idsList[j]].countryLabel.value; | ||
string questionText = $"What is the capital of {countryName}?"; | ||
string[] answers = new string[4]; | ||
|
||
// Place of the correct answer | ||
int correctAnswer = 0; | ||
|
||
// Save the capital of the specific country on first place | ||
answers[correctAnswer] = capitalCountries[idsList[j]].capitalLabel.value; | ||
|
||
// Get 3 random wrong answers from all the capitals | ||
int[] wrongIds = new int[3]; | ||
for (int w = 1; w < 4; w++) | ||
{ | ||
int wrongId = r.Next(capitalCountries.Length); | ||
while (idsList[j] == wrongId || wrongIds.Contains(wrongId)) | ||
{ | ||
wrongId = r.Next(capitalCountries.Length); | ||
} | ||
// Adding the id of the wrongAnswer to the array | ||
wrongIds[w - 1] = wrongId; | ||
answers[w] = capitalCountries[wrongId].capitalLabel.value; | ||
} | ||
|
||
questions[j] = new Question(questionText, answers, correctAnswer); | ||
} | ||
|
||
return questions; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using Newtonsoft.Json; | ||
using WikiDataTest.Controllers; | ||
using WikiDataTest.Models; | ||
|
||
namespace WikiDataTest.Services | ||
{ | ||
public class WikiDataService | ||
{ | ||
public WikiDataService() { } | ||
|
||
public async Task<RootObject> GetQuestions(string sparqlQuery) | ||
{ | ||
string endpointUrl = "https://query.wikidata.org/sparql?query="; | ||
|
||
var fullUrl = endpointUrl + sparqlQuery; | ||
|
||
using (var client = new HttpClient()) | ||
{ | ||
client.DefaultRequestHeaders.Add("User-Agent", "Sergiollende/1.0"); | ||
client.DefaultRequestHeaders.Add("Accept", "application/sparql-results+json"); | ||
|
||
HttpResponseMessage response = await client.GetAsync(fullUrl); | ||
|
||
string responseBody = await response.Content.ReadAsStringAsync(); | ||
|
||
RootObject jsonResult = JsonConvert.DeserializeObject<RootObject>(responseBody); | ||
|
||
return jsonResult; | ||
} | ||
} | ||
} | ||
} |