mnakos icon

Μετατροπή ανδρικών ονομάτων από ονομαστική σε κλιτική πτώση

mnakos | PRO | 07/29/16 12:51:40 PM UTC | 0 ⭐ | 316 👁️ | Never ⏰ | []
C# |

2.36 KB

|

None

|

0 👍

/

0 👎

public static string HandleGreekMaleMajorNouns(string name)
{
    // Make some first checks
    if (string.IsNullOrEmpty(name) || name.Length < 3)
        return name;
 
    Regex re_has_greek = new Regex(@"[α-ωΑ-Ω]");
    if (!re_has_greek.IsMatch(name))
        return name;
 
    // Check last character. If it is ς, σ or Σ then assume it is Greek male major noun
    int name_length = name.Length;
 
    // Check if name ends with vowel and [σςΣ]
    // In case we have non Greek names like Οβρένοβιτς no modification must be done
    // Also works for female nouns
    Regex re = new Regex(@"[αεηιουωΑΕΗΙΟΥΩάέήίόύώΆΈΉΊΌΎΏ][σςΣ]$");
    if (!re.IsMatch(name))
        return name;
 
    // Get the pre-final letter
    string previous_character = name.Substring(name_length - 2, 1);
    // In case pre-final is ο replace with ε or Ε
    string replace_letter = previous_character;
    if (previous_character == "ο")
        replace_letter = "ε";
    else if (previous_character == "Ο")
        replace_letter = "Ε";
 
    // name contains σ, so must be handled, according to rules
    // Check if name has tonos ' anywhere. If not, then handle it in a more simple way
    Regex re_tonos = new Regex(@"[άέήίόύώΆΈΉΊΌΎΏ]");
    if (!re_tonos.IsMatch(name))
        return name.Substring(0, name_length - 2) + replace_letter;
 
    // MORE COMPLICATED! MUST FIND TONOS POSITION
    // Check if name ends with ος or ός
    Regex re_ostonos = new Regex(@"[όΌ][σςΣ]$");
    Regex re_os_gen = new Regex(@"[οΟ][σςΣ]$");
    var os_tonos_mtch = re_ostonos.Match(name);
    if (os_tonos_mtch.Success)
    {
        return name.Substring(0, name_length - 2) + (previous_character == "ό" ? "έ" : "Έ");
    }
 
    var os_gen_mtch = re_os_gen.Match(name);
    if (os_gen_mtch.Success)
    {
        // Check if tonos is in paraligousa and ends with ος
        Regex re_paraligousa1 = new Regex(@"[άέήίόύώΆΈΉΊΌΎΏ][^άέήίόύώΆΈΉΊΌΎΏαεηιουωΑΕΗΙΟΥΩ]{1,3}[οΟ][σςΣ]$");
        Regex re_paraligousa2 = new Regex(@"[άέήίόύώΆΈΉΊΌΎΏ][οΟ][σςΣ]$");
        var paraligousa_mtch = re_paraligousa1.Match(name);
        var paraligousa_mtch2 = re_paraligousa2.Match(name);
        if (paraligousa_mtch.Success || paraligousa_mtch2.Success)
            return name.Substring(0, name_length - 1);
        else
            return name.Substring(0, name_length - 2) + replace_letter;
    }
 
    return name.Substring(0, name_length - 1);
}

Comments