Python Forum

Full Version: A function for correctly using "a/an" before a word
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I want to make a function that, when passed a string containing a single word, returns that word preceded by the indefinite article "a" or "an," depending on which is correct. This would be easy enough to do if the rule we were taught as kids were correct: "a" before a consonant, "an" before a vowel. But it isn't. It's "a" before a consonant sound and "an" before a vowel sound; hence, "an umbrella" but "a unicorn" (YOO-ni-corn). How would you go about writing this function? Would you try to compile an exhaustive list of "exceptions" from online sources or work with some kind of API or what? Also, how would you find out if someone has already done this?
(Oct-07-2019, 05:51 PM)Exsul1 Wrote: [ -> ]Also, how would you find out if someone has already done this?
Did you try to google it?
First result is close to what you want - https://stackoverflow.com/questions/2033...xts-python

Then looking at the answers you see that you probably will need NLTK
(Oct-07-2019, 05:51 PM)Exsul1 Wrote: [ -> ][ ... ] word preceded by the indefinite article "a" or "an," depending on which is correct. This would be easy enough to do if the rule we were taught as kids were correct: "a" before a consonant, "an" before a vowel. But it isn't. It's "a" before a consonant sound and "an" before a vowel sound; hence, "an umbrella" but "a unicorn" (YOO-ni-corn).

Hi!

Just a thought on linguistics, not on Python:

Although, it seems that the rule for words beginning with an 'h' depends on the sound, it seems that for native speakers, it actually depends on tradition, even on the place you have been grown up, or studied.

Some people say 'a historic occasion', while others 'an historic occasion'.
Some people say 'a herb', while others 'an herb'.
Some people say 'a hotel', while others 'an hotel'.

The same could apply to some other words I've heard using either 'a' or 'an' before them: hysterical, heroic, horrific, and human, and the list is not over.

More on this issue:

http://editingandwritingservices.com/a-o...ng-with-h/

http://www.scribe.com.au/tip-w005.html

https://www.quora.com/Which-article-shou...al-A-or-an

https://www.writing-skills.com/hit-or-my...re-h-words

http://www.stgeorges.co.uk/blog/a-or-an-...r-an-hotel

https://english.stackexchange.com/questi...e-letter-h

https://www.lexico.com/en/grammar/a-hist...oric-event

https://www.merriam-webster.com/words-at...it-a-or-an

All the best,