Advertisement
mnakos

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

Jul 29th, 2016
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.36 KB | None | 0 0
  1. public static string HandleGreekMaleMajorNouns(string name)
  2. {
  3.     // Make some first checks
  4.     if (string.IsNullOrEmpty(name) || name.Length < 3)
  5.         return name;
  6.  
  7.     Regex re_has_greek = new Regex(@"[α-ωΑ-Ω]");
  8.     if (!re_has_greek.IsMatch(name))
  9.         return name;
  10.  
  11.     // Check last character. If it is ς, σ or Σ then assume it is Greek male major noun
  12.     int name_length = name.Length;
  13.  
  14.     // Check if name ends with vowel and [σςΣ]
  15.     // In case we have non Greek names like Οβρένοβιτς no modification must be done
  16.     // Also works for female nouns
  17.     Regex re = new Regex(@"[αεηιουωΑΕΗΙΟΥΩάέήίόύώΆΈΉΊΌΎΏ][σςΣ]$");
  18.     if (!re.IsMatch(name))
  19.         return name;
  20.  
  21.     // Get the pre-final letter
  22.     string previous_character = name.Substring(name_length - 2, 1);
  23.     // In case pre-final is ο replace with ε or Ε
  24.     string replace_letter = previous_character;
  25.     if (previous_character == "ο")
  26.         replace_letter = "ε";
  27.     else if (previous_character == "Ο")
  28.         replace_letter = "Ε";
  29.  
  30.     // name contains σ, so must be handled, according to rules
  31.     // Check if name has tonos ' anywhere. If not, then handle it in a more simple way
  32.     Regex re_tonos = new Regex(@"[άέήίόύώΆΈΉΊΌΎΏ]");
  33.     if (!re_tonos.IsMatch(name))
  34.         return name.Substring(0, name_length - 2) + replace_letter;
  35.  
  36.     // MORE COMPLICATED! MUST FIND TONOS POSITION
  37.     // Check if name ends with ος or ός
  38.     Regex re_ostonos = new Regex(@"[όΌ][σςΣ]$");
  39.     Regex re_os_gen = new Regex(@"[οΟ][σςΣ]$");
  40.     var os_tonos_mtch = re_ostonos.Match(name);
  41.     if (os_tonos_mtch.Success)
  42.     {
  43.         return name.Substring(0, name_length - 2) + (previous_character == "ό" ? "έ" : "Έ");
  44.     }
  45.  
  46.     var os_gen_mtch = re_os_gen.Match(name);
  47.     if (os_gen_mtch.Success)
  48.     {
  49.         // Check if tonos is in paraligousa and ends with ος
  50.         Regex re_paraligousa1 = new Regex(@"[άέήίόύώΆΈΉΊΌΎΏ][^άέήίόύώΆΈΉΊΌΎΏαεηιουωΑΕΗΙΟΥΩ]{1,3}[οΟ][σςΣ]$");
  51.         Regex re_paraligousa2 = new Regex(@"[άέήίόύώΆΈΉΊΌΎΏ][οΟ][σςΣ]$");
  52.         var paraligousa_mtch = re_paraligousa1.Match(name);
  53.         var paraligousa_mtch2 = re_paraligousa2.Match(name);
  54.         if (paraligousa_mtch.Success || paraligousa_mtch2.Success)
  55.             return name.Substring(0, name_length - 1);
  56.         else
  57.             return name.Substring(0, name_length - 2) + replace_letter;
  58.     }
  59.  
  60.     return name.Substring(0, name_length - 1);
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement