Compare commits

2 Commits
Author SHA1 Message Date
raymon ef9b5ad85f Call the existing helper methods to form a program 2025-08-04 11:01:34 +02:00
raymon 19a7658c51 Pass properties as integers internally 2025-08-04 11:01:34 +02:00
2 changed files with 21 additions and 17 deletions
+12 -12
View File
@@ -5,23 +5,23 @@ pub mod wikipedia_infobox_analyzer {
use itertools::Itertools;
/// 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<u64> {
let mut lines = template.lines();
let used = lines
.find(|line| line.starts_with("{{") && line.contains("Wikidata|"))
.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();
let re = Regex::new(r"P(\d+)").unwrap();
// Find all matches and collect the properties into vector
re.find_iter(used)
.map(|m| m.as_str().to_owned())
re.captures_iter(used)
.map(|m| m[1].parse().unwrap())
.collect()
}
/// Creates an ascii infobox out of the properties
pub fn format_infobox_from_used_properties(name: String, properties: Vec<String>) -> String {
pub fn format_infobox_from_used_properties(name: String, properties: Vec<u64>) -> String {
let mut ascii_table = AsciiTable::default();
ascii_table.set_max_width(26);
ascii_table
@@ -29,7 +29,7 @@ pub mod wikipedia_infobox_analyzer {
.set_header(name)
.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![format!("P{v}")]).collect();
ascii_table.format(data)
}
@@ -116,9 +116,9 @@ mod tests {
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"
18, 154, 1128, 4103, 2139, 2295, 2226, 856, 169,
1448, 159, 749, 355, 17, 1056, 452, 576, 112, 127,
856, 2096
]
);
@@ -128,8 +128,8 @@ mod tests {
assert_eq!(
extract_used_properties_from_template(contents),
vec![
"P18", "P154", "P170", "P178", "P275", "P277", "P306", "P348", "P400", "P548",
"P571", "P577", "P856", "P1324", "P2096"
18, 154, 170, 178, 275, 277, 306, 348, 400, 548,
571, 577, 856, 1324, 2096
]
);
}
@@ -139,7 +139,7 @@ mod tests {
assert_eq!(
format_infobox_from_used_properties(
"Earth".to_string(),
vec!["P31".to_string(), "P361".to_string(), "P571".to_string()]
vec![31, 361, 571]
),
concat![
"┌───────┐\n",
+9 -5
View File
@@ -15,14 +15,18 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = Cli::from_args();
// Input to the program
let qid = fetch_wiki_item_by_article_title(
args.wikipedia_article_title,
args.wikipedia_language_code,
);
let _template_file = std::fs::read_to_string(&args.infobox_template_file)?;
let title = args.wikipedia_article_title;
let language = args.wikipedia_language_code;
let _template = std::fs::read_to_string(&args.infobox_template_file)?;
let qid = fetch_wiki_item_by_article_title(title.clone(), language.clone());
let properties = fetch_properties_for_wiki_item(qid);
let table = format_infobox_from_used_properties(title.clone(), properties);
println!("Qid: {qid}");
println!("Template file exists");
println!("{table}");
Ok(())
}