Merge pull request 'colophon: role words, page counts and title lines are not people' (#21) from colophon-cip-roles into main

This commit is contained in:
funkpower committed 2026-11-24 08:59:47 -05:00
commit bd933157c9
2 files changed
+212 -13

No files matched your search

+173 -13
View File
@@ -95,34 +95,138 @@ fn tidy(s: &str) -> String {
.to_string() .to_string()
} }
/// Where a statement of responsibility stops naming people: the physical
/// description ("p. cm.") and the identifiers a cataloguing block runs on to.
static STATEMENT_END: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"(?i)\b(?:p\.\s?cm\b|pages\s+cm\b|cm\.|ISSN\b|ISBN\b)").expect("a valid pattern")
});
/// A role written before the names it applies to, and whose names they are.
const LEADING_ROLES: &[(&str, Role)] = &[
("edited and translated by ", Role::Editor),
("edited with an introduction by ", Role::Editor),
("edited by ", Role::Editor),
("selected and edited by ", Role::Editor),
("compiled by ", Role::Editor),
("general editor ", Role::Editor),
("translated by ", Role::Skip),
("translated from ", Role::Skip),
("translated with ", Role::Skip),
("with ", Role::Skip),
("foreword by ", Role::Skip),
("introduction by ", Role::Skip),
("illustrated by ", Role::Skip),
("notes by ", Role::Skip),
("by ", Role::Author),
];
/// A role written after a name: "Irving B. Weiner, editor-in-chief".
const TRAILING_ROLES: &[&str] = &[
"editor-in-chief",
"general editor",
"editors",
"editor",
"eds.",
"eds",
"ed.",
];
#[derive(Clone, Copy, PartialEq)]
enum Role {
Author,
Editor,
Skip,
}
/// Split a statement of responsibility into who wrote and who edited. /// Split a statement of responsibility into who wrote and who edited.
///
/// Every name is held to the shape of a person's name, so the role words,
/// fragments and page counts a cataloguing block is full of ("edited by" on
/// a line of its own, "translated from the French", "p. cm") are dropped
/// rather than read as people.
fn people(statement: &str) -> (Vec<String>, Vec<String>) { fn people(statement: &str) -> (Vec<String>, Vec<String>) {
let statement = match STATEMENT_END.find(statement) {
Some(found) => &statement[..found.start()],
None => statement,
};
let mut authors = Vec::new(); let mut authors = Vec::new();
let mut editors = Vec::new(); let mut editors = Vec::new();
for part in statement.split(';') { for part in statement.split(';') {
// "[edited by] Konrad H. Kinzl": the role in the brackets a
// cataloguer adds when the book did not state it.
let part = part.trim().replacen('[', "", 1).replacen(']', "", 1);
let part = part.trim(); let part = part.trim();
let lower = part.to_lowercase(); let lower = part.to_lowercase();
let (target, rest) = if let Some(rest) = lower.strip_prefix("edited by ") { let (mut role, mut rest) = (Role::Author, part);
(&mut editors, &part[part.len() - rest.len()..]) for (prefix, given) in LEADING_ROLES {
} else if lower.starts_with("translated by ") if lower.starts_with(prefix) {
|| lower.starts_with("with ") role = *given;
|| lower.starts_with("foreword by ") rest = &part[prefix.len()..];
|| lower.starts_with("introduction by ") break;
{ }
}
if role == Role::Skip {
continue; continue;
} else { }
(&mut authors, part) // "Alan Carr.— 2nd ed": the edition statement that follows the names.
}; let rest = EDITION_TAIL.replace(rest, "");
for name in rest.split(" and ").flat_map(|n| n.split(',')) { for name in rest.split(" and ").flat_map(|n| n.split(',')) {
let name = tidy(name); let mut name = tidy(name);
if name.len() >= 3 && name.split_whitespace().count() <= 5 { let mut this_role = role;
target.push(name); let lowered = name.to_lowercase();
if let Some(suffix) = TRAILING_ROLES.iter().find(|r| lowered.ends_with(*r)) {
name = tidy(&name[..name.len() - suffix.len()]);
this_role = Role::Editor;
}
// A role alone on its part names whoever came before it.
if name.is_empty() && this_role == Role::Editor {
if let Some(last) = authors.pop() {
editors.push(last);
}
continue;
}
if name.split_whitespace().count() > 5 {
continue;
}
let Some(person) = bibclean::author::clean(&name) else {
continue;
};
if !reads_as_a_name(&person) {
continue;
}
if person.split_whitespace().count() < 2 {
continue;
}
match this_role {
Role::Editor => editors.push(person),
_ => authors.push(person),
} }
} }
} }
(authors, editors) (authors, editors)
} }
/// Whether a candidate reads as a name rather than a line of a title: no
/// little words ("Paths TO"), and no word left in capitals beside words that
/// are not ("WISDOM’S Journey").
pub(crate) fn reads_as_a_name(name: &str) -> bool {
const LITTLE: &[&str] = &[
"a", "an", "and", "for", "in", "of", "on", "the", "to", "with",
];
let words: Vec<&str> = name.split_whitespace().collect();
if words
.iter()
.any(|w| LITTLE.contains(&w.to_lowercase().as_str()))
{
return false;
}
let shouting = |w: &&str| {
let letters: Vec<char> = w.chars().filter(|c| c.is_alphabetic()).collect();
letters.len() > 3 && letters.iter().all(|c| c.is_uppercase())
};
!(words.iter().any(shouting) && !words.iter().all(shouting))
}
fn filed_to_reading(line: &str) -> Option<String> { fn filed_to_reading(line: &str) -> Option<String> {
let caps = FILED_NAME.captures(line)?; let caps = FILED_NAME.captures(line)?;
Some(format!( Some(format!(
@@ -272,3 +376,59 @@ pub fn read(front: &str) -> Option<Cip> {
cip.isbns.dedup(); cip.isbns.dedup();
if cip.is_empty() { None } else { Some(cip) } if cip.is_empty() { None } else { Some(cip) }
} }
#[cfg(test)]
mod people_tests {
use super::people;
#[test]
fn role_words_are_not_people() {
for statement in [
"edited by",
"translated from the French",
"selected and",
"index",
"ISSN 0920-",
] {
assert_eq!(people(statement), (vec![], vec![]), "{statement}");
}
}
#[test]
fn the_names_are_read_and_the_roles_kept() {
assert_eq!(
people("Greg Palast. p. cm"),
(vec!["Greg Palast".to_string()], vec![])
);
assert_eq!(
people("Alan Carr.— 2nd ed"),
(vec!["Alan Carr".to_string()], vec![])
);
assert_eq!(
people("[edited by] Konrad H. Kinzl"),
(vec![], vec!["Konrad H. Kinzl".to_string()])
);
assert_eq!(people("Paths TO"), (vec![], vec![]));
assert_eq!(
people("Irving B. Weiner, editor-in-chief"),
(vec![], vec!["Irving B. Weiner".to_string()])
);
assert_eq!(
people("edited by Stanley Hauerwas and Samuel Wells"),
(
vec![],
vec!["Stanley Hauerwas".to_string(), "Samuel Wells".to_string()]
)
);
assert_eq!(
people("by David W. Fischer and Alan E. Bessette ; foreword by Gary Lincoff"),
(
vec![
"David W. Fischer".to_string(),
"Alan E. Bessette".to_string()
],
vec![]
)
);
}
}
+39
View File
@@ -312,14 +312,53 @@ fn is_furniture(line: &str) -> bool {
false false
} }
/// The people a byline names, each held to the shape of a person's name:
/// a title page sets a series name or a line of the title in the same
/// capitals as its author ("MARESFIELD LIBRARY", "A BASIC THEORY OF").
fn people(who: &str) -> Vec<String> { fn people(who: &str) -> Vec<String> {
who.split(&[';', '&'][..]) who.split(&[';', '&'][..])
.flat_map(|part| part.split(" and ")) .flat_map(|part| part.split(" and "))
.map(|name| name.trim().trim_end_matches(['.', ',']).trim().to_string()) .map(|name| name.trim().trim_end_matches(['.', ',']).trim().to_string())
.filter(|name| name.len() >= 3 && name.split_whitespace().count() <= 5) .filter(|name| name.len() >= 3 && name.split_whitespace().count() <= 5)
.filter_map(|name| bibclean::author::clean(&name))
.filter(|name| crate::cip::reads_as_a_name(name))
.filter(|name| name.split_whitespace().count() >= 2 || is_known_mononym(name))
.collect() .collect()
} }
/// A single name is a person only where it is one of the handful that go by
/// one: anything else alone on a byline line is a word of the title.
fn is_known_mononym(name: &str) -> bool {
const MONONYMS: &[&str] = &[
"Aeschylus",
"Aristotle",
"Augustine",
"Cicero",
"Dante",
"Epictetus",
"Euripides",
"Herodotus",
"Hesiod",
"Homer",
"Josephus",
"Livy",
"Lucretius",
"Ovid",
"Plato",
"Plotinus",
"Plutarch",
"Proclus",
"Seneca",
"Sophocles",
"Tacitus",
"Thucydides",
"Virgil",
"Voltaire",
"Xenophon",
];
MONONYMS.contains(&name)
}
/// Words a line ends on when the typesetter broke a title across it. /// Words a line ends on when the typesetter broke a title across it.
/// ///
/// `Esotericism in` / `Western Culture` is one title set on two lines, and /// `Esotericism in` / `Western Culture` is one title set on two lines, and