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

+11
View File
@@ -98,6 +98,17 @@ with its thirty commonest function words. The score is the share of a text's
words found in each list, and the lists are the same length so the scores words found in each list, and the lists are the same length so the scores
compare. compare.
**Markup is not language.** HTML comments such as page markers, and the
targets of markdown links and images, are left out before anything is
counted: an image named by a hash reads as a run of two-letter words.
**Letter-spaced type is read by its whole words.** A scan of type set
letter-spaced comes out of OCR as "w i t h" and "n o t". Single letters are
not words of any language, but "a", "e", "o" and "y" are among the commonest
words of several, so when more than a quarter of the words are single letters
they are left out. If what remains is mostly one- and two-letter fragments, as
text decoded with the wrong font table is, there is no answer.
**A close call is no answer.** The winner must be a quarter ahead of the **A close call is no answer.** The winner must be a quarter ahead of the
runner-up. `de` is among the commonest words of four languages at once, and on runner-up. `de` is among the commonest words of four languages at once, and on
its own settles none of them — so a text that short comes back with the script its own settles none of them — so a text that short comes back with the script
+45
View File
@@ -66,6 +66,7 @@ impl corpus_core::Record for Verdict {
/// Read a text's language. /// Read a text's language.
pub fn detect(path: &str, text: &str) -> Verdict { pub fn detect(path: &str, text: &str) -> Verdict {
let sample: String = text.chars().take(SAMPLE_CHARS).collect(); let sample: String = text.chars().take(SAMPLE_CHARS).collect();
let sample = prose(&sample);
let (script, share) = script::detect(&sample); let (script, share) = script::detect(&sample);
let mut verdict = Verdict { let mut verdict = Verdict {
@@ -135,6 +136,36 @@ pub fn detect(path: &str, text: &str) -> Verdict {
verdict verdict
} }
/// The text without the markup that is no language: HTML comments such as
/// page markers, and the targets of markdown links and images, whose file
/// names and hashes read as words.
fn prose(text: &str) -> String {
let mut out = String::with_capacity(text.len());
let mut rest = text;
loop {
let comment = rest.find("<!--");
let target = rest.find("](");
let (at, open, close) = match (comment, target) {
(Some(c), Some(t)) if t < c => (t, 2, ")"),
(Some(c), _) => (c, 4, "-->"),
(None, Some(t)) => (t, 2, ")"),
(None, None) => break,
};
out.push_str(&rest[..at]);
out.push(' ');
let after = &rest[at + open..];
match after.find(close) {
Some(end) => rest = &after[end + close.len()..],
None => {
rest = "";
break;
}
}
}
out.push_str(rest);
out
}
/// The one language a script is used for, where there is only one. /// The one language a script is used for, where there is only one.
fn sole_language(script: Script) -> Option<(&'static str, &'static str, &'static str)> { fn sole_language(script: Script) -> Option<(&'static str, &'static str, &'static str)> {
Some(match script { Some(match script {
@@ -180,6 +211,20 @@ mod tests {
assert!(found.confidence > 0.0); assert!(found.confidence > 0.0);
} }
#[test]
fn page_markers_and_image_hashes_are_not_read() {
assert_eq!(
prose("one <!-- Page 7 --> two ![](asset:a1d79e0b75ad8de2) three [x](y.html) four"),
"one two ![ three [x four"
);
let hashes = "<!-- Page 7 --> ![](asset:a1d79e0b75ad8de2a5f800b2d0a8d0c9a7bac1b27df2371a4489adc4a8b631ee) ".repeat(40);
let found = detect(
"x.md",
&format!("{hashes} The Wall: Growing Up Behind the Iron Curtain"),
);
assert_ne!(found.language.as_deref(), Some("la"));
}
#[test] #[test]
fn a_script_only_one_language_uses_needs_no_words() { fn a_script_only_one_language_uses_needs_no_words() {
for (text, code) in [ for (text, code) in [
+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 /// commonest. It is comparable across languages because each list is the same
/// length. /// length.
pub fn score(text: &str, script: Script) -> Vec<(&'static Language, f64)> { 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() { if words.is_empty() {
return Vec::new(); 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] #[test]
fn the_cyrillic_pair_are_told_apart() { fn the_cyrillic_pair_are_told_apart() {
assert_eq!( assert_eq!(