diff --git a/crates/bibclean/README.md b/crates/bibclean/README.md index c95ccc3..721c00f 100644 --- a/crates/bibclean/README.md +++ b/crates/bibclean/README.md @@ -99,7 +99,8 @@ through this and back into whatever it came from. ## Recovering a missing author A book says who wrote it on its own copyright page. Hand in the opening pages -as `front_matter` and a record whose author is missing gets a name proposed, +as `front_matter` and a record whose author is missing, a placeholder, or the title copied into the +field gets a name proposed, with the evidence and how far it is worth trusting: ``` @@ -183,6 +184,7 @@ bibclean books.ndjson | jq -c 'select(.slug_proposal.tier | IN("placeholder-auth | `unknown_author` | 3 | "Unknown", "Anonymous", empty | | `filename_title` | 3 | A bare filename, extension and all | | `junk_title` | 3 | The name of the tool that made the file, not the work | +| `author_is_title` | 4 | The title copied into the author field | | `publisher_as_author` | 3 | An imprint in the author field | | `title_in_author` | 3 | The title is a placeholder and the real title is in the author field | | `allcaps_title` | 2 | Shouting | diff --git a/crates/bibclean/src/lib.rs b/crates/bibclean/src/lib.rs index 49ad3e3..c3ff935 100644 --- a/crates/bibclean/src/lib.rs +++ b/crates/bibclean/src/lib.rs @@ -237,11 +237,16 @@ pub fn clean(book: &Book) -> Cleaned { _ => None, }; - // Only for a record that has no author to overwrite. A name recovered - // from the text is evidence, not an improvement on one somebody entered. + // Only for a record that has no real author to overwrite: none, a + // placeholder, or the title copied into the field. A name recovered from + // the text is evidence, not an improvement on one somebody entered. let author_proposal = reasons .iter() - .any(|reason| reason == "unknown_author" || reason == "placeholder_author") + .any(|reason| { + reason == "unknown_author" + || reason == "placeholder_author" + || reason == "author_is_title" + }) .then(|| { author::best( book.title.as_deref().unwrap_or(""), diff --git a/crates/bibclean/src/reasons.rs b/crates/bibclean/src/reasons.rs index 7fd08d8..111a6bf 100644 --- a/crates/bibclean/src/reasons.rs +++ b/crates/bibclean/src/reasons.rs @@ -149,6 +149,7 @@ const PERSON_SUFFIXES: &[&str] = &["ed", "eds", "ii", "iii", "iv", "jr", "md", " /// What each fault is worth when the worst records are wanted first. const WEIGHTS: &[(&str, u32)] = &[ ("allcaps_title", 2), + ("author_is_title", 4), ("author_last_first", 1), ("author_role_suffix", 2), ("edited_volume", 1), @@ -248,6 +249,9 @@ pub fn problems(title: Option<&str>, author: Option<&str>, isbn: Option<&str>) - { reasons.push("possible_swap"); } + if author_is_title(title, author) { + reasons.push("author_is_title"); + } if title_in_author(Some(title), Some(author)) { reasons.push("title_in_author"); } @@ -280,6 +284,38 @@ pub fn worth_fixing(reasons: &[String]) -> bool { reasons.iter().any(|reason| reason != "missing_isbn") } +/// Whether the author field holds the title, copied there by whatever made +/// the record: "Cultural Imperialism" by "Cultural Imperialism". +/// +/// The author has to be the whole title, or the whole of the title before its +/// subtitle or a bracketed aside, compared without case or punctuation. A +/// person whose name merely opens the title ("Plato" for "Plato and the +/// Republic") is not caught, because the name is not all of the title, and +/// nor is a person named before the subtitle: "Isaac Newton: Philosophical +/// Writings" is by Isaac Newton. +pub fn author_is_title(title: &str, author: &str) -> bool { + let bare_author = author.split('(').next().unwrap_or(""); + let folded = fold(bare_author); + if folded.is_empty() { + return false; + } + if folded == fold(title.split('(').next().unwrap_or("")) { + return true; + } + folded == fold(title.split([':', '(']).next().unwrap_or("")) && !looks_like_person(bare_author) +} + +/// Lowercase words, punctuation dropped. +fn fold(text: &str) -> String { + text.chars() + .map(|c| if c.is_alphanumeric() { c } else { ' ' }) + .collect::() + .to_lowercase() + .split_whitespace() + .collect::>() + .join(" ") +} + /// Whether a title is a placeholder rather than a work's name. pub(crate) fn is_placeholder_title(title: &str) -> bool { PLACEHOLDER_TITLE.is_match(title.trim_matches(textkit::is_space)) @@ -384,6 +420,59 @@ fn is_publisher(author: &str) -> bool { .is_ok() } +#[cfg(test)] +mod author_is_title_tests { + use super::*; + + #[test] + fn a_title_in_the_author_field_is_named() { + for (title, author) in [ + ("Cultural Imperialism", "Cultural Imperialism"), + ("Consciousness Reconsidered", "Consciousness Reconsidered"), + ("The Brain", "The Brain (1979)"), + ( + "A Slim Book About Narrow Content", + "A Slim Book About Narrow Content (Contem", + ), + ( + "Neural Correlates of Consciousness: Empirical and Conceptual Questions", + "Neural Correlates Of Consciousness", + ), + ("Reed", "Reed"), + ] { + assert!(author_is_title(title, author), "{title} / {author}"); + assert!( + problems(Some(title), Some(author), None).contains(&"author_is_title".to_string()) + ); + } + } + + #[test] + fn a_person_who_shares_words_with_the_title_is_not() { + for (title, author) in [ + ("Plato and the Republic", "Plato"), + ("Kant: A Very Short Introduction", "Roger Scruton"), + ("Hegel and Aristotle", "Alfredo Ferrarin"), + ("Aristotle", "Jonathan Barnes"), + ("Isaac Newton: Philosophical Writings", "Isaac Newton"), + ( + "Margaret Cavendish: Observations Upon Experimental Philosophy", + "Margaret Cavendish", + ), + // Two capitalised words read as a name, so before a subtitle they + // are given the benefit of the doubt even when they are a title. + ( + "Cultural Imperialism: A Critical Introduction", + "Cultural Imperialism", + ), + ("", "Someone"), + ("Untitled", ""), + ] { + assert!(!author_is_title(title, author), "{title} / {author}"); + } + } +} + #[cfg(test)] mod tests { use super::*;