Advertisement
Krythic

WordFlow Detector (ChatGPT)

May 11th, 2024
641
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.78 KB | None | 0 0
  1. public class WordFlowDetector
  2. {
  3.     private HashSet<char> vowels = new HashSet<char> { 'a', 'e', 'i', 'o', 'u', 'y' };
  4.     private HashSet<char> consonants = new HashSet<char> { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'z' };
  5.  
  6.     public bool IsWellFlowed(string word)
  7.     {
  8.         word = word.ToLower();
  9.         List<string> syllables = SplitIntoSyllables(word);
  10.         bool isVowelStart = false;
  11.  
  12.         foreach (string syllable in syllables)
  13.         {
  14.             bool isSyllableWellFlowed = IsSyllableWellFlowed(syllable, isVowelStart);
  15.             if (!isSyllableWellFlowed)
  16.                 return false;
  17.  
  18.             isVowelStart = !isVowelStart;
  19.         }
  20.  
  21.         return true;
  22.     }
  23.  
  24.     private List<string> SplitIntoSyllables(string word)
  25.     {
  26.         List<string> syllables = new List<string>();
  27.         string currentSyllable = "";
  28.  
  29.         for (int i = 0; i < word.Length; i++)
  30.         {
  31.             char currentChar = word[i];
  32.             currentSyllable += currentChar;
  33.  
  34.             if (i == word.Length - 1 || IsVowel(currentChar) != IsVowel(word[i + 1]))
  35.             {
  36.                 syllables.Add(currentSyllable);
  37.                 currentSyllable = "";
  38.             }
  39.         }
  40.  
  41.         return syllables;
  42.     }
  43.  
  44.     private bool IsSyllableWellFlowed(string syllable, bool isVowelStart)
  45.     {
  46.         char startChar = isVowelStart ? 'v' : 'c';
  47.  
  48.         foreach (char c in syllable)
  49.         {
  50.             if ((IsVowel(c) && startChar != 'v') || (!IsVowel(c) && startChar != 'c'))
  51.                 return false;
  52.  
  53.             startChar = (startChar == 'v') ? 'c' : 'v';
  54.         }
  55.  
  56.         return true;
  57.     }
  58.  
  59.     private bool IsVowel(char c)
  60.     {
  61.         return vowels.Contains(c);
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement