10

Are there any libraries for .NET that deal with determining the Indefinite Article of a noun?

My crude attempt is below, which will probably work for 99% of my usage (which is acceptable) just wondering if there are any established alternatives?

public static string GetIndefinateArticle(string noun)
{
    if(string.IsNullOrEmpty(noun))
        return noun;

    var first = noun[0];

    if(first == 'a' ||
        first == 'e' ||
        first == 'i' ||
        first == 'o')
        return "an " + noun;

    return "a " + noun;
}

Update: Eamon pointed out a duplicate question in the comments: How can I correctly prefix a word with "a" and "an"? I'll leave this Q here and open though, because I still don't really have an answer.

Community
  • 1
  • 1
Andrew Bullock
  • 36,616
  • 34
  • 155
  • 231

7 Answers7

7

If this is something you need done seriously, you may consider porting the Ruby Linguistics (English) library to .Net. It's open source & does a pretty good job of calculating the correct articles.

http://deveiate.org/projects/Linguistics/

Mark McDonald
  • 7,571
  • 6
  • 46
  • 53
5

Since all you're really doing is check for patterns in the string, you could use a regular expression. This should also allow for future expansion of letter combos like lutge098 talked about:

public static string GetIndefinateArticle(string noun)
{
    if (Regex.IsMatch(noun, "^([aeio]|un|ul)", RegexOptions.IgnoreCase))
        return "an " + noun;
    else
        return "a " + noun;
}
TheSoftwareJedi
  • 34,421
  • 21
  • 109
  • 151
CAbbott
  • 8,078
  • 4
  • 31
  • 38
4

I implemented a library to do this: https://github.com/eamonnerbonne/a-vs-an; it's AvsAn on nuget. It's based on real usage patterns in wikipedia and hence even deals well with tricky things like...

  • "an 0800 number"
  • "an ∞ of oregano"
  • "a NASA flight"
  • "an NSA analyst"

In other words, it usually even will deal reasonably with many things that aren't normal words.

Eamon Nerbonne
  • 47,023
  • 20
  • 101
  • 166
2

I've ported a function from Python that correctly determines vowel sounds in C# and posted it as an answer to the question Programmatically determine whether to describe an object with a or an?. You can see the code snippet here. It is indeed more complicated than just looking at vowels.

Community
  • 1
  • 1
Stuart
  • 583
  • 5
  • 9
0

What i would do is:

var first = noun[0];
var second = noun[1];

if(first == 'a' ||
    first == 'e' ||
    first == 'i' ||
    first == 'o')
    return "an " + self;

if(first == 'u')
    if (second == 'n' ||
        second == 'l')
        return "an " + self;

if(first == 'h')
    if (second == 'i')
        return "an " + self;

return "a " + self;

So you can define some cases where some letters in combination with each other form a certain sound. Hope this helps.

Andrew Bullock
  • 36,616
  • 34
  • 155
  • 231
lugte098
  • 2,271
  • 5
  • 21
  • 30
  • 1
    Your `if` statement for `h` is very wrong. It would produce `an hit` and `a honor`, both of which are incorrect. –  Apr 06 '10 at 14:25
  • Are you familiar with a control statement called `switch`? – ANeves Apr 06 '10 at 14:37
  • @Matthew Ferreira: keep in mind that this is just an example of concept. – lugte098 Apr 06 '10 at 14:44
  • 2
    The h is an unsolvable problem without a dictionary. Make your program speak Cockney, they always have a silent h so you can always pick "an". – Hans Passant Apr 06 '10 at 15:19
  • I think that the question itself is unsolvable. Many words also have multiple pronunciations which are valid. –  Apr 06 '10 at 15:20
  • @Matthew Ferreira yeah i appreciate its probably very difficult to solve completely for this reason, however i'll willingly accept a solution that works most of the time, was just looking for the best way to achieve "most" – Andrew Bullock Apr 06 '10 at 15:34
  • The 'u' solution is also incorrect - you will end up with *an uniform* and *a umbrella*. Like i mentioned in my answer there are a lot of complexities to look for, which can make your code a bit of a beast. – slugster May 04 '11 at 08:22
0

The basic rule of "a" before a consonant and "an" before a vowel gets you most of the way there, that would be very easy to implement. The problem is the "sounds-like a vowel = an" case -- that would be much harder.

Joel Spolsky
  • 33,372
  • 17
  • 89
  • 105
debracey
  • 6,517
  • 1
  • 30
  • 56
0

No, and it isn't as simple as just whacking an extra n when the next character is a vowel. There are a whole bunch of subtleties around it, and you also have to consider how to handle h - some use an before it, some don't.

This is also English specific, and the framework is relatively language agnostic.

This means you will have to cook it up yourself :)

slugster
  • 49,403
  • 14
  • 95
  • 145