Advertisement
yahorrr

Untitled

Sep 25th, 2022
978
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.20 KB | None | 0 0
  1. using System;
  2. using System.Globalization;
  3. using System.Text;
  4.  
  5. namespace LanguageGame
  6. {
  7.     public static class Translator
  8.     {
  9.         /// <summary>
  10.         /// Translates from English to Pig Latin. Pig Latin obeys a few simple following rules:
  11.         /// - if word starts with vowel sounds, the vowel is left alone, and most commonly 'yay' is added to the end;
  12.         /// - if word starts with consonant sounds or consonant clusters, all letters before the initial vowel are
  13.         ///   placed at the end of the word sequence. Then, "ay" is added.
  14.         /// Note: If a word begins with a capital letter, then its translation also begins with a capital letter,
  15.         /// if it starts with a lowercase letter, then its translation will also begin with a lowercase letter.
  16.         /// </summary>
  17.         /// <param name="phrase">Source phrase.</param>
  18.         /// <returns>Phrase in Pig Latin.</returns>
  19.         /// <exception cref="ArgumentException">Thrown if phrase is null or empty.</exception>
  20.         /// <example>
  21.         /// "apple" -> "appleyay"
  22.         /// "Eat" -> "Eatyay"
  23.         /// "explain" -> "explainyay"
  24.         /// "Smile" -> "Ilesmay"
  25.         /// "Glove" -> "Oveglay".
  26.         /// </example>
  27.         public static string TranslateToPigLatin(string phrase)
  28.         {
  29.             if (string.IsNullOrWhiteSpace(phrase))
  30.             {
  31.                 throw new ArgumentException("Phrase is null, empty or whitespace.", nameof(phrase));
  32.             }
  33.  
  34.             StringBuilder result = new StringBuilder();
  35.             int position = 0;
  36.             int endOfWord = 0;
  37.  
  38.             while ((position = IndexOfLetter(phrase, endOfWord)) != -1)
  39.             {
  40.                 if (endOfWord < position)
  41.                 {
  42.                     result.Append(phrase[endOfWord..position]);
  43.                 }
  44.  
  45.                 endOfWord = EndOfWord(phrase, position);
  46.  
  47.                 result.Append(ToPigWord(phrase[position..endOfWord]));
  48.             }
  49.  
  50.             return endOfWord < phrase.Length ? result.Append(phrase[endOfWord..]).ToString() : result.ToString();
  51.         }
  52.  
  53.         private static bool IsVowel(char symbol) => symbol is 'a' || symbol is 'A' ||
  54.                                                     symbol is 'o' || symbol is 'O' ||
  55.                                                     symbol is 'e' || symbol is 'E' ||
  56.                                                     symbol is 'i' || symbol is 'I' ||
  57.                                                     symbol is 'u' || symbol is 'U';
  58.  
  59.         private static string ToPigWord(string phrase)
  60.         {
  61.             if (IsVowel(phrase[0]))
  62.             {
  63.                 return phrase + "yay";
  64.             }
  65.  
  66.             int position = VowelFirstPosition(phrase);
  67.  
  68.             if (position == -1)
  69.             {
  70.                 return phrase + "ay";
  71.             }
  72.  
  73.             return char.IsUpper(phrase[0])
  74.                 ? char.ToUpper(phrase[position], CultureInfo.InvariantCulture) + phrase[(position + 1) ..] +
  75.                   char.ToLower(phrase[0], CultureInfo.InvariantCulture) + phrase[1..position] + "ay"
  76.                 : phrase[position..] + phrase[..position] + "ay";
  77.         }
  78.  
  79.         private static int VowelFirstPosition(string phrase, int position = 0)
  80.         {
  81.             for (int i = position; i < phrase.Length; i++)
  82.             {
  83.                 if (IsVowel(phrase[i]))
  84.                 {
  85.                     return i;
  86.                 }
  87.             }
  88.  
  89.             return -1;
  90.         }
  91.  
  92.         private static int EndOfWord(string phrase, int position = 0)
  93.         {
  94.             for (int i = position; i < phrase.Length; i++)
  95.             {
  96.                 if (!char.IsLetter(phrase[i]) && phrase[i] != '’')
  97.                 {
  98.                     return i;
  99.                 }
  100.             }
  101.  
  102.             return phrase.Length;
  103.         }
  104.  
  105.         private static int IndexOfLetter(string phrase, int position = 0)
  106.         {
  107.             for (int i = position; i < phrase.Length; i++)
  108.             {
  109.                 if (char.IsLetter(phrase[i]) || phrase[i] is '’')
  110.                 {
  111.                     return i;
  112.                 }
  113.             }
  114.  
  115.             return -1;
  116.         }
  117.     }
  118. }
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement