fix: detect upper-accented+currency mojibake at string start (fixes #222)#232
Open
gaoflow wants to merge 1 commit into
Open
fix: detect upper-accented+currency mojibake at string start (fixes #222)#232gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
When an upper-accented letter (such as Ã) is followed by a currency
symbol (such as ¥, YEN SIGN) at the very beginning of a string, the
badness heuristic failed to detect it as mojibake. An existing pattern
(`\s [{upper_accented}] [{currency}]`) already caught this case when
a preceding whitespace was present, but the start-of-string case was
missing.
Add a BADNESS_RE pattern `^[{upper_accented}][{currency}]\w` that
matches this sequence at position 0 only when followed by a word
character. The trailing `\w` ensures the pattern does not match the
isolated 2-character substring that `decode_inconsistent_utf8` passes
to `is_bad()` during processing of other text, preventing false
positives on ambiguous embedded sequences like "Dråber".
Fixes rspeer#222.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The badness heuristic in
is_bad()misses UTF-8 mojibake when an upper-accented letter followed by a currency symbol appears at the very beginning of a string. An existing pattern (\s [{upper_accented}] [{currency}]) already catches this when preceded by whitespace, but the start-of-string case was missing.For example,
fix_encoding("Ã¥klagarmyndighets")returns"Ã¥klagarmyndighets"unchanged instead of correctly decoding to"åklagarmyndighets".Root cause: No BADNESS_RE pattern matches
[{upper_accented}][{currency}]at position 0 without a preceding space or lowercase letter.Fix: Add
^[{upper_accented}][{currency}]\wto detect this sequence at string start. The trailing\wis required so the pattern does not match the isolated 2-character substring thatdecode_inconsistent_utf8passes tois_bad(), preventing false positives on ambiguous embedded sequences (like the"Dråber"negative test case from issue #202).Changes:
ftfy/badness.py: Add new BADNESS_RE alternation (4 lines)tests/test-cases/synthetic.json: Add test case for the reported bug