Extract used wikidata properties from infobox template

This commit is contained in:
2025-08-03 13:40:55 +02:00
parent 9d4136c993
commit e9cfb0293b
5 changed files with 1015 additions and 0 deletions
+56
View File
@@ -0,0 +1,56 @@
#[doc = include_str!("../README.md")]
pub mod wikipedia_infobox_analyzer {
use regex::Regex;
/// Looks for a used template that does lists wikidata properties listing
pub fn extract_used_properties_from_template(template: String) -> Vec<String> {
let lines = template.lines();
let used = lines
.filter(|line| line.starts_with("{{Gebruikt Wikidata"))
.nth(0)
.expect("Template should have a line declaring which properties are used");
// The properties are listed inside the template with their number: P1|P2|P...
let re = Regex::new(r"P\d+").unwrap();
// Find all matches and collect into vector
let properties = re
.find_iter(used)
.map(|m| m.as_str().to_owned())
.collect::<Vec<String>>();
return properties;
}
}
#[cfg(test)]
mod tests {
use super::wikipedia_infobox_analyzer::*;
use std::fs;
#[test]
fn test_extract_used_properties_from_template() {
let contents = fs::read_to_string("examples/templates/nl_infobox_bedrijf")
.expect("Template file should exist");
assert_eq!(
extract_used_properties_from_template(contents),
vec![
"P18", "P154", "P1128", "P4103", "P2139", "P2295", "P2226", "P856", "P169",
"P1448", "P159", "P749", "P355", "P17", "P1056", "P452", "P576", "P112", "P127",
"P856", "P2096"
]
);
let contents = fs::read_to_string("examples/templates/nl_infobox_software")
.expect("Template file should exist");
assert_eq!(
extract_used_properties_from_template(contents),
vec![
"P18", "P154", "P170", "P178", "P275", "P277", "P306", "P348", "P400", "P548",
"P571", "P577", "P856", "P1324", "P2096"
]
);
}
}