From a8f08001924ac23c045736200fada777b3a2f612 Mon Sep 17 00:00:00 2001 From: funkpower Date: Tue, 24 Nov 2026 08:47:06 -0500 Subject: [PATCH] seriesof and bibclean: numbers and release tags written around a title seriesof gains the numbered method: "Known Space-038-The Soft Weapon", "Ice and Fire 2 - A Clash of Kings", "James Bond - 032 - Blast from the Past" give the series, the number and the title without them. The number has to stand between separators on both sides, so "The 200-Year Plot", "The 12-Month Gardener" and "C.350-C.1450" stay titles. It is looked for before a volume marker, so "Known Space-017-The Ethics of Madness (Part 1)" is number 17 of Known Space and keeps its "Part 1". bibclean names and repairs two more: numbered_title, a bare number in front ("01 - The Three Musketeers", at most 200, so "911 - ..." and a year stay), and release_tags after the title ("(BD) (v4 0) (lit)", "(Scan, OCR)", "(no OCR)"). Over a 6,022-record catalogue: 73 numbered titles across Known Space, Ice and Fire, James Bond, Wild Cards and Time Odyssey; 12 bare numbers; 4 sets of release tags. None wrong. --- crates/bibclean/README.md | 3 + crates/bibclean/src/fix.rs | 36 +++++++++++ crates/bibclean/src/lib.rs | 46 +++++++++++++- crates/bibclean/src/reasons.rs | 8 +++ crates/seriesof/src/lib.rs | 42 ++++++++++++- crates/seriesof/src/numbered.rs | 106 ++++++++++++++++++++++++++++++++ 6 files changed, 238 insertions(+), 3 deletions(-) create mode 100644 crates/seriesof/src/numbered.rs diff --git a/crates/bibclean/README.md b/crates/bibclean/README.md index 3ac92b1..5f447c8 100644 --- a/crates/bibclean/README.md +++ b/crates/bibclean/README.md @@ -189,6 +189,8 @@ bibclean books.ndjson | jq -c 'select(.slug_proposal.tier | IN("placeholder-auth | `author_is_title` | 4 | The title copied into the author field | | `author_role_suffix` | 2 | A catalogue heading's dates, role or fuller form in the author field: "Lossky, Vladimir, 1903-1958", "Vico, Giambattista (Author)" | | `author_list_commas` | 1 | Several authors joined with commas or semicolons rather than "&" | +| `numbered_title` | 1 | A set's number in front of the title: "01 - The Three Musketeers" | +| `release_tags` | 1 | A release group's tags after the title: "(v4 0) (lit)", "(Scan, OCR)" | | `name_in_title` | 2 | The author's name in front of the title, filename fashion: "Wilfred R Bion-Cogitations-Karnac Books (1991)" | | `account_author` | 3 | The login of whoever made the file: "mymarasigan", "HP_Administrator" | | `publisher_as_author` | 3 | An imprint in the author field | @@ -217,6 +219,7 @@ an author field is wrong while "Oxford, Michael" is a person. | `filename_title` | A document extension comes off the end | | `placeholder_suffix` | The "- Unknown" comes off the end | | `author_role_suffix`, `author_list_commas` | The heading put as a byline: dates, roles and fuller forms off, each name given names first, the names joined with "&". A field that is only "Surname, Given" is left alone, since some catalogues keep that order on purpose; `bibclean::heading::as_byline` turns it round for a caller that wants bylines | +| `numbered_title`, `release_tags` | The number comes off the front, the tags off the end. A series named in front as well ("Known Space-038-…") is `seriesof`'s, which keeps the series | | `name_in_title` | The names come off the front, and the publisher, year and series off the end; the names are proposed as the author. Made only when the record has no real author, or already has the same one | The last three are made only when what is left is still a title: "Microsoft diff --git a/crates/bibclean/src/fix.rs b/crates/bibclean/src/fix.rs index dfaba72..5c1f408 100644 --- a/crates/bibclean/src/fix.rs +++ b/crates/bibclean/src/fix.rs @@ -184,6 +184,42 @@ pub fn strip_name_prefix(title: &str) -> Option { worth_keeping(title, &rest) } +/// "01 - The Three Musketeers": a number in front of the title, where a +/// filename counted the volumes of a set without naming it. +static LEADING_NUMBER: LazyLock = LazyLock::new(|| { + Regex::new(&format!( + r"^{SPACE}*(\d{{1,3}}){SPACE}*[-–—.]{SPACE}+(\p{{Lu}}.*)$" + )) + .expect("a valid pattern") +}); + +/// The tags a release group puts after a title: "(v4 0)", "(lit)", "(BD)". +static RELEASE_TAGS: LazyLock = LazyLock::new(|| { + Regex::new(&format!( + r"(?i)(?:{SPACE}*\((?:v{SPACE}?\d+(?:[ ._]\d+)*|lit|html?|rtf|txt|pdb|bd|retail|scan(?:,{SPACE}*ocr)?|no{SPACE}ocr|ocr)\))+{SPACE}*$" + )) + .expect("a valid pattern") +}); + +/// The number a set put in front of the title, and the title without it. +/// +/// Only a small number: "911 - Decent into Tyranny" is a date, not volume +/// nine hundred and eleven, and a year in front ("2003 - …") is the year. +pub fn strip_leading_number(title: &str) -> Option<(u32, String)> { + let found = LEADING_NUMBER.captures(title)?; + let number: u32 = found[1].parse().ok()?; + if number > 200 { + return None; + } + worth_keeping(title, &found[2]).map(|rest| (number, rest)) +} + +/// A title without the tags a release group put after it. +pub fn strip_release_tags(title: &str) -> Option { + let found = RELEASE_TAGS.find(title)?; + worth_keeping(title, &title[..found.start()]) +} + /// The stripped title, if it is a different title and still a title. fn worth_keeping(original: &str, stripped: &str) -> Option { let stripped = stripped diff --git a/crates/bibclean/src/lib.rs b/crates/bibclean/src/lib.rs index 353395b..049c66a 100644 --- a/crates/bibclean/src/lib.rs +++ b/crates/bibclean/src/lib.rs @@ -161,10 +161,15 @@ pub fn clean(book: &Book) -> Cleaned { // name in front, then the extension, which a filename writes after the // placeholder author ("Title - Unknown.pdf"), then that author. type Strip = fn(&str) -> Option; - let wrappers: [(Strip, &str); 3] = [ + fn strip_number(title: &str) -> Option { + fix::strip_leading_number(title).map(|(_, rest)| rest) + } + let wrappers: [(Strip, &str); 5] = [ (fix::strip_producer, "junk_title"), (fix::strip_extension, "filename_title"), (fix::strip_placeholder_suffix, "placeholder_suffix"), + (fix::strip_release_tags, "release_tags"), + (strip_number, "numbered_title"), ]; for (strip, why) in wrappers { if let Some(title) = cleaned.title.clone() @@ -502,6 +507,45 @@ mod tests { } } + #[test] + fn a_counted_title_and_release_tags_come_off() { + for (title, want, why) in [ + ( + "01 - The Three Musketeers", + "The Three Musketeers", + "numbered_title", + ), + ( + "06 - The Man in the Iron Mask", + "The Man in the Iron Mask", + "numbered_title", + ), + ( + "A Clash of Kings (BD) (v4 0) (lit)", + "A Clash of Kings", + "release_tags", + ), + ( + "Orpheus and the Roots of Platonism (no OCR)", + "Orpheus and the Roots of Platonism", + "release_tags", + ), + ] { + let cleaned = clean(&book(title, "Someone Else")); + assert_eq!(cleaned.book.title.as_deref(), Some(want), "{title}"); + assert!(whys(&cleaned).contains(&why), "{title}"); + } + for title in [ + "911 - Decent into Tyranny", + "2003 - Uprising! The Hungarian Revolution of 1956", + "1984", + "The Book of Lit", + ] { + let cleaned = clean(&book(title, "Someone Else")); + assert_eq!(cleaned.book.title.as_deref(), Some(title), "{title}"); + } + } + #[test] fn a_shouting_title_is_cased_and_the_change_is_recorded() { let cleaned = clean(&book("THE HUMAN CONDITION", "Arendt, Hannah")); diff --git a/crates/bibclean/src/reasons.rs b/crates/bibclean/src/reasons.rs index 521fcc1..c78aec7 100644 --- a/crates/bibclean/src/reasons.rs +++ b/crates/bibclean/src/reasons.rs @@ -166,6 +166,7 @@ const WEIGHTS: &[(&str, u32)] = &[ ("mojibake_author", 3), ("mojibake_title", 3), ("name_in_title", 2), + ("numbered_title", 1), ("numeric_author", 4), ("path_title", 4), ("placeholder_author", 4), @@ -173,6 +174,7 @@ const WEIGHTS: &[(&str, u32)] = &[ ("placeholder_title", 4), ("possible_swap", 2), ("publisher_as_author", 3), + ("release_tags", 1), ("sentence_case_title", 1), ("title_in_author", 3), ("underscore_title", 1), @@ -207,6 +209,12 @@ pub fn problems(title: Option<&str>, author: Option<&str>, isbn: Option<&str>) - if URL_TITLE.is_match(title) { reasons.push("url_title"); } + if crate::fix::strip_leading_number(title).is_some() { + reasons.push("numbered_title"); + } + if crate::fix::strip_release_tags(title).is_some() { + reasons.push("release_tags"); + } if FILENAME_TITLE.is_match(title) { reasons.push("filename_title"); } diff --git a/crates/seriesof/src/lib.rs b/crates/seriesof/src/lib.rs index 9652cd7..8854b0f 100644 --- a/crates/seriesof/src/lib.rs +++ b/crates/seriesof/src/lib.rs @@ -18,13 +18,19 @@ //! numbered, so filing one under a numbered series makes every one of them //! display as volume one, asserting an order the data does not have. Those //! get a series and no number. +//! +//! A **numbered title** — "Known Space-038-The Soft Weapon" — is a position +//! written the way a filename writes it, series and number in front. It +//! gives the series, the number, and the title with both taken off. +pub mod numbered; pub mod publisher; pub mod volume; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; +pub use numbered::parse_numbered; pub use publisher::publisher_series; pub use volume::{parse_volume, roman_or_int}; @@ -60,6 +66,9 @@ pub enum Method { Volume, /// Publisher shorthand in a parenthetical: membership, not position. Publisher, + /// A series and a number written in front of the title, filename + /// fashion: a position, and the title without them. + Numbered, } fn version() -> u32 { @@ -74,9 +83,25 @@ impl corpus_core::Record for Proposal { /// What a title and author say about the series, if anything. /// -/// The volume marker is looked for first: it carries a number, and a number -/// is worth more than membership. +/// A numbered title is looked for first, then a volume marker: both carry a +/// number, and a number is worth more than membership. pub fn series_of(title: &str, author: Option<&str>) -> Option { + // A numbered title first: "Known Space-017-The Ethics of Madness (Part + // 1)" is the seventeenth of Known Space, and the "Part 1" after it is the + // story's own business, not a series called "Known Space-017-The Ethics + // of Madness". + if let Some((series, volume, rest)) = parse_numbered(title) { + return Some(Proposal { + v: corpus_core::V, + title: title.to_string(), + author: author.map(str::to_string), + series, + volume: Some(volume), + proposed_title: Some(rest), + method: Method::Numbered, + }); + } + if let Some((series, volume)) = parse_volume(title) { return Some(Proposal { v: corpus_core::V, @@ -132,6 +157,19 @@ mod tests { assert_eq!(found.volume, Some(2)); } + #[test] + fn a_numbered_title_wins_over_a_part_inside_it() { + let found = + series_of("Known Space-017-The Ethics of Madness (Part 1)", None).expect("a series"); + assert_eq!(found.series, "Known Space"); + assert_eq!(found.volume, Some(17)); + assert_eq!(found.method, Method::Numbered); + assert_eq!( + found.proposed_title.as_deref(), + Some("The Ethics of Madness (Part 1)") + ); + } + #[test] fn an_ordinary_title_proposes_nothing() { assert_eq!(series_of("Being and Time", Some("Heidegger")), None); diff --git a/crates/seriesof/src/numbered.rs b/crates/seriesof/src/numbered.rs new file mode 100644 index 0000000..32f06db --- /dev/null +++ b/crates/seriesof/src/numbered.rs @@ -0,0 +1,106 @@ +//! A series and a number written in front of the title, as a filename +//! writes them: "Known Space-038-The Soft Weapon", "Ice and Fire 2 - A Clash +//! of Kings", "James Bond - 032 - Blast from the Past". + +use std::sync::LazyLock; + +use fancy_regex::Regex; + +use crate::volume::roman_or_int; + +/// The series, the number between separators, then the title. The number +/// has to stand between separators on both sides, so "The 200-Year Plot" +/// and "The 12-Month Gardener" are titles with numbers in them, not +/// numbered titles. +static NUMBERED: LazyLock = LazyLock::new(|| { + Regex::new( + r"^(?P[\p{L}][^:\d]{1,60}?)(?:\s+(?P\d{1,3})\s+[-–—.]\s+|\s*[-–—]\s*(?P\d{1,3})\s*[-–—]\s*)(?P\p{Lu}.*)$", + ) + .expect("a valid pattern") +}); + +/// A series name, a volume and the title without them, or `None` when the +/// title does not open that way. +pub fn parse_numbered(title: &str) -> Option<(String, u32, String)> { + let found = NUMBERED.captures(title.trim()).ok().flatten()?; + let series = found["series"].trim_matches(|c: char| c.is_whitespace() || "-–—".contains(c)); + if series.split_whitespace().count() > 6 || !series.chars().any(char::is_alphabetic) { + return None; + } + // "00 - The Hedge Knight": a prequel numbered zero is still in the + // series, and zero is its place. + let number = found.name("a").or_else(|| found.name("b"))?.as_str(); + let volume = if number.chars().all(|c| c == '0') { + 0 + } else { + roman_or_int(number)? + }; + let rest = found["title"].trim().trim_end_matches('.').trim(); + if rest.is_empty() { + return None; + } + Some((series.to_string(), volume, rest.to_string())) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_numbered_title_gives_series_volume_and_title() { + for (title, series, volume, rest) in [ + ( + "Known Space-038-The Soft Weapon", + "Known Space", + 38, + "The Soft Weapon", + ), + ( + "Ice and Fire 2 - A Clash of Kings", + "Ice and Fire", + 2, + "A Clash of Kings", + ), + ( + "Ice and Fire 00 - The Hedge Knight", + "Ice and Fire", + 0, + "The Hedge Knight", + ), + ( + "James Bond - 032 - Blast from the Past", + "James Bond", + 32, + "Blast from the Past", + ), + ("Time Odyssey 2 - Sunstorm", "Time Odyssey", 2, "Sunstorm"), + ( + "Known Space-040-Destroyer of Worlds.", + "Known Space", + 40, + "Destroyer of Worlds", + ), + ] { + assert_eq!( + parse_numbered(title), + Some((series.to_string(), volume, rest.to_string())), + "{title}" + ); + } + } + + #[test] + fn a_title_with_a_number_in_it_is_not_numbered() { + for title in [ + "Infiltration: The 200-Year Plot to Destroy the Catholic Church", + "The 12-Month Vegetable Gardener's Handbook", + "The Cambridge History of Medieval Political Thought C.350-C.1450", + "The Chronicles of Narnia Complete 7-Book Collection", + "CUUS309-FM p1-16", + "01 - The Three Musketeers", + "Being and Time", + ] { + assert_eq!(parse_numbered(title), None, "{title}"); + } + } +}