This question already has answers here:
Closed 7 years ago.
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.
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/
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.
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.
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;
}
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.
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 :)
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.