tags:

views:

135

answers:

3

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;
}
+1  A: 

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.

lugte098
Your `if` statement for `h` is very wrong. It would produce `an hit` and `a honor`, both of which are incorrect.
Matthew Ferreira
Are you familiar with a control statement called `switch`?
ANeves
@Matthew Ferreira: keep in mind that this is just an example of concept.
lugte098
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
I think that the question itself is unsolvable. Many words also have multiple pronunciations which are valid.
Matthew Ferreira
@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
+2  A: 

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.Matches(noun, "^([aeio]|un|ul)", RegexOptions.IgnoreCase))
        return "an " + noun;
    else
        return "a " + noun;
}
CAbbott
It will be a long and difficult regex probably, but more effective
lugte098
@lugte You haven't seen a long and difficult regex until you've seen the proper way of validating an email address. It was **over 6 lines long!**
Earlz
@Earlz If you put it that way, i guess i haven't :D
lugte098
+4  A: 

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/

C4H5As
+1 for answering the question (is there a library) rather than cobbling together some code.
Kirk Broadhurst
Seeing as IronRuby has just been released, there may be no need to port! I shall play tomorrow. Thanks
Andrew Bullock