Advertisement
Krythic

WordFlow Detector v2 (ChatGPT)

May 11th, 2024
734
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.62 KB | None | 0 0
  1. using System.Collections.Generic;
  2.  
  3. public class WordFlowDetector
  4. {
  5.     private HashSet<char> vowels = new HashSet<char> { 'a', 'e', 'i', 'o', 'u', 'y' };
  6.  
  7.     public bool IsWellFlowed(string word)
  8.     {
  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.         StringBuilder currentSyllable = new StringBuilder();
  28.  
  29.         for (int i = 0; i < word.Length; i++)
  30.         {
  31.             char currentChar = word[i];
  32.             currentSyllable.Append(currentChar);
  33.  
  34.             if (i == word.Length - 1 || IsVowel(currentChar) != IsVowel(word[i + 1])))
  35.             {
  36.                 syllables.Add(currentSyllable.ToString());
  37.                 currentSyllable.Clear();
  38.             }
  39.         }
  40.  
  41.         return syllables;
  42.     }
  43.  
  44.     private bool IsSyllableWellFlowed(string syllable, bool isVowelStart)
  45.     {
  46.         bool isVowelExpected = IsVowel(syllable[0]) == isVowelStart;
  47.        
  48.         for (int i = 0; i < syllable.Length; i++)
  49.         {
  50.             if (IsVowel(syllable[i]) != isVowelExpected)
  51.                 return false;
  52.         }
  53.  
  54.         return true;
  55.     }
  56.  
  57.     private bool IsVowel(char c)
  58.     {
  59.         return vowels.Contains(c);
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement