Created
April 15, 2011 13:57
-
-
Save mccutchen/921740 to your computer and use it in GitHub Desktop.
Code we use to replace "dumb" vulgar fractions with their Unicode counterparts
This file contains 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
# Map ASCII fractions to fancy fractions | |
FRACTION_MAP = { | |
u'1/2' : u'\u00BD', | |
u'1/4' : u'\u00BC', | |
u'3/4' : u'\u00BE', | |
u'1/3' : u'\u2153', | |
u'2/3' : u'\u2154', | |
u'1/5' : u'\u2155', | |
u'2/5' : u'\u2156', | |
u'3/5' : u'\u2157', | |
u'4/5' : u'\u2158', | |
u'1/6' : u'\u2159', | |
u'5/6' : u'\u215A', | |
u'1/8' : u'\u215B', | |
u'3/8' : u'\u215C', | |
u'5/8' : u'\u215D', | |
u'7/8' : u'\u215E', | |
} | |
FRACRE = re.compile(ur'(\d+\s+)?(%s)\b' % u'|'.join(FRACTION_MAP)) | |
def _substitute_fractions(m, lookup=FRACTION_MAP.get): | |
whole, frac = m.groups() | |
frac = lookup(frac) | |
return whole.rstrip() + frac if whole else frac | |
def nicefractions(s, sub=FRACRE.sub): | |
"""Replaces dumb vulgar fractions (e.g. "1/2") with their nice Unicode | |
counterparts (e.g. u"\u00BD").""" | |
try: | |
return sub(_substitute_fractions, s) | |
except: | |
return s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The actual compiled regular expression looks like this, after the keys from
FRACTION_MAP
are joined and subsituted in: