Skip to main content

uni2ascii

Convert unicode to closest ASCII equivalent.

There are many unicode glyphs whose appearance is very similar to ASCII characters. This script converts these codepoints to their ASCII equivalent. For example, the string і lоѵе üńісοdе contains only 2 ASCII characters, despite the fact that all but üń look fine at first glance. To convert to ASCII:

> echo і lоѵе üńісοdе | uni2ascii
i love unicode

The default action is to leave untouched any non-ascii that uni2ascii.py doesn't know about. This can be overridden with command line arguments. Call uni2ascii -h for help.

You can also call from python:

from uni2ascii import uni2ascii
ascii_string = uni2ascii('і lоѵе üńісοdе')

It's quite easy to add new transliterations by just copying and pasting offending strings into the code. See the function get_translits() in __init__.py. Feel free to contact me or do a pull request if you find useful ones that aren't there.

Notes

uni2ascii was written to handle particular data we had on hand. There are plenty of missing transliterations. I'm happy to add new ones!

Input encoding must be utf-8.

Feel free to modify - it's not likely it'll work exactly correctly for you out of the box.

The code will no longer work in python2 -- I added some unicode normalization from unicodedata and haven't quite figured out how to make it work in python2 and python3 simultaneously.

This was not designed to thwart homograph attacks, but rather to help with text normalization of English, where unicode sometimes sneaks in.

Install

pip install uni2ascii-janin

For the most up to date:

pip install git+https://github.com/ajanin/uni2ascii.git

Alternatives

The Python module unidecode

Very similar in spirit, but doesn't handle punctuation and makes some choices I disagree with.

iconv

If you call iconv -t //TRANSLIT, it'll do some of what uni2ascii does, but a bunch of stuff is missing that is important to our application.

Image processing approaches

There have been a few projects that actually look at the generated pixels to determine if two glyphs are too similar. I love the idea, but wanted more fine grain control.

Release files for uni2ascii-janin 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for uni2ascii-janin 1.1.0
File Size Uploaded
uni2ascii_janin-1.1.0.tar.gz 6.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for uni2ascii-janin 1.1.0
File Interpreter ABI Platform
uni2ascii_janin-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 12.7 kB

Release files / uni2ascii_janin-1.1.0.tar.gz

Download URL uni2ascii_janin-1.1.0.tar.gz
Size 6.0 kB
Tags Source
SHA-256 checksum
How to use checksums
e26062c2a15bdaaf4f8d51185fdc828a2e988cdef3a52e6aa1a049de568bbfe1
BLAKE2b-256 checksum
How to use checksums
3c0c941fae18a71e3a921e7ef1602c60be9c406e48071c39704c9b6f925640a5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.10.14

Release files / uni2ascii_janin-1.1.0-py3-none-any.whl

Download URL uni2ascii_janin-1.1.0-py3-none-any.whl
Size 6.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
550301f6f7d8d2b91b1530230b30fee5e71f851764368cd32bb13642e05628d1
BLAKE2b-256 checksum
How to use checksums
7f1b0aad90491191c24feaafa76caaa9524d4ee23871fee02776bc8b47568f29
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.10.14

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page