diff --git a/crates/bibclean/README.md b/crates/bibclean/README.md index 6b74abb..8fdf707 100644 --- a/crates/bibclean/README.md +++ b/crates/bibclean/README.md @@ -202,6 +202,9 @@ bibclean books.ndjson | jq -c 'select(.slug_proposal.tier | IN("placeholder-auth | `missing_apostrophe` | 2 | "Lossky s Argument" | | `possible_swap` | 2 | Title and author look to be the wrong way round | | `underscore_title` | 1 | Underscores standing in for spaces and colons | +| `invalid_isbn` | 3 | A stored ISBN whose check digit disagrees | +| `placeholder_isbn` | 3 | A run of one digit standing in for an ISBN: "000-0-000-00000-0" | +| `isbn_with_label` | 3 | Ten digits beginning `13978`: the label of an ISBN-13 read as part of the number, cut short and given a fresh check digit | | `missing_isbn` | 0 | Reported, but not a reason to do anything | Each test is deliberately conservative. A real book called "The Document" or diff --git a/crates/bibclean/src/isbn.rs b/crates/bibclean/src/isbn.rs index 6ad3a33..fc5735c 100644 --- a/crates/bibclean/src/isbn.rs +++ b/crates/bibclean/src/isbn.rs @@ -8,7 +8,7 @@ use textkit::pattern::SPACE; /// An ISBN announced as one, in running text or a catalogue field. static ANNOUNCED: LazyLock = LazyLock::new(|| { Regex::new(&format!( - r"(?i)ISBN[-{SPACE}:]*((?:97[89][{SPACE}-]?)?(?:[0-9][{SPACE}-]?){{9}}[0-9Xx])" + r"(?i)ISBN(?:[-{SPACE}]?1[03]\b)?[-{SPACE}:]*((?:97[89][{SPACE}-]?)?(?:[0-9][{SPACE}-]?){{9}}[0-9Xx])" )) .expect("a valid pattern") }); @@ -70,11 +70,39 @@ pub fn digits(text: &str) -> String { .collect() } -/// An ISBN announced in a piece of text, if it is the right length. +/// The first ISBN announced in a piece of text whose check digit agrees. +/// +/// The form's own label is not part of the number: "ISBN-13 978-0-521-65706-8" +/// is 9780521657068, never the ten digits 1397805216 that start it. pub fn from_text(text: &str) -> Option { - let found = ANNOUNCED.captures(text)?; - let digits = digits(&found[1]); - matches!(digits.len(), 10 | 13).then_some(digits) + ANNOUNCED + .captures_iter(text) + .map(|found| digits(&found[1])) + .find(|digits| matches!(digits.len(), 10 | 13) && valid(digits)) +} + +/// What is wrong with a stored ISBN, if anything. +/// +/// `invalid_isbn` when the check digit disagrees; `placeholder_isbn` for a +/// run of one digit, which a publisher's template prints before the number +/// is assigned; `isbn_with_label` for ten digits that begin `13978` or +/// `13979`, the label of an ISBN-13 read as part of the number, cut short to +/// ten and given a fresh check digit. +pub fn fault(text: &str) -> Option<&'static str> { + let digits = digits(text); + if digits.is_empty() { + return None; + } + if digits.chars().all(|c| c == digits.as_bytes()[0] as char) { + return Some("placeholder_isbn"); + } + if !valid(&digits) { + return Some("invalid_isbn"); + } + if digits.len() == 10 && (digits.starts_with("13978") || digits.starts_with("13979")) { + return Some("isbn_with_label"); + } + None } /// A checksum-valid ISBN embedded in a filename. @@ -123,6 +151,40 @@ mod tests { assert_eq!(from_text("no identifier here"), None); } + #[test] + fn the_label_of_an_isbn_13_is_not_part_of_it() { + assert_eq!( + from_text("ISBN-13 978-0-521-65706-8 paperback"), + Some("9780521657068".to_string()) + ); + assert_eq!( + from_text("ISBN 13: 9780521657068"), + Some("9780521657068".to_string()) + ); + assert_eq!( + from_text("ISBN-10: 0-521-52888-7 (pbk.)"), + Some("0521528887".to_string()) + ); + } + + #[test] + fn a_number_with_a_wrong_check_digit_gives_way_to_the_next() { + assert_eq!( + from_text("ISBN 0-19-823790-2 ISBN 0-19-823790-1"), + Some("0198237901".to_string()) + ); + } + + #[test] + fn a_stored_isbn_that_cannot_be_right_is_named() { + assert_eq!(fault("9780198237907"), None); + assert_eq!(fault("0198237902"), Some("invalid_isbn")); + assert_eq!(fault("0000000000000"), Some("placeholder_isbn")); + assert_eq!(fault("000-0-000-00000-0"), Some("placeholder_isbn")); + assert_eq!(fault("1397805218"), Some("isbn_with_label")); + assert_eq!(fault(""), None); + } + #[test] fn a_filename_that_is_an_isbn_gives_it_up() { assert_eq!( diff --git a/crates/bibclean/src/reasons.rs b/crates/bibclean/src/reasons.rs index 3a35545..fa00ad7 100644 --- a/crates/bibclean/src/reasons.rs +++ b/crates/bibclean/src/reasons.rs @@ -161,6 +161,8 @@ const WEIGHTS: &[(&str, u32)] = &[ ("edited_volume", 1), ("filename_title", 3), ("html_entity", 2), + ("invalid_isbn", 3), + ("isbn_with_label", 3), ("junk_title", 3), ("lowercase_title", 2), ("missing_apostrophe", 2), @@ -171,6 +173,7 @@ const WEIGHTS: &[(&str, u32)] = &[ ("numeric_author", 4), ("path_title", 4), ("placeholder_author", 4), + ("placeholder_isbn", 3), ("placeholder_suffix", 2), ("placeholder_title", 4), ("possible_swap", 2), @@ -303,12 +306,11 @@ pub fn problems(title: Option<&str>, author: Option<&str>, isbn: Option<&str>) - if title_in_author(Some(title), Some(author)) { reasons.push("title_in_author"); } - if isbn - .unwrap_or("") - .trim_matches(textkit::is_space) - .is_empty() - { + let isbn = isbn.unwrap_or("").trim_matches(textkit::is_space); + if isbn.is_empty() { reasons.push("missing_isbn"); + } else if let Some(fault) = crate::isbn::fault(isbn) { + reasons.push(fault); } reasons.into_iter().map(str::to_string).collect()