Skip to content

Instantly share code, notes, and snippets.

@jfromaniello
Created October 31, 2009 10:48
Show Gist options
  • Save jfromaniello/223024 to your computer and use it in GitHub Desktop.
Save jfromaniello/223024 to your computer and use it in GitHub Desktop.
/// <summary>
/// </summary>
/// <remarks> There is a lot of useless information
/// in <see cref="http://buscon.rae.es/dpdI/SrvltGUIBusDPD?lema=g%E9nero2">RAE.</see></remarks>
public class GenderGuesser
{
private const string LastVocalPattern = @"(?<vocal>[aeiouáéíóú])[^aeiouáéíóú]*\z";
private static readonly string[] femaleEndings = new[]
{
"ción", "sión", "triz",
};
private static readonly string[] maleStart = new[]
{
"a", "ha"
};
private static readonly Regex reg = new Regex(LastVocalPattern);
public virtual Gender Guess(string word)
{
word = word.Split(' ').First();
var hasFemaleEnding = HasFemaleEnding(word);
if (!hasFemaleEnding && HasMaleStart(word) && !word.EndsWith("s", StringComparison.InvariantCultureIgnoreCase))
{
return Gender.Male;
}
if (hasFemaleEnding )
{
return Gender.Female;
}
if (GetLastVocal(word).Equals('a'))
{
return Gender.Female;
}
return Gender.Male;
}
private static bool HasMaleStart(string word)
{
return maleStart.Any(e => word.StartsWith(e, StringComparison.InvariantCultureIgnoreCase));
}
private static bool HasFemaleEnding(string word)
{
return femaleEndings.Any(e => word.EndsWith(e, StringComparison.InvariantCultureIgnoreCase));
}
private static char GetLastVocal(string w)
{
var value = reg.Match(w).Groups["vocal"].Value;
if (value.Length > 0) return value[0];
throw new InvalidOperationException("Palabra sin vocales...mmmmmmm.");
}
}
public enum Gender
{
Male,
Female,
Both,
Semantic
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment