Dustin Boswell - Brain Dumps about Computers, Programming, and Everything Else
dustwell.com About Me Past Academic Work Photo Album Links I Use
Articles by Me Subscribe: RSS
Oil ETFs leak money Yes/No proposition bets div, span, and CSS "display:" Pair programming + screen Dear VirginAmerica.com SSH keys in 2 easy steps "An hour" vs. "A hour" How to hash passwords Snapshotting with Rsync MacBook Pro Sharp Edge Fixing Your Flaky Internet How X-over-SSH works Drinking Distilled Water Politician != Decider Understanding iTunes files My Audi A4 Gas Mileage djb-dns installation Vim Cheat Sheet
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".

... 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.) That Python file contains a function should_preceed_with_an("phrase...") that returns True or False.

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

def a_or_an(phrase):
  if should_preceed_with_an(phrase): return "an"
  else: return "a"

blog comments powered by Disqus