mirror of
https://git.pumpin.online/funkpower/catena.git
synced 2026-11-24 19:32:56 +00:00
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:
3 files changed
+94
-1
No files matched your search
@@ -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("<!--");
|
||||
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.
|
||||
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 <!-- Page 7 --> two  three [x](y.html) four"),
|
||||
"one two  ".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 [
|
||||
|
||||
Reference in new issue
Block a user