bibclean: an ISBN-13's label is not part of the number

isbn::from_text read "ISBN-13 978-0-521-65706-8" as 1397805216: the label's
13 and the first eight digits. It now skips a -10/-13 label and returns the
first announced number whose check digit agrees.

isbn::fault names a stored ISBN that cannot be right, and problems() reports
it: invalid_isbn, placeholder_isbn (000-0-000-00000-0) and isbn_with_label
(ten digits beginning 13978 or 13979).
This commit is contained in:
funkpower committed 2026-11-24 11:24:35 -05:00
1 parent da7df1aaea
commit 1c7005460a
3 files changed
+77 -10

No files matched your search

+3
View File
@@ -202,6 +202,9 @@ bibclean books.ndjson | jq -c 'select(.slug_proposal.tier | IN("placeholder-auth
| `missing_apostrophe` | 2 | "Lossky s Argument" | | `missing_apostrophe` | 2 | "Lossky s Argument" |
| `possible_swap` | 2 | Title and author look to be the wrong way round | | `possible_swap` | 2 | Title and author look to be the wrong way round |
| `underscore_title` | 1 | Underscores standing in for spaces and colons | | `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 | | `missing_isbn` | 0 | Reported, but not a reason to do anything |
Each test is deliberately conservative. A real book called "The Document" or Each test is deliberately conservative. A real book called "The Document" or
+67 -5
View File
@@ -8,7 +8,7 @@ use textkit::pattern::SPACE;
/// An ISBN announced as one, in running text or a catalogue field. /// An ISBN announced as one, in running text or a catalogue field.
static ANNOUNCED: LazyLock<Regex> = LazyLock::new(|| { static ANNOUNCED: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(&format!( 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") .expect("a valid pattern")
}); });
@@ -70,11 +70,39 @@ pub fn digits(text: &str) -> String {
.collect() .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<String> { pub fn from_text(text: &str) -> Option<String> {
let found = ANNOUNCED.captures(text)?; ANNOUNCED
let digits = digits(&found[1]); .captures_iter(text)
matches!(digits.len(), 10 | 13).then_some(digits) .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. /// A checksum-valid ISBN embedded in a filename.
@@ -123,6 +151,40 @@ mod tests {
assert_eq!(from_text("no identifier here"), None); 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] #[test]
fn a_filename_that_is_an_isbn_gives_it_up() { fn a_filename_that_is_an_isbn_gives_it_up() {
assert_eq!( assert_eq!(
+7 -5
View File
@@ -161,6 +161,8 @@ const WEIGHTS: &[(&str, u32)] = &[
("edited_volume", 1), ("edited_volume", 1),
("filename_title", 3), ("filename_title", 3),
("html_entity", 2), ("html_entity", 2),
("invalid_isbn", 3),
("isbn_with_label", 3),
("junk_title", 3), ("junk_title", 3),
("lowercase_title", 2), ("lowercase_title", 2),
("missing_apostrophe", 2), ("missing_apostrophe", 2),
@@ -171,6 +173,7 @@ const WEIGHTS: &[(&str, u32)] = &[
("numeric_author", 4), ("numeric_author", 4),
("path_title", 4), ("path_title", 4),
("placeholder_author", 4), ("placeholder_author", 4),
("placeholder_isbn", 3),
("placeholder_suffix", 2), ("placeholder_suffix", 2),
("placeholder_title", 4), ("placeholder_title", 4),
("possible_swap", 2), ("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)) { if title_in_author(Some(title), Some(author)) {
reasons.push("title_in_author"); reasons.push("title_in_author");
} }
if isbn let isbn = isbn.unwrap_or("").trim_matches(textkit::is_space);
.unwrap_or("") if isbn.is_empty() {
.trim_matches(textkit::is_space)
.is_empty()
{
reasons.push("missing_isbn"); reasons.push("missing_isbn");
} else if let Some(fault) = crate::isbn::fault(isbn) {
reasons.push(fault);
} }
reasons.into_iter().map(str::to_string).collect() reasons.into_iter().map(str::to_string).collect()