Skip to content

Instantly share code, notes, and snippets.

@stefanfoulis
Created August 18, 2011 16:09
Show Gist options
  • Save stefanfoulis/1154405 to your computer and use it in GitHub Desktop.
Save stefanfoulis/1154405 to your computer and use it in GitHub Desktop.
#-*- coding: utf-8 -*-
"""
Provides an easy way to turn any ascii string into a funky unicode character massacre.
Example usage:
>>> from unicodefy import unicodefy
>>> print unicodefy(u'We are not ASCII')
Wë ãrè ñót ÅŠÇÎÌ
"""
import random
import unicodedata
ALL_WEIRD_CHARACTERS = u'ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ'
def build_map(weird_characters):
r = {}
for weird_character in weird_characters:
similar_ascii_character = unicodedata.normalize('NFKD', weird_character).encode('ascii', 'ignore')
if similar_ascii_character in r:
r[similar_ascii_character].append(weird_character)
else:
r[similar_ascii_character] = [weird_character]
return r
MAP = build_map(ALL_WEIRD_CHARACTERS)
def unicodefy(text):
"""
Unicodefies the provided string.
"""
text = unicode(text)
r = []
for c in text:
r.append(random.choice(MAP.get(c, c)))
return ''.join(r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment