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:
funkpower committed 2026-11-24 07:23:24 -05:00
1 parent 2d885b5afc
commit 15b92d7457
7 files changed
+359 -1

No files matched your search

+26 -1
View File
@@ -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.
///