Source code for putting "a" or "an" in front of a word.
February 11, 2010
English is a funny language - you say "a usual person", but "an unusual person". There is no simple set of rules for how to decide this. Instead, you have to rely on whether the next word starts with a vowel sound.
Instead, I think the "right" solution is just to have a list of the words that should be prefixed with "an".
aardvark able ... words that should be prefixed with "an" ...I searched far and wide, but couldn't find one, so I created a list of words that should be prefixed with "an" myself, based on which words in the CMU pronunciation dictionary started with one of the vowel-like phonemes:
["AA", "AE", "AH", "AO", "AW", "AY", "EH", "ER", "EY", "IH", "IY", "OW", "OY", "UH", "UW"]
Files for you to Download
I am now sharing this list to the rest of the world (it's public domain, use it how you like.)- preceed_with_an.txt [146KB in size, 16,831 words]
- preceed_with_an.py [147KB in size, it embeds the list above]
A Django Template Filter
I now use this code in my Django templates by doing:I am looking for {{ phrase|a_or_an }} {{ phrase }}
which will produce:
I am looking for a cat. I am looking for an hour-glass. I am looking for an unusual person. I am looking for a usual person.To make use of a_or_an you have to define it in one of your app/templatetags/ files:
from preceed_with_an import should_preceed_with_an @register.filter def a_or_an(phrase): if should_preceed_with_an(phrase): return "an" else: return "a"
blog comments powered by Disqus