Cleanup the code made so far

master
Raymon Zutekouw 3 days ago
parent cd8d5f14d1
commit 0097b105a1
Signed by: raymon
GPG Key ID: 0E62222846283925
  1. 29
      src/lib.rs

@ -6,22 +6,18 @@ pub mod wikipedia_infobox_analyzer {
/// Looks for a used template that does lists wikidata properties listing /// Looks for a used template that does lists wikidata properties listing
pub fn extract_used_properties_from_template(template: String) -> Vec<String> { pub fn extract_used_properties_from_template(template: String) -> Vec<String> {
let lines = template.lines(); let mut lines = template.lines();
let used = lines let used = lines
.filter(|line| line.starts_with("{{Gebruikt Wikidata")) .find(|line| line.starts_with("{{Gebruikt Wikidata"))
.nth(0)
.expect("Template should have a line declaring which properties are used"); .expect("Template should have a line declaring which properties are used");
// The properties are listed inside the template with their number: P1|P2|P... // The properties are listed inside the template with their number: P1|P2|P...
let re = Regex::new(r"P\d+").unwrap(); let re = Regex::new(r"P\d+").unwrap();
// Find all matches and collect into vector // Find all matches and collect the properties into vector
let properties = re re.find_iter(used)
.find_iter(used) .map(|m| m.as_str().to_owned())
.map(|m| m.as_str().to_owned()) .collect()
.collect::<Vec<String>>();
return properties;
} }
/// Creates an ascii infobox out of the properties /// Creates an ascii infobox out of the properties
@ -34,23 +30,20 @@ pub mod wikipedia_infobox_analyzer {
.set_align(Align::Left); .set_align(Align::Left);
let data: Vec<Vec<&String>> = properties.iter().map(|v| vec![v]).collect(); let data: Vec<Vec<&String>> = properties.iter().map(|v| vec![v]).collect();
return ascii_table.format(data); ascii_table.format(data)
} }
/// Fetches the wikidata label for the wiki property /// Fetches the wikidata label for the wiki property
pub async fn fetch_name_for_wiki_property(pid: u64) -> String { pub async fn fetch_name_for_wiki_property(pid: u64) -> String {
let query = format!( let query = format!("
"
SELECT ?label SELECT ?label
WHERE WHERE
{{ {{
wd:P{} rdfs:label ?label. wd:P{pid} rdfs:label ?label.
FILTER(lang(?label) = \"en\") FILTER(lang(?label) = \"en\")
SERVICE wikibase:label {{ bd:serviceParam wikibase:language 'en'.}} SERVICE wikibase:label {{ bd:serviceParam wikibase:language 'en'.}}
}} }}
", ");
pid
);
let api = mediawiki::api::Api::new("https://www.wikidata.org/w/api.php") let api = mediawiki::api::Api::new("https://www.wikidata.org/w/api.php")
.await .await
@ -63,7 +56,7 @@ pub mod wikipedia_infobox_analyzer {
let re = Regex::new("\"value\": \"(.+)\"").unwrap(); let re = Regex::new("\"value\": \"(.+)\"").unwrap();
let Some(label) = re.captures(&res_s) else { return "<unknown>".to_string() }; let Some(label) = re.captures(&res_s) else { return "<unknown>".to_string() };
return label[1].to_string(); label[1].to_string()
} }
/// Fetches the properties which have claims for the given wiki item inside of wikidata /// Fetches the properties which have claims for the given wiki item inside of wikidata

Loading…
Cancel
Save