Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trafnar/1686420 to your computer and use it in GitHub Desktop.
Save trafnar/1686420 to your computer and use it in GitHub Desktop.
require 'linguistics'
# Helper function to pick the correct indefinite article for a number
# e.g. number_with_indefinite_article(10) #=> "a 10"
# e.g. number_with_indefinite_article(80, "$") #=> "an $80"
def number_with_indefinite_article(number, prefix=nil)
if [11,18].include?(number)
article = "an"
elsif number.to_s[0] == "8"
article = "an"
else
word = Linguistics::EN.numwords(number)
if ["eleven", "eighteen"].include?(word.split(" ")[0])
article = "an"
else number.to_s[0] == "1"
article = "a"
end
end
[article, " ", prefix, number].compact.join
end
@trafnar
Copy link
Author

trafnar commented Jan 27, 2012

I believe only 8, 11, 18 and any subsequent numbers beginning with the number 8, or the words "eleven" or "eighteen" should use "an", so my version just looks for those special cases. the number 1 starts with a "w" sound so it should use "a" rather than "an", which fails the vowel test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment