Created
November 6, 2014 20:25
-
-
Save joshfinnie/ba874dc68bd691bfbfe8 to your computer and use it in GitHub Desktop.
anify
This file contains hidden or 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
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