Merge pull request 'bibclean: set a title's punctuation as a title page sets it' (#24) from bibclean-title-punctuation into main

This commit is contained in:
funkpower committed 2026-11-24 09:13:31 -05:00
commit da7df1aaea
4 files changed
+94 -1

No files matched your search

+2
View File
@@ -190,6 +190,7 @@ 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 "&" |
| `title_punctuation` | 1 | A space before a colon, double spaces, a dangling separator or full stop, "Jesuits, The" |
| `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)" |
@@ -221,6 +222,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 |
| `title_punctuation` | Set as a title page sets it; a full stop after an initial or abbreviation stays |
| `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 |
+35
View File
@@ -259,6 +259,41 @@ pub fn decode_entities(text: &str) -> Option<String> {
(decoded != text).then(|| decoded.into_owned())
}
/// A title's punctuation set the way a title page sets it: no space before
/// a colon ("Sex Wars : Sexual Dissent" is a catalogue's), one space between
/// words, no dangling separator or full stop at the end, and an article put
/// back in front ("Jesuits, The" is a filing form).
pub fn tidy_punctuation(title: &str) -> Option<String> {
static BEFORE_COLON: LazyLock<Regex> =
LazyLock::new(|| Regex::new(&format!(r"{SPACE}+:{SPACE}*")).expect("a valid pattern"));
static INVERTED_ARTICLE: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(&format!(
r"^(?P<head>[^:,]+),{SPACE}*(?P<article>The|A|An)(?P<tail>{SPACE}*:.*)?$"
))
.expect("a valid pattern")
});
let mut out = BEFORE_COLON.replace_all(title, ": ").into_owned();
out = SPACE_RUN.replace_all(&out, " ").into_owned();
let trimmed = out.trim_matches(textkit::is_space);
// A full stop ends a title only when it ends an initial or an
// abbreviation: "Crusade for Pan-Europe. Autobiography of a man." loses
// it, "Martin Luther King Jr." keeps it.
let last_word = trimmed.split_whitespace().last().unwrap_or("");
let keep_stop =
last_word.trim_end_matches('.').chars().count() <= 3 || trimmed.ends_with("...");
let mut cleaned = trimmed
.trim_end_matches([',', ';', ':', '-', '–', '—', ' '])
.to_string();
if cleaned.ends_with('.') && !keep_stop {
cleaned.pop();
}
if let Some(found) = INVERTED_ARTICLE.captures(&cleaned) {
let tail = found.name("tail").map_or("", |t| t.as_str()).trim_start();
cleaned = format!("{} {}{}", &found["article"], found["head"].trim(), tail);
}
worth_keeping(title, &cleaned).filter(|fixed| fixed != title)
}
/// 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
+53 -1
View File
@@ -240,6 +240,18 @@ pub fn clean(book: &Book) -> Cleaned {
cleaned.author = Some(byline);
}
if let Some(title) = cleaned.title.clone()
&& let Some(fixed) = fix::tidy_punctuation(&title)
{
changes.push(Change {
field: "title".into(),
from: title,
to: fixed.clone(),
why: "title_punctuation".into(),
});
cleaned.title = Some(fixed);
}
// Underscores first: a title mangled by a filename may also be shouting,
// and title-casing it before the underscores come out would case the
// format suffix as though it were a word.
@@ -440,7 +452,6 @@ mod tests {
"Gödel, Escher, Bach - An Eternal Golden Braid",
"The Pdf Primer",
"Cafe Society",
"A Title Left Trailing -",
] {
let cleaned = clean(&book(title, "Someone"));
assert_eq!(cleaned.book.title.as_deref(), Some(title), "{title}");
@@ -566,6 +577,47 @@ mod tests {
}
}
#[test]
fn a_dangling_separator_is_punctuation_not_a_wrapper() {
let cleaned = clean(&book("A Title Left Trailing -", "Someone"));
assert_eq!(cleaned.book.title.as_deref(), Some("A Title Left Trailing"));
assert_eq!(whys(&cleaned), ["title_punctuation"]);
}
#[test]
fn a_titles_punctuation_is_set_as_printed() {
for (title, want) in [
(
"Sex Wars : Sexual Dissent and Political Culture",
"Sex Wars: Sexual Dissent and Political Culture",
),
("Jesuits, The", "The Jesuits"),
(
"Bible Made Impossible, The: Moving from Biblicism",
"The Bible Made Impossible: Moving from Biblicism",
),
("Wissenschaft Der Logik -", "Wissenschaft Der Logik"),
(
"Crusade for Pan-Europe. Autobiography of a man and a movement.",
"Crusade for Pan-Europe. Autobiography of a man and a movement",
),
("Passions of Christ", "Passions of Christ"),
] {
let cleaned = clean(&book(title, "Someone"));
assert_eq!(cleaned.book.title.as_deref(), Some(want), "{title}");
}
for title in [
"Martin Luther King Jr.",
"Wait...",
"Being and Time",
"Me, Myself and I",
"Hegel: The Phenomenology",
] {
let cleaned = clean(&book(title, "Someone"));
assert_eq!(cleaned.book.title.as_deref(), Some(title), "{title}");
}
}
#[test]
fn a_character_reference_is_decoded() {
let cleaned = clean(&book("Master &amp; Apprentice", "Claudia Gray"));
+4
View File
@@ -178,6 +178,7 @@ const WEIGHTS: &[(&str, u32)] = &[
("release_tags", 1),
("sentence_case_title", 1),
("title_in_author", 3),
("title_punctuation", 1),
("underscore_title", 1),
("unknown_author", 3),
("url_title", 4),
@@ -210,6 +211,9 @@ 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::tidy_punctuation(title).is_some() {
reasons.push("title_punctuation");
}
if crate::fix::strip_leading_number(title).is_some() {
reasons.push("numbered_title");
}