mirror of
https://git.pumpin.online/funkpower/catena.git
synced 2026-11-24 19:32:56 +00:00
bibclean: set a title's punctuation as a title page sets it
title_punctuation (weight 1): a space before a colon ("Sex Wars :
Sexual Dissent", a catalogue's habit), runs of spaces, a dangling
separator or full stop at the end (kept after an initial or an
abbreviation), and an article filed behind the title ("Jesuits, The")
put back in front. 128 titles in a 6,022-record catalogue.
This commit is contained in:
4 files changed
+94
-1
No files matched your search
@@ -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
|
||||
|
||||
@@ -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 & Apprentice", "Claudia Gray"));
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user