18

Are there any javascript libraries that exist for determining the indefinite article ("a" vs "an") of a noun?

I can start with a simple regex like so:

var pattern = /^([aeiou])/i;
pattern.test("umbrella");

but this doesn't handle a case like: "user" which should return false (you wouldn't say "an user clicked the button").

frshca
  • 1,309
  • 2
  • 15
  • 24
  • 6
    Yup, gotta love the English language. – Mike Christensen Mar 12 '13 at 16:19
  • First of all you'd need a complete set of rules (there are more exceptions from the default rule, then just "user"), then you can begin to think about how to implement those rules. – Ridcully Mar 12 '13 at 16:20
  • 1
    I think a good tradeoff is to just use what you have, and put in a list of (common) exceptions. Works relatively well. – Xymostech Mar 12 '13 at 16:21
  • just make an exception list that you can add to when you find an example. check if the instance meets the exception, if not go with the general rule. ditto @xymostech – nathan hayfield Mar 12 '13 at 16:21
  • 1
    The issue is the rules need to work based on the *sounds*, not the letters. For example, if *user* was pronounced *oooo-zer* then you'd say *an*. – Mike Christensen Mar 12 '13 at 16:22
  • On the site linked to by FJ's answer, they provide some good examples: "an NSA analyst", "an honest opinion". The list goes on, I'm sure. – Xymostech Mar 12 '13 at 16:34
  • 1
    The joy of parsing natural language. – dutt Mar 12 '13 at 16:34

3 Answers3

13

The following Javascript library by Eamon Nerbonne should be what you are looking for:
http://home.nerbonne.org/A-vs-An/AvsAn.js

This was actually created as an answer to a similar question here on SO:
https://stackoverflow.com/a/1288473/505154

The approach used here was to download Wikipedia, filter it, and create a prefix database to determine a/an, which should be more accurate than most rules-based systems. You can find more information at the following location (also linked in the above answer):
http://home.nerbonne.org/A-vs-An/

There is also a link there to an alternative implementation by Chad Kirby:
https://github.com/uplake/Articles

Community
  • 1
  • 1
Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
3

Here's a really simple one I wrote a little while ago:

https://github.com/rigoneri/indefinite-article.js

I hope it helps!

Rodrigo Neri
  • 304
  • 3
  • 4
0

I've found this library useful and easy to use: https://www.npmjs.com/package/indefinite .

Examples for the docs:

var a = require('indefinite');

console.log(a('apple')); // "an apple"
console.log(a('banana')); // "a banana"
console.log(a('UFO')); // 'a UFO'
console.log(a('hour')); // 'an hour'
console.log(a('ukelele')); // 'a ukelele'
console.log(a('banana', { capitalize: true })); // 'A banana'
console.log(a('UGLY SWEATER', { caseInsensitve: true })); // 'an UGLY SWEATER'
console.log(a('2')); // 'a 2'
console.log(a('8')); // 'an 8'
Andrew Taylor
  • 610
  • 7
  • 26