diff --git a/crates/langid/README.md b/crates/langid/README.md index e417b80..b4c3010 100644 --- a/crates/langid/README.md +++ b/crates/langid/README.md @@ -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 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 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 diff --git a/crates/langid/src/lib.rs b/crates/langid/src/lib.rs index a8eb40a..2494d9c 100644 --- a/crates/langid/src/lib.rs +++ b/crates/langid/src/lib.rs @@ -66,6 +66,7 @@ impl corpus_core::Record for Verdict { /// Read a text's language. pub fn detect(path: &str, text: &str) -> Verdict { let sample: String = text.chars().take(SAMPLE_CHARS).collect(); + let sample = prose(&sample); let (script, share) = script::detect(&sample); let mut verdict = Verdict { @@ -135,6 +136,36 @@ pub fn detect(path: &str, text: &str) -> 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(""), + (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. fn sole_language(script: Script) -> Option<(&'static str, &'static str, &'static str)> { Some(match script { @@ -180,6 +211,20 @@ mod tests { assert!(found.confidence > 0.0); } + #[test] + fn page_markers_and_image_hashes_are_not_read() { + assert_eq!( + prose("one two ![](asset:a1d79e0b75ad8de2) three [x](y.html) four"), + "one two ![ three [x four" + ); + let hashes = " ![](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] fn a_script_only_one_language_uses_needs_no_words() { for (text, code) in [ diff --git a/crates/langid/src/words.rs b/crates/langid/src/words.rs index 58efa55..31af412 100644 --- a/crates/langid/src/words.rs +++ b/crates/langid/src/words.rs @@ -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!(