Source code for putting "a" or "an" in front of a word.

来源:互联网 时间:1970-01-01

English is a funny language - you say " ausual person", but " anunusual 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".

aardvarkable... 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] [147KB in size, it embeds the list above] That Python file contains a function should_preceed_with_an("phrase...")that returns Trueor False


A Django Template FilterI 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_anyou have to define it in one of your app/templatetags/


from preceed_with_an import [email protected] a_or_an(phrase): if should_preceed_with_an(phrase): return "an" else: return "a"