Skip to content

Instantly share code, notes, and snippets.

@mccutchen
Created April 15, 2011 13:57
Show Gist options
  • Save mccutchen/921740 to your computer and use it in GitHub Desktop.
Save mccutchen/921740 to your computer and use it in GitHub Desktop.
Code we use to replace "dumb" vulgar fractions with their Unicode counterparts
# 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
@mccutchen
Copy link
Author

The actual compiled regular expression looks like this, after the keys from FRACTION_MAP are joined and subsituted in:

(\d+\s+)?(1/2|1/3|1/4|1/5|1/6|1/8|2/3|2/5|3/4|3/5|3/8|4/5|5/6|5/8|7/8)\b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment