langid: read past markup and letter-spaced scans

Markdown image targets and HTML page markers are left out before counting:
an image named by a hash read as two-letter words and made one memoir Latin.

Scanned letter-spaced type ("w i t h", "n o t") filled the sample with
single letters, and a, e, o and y are common words in several Romance
languages, so English lost its margin and came back with no answer. When
more than a quarter of the words are single letters they are left out; if
what remains is mostly fragments, as wrongly decoded fonts are, there is
still no answer.
This commit is contained in:
funkpower committed 2026-11-24 11:46:23 -05:00
1 parent eb27662f69
commit 29d64ae8c2
3 files changed
+94 -1

No files matched your search

+38 -1
View File
@@ -215,7 +215,26 @@ pub const MARGIN: f64 = 1.25;
/// commonest. It is comparable across languages because each list is the same
/// length.
pub fn score(text: &str, script: Script) -> Vec<(&'static Language, f64)> {
let words = tokens(text);
let mut words = tokens(text);
// Scanned type set letter-spaced comes out of OCR as "w i t h" and
// "n o t": the letters are not words of any language, but "a", "e", "o"
// and "y" are among the commonest words of several, which hands the text
// to whichever has most of them. Ordinary prose is a few percent single
// letters; past a quarter they are spacing and are left out.
let singles = words
.iter()
.filter(|word| word.chars().count() == 1)
.count();
if singles * 4 > words.len() {
words.retain(|word| word.chars().count() > 1);
// Most of what is left of real prose has three letters or more;
// text decoded with the wrong font table leaves mostly pairs like
// "gf" and "hg", which are no language however they score.
let long = words.iter().filter(|word| word.chars().count() > 2).count();
if long * 2 < words.len() {
words.clear();
}
}
if words.is_empty() {
return Vec::new();
}
@@ -294,6 +313,24 @@ mod tests {
}
}
#[test]
fn letter_spaced_scanning_is_read_by_its_whole_words() {
let text = "charges. Smith said the young man's family was grateful to h i m \
for guidance he gave. Smith always managed to get o u t o f bad situations. \
But he could n o t stop his body clock. I felt m o r e at ease after the crises w i t h \
work passed, and Smith continued working. I t d i d n o t occur to me that he had \
come to therapy to die. He seemed to be battling f o r life. T o me, Smith seemed";
assert_eq!(best(text, Script::Latin), Some("en"));
}
#[test]
fn a_wrongly_decoded_font_is_no_language() {
let text = "9G::99$>9$4?FH>6$H3:999$3$ 9H:>>:@>6 >6@30?>6$F9B$3@9$64$H34G:@9 3W:@$;64$0>@5$9@36$ \
3:99$;64$0>@5$9@36$;H59>9$6$ 3XG>3;6@$@$;6>@3$:6@3:@3 4>FG$64$W>6@$H37F;$9F gf hg ab fa ff ge ea \
ABCDEBFGHFAIJKFLEMNCEOFPJQMEKREFASFPHKKFTMSOFJKBFUVMCWDXYVEMFZXNNCDD page ba aa cb";
assert_eq!(best(text, Script::Latin), None);
}
#[test]
fn the_cyrillic_pair_are_told_apart() {
assert_eq!(