mirror of
https://git.pumpin.online/funkpower/catena.git
synced 2026-11-24 19:32:56 +00:00
bibclean: repair mojibake, word-processor prefixes, extensions and "- Unknown"
textkit gains mojibake::repair, which reads UTF-8 that was decoded as Windows-1252 back into what it was. It is certain rather than guessed: every character has to turn back into a byte and the bytes have to be valid UTF-8, which real Latin-1 text almost never is. bibclean names two new faults, mojibake_title/mojibake_author and placeholder_suffix, and now repairs four it only named before: - mojibake in the title or author; - "Microsoft Word - " and the like on the front of a title (junk_title); - a document extension on the end (filename_title); - a placeholder author on the end, "Title - Unknown" (placeholder_suffix). A wrapper is taken off only when what is left is still a title, so "Microsoft Word - Document2" and "0195152972.pdf" stay flagged. "Document1", "Document2" and so on now count as placeholder titles. Run over a 6,022-record catalogue: 120 records repaired by the new rules (67 suffixes, 28 extensions, 21 prefixes, 4 mojibake), none of them wrong.
This commit is contained in:
7 files changed
+359
-1
No files matched your search
@@ -24,6 +24,23 @@ static SPACE_RUN: LazyLock<Regex> =
|
||||
/// A run of letters, by Python's definition of a letter.
|
||||
static LETTERS: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\p{L}+").expect("a valid pattern"));
|
||||
|
||||
/// The name of the program a document was made in, put in front of the title
|
||||
/// by the program and carried into the catalogue by whatever read it.
|
||||
static PRODUCER_PREFIX: LazyLock<Regex> = LazyLock::new(|| {
|
||||
Regex::new(&format!(
|
||||
r"(?i)^{SPACE}*microsoft{SPACE}+(?:office{SPACE}+)?(?:word|powerpoint|excel|publisher){SPACE}*[-–—:]{SPACE}*"
|
||||
))
|
||||
.expect("a valid pattern")
|
||||
});
|
||||
|
||||
/// A document's file extension on the end of its title.
|
||||
static DOCUMENT_EXTENSION: LazyLock<Regex> = LazyLock::new(|| {
|
||||
Regex::new(&format!(
|
||||
r"(?i)\.(?:pdf|epub|mobi|azw3?|txt|wpd|docx?|odt|rtf|tex|qxd|indd|pm\d|djvu){SPACE}*$"
|
||||
))
|
||||
.expect("a valid pattern")
|
||||
});
|
||||
|
||||
/// Words that stay lowercase inside a title.
|
||||
const SMALL_WORDS: &[&str] = &[
|
||||
"a", "an", "and", "as", "at", "but", "by", "for", "from", "in", "into", "nor", "of", "on",
|
||||
@@ -112,6 +129,45 @@ pub fn clean_underscores(title: &str) -> String {
|
||||
.to_string()
|
||||
}
|
||||
|
||||
/// A title without the name of the program that made the file.
|
||||
///
|
||||
/// "Microsoft Word - The Virgin.doc" is "The Virgin.doc"; the extension is a
|
||||
/// separate fault with its own repair. `None` when there is no such prefix,
|
||||
/// or when what is left would be no title at all ("Microsoft Word -
|
||||
/// Document2" is a placeholder either way, and a shorter placeholder is not a
|
||||
/// repair).
|
||||
pub fn strip_producer(title: &str) -> Option<String> {
|
||||
let found = PRODUCER_PREFIX.find(title)?;
|
||||
worth_keeping(title, &title[found.end()..])
|
||||
}
|
||||
|
||||
/// A title without the file extension a filename left on it.
|
||||
///
|
||||
/// `None` when there is none, or when what is left is not a title: a bare
|
||||
/// number such as an ISBN used as the filename stays as it is, so the fault
|
||||
/// stays visible.
|
||||
pub fn strip_extension(title: &str) -> Option<String> {
|
||||
let found = DOCUMENT_EXTENSION.find(title)?;
|
||||
worth_keeping(title, &title[..found.start()])
|
||||
}
|
||||
|
||||
/// A title without the placeholder author a "Title - Author" filename left on
|
||||
/// its end: "The Birth of Purgatory - Unknown" is "The Birth of Purgatory".
|
||||
pub fn strip_placeholder_suffix(title: &str) -> Option<String> {
|
||||
let found = crate::reasons::PLACEHOLDER_SUFFIX.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
|
||||
.trim_matches(|c: char| textkit::is_space(c) || c == '-' || c == ':' || c == ',')
|
||||
.to_string();
|
||||
let has_letters = stripped.chars().any(char::is_alphabetic);
|
||||
(has_letters && stripped != original && !crate::reasons::is_placeholder_title(&stripped))
|
||||
.then_some(stripped)
|
||||
}
|
||||
|
||||
/// Capitalise the first letter and lowercase the rest.
|
||||
fn capitalise(word: &str) -> String {
|
||||
let lowered = word.to_lowercase();
|
||||
|
||||
@@ -135,6 +135,50 @@ pub fn clean(book: &Book) -> Cleaned {
|
||||
book.isbn.as_deref(),
|
||||
);
|
||||
|
||||
// Mojibake before anything that reads the text: every other repair
|
||||
// matches on characters, and these are the wrong characters.
|
||||
for (field, why) in [("title", "mojibake_title"), ("author", "mojibake_author")] {
|
||||
let slot = if field == "title" {
|
||||
&mut cleaned.title
|
||||
} else {
|
||||
&mut cleaned.author
|
||||
};
|
||||
if let Some(value) = slot.clone()
|
||||
&& let Some(fixed) = textkit::mojibake::repair(&value)
|
||||
{
|
||||
changes.push(Change {
|
||||
field: field.into(),
|
||||
from: value,
|
||||
to: fixed.clone(),
|
||||
why: why.into(),
|
||||
});
|
||||
*slot = Some(fixed);
|
||||
}
|
||||
}
|
||||
|
||||
// The wrappers a file puts around a title, outermost first: the program's
|
||||
// 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] = [
|
||||
(fix::strip_producer, "junk_title"),
|
||||
(fix::strip_extension, "filename_title"),
|
||||
(fix::strip_placeholder_suffix, "placeholder_suffix"),
|
||||
];
|
||||
for (strip, why) in wrappers {
|
||||
if let Some(title) = cleaned.title.clone()
|
||||
&& let Some(fixed) = strip(&title)
|
||||
{
|
||||
changes.push(Change {
|
||||
field: "title".into(),
|
||||
from: title,
|
||||
to: fixed.clone(),
|
||||
why: why.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.
|
||||
@@ -247,6 +291,90 @@ mod tests {
|
||||
assert_eq!(cleaned.severity, 0);
|
||||
}
|
||||
|
||||
fn whys(cleaned: &Cleaned) -> Vec<&str> {
|
||||
cleaned
|
||||
.changes
|
||||
.iter()
|
||||
.map(|change| change.why.as_str())
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_word_processor_title_loses_its_wrappers() {
|
||||
let cleaned = clean(&book(
|
||||
"Microsoft Word - The Yoga of Hesychasm.doc",
|
||||
"William Stoddart",
|
||||
));
|
||||
assert_eq!(cleaned.book.title.as_deref(), Some("The Yoga of Hesychasm"));
|
||||
assert_eq!(whys(&cleaned), ["junk_title", "filename_title"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_placeholder_author_on_the_end_of_a_title_comes_off() {
|
||||
let cleaned = clean(&book("The Birth of Purgatory - Unknown", "Unknown"));
|
||||
assert_eq!(
|
||||
cleaned.book.title.as_deref(),
|
||||
Some("The Birth of Purgatory")
|
||||
);
|
||||
assert_eq!(whys(&cleaned), ["placeholder_suffix"]);
|
||||
// The author is still unknown, and still said to be.
|
||||
assert!(
|
||||
cleaned
|
||||
.reasons
|
||||
.iter()
|
||||
.any(|reason| reason == "unknown_author")
|
||||
);
|
||||
|
||||
let cleaned = clean(&book("Illuminati 666 - Unknown.pdf", "Unknown"));
|
||||
assert_eq!(cleaned.book.title.as_deref(), Some("Illuminati 666"));
|
||||
assert_eq!(whys(&cleaned), ["filename_title", "placeholder_suffix"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_misread_title_and_author_are_read_again() {
|
||||
let cleaned = clean(&book("L'impérialisme américain", "Henri Hausér"));
|
||||
assert_eq!(
|
||||
cleaned.book.title.as_deref(),
|
||||
Some("L'impérialisme américain")
|
||||
);
|
||||
assert_eq!(cleaned.book.author.as_deref(), Some("Henri Hausér"));
|
||||
assert_eq!(whys(&cleaned), ["mojibake_title", "mojibake_author"]);
|
||||
assert!(
|
||||
cleaned
|
||||
.reasons
|
||||
.iter()
|
||||
.any(|reason| reason == "mojibake_title")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_wrapper_around_nothing_is_left_for_a_person() {
|
||||
// What is inside the wrappers is no title either, so taking them off
|
||||
// would only hide the fault.
|
||||
for title in [
|
||||
"Microsoft Word - Document2",
|
||||
"0195152972.pdf",
|
||||
"Untitled - Unknown",
|
||||
] {
|
||||
let cleaned = clean(&book(title, "Someone"));
|
||||
assert_eq!(cleaned.book.title.as_deref(), Some(title), "{title}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn titles_that_only_look_wrapped_are_left_alone() {
|
||||
for title in [
|
||||
"Word and Object",
|
||||
"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}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_shouting_title_is_cased_and_the_change_is_recorded() {
|
||||
let cleaned = clean(&book("THE HUMAN CONDITION", "Arendt, Hannah"));
|
||||
|
||||
@@ -44,7 +44,7 @@ static PATH_TITLE: LazyLock<Regex> =
|
||||
/// placeholder.
|
||||
static PLACEHOLDER_TITLE: LazyLock<Regex> = LazyLock::new(|| {
|
||||
pattern(
|
||||
r"(?i)^\W*(?:some[\s_-]*title|no[\s_-]*title|untitled|unnamed|title|document|documento|test|sample|default|placeholder|tbd|todo|draft|temp|new[\s_-]*document|book\d*|file\d*|scan\d*|doc\d*|n/?a|null|none|x{3,}|unknown)\W*$",
|
||||
r"(?i)^\W*(?:some[\s_-]*title|no[\s_-]*title|untitled|unnamed|title|document\d*|documento|test|sample|default|placeholder|tbd|todo|draft|temp|new[\s_-]*document|book\d*|file\d*|scan\d*|doc\d*|n/?a|null|none|x{3,}|unknown)\W*$",
|
||||
)
|
||||
});
|
||||
|
||||
@@ -57,6 +57,14 @@ static PLACEHOLDER_AUTHOR: LazyLock<Regex> = LazyLock::new(|| {
|
||||
/// An author field that opens with a bare three or four digit number is a
|
||||
/// publication year or a series index in the wrong column, never a person.
|
||||
/// Anchored, so a real name containing a number is untouched.
|
||||
/// A placeholder author left on the end of a title, the way a "Title - Author"
|
||||
/// filename writes a book whose author nobody knew.
|
||||
pub(crate) static PLACEHOLDER_SUFFIX: LazyLock<Regex> = LazyLock::new(|| {
|
||||
pattern(&format!(
|
||||
r"(?i){SPACE}+[-–—]{SPACE}+(?:unknown(?:{SPACE}+author)?|anonymous|n/?a){SPACE}*$"
|
||||
))
|
||||
});
|
||||
|
||||
static NUMERIC_AUTHOR: LazyLock<Regex> =
|
||||
LazyLock::new(|| pattern(&format!(r"^{SPACE}*\d{{3,4}}{SPACE}*(?:$|[{SPACE}.\-–—])")));
|
||||
|
||||
@@ -148,9 +156,12 @@ const WEIGHTS: &[(&str, u32)] = &[
|
||||
("junk_title", 3),
|
||||
("lowercase_title", 2),
|
||||
("missing_apostrophe", 2),
|
||||
("mojibake_author", 3),
|
||||
("mojibake_title", 3),
|
||||
("numeric_author", 4),
|
||||
("path_title", 4),
|
||||
("placeholder_author", 4),
|
||||
("placeholder_suffix", 2),
|
||||
("placeholder_title", 4),
|
||||
("possible_swap", 2),
|
||||
("publisher_as_author", 3),
|
||||
@@ -199,6 +210,15 @@ pub fn problems(title: Option<&str>, author: Option<&str>, isbn: Option<&str>) -
|
||||
{
|
||||
reasons.push("numeric_author");
|
||||
}
|
||||
if textkit::mojibake::is_mojibake(title) {
|
||||
reasons.push("mojibake_title");
|
||||
}
|
||||
if textkit::mojibake::is_mojibake(author) {
|
||||
reasons.push("mojibake_author");
|
||||
}
|
||||
if PLACEHOLDER_SUFFIX.is_match(title) {
|
||||
reasons.push("placeholder_suffix");
|
||||
}
|
||||
if title.contains('_') {
|
||||
reasons.push("underscore_title");
|
||||
}
|
||||
@@ -260,6 +280,11 @@ pub fn worth_fixing(reasons: &[String]) -> bool {
|
||||
reasons.iter().any(|reason| reason != "missing_isbn")
|
||||
}
|
||||
|
||||
/// Whether a title is a placeholder rather than a work's name.
|
||||
pub(crate) fn is_placeholder_title(title: &str) -> bool {
|
||||
PLACEHOLDER_TITLE.is_match(title.trim_matches(textkit::is_space))
|
||||
}
|
||||
|
||||
/// Whether the title field is a placeholder and the author field is holding
|
||||
/// the real title.
|
||||
///
|
||||
|
||||
Reference in new issue
Block a user