Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Globalization;
- using System.Text;
- namespace LanguageGame
- {
- public static class Translator
- {
- /// <summary>
- /// Translates from English to Pig Latin. Pig Latin obeys a few simple following rules:
- /// - if word starts with vowel sounds, the vowel is left alone, and most commonly 'yay' is added to the end;
- /// - if word starts with consonant sounds or consonant clusters, all letters before the initial vowel are
- /// placed at the end of the word sequence. Then, "ay" is added.
- /// Note: If a word begins with a capital letter, then its translation also begins with a capital letter,
- /// if it starts with a lowercase letter, then its translation will also begin with a lowercase letter.
- /// </summary>
- /// <param name="phrase">Source phrase.</param>
- /// <returns>Phrase in Pig Latin.</returns>
- /// <exception cref="ArgumentException">Thrown if phrase is null or empty.</exception>
- /// <example>
- /// "apple" -> "appleyay"
- /// "Eat" -> "Eatyay"
- /// "explain" -> "explainyay"
- /// "Smile" -> "Ilesmay"
- /// "Glove" -> "Oveglay".
- /// </example>
- public static string TranslateToPigLatin(string phrase)
- {
- if (string.IsNullOrWhiteSpace(phrase))
- {
- throw new ArgumentException("Phrase is null, empty or whitespace.", nameof(phrase));
- }
- StringBuilder result = new StringBuilder();
- int position = 0;
- int endOfWord = 0;
- while ((position = IndexOfLetter(phrase, endOfWord)) != -1)
- {
- if (endOfWord < position)
- {
- result.Append(phrase[endOfWord..position]);
- }
- endOfWord = EndOfWord(phrase, position);
- result.Append(ToPigWord(phrase[position..endOfWord]));
- }
- return endOfWord < phrase.Length ? result.Append(phrase[endOfWord..]).ToString() : result.ToString();
- }
- private static bool IsVowel(char symbol) => symbol is 'a' || symbol is 'A' ||
- symbol is 'o' || symbol is 'O' ||
- symbol is 'e' || symbol is 'E' ||
- symbol is 'i' || symbol is 'I' ||
- symbol is 'u' || symbol is 'U';
- private static string ToPigWord(string phrase)
- {
- if (IsVowel(phrase[0]))
- {
- return phrase + "yay";
- }
- int position = VowelFirstPosition(phrase);
- if (position == -1)
- {
- return phrase + "ay";
- }
- return char.IsUpper(phrase[0])
- ? char.ToUpper(phrase[position], CultureInfo.InvariantCulture) + phrase[(position + 1) ..] +
- char.ToLower(phrase[0], CultureInfo.InvariantCulture) + phrase[1..position] + "ay"
- : phrase[position..] + phrase[..position] + "ay";
- }
- private static int VowelFirstPosition(string phrase, int position = 0)
- {
- for (int i = position; i < phrase.Length; i++)
- {
- if (IsVowel(phrase[i]))
- {
- return i;
- }
- }
- return -1;
- }
- private static int EndOfWord(string phrase, int position = 0)
- {
- for (int i = position; i < phrase.Length; i++)
- {
- if (!char.IsLetter(phrase[i]) && phrase[i] != '’')
- {
- return i;
- }
- }
- return phrase.Length;
- }
- private static int IndexOfLetter(string phrase, int position = 0)
- {
- for (int i = position; i < phrase.Length; i++)
- {
- if (char.IsLetter(phrase[i]) || phrase[i] is '’')
- {
- return i;
- }
- }
- return -1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement