Misidentified Language
Warning:
Warning: This document appears to be written in Catalan but the html start tag has lang="en". Consider using lang="ca" (or variant) instead.
Reproduction:
- Check By: Address
- Input: https://chuckterry.me/et-3400-simulator/src/index.html
- Link to Commit containing above URL source at time of Run: https://github.com/ChuckTerry/et-3400-simulator/blob/b4183a0fb511a7e20b5109abafd59c25f9485fa1/src/index.html
Additional Information:
This is the index.html file for a visual microprocessor trainer emulator I'm working on. The file contains 56 lines of HTML and has a 14,000-line SVG in-lined in the document. It contains an explicit lang="en" attribute. If I can provide additional information, please let me know!
I confirm this is still the case today, even if I substitute the language profiles with refreshed ones available at https://github.com/tenzap/language-detection-profiles
Sample source taken from https://raw.githubusercontent.com/ChuckTerry/et-3400-simulator/b4183a0fb511a7e20b5109abafd59c25f9485fa1/src/index.html
This is because the <svg> element contains a <style> element. They are both in the <body> of the document. The content of that style element is used as input text to detect the language. If I remove the content of the <style> element, the misdetection is gone. So I think this is a bug and the <style> element should not be taken to detect the language.
Title should be updated to something like: "content of <style> element is used to detect language. Should not be"
@sideshowbarker, @j9t, or any developer, would you fix this please? It looks like a real bug in vnu. Code in <style> element (nested in SVG element) should not be used to detect languages. There are at least 2 issues reported here that would be fixed. This one and #1318.
OK, thanks @ChuckTerry for reporting this, and thanks @tenzap for prodding me to investigate.
At https://github.com/validator/validator/pull/1915 I have a patch with that change that fixes this — and that probably fixes most all other cases people have run into with the language detector misidentifying the language of documents.
For the record here, an explanation:
In https://chuckterry.me/et-3400-simulator/src/index.html the only text the language detector sees and checks is this string:
Open Pop-Out
That’s it. Just those 12 characters. Whatever other text there may be in that document, the language detector is ignoring it.
Now, why it thinks “Open Pop-Out” is Catalan (or actually Dutch is what it says for me locally), I dunno. But in general, when the language detector operates on only that small of an amount of text, it can unexpectedly make some wildly wrong guesses.
So the change I made in https://github.com/validator/validator/pull/1915 causes the language checker to just not check documents like this one that has only 12 characters it ends up basing its guess on.
After the https://github.com/validator/validator/pull/1915 change lands, the language detector will check to see if it has at least 1024 characters (about 150–200 words) to check — and if it doesn’t have that much, it won’t do any guessing.
@sideshowbarker, @j9t, or any developer, would you fix this please? It looks like a real bug in vnu. Code in
<style>element (nested in SVG element) should not be used to detect languages.
The language detector isn’t checking the contents of that SVG style element. The language detector only ever checks HTML elements (that is, elements in the HTML namespace). It skips over all SVG elements (and MathML elements).
If I leave that style element in there but just replace that “Open Pop-Out” text in the document with “This document is written in English.”, then the language detector no longer reports any warning.
But yeah, if I remove that style element, then the language detector also stops reporting that warning.
The thing is, one existing check that language detector already does — one that’s somewhat related to the check in the patch at https://github.com/validator/validator/pull/1915 — is: It checks that the total number of characters it finds in the element content of entire document is more than 1024 characters. And for that check, it does actually also include the characters that are in any SVG or MathML elements.
But to be clear: Like I said, it doesn’t actually necessarily run the language detector on all those characters. It just first only looks at the total character count, as an initial check to decide whether it should proceed any further.
So I guess what’s probably happening is, the characters from the content of that SVG style element get included in the total character count — which causes the total character count to exceed 1024, which then causes the language detector to be run on whatever actual “checkable” text content we extracted from the document.
But in this case, even though the document’s total text-content character count is apparently more than 1024 characters, the language detector ends up being run on only those 12 characters of “Open Pop-Out” text. (And gets it wrong.)
But if the style element is removed, then I guess that pushes the total character count below 1024 characters, and so then the code just bails at that point, and never ends up running the language detector on that “Open Pop-Out” text.
I guess the other issue #1318 which has much more content is not related to the minimal count of characters but to the SVG/style element. I still believe the CSS code of the style element should not be passed to langdetect.
I still believe the CSS code of the style element should not be passed to langdetect.
I agree with you about that 100%. I also believe the CSS code of the style element should not be passed to langdetect. And that’s why I already have the code written so that the CSS content of the style element is not passed to langdetect.
To repeat: What happens in the code is that the non-langdetect part of the code does some checks to figure out what text, if anything, from the document should be eventually passed on to the langdetect library code to check.
And among the information it used to make that decision is this:
https://github.com/validator/validator/blob/b95cab88d4dbf05f5167498875e3504b393fb3f9/src/nu/validator/checker/LanguageDetectingChecker.java#L108-L110
So, it skips all style elements, and script elements, and all other elements whose names are in that string array.
And it also has a check here:
https://github.com/validator/validator/blob/b95cab88d4dbf05f5167498875e3504b393fb3f9/src/nu/validator/checker/LanguageDetectingChecker.java#L862-L865
That if ("http://www.w3.org/1999/xhtml" != uri) causes it to return early if the element it’s looking at isn’t an element in the HTML namespace. So, that means it completely skips all SVG elements, and all MathML elements.
So from that I hope it’s clear that the code is not passing any CSS content on to the langdetect library to check.
Sorry for my delayed response here, I've had notifications muted the past few weeks while on vacation.
@tenzap Thank you for resurfacing the issue and looking into it!
@sideshowbarker Thank you for the super detailed issue explanation and fix!
Luckily, it sounds like this was for the most part an edge case. I just worked on a similar PR three weeks ago, but it was JSON-like objects (double-quoted keys) inside TypeScript files. The timing on this fix is impeccable though, as I'm prepping to start back on this project within the next two weeks. Thanks again guys & keep up the great work!