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.
This commit is contained in:
funkpower committed 2026-11-24 08:47:06 -05:00
1 parent a77df8515e
commit a8f0800192
6 files changed
+238 -3

No files matched your search

+36
View File
@@ -184,6 +184,42 @@ pub fn strip_name_prefix(title: &str) -> Option<String> {
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<Regex> = 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<Regex> = 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<String> {
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<String> {
let stripped = stripped
+45 -1
View File
@@ -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<String>;
let wrappers: [(Strip, &str); 3] = [
fn strip_number(title: &str) -> Option<String> {
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"));
+8
View File
@@ -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");
}