mirror of
https://git.pumpin.online/funkpower/catena.git
synced 2026-11-24 19:32:56 +00:00
Merge pull request 'colophon: the book's own ISBN first, not the series list's' (#17) from colophon-isbn-ranking into main
This commit is contained in:
commit
5acdb60e5c
2 files changed
+186
-13
No files matched your search
@@ -17,8 +17,17 @@ pub struct Imprint {
|
|||||||
pub place: Option<String>,
|
pub place: Option<String>,
|
||||||
/// The year of this edition, preferring first publication.
|
/// The year of this edition, preferring first publication.
|
||||||
pub year: Option<u16>,
|
pub year: Option<u16>,
|
||||||
/// Every checksum-valid ISBN printed on the page, in the order printed.
|
/// Every checksum-valid ISBN printed on the page, the book's own first.
|
||||||
|
///
|
||||||
|
/// A page that lists the other titles in a series prints each of their
|
||||||
|
/// ISBNs once, often before the book's own; the book's own is printed
|
||||||
|
/// again in its cataloguing block and on its copyright line. So the
|
||||||
|
/// ISBNs are ranked by how often the page prints them, and the order
|
||||||
|
/// printed only breaks a tie.
|
||||||
pub isbns: Vec<String>,
|
pub isbns: Vec<String>,
|
||||||
|
/// What the page says about an ISBN, where it says something: "vol. 2",
|
||||||
|
/// "hardback", "ebook". Keyed by the ISBN.
|
||||||
|
pub notes: Vec<(String, String)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
static PUBLISHED_BY: LazyLock<Regex> = LazyLock::new(|| {
|
static PUBLISHED_BY: LazyLock<Regex> = LazyLock::new(|| {
|
||||||
@@ -62,25 +71,69 @@ fn tidy(s: &str) -> String {
|
|||||||
.to_string()
|
.to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Every checksum-valid ISBN announced in a piece of text.
|
/// Every checksum-valid ISBN announced in a piece of text, the most often
|
||||||
|
/// printed first.
|
||||||
pub fn isbns(text: &str) -> Vec<String> {
|
pub fn isbns(text: &str) -> Vec<String> {
|
||||||
let mut out: Vec<String> = Vec::new();
|
stated(text).into_iter().map(|(isbn, _)| isbn).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The ISBNs a text announces, each with what is printed after it, ranked as
|
||||||
|
/// [`Imprint::isbns`] says.
|
||||||
|
fn stated(text: &str) -> Vec<(String, Option<String>)> {
|
||||||
|
// (isbn, times printed, first printed at, note)
|
||||||
|
let mut seen: Vec<(String, usize, usize, Option<String>)> = Vec::new();
|
||||||
for caps in ISBN.captures_iter(text) {
|
for caps in ISBN.captures_iter(text) {
|
||||||
let digits = bibclean::isbn::digits(&caps[1]);
|
let digits = bibclean::isbn::digits(&caps[1]);
|
||||||
if matches!(digits.len(), 10 | 13)
|
if !(matches!(digits.len(), 10 | 13) && bibclean::isbn::valid(&digits)) {
|
||||||
&& bibclean::isbn::valid(&digits)
|
continue;
|
||||||
&& !out.contains(&digits)
|
}
|
||||||
{
|
let end = caps.get(0).map_or(0, |whole| whole.end());
|
||||||
out.push(digits);
|
let note = note_after(&text[end..]);
|
||||||
|
match seen.iter_mut().find(|(isbn, ..)| *isbn == digits) {
|
||||||
|
Some(entry) => {
|
||||||
|
entry.1 += 1;
|
||||||
|
if entry.3.is_none() {
|
||||||
|
entry.3 = note;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => seen.push((digits, 1, end, note)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
out
|
seen.sort_by(|a, b| b.1.cmp(&a.1).then(a.2.cmp(&b.2)));
|
||||||
|
seen.into_iter()
|
||||||
|
.map(|(isbn, _, _, note)| (isbn, note))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// "(vol. 2, doml 29: alk. paper)", "hardback", "(pbk.)": what follows an
|
||||||
|
/// ISBN on its line, up to the next ISBN.
|
||||||
|
fn note_after(rest: &str) -> Option<String> {
|
||||||
|
let line = rest.split('\n').next().unwrap_or("");
|
||||||
|
let line = match line.to_lowercase().find("isbn") {
|
||||||
|
Some(at) => &line[..at],
|
||||||
|
None => line,
|
||||||
|
};
|
||||||
|
let note = line
|
||||||
|
.trim_start_matches(|c: char| c.is_whitespace() || c == '(' || c == '-' || c == '–')
|
||||||
|
.split(')')
|
||||||
|
.next()
|
||||||
|
.unwrap_or("")
|
||||||
|
.trim()
|
||||||
|
.trim_end_matches([',', ';', '.', '–', '-'])
|
||||||
|
.trim();
|
||||||
|
let has_words = note.chars().filter(|c| c.is_alphabetic()).count() >= 2;
|
||||||
|
(has_words && note.chars().count() <= 60).then(|| note.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Read the copyright page.
|
/// Read the copyright page.
|
||||||
pub fn read(text: &str) -> Imprint {
|
pub fn read(text: &str) -> Imprint {
|
||||||
|
let found = stated(text);
|
||||||
let mut out = Imprint {
|
let mut out = Imprint {
|
||||||
isbns: isbns(text),
|
isbns: found.iter().map(|(isbn, _)| isbn.clone()).collect(),
|
||||||
|
notes: found
|
||||||
|
.into_iter()
|
||||||
|
.filter_map(|(isbn, note)| note.map(|note| (isbn, note)))
|
||||||
|
.collect(),
|
||||||
..Imprint::default()
|
..Imprint::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -111,3 +164,36 @@ pub fn read(text: &str) -> Imprint {
|
|||||||
});
|
});
|
||||||
out
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn the_books_own_isbn_outranks_a_series_list() {
|
||||||
|
let page = "Other titles in the series\n\
|
||||||
|
THE CAMBRIDGE COMPANION TO CHRISTIAN DOCTRINE edited by Colin Gunton\n\
|
||||||
|
ISBN 0 521 47118 4 hardback ISBN 0 521 47695 8 paperback\n\
|
||||||
|
THE CAMBRIDGE COMPANION TO BIBLICAL INTERPRETATION edited by John Barton\n\
|
||||||
|
ISBN 0 521 48144 9 hardback ISBN 0 521 48593 2 paperback\n\
|
||||||
|
Library of Congress cataloguing in publication data\n\
|
||||||
|
ISBN 0 521 48144 9 (hardback) – ISBN 0 521 48593 2 (paperback)\n\
|
||||||
|
ISBN 0 521 48144 9 hardback\n";
|
||||||
|
let found = read(page);
|
||||||
|
assert_eq!(found.isbns[0], "0521481449");
|
||||||
|
assert!(
|
||||||
|
found
|
||||||
|
.notes
|
||||||
|
.contains(&("0521481449".to_string(), "hardback".to_string()))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_volumes_isbn_keeps_its_note() {
|
||||||
|
let page = "ISBN 978-0-674-72666-6 (vol. 1, doml 28 : alk. paper)\n\
|
||||||
|
ISBN 978-0-674-73083-0 (vol. 2, doml 29: alk. paper)\n";
|
||||||
|
let found = read(page);
|
||||||
|
assert_eq!(found.isbns, ["9780674726666", "9780674730830"]);
|
||||||
|
assert_eq!(found.notes[1].1, "vol. 2, doml 29: alk. paper");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -154,6 +154,45 @@ impl Printed {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// "Volume II", "vol. 2", "Part Three": the number of the volume a text names.
|
||||||
|
fn volume_of(text: &str) -> Option<u32> {
|
||||||
|
static VOLUME: std::sync::LazyLock<regex::Regex> = std::sync::LazyLock::new(|| {
|
||||||
|
regex::Regex::new(
|
||||||
|
r"(?i)\b(?:vol(?:ume)?|part|book|tome|band)\.?\s*([0-9]{1,2}|[ivxlc]{1,6}|one|two|three|four|five|six|seven|eight|nine|ten)\b",
|
||||||
|
)
|
||||||
|
.expect("a valid pattern")
|
||||||
|
});
|
||||||
|
let word = VOLUME.captures(text)?.get(1)?.as_str().to_lowercase();
|
||||||
|
if let Ok(n) = word.parse() {
|
||||||
|
return Some(n);
|
||||||
|
}
|
||||||
|
let words = [
|
||||||
|
"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
|
||||||
|
];
|
||||||
|
if let Some(at) = words.iter().position(|w| *w == word) {
|
||||||
|
return u32::try_from(at + 1).ok();
|
||||||
|
}
|
||||||
|
let mut total = 0u32;
|
||||||
|
let mut last = 0u32;
|
||||||
|
for c in word.chars().rev() {
|
||||||
|
let value = match c {
|
||||||
|
'i' => 1,
|
||||||
|
'v' => 5,
|
||||||
|
'x' => 10,
|
||||||
|
'l' => 50,
|
||||||
|
'c' => 100,
|
||||||
|
_ => return None,
|
||||||
|
};
|
||||||
|
if value < last {
|
||||||
|
total = total.checked_sub(value)?;
|
||||||
|
} else {
|
||||||
|
total += value;
|
||||||
|
last = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(total > 0).then_some(total)
|
||||||
|
}
|
||||||
|
|
||||||
/// Which page holds the title, given where the copyright page is.
|
/// Which page holds the title, given where the copyright page is.
|
||||||
///
|
///
|
||||||
/// The title page is the last page before the copyright page that is a page
|
/// The title page is the last page before the copyright page that is a page
|
||||||
@@ -325,14 +364,45 @@ pub fn read(text: &str, budget: usize) -> Printed {
|
|||||||
|
|
||||||
let imprint_text = copyright.map_or(front.as_str(), |i| pages[i].text.as_str());
|
let imprint_text = copyright.map_or(front.as_str(), |i| pages[i].text.as_str());
|
||||||
let stated = imprint::read(imprint_text);
|
let stated = imprint::read(imprint_text);
|
||||||
for isbn in stated.isbns {
|
// ISBNs are ranked over the whole front matter, not the one page taken
|
||||||
if !out.isbns.contains(&isbn) {
|
// for the copyright page: a page listing the series' other titles is
|
||||||
out.isbns.push(isbn);
|
// full of ISBNs and is taken for it, and on that page alone every ISBN
|
||||||
|
// is printed once.
|
||||||
|
let ranked = imprint::read(&front);
|
||||||
|
for isbn in &ranked.isbns {
|
||||||
|
if !out.isbns.contains(isbn) {
|
||||||
|
out.isbns.push(isbn.clone());
|
||||||
out.read_from
|
out.read_from
|
||||||
.entry("isbns".into())
|
.entry("isbns".into())
|
||||||
.or_insert(Source::CopyrightPage);
|
.or_insert(Source::CopyrightPage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// The page's own ranking, most often printed first, wins over the
|
||||||
|
// cataloguing block's, which is only sorted.
|
||||||
|
out.isbns.sort_by_key(|isbn| {
|
||||||
|
ranked
|
||||||
|
.isbns
|
||||||
|
.iter()
|
||||||
|
.position(|seen| seen == isbn)
|
||||||
|
.unwrap_or(usize::MAX)
|
||||||
|
});
|
||||||
|
// A set prints every volume's ISBN in every volume. The one whose note
|
||||||
|
// names this book's volume is this book's.
|
||||||
|
let heading = format!(
|
||||||
|
"{} {}",
|
||||||
|
out.title.as_deref().unwrap_or(""),
|
||||||
|
out.subtitle.as_deref().unwrap_or("")
|
||||||
|
);
|
||||||
|
if let Some(volume) = volume_of(&heading)
|
||||||
|
&& let Some((isbn, _)) = ranked
|
||||||
|
.notes
|
||||||
|
.iter()
|
||||||
|
.find(|(_, note)| volume_of(note) == Some(volume))
|
||||||
|
&& let Some(at) = out.isbns.iter().position(|held| held == isbn)
|
||||||
|
{
|
||||||
|
let isbn = out.isbns.remove(at);
|
||||||
|
out.isbns.insert(0, isbn);
|
||||||
|
}
|
||||||
if let Some(publisher) = stated.publisher {
|
if let Some(publisher) = stated.publisher {
|
||||||
out.publisher = Some(publisher);
|
out.publisher = Some(publisher);
|
||||||
out.note("publisher", Source::CopyrightPage);
|
out.note("publisher", Source::CopyrightPage);
|
||||||
@@ -360,3 +430,20 @@ pub fn read(text: &str, budget: usize) -> Printed {
|
|||||||
}
|
}
|
||||||
out
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod volume_tests {
|
||||||
|
use super::volume_of;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn volume_numbers_are_read() {
|
||||||
|
assert_eq!(
|
||||||
|
volume_of("On Difficulties in the Church Fathers: The Ambigua, Volume II"),
|
||||||
|
Some(2)
|
||||||
|
);
|
||||||
|
assert_eq!(volume_of("vol. 2, doml 29: alk. paper"), Some(2));
|
||||||
|
assert_eq!(volume_of("Part Three"), Some(3));
|
||||||
|
assert_eq!(volume_of("Book XIV"), Some(14));
|
||||||
|
assert_eq!(volume_of("The Book of Disquiet"), None);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user