FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Added 'errors' flag with options such as 'replace' by bob1sparks · Pull Request #53 · avian2/unidecode · GitHub

Repository navigation

Added 'errors' flag with options such as 'replace' - #53

Closed
bob1sparks wants to merge 1 commit into
avian2:masterfrom
bob1sparks:master
Closed

bob1sparks wants to merge 1 commit into
avian2:masterfrom
bob1sparks:master

Conversation

Copy link
Copy Markdown

I added an optional errors flag parameter so that the caller has some control.

EG buf = unidecode.unidecode(textvalue, errors='preserve')

where:

'ignore' characters are dropped if no replacements are found in the tables (default)
'strict' an exception is thrown if no value is found in the tables
'replace' a ? is substituted if no replacement is found
'preserve' the existing unicode character is kept.

ignore is the default so behavior is not changed for existing projects.

this is inspired by python builtin bytes.decode

Why this change was required:

My immediate use case was to leave any characters not found in the translation tables in place rather than drop them. I am normalizing spam text such as phařm pꞧoduct to pharm product. In this case since the homoglyph 'r' in product is not found I want to leave it as pharm pꞧoduct rather than pharm poduct. The human reader makes the final adjustment in this case.

In the future I hope to add a training facility to customize the code tables.

avian2 commented Nov 29, 2019

Copy link
Copy Markdown
Owner

Hi

thanks for submitting a pull request. I have a few problems with it however:

  • It breaks existing tests (see Travis log). It also adds new code without adding tests to cover it and doesn't update documentation.
  • I think this functionality doesn't belong in the unidecode() function. First, it makes the function return conceptually different things depending on the errors parameter (which is badly named in this context: why are non-ASCII characters called "errors"?). The function now returns either an ASCII string or a Unicode string. On Python 2 these would actually be different Python types.
  • I think it would be better not to try to replicate standard library behavior. A cleaner way would be perhaps to integrate with it, for example through the codecs.register_error mechanism (ref). Through this interface it would be possible to do for example string.encode('ASCII', errors='unidecode').

Comment thread unidecode/__init__.py
raise e

# set return value to the found charace
if table and len(table) > position and table[position] != '' :

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This code assumes that if the transliteration table contains an empty string, it means there is no transliteration data about this character. This isn't true. Some characters are purposely replaced with an empty string (e.g. some zero width spaces like U+2060, etc.). On the other hand, some characters missing from the tables have [?] in the tables.

The [?] cases and the fact that it isn't possible to distinguish between the "no data" and "replace with empty string" cases is a bug in Unidecode.

bob1sparks commented Nov 30, 2019 via email

Copy link
Copy Markdown
Author

avian2 added a commit that referenced this pull request Dec 20, 2020
This implements the idea in #53

avian2 commented Jan 5, 2021

Copy link
Copy Markdown
Owner

I'm tempted to close this pull request since my recent commit implemented the functionality you proposed here.

I'm less hesitant to include the errors='preserve' option now since Python 2 is very much EOL. In Python 3 Unidecode always returns a string object so my objection that the type of the return value would change with this option is no longer relevant.

I also made some effort to make a distinction in the tables between the case of "replacement is unknown" and "replace with empty string" - it's not perfect and I'm sure in the future there will be more fixes necessary to the tables in this regard. But I think it's a good start for now.

Regarding my register_error comment, I think I misunderstood what you were trying to accomplish. It seems wrong to me now that I suggested that. Sorry.

bob1sparks commented Jan 6, 2021 via email

Copy link
Copy Markdown
Author

avian2 commented Jan 8, 2021

Copy link
Copy Markdown
Owner

I'm closing this then. If you want to contribute new character replacements, please open a new pull request. Thanks.

avian2 closed this Jan 8, 2021
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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants


Back | FazBrowse Home | New Git URL