Skip to content

Instantly share code, notes, and snippets.

@joshfinnie
Created November 6, 2014 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshfinnie/ba874dc68bd691bfbfe8 to your computer and use it in GitHub Desktop.
Save joshfinnie/ba874dc68bd691bfbfe8 to your computer and use it in GitHub Desktop.
anify
import re
from django import template
from django.utils.encoding import force_unicode
from django.template.defaultfilters import stringfilter
CONSONANT_SOUND = re.compile(r'''
one(![ir])
''', re.IGNORECASE | re.VERBOSE)
VOWEL_SOUND = re.compile(r'''
[aeio]|
u([aeiou]|[^n][^aeiou]|ni[^dmnl]|nil[^l])|
h(ier|onest|onou?r|ors\b|our(!i))|
[fhlmnrsx]\b
''', re.IGNORECASE | re.VERBOSE)
register = template.Library()
@register.filter
@stringfilter
def anify(text):
"""
Guess "a" vs "an" based on the phonetic value of the text.
"An" is used for the following words / derivatives with an unsounded "h":
heir, honest, hono[u]r, hors (d'oeuvre), hour
"An" is used for single consonant letters which start with a vowel sound.
"A" is used for appropriate words starting with "one".
An attempt is made to guess whether "u" makes the same sound as "y" in
"you".
"""
text = str(text.encode('utf-8'))
text = force_unicode(text)
if not CONSONANT_SOUND.match(text) and VOWEL_SOUND.match(text):
return 'n'
return ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment