|
|
|
@ -35,6 +35,35 @@ pub mod wikipedia_infobox_analyzer { |
|
|
|
|
let data: Vec<Vec<&String>> = properties.iter().map(|v| vec![v]).collect(); |
|
|
|
|
return ascii_table.format(data); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Fetches the wikidata label for the wiki property
|
|
|
|
|
pub async fn fetch_name_for_wiki_property(pid: u64) -> String { |
|
|
|
|
let query = format!( |
|
|
|
|
" |
|
|
|
|
SELECT ?label |
|
|
|
|
WHERE |
|
|
|
|
{{ |
|
|
|
|
wd:P{} rdfs:label ?label. |
|
|
|
|
FILTER(lang(?label) = \"en\") |
|
|
|
|
SERVICE wikibase:label {{ bd:serviceParam wikibase:language 'en'.}} |
|
|
|
|
}} |
|
|
|
|
", |
|
|
|
|
pid |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
let api = mediawiki::api::Api::new("https://www.wikidata.org/w/api.php") |
|
|
|
|
.await |
|
|
|
|
.expect("Wikidata should return the api endpoint"); |
|
|
|
|
let res = api.sparql_query(&query) |
|
|
|
|
.await |
|
|
|
|
.expect("Query should be able to retrieve label"); |
|
|
|
|
let res_s = serde_json::to_string_pretty(&res).unwrap(); |
|
|
|
|
|
|
|
|
|
let re = Regex::new("\"value\": \"(.+)\"").unwrap(); |
|
|
|
|
let Some(label) = re.captures(&res_s) else { return "<unknown>".to_string() }; |
|
|
|
|
|
|
|
|
|
return label[1].to_string(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[cfg(test)] |
|
|
|
@ -86,4 +115,10 @@ mod tests { |
|
|
|
|
] |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[tokio::test] |
|
|
|
|
async fn test_fetch_name_for_wiki_property() { |
|
|
|
|
let name = fetch_name_for_wiki_property(31).await; |
|
|
|
|
assert_eq!(name, "instance of") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|