a missing word, a hidden dependency, and a second pass that changed things

This commit is contained in:
funkpower
2026-09-19 13:00:29 -04:00
parent 6e26d5cf4b
commit b0743d5bf7
11 changed files with 234 additions and 35 deletions
+34 -11
View File
@@ -44,7 +44,7 @@ const SPARSE_CHARS_PER_PAGE: f64 = 200.0;
const COMMON_WORDS: &[&str] = &[
"a", "all", "an", "and", "are", "as", "at", "be", "been", "but", "by", "for", "from", "had",
"have", "his", "in", "is", "it", "more", "not", "of", "on", "one", "or", "that", "the",
"there", "they", "this", "to", "was", "were", "which", "with", "you",
"there", "they", "this", "to", "was", "we", "were", "which", "with", "you",
];
const HEALTHY_WORD_RATIO: f64 = 0.10;
@@ -133,16 +133,18 @@ pub fn triage(sample: &Sample<'_>, limits: &Limits) -> Triage {
sample.middle_pages
));
if reflect::undo(sample.middle).is_some() {
notes.push("reflecting the character codes recovers ordinary text".into());
return Triage {
layer: TextLayer::ReflectedFont,
needs_ocr: false,
notes,
};
}
// Damage can only be judged where there is enough text to judge it, and
// that guard comes first — including before the recovery attempt, which
// on a short sample would otherwise answer for the whole document.
if middle.len() >= limits.min_chars_to_judge {
if reflect::undo(sample.middle).is_some() {
notes.push("reflecting the character codes recovers ordinary text".into());
return Triage {
layer: TextLayer::ReflectedFont,
needs_ocr: false,
notes,
};
}
let letters = score::letter_share(&middle);
let latin1 = score::latin1_share(&middle);
notes.push(format!("letters {letters:.2}, latin-1 {latin1:.2}"));
@@ -248,8 +250,29 @@ mod tests {
}
#[test]
fn the_common_word_list_is_sorted_so_it_can_be_searched() {
fn the_common_word_list_is_sorted_and_whole() {
assert!(COMMON_WORDS.windows(2).all(|pair| pair[0] < pair[1]));
assert_eq!(COMMON_WORDS.len(), 37, "a word has gone missing");
}
#[test]
fn a_short_sample_is_judged_on_density_rather_than_damage() {
// Too little text to call it damaged, which is what the density gate
// is for. Answering from the recovery attempt instead would route the
// file away from the OCR it needs.
let stored: String = "The mill stood at the northern end. "
.repeat(4)
.chars()
.map(|c| {
let reflected = 0x120 - c as u32;
char::from_u32(reflected)
.filter(|_| (0xa1..=0xff).contains(&reflected))
.unwrap_or(c)
})
.collect();
let found = triage(&sample(&prose(), &stored, Some(400)), &Limits::default());
assert_eq!(found.layer, TextLayer::Sparse, "{:#?}", found.notes);
assert!(found.needs_ocr);
}
#[test]
+62 -7
View File
@@ -43,7 +43,8 @@ struct Args {
#[arg(long)]
json: bool,
/// Say nothing; answer with the exit code alone.
/// Write no verdict; answer with the exit code alone. Diagnostics still go
/// to stderr, because a silent failure is worse than a quiet one.
#[arg(long, short)]
quiet: bool,
}
@@ -62,11 +63,17 @@ fn main() -> ExitCode {
}
};
if which("pdftotext").is_none() {
eprintln!("pdftriage: pdftotext is not on PATH (it ships with poppler)");
return ExitCode::from(exit::UNAVAILABLE);
for program in ["pdftotext", "pdfinfo"] {
if which(program).is_none() {
eprintln!("pdftriage: {program} is not on PATH (it ships with poppler)");
return ExitCode::from(exit::UNAVAILABLE);
}
}
// Both of them, because without the page count the sample window falls
// back to the front of the book and the density gate cannot run at all —
// and a file would then be judged differently on two machines that both
// have everything the tool said it needed.
let pages = page_count(&args.path);
let (first, last) = pdftriage::middle_window(pages, SPAN);
@@ -179,11 +186,41 @@ fn page_count(path: &Path) -> Option<u32> {
.and_then(|count| count.trim().parse().ok())
}
/// Find a program on `PATH`, the way a shell would.
///
/// Checking that a file exists is not enough: a non-executable file of the
/// right name shadows the real program and turns a clear "not installed" into
/// an obscure failure several steps later. On Windows the name on `PATH` also
/// carries an extension, which is what `PATHEXT` lists.
fn which(program: &str) -> Option<PathBuf> {
let path = std::env::var_os("PATH")?;
let extensions: Vec<String> = match std::env::var("PATHEXT") {
Ok(list) => std::iter::once(String::new())
.chain(list.split(';').map(str::to_lowercase))
.collect(),
Err(_) => vec![String::new()],
};
std::env::split_paths(&path)
.map(|dir| dir.join(program))
.find(|candidate| candidate.is_file())
.flat_map(|dir| {
extensions
.iter()
.map(move |extension| dir.join(format!("{program}{extension}")))
})
.find(|candidate| is_program(candidate))
}
#[cfg(unix)]
fn is_program(path: &std::path::Path) -> bool {
use std::os::unix::fs::PermissionsExt as _;
std::fs::metadata(path)
.is_ok_and(|meta| meta.is_file() && meta.permissions().mode() & 0o111 != 0)
}
#[cfg(not(unix))]
fn is_program(path: &std::path::Path) -> bool {
path.is_file()
}
#[cfg(test)]
@@ -192,7 +229,25 @@ mod tests {
#[test]
fn a_program_that_exists_is_found_and_one_that_does_not_is_not() {
assert!(which("sh").is_some() || which("sh.exe").is_some());
assert!(which("sh").is_some(), "every system has a shell");
assert!(which("definitely-not-a-program-93a1").is_none());
}
#[cfg(unix)]
#[test]
fn a_file_that_is_not_executable_is_not_a_program() {
use std::os::unix::fs::PermissionsExt as _;
let dir = std::env::temp_dir().join("pdftriage-which");
std::fs::create_dir_all(&dir).expect("a writable directory");
let path = dir.join("not-a-program-8813");
std::fs::write(&path, b"#!/bin/sh\n").expect("a writable path");
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o644))
.expect("permissions to set");
assert!(!is_program(&path));
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755))
.expect("permissions to set");
assert!(is_program(&path));
}
}