简体   繁体   中英

Separating Syllables into an array or list Unity C#

Im trying to seperate each syllable of an english word/name into a list. The code I have is counting the syllables perfectly which I also need, but I cannot figure out how to seperate each syllable from the words given. Im using an input field but for now the example word is my name.

im very new to programming so please keep that in mind.

preferred example output would be something like {"joh", "nat", "hon"}

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SyllableSeperator : MonoBehaviour
{
    string word = "johnathon";

    private void Start()
    {
        SyllableCount(word);
    }

    public static int SyllableCount(string word)
    {
        word = word.ToLower().Trim();
        List<string> wordList = new List<string>();
        bool lastWasVowel = false;
        string vowels = "aeiouy";
        int count = 0;

        foreach (char c in word)
        {
            if (vowels.Contains(c))
            {
                if (!lastWasVowel)
                {
                    count++;
                    lastWasVowel = true;
                }
            }
            else
            {
                lastWasVowel = false;
            }
        }
        
        if((word.EndsWith("e") || (word.EndsWith("es") || word.EndsWith("ed"))) && !word.EndsWith("le"))
                {
                    count--;
                }

        return count;
    }
}

what i have tried just does not work at all so I figured I wouldn't post that code.

Again please keep in mind that I am brand new to Unity and programming in general. Someone may have already asked how to do this but the code was too advanced for me.

Assuming you are checking the syllable by going through each character, you can make a temporary string and keep adding characters to the string.

Once you determine that string is good enough to be a syllable, add it to the list.

Like so:

public static int GetSyllableCount(string word, out List<string> syllableList)
{
    word = word.ToLower().Trim();
    List<string> syllableList = new List<string>();
    bool lastWasVowel = false;
    string vowels = "aeiouy";

    StringBuilder currSyllable = new StringBuilder();

    foreach (char c in word)
    {
        if (vowels.Contains(c))
        {
            if (!lastWasVowel)
            {
                lastWasVowel = true;

                // Finish this syllable and add to the list
                syllableList.Add(currSyllable.ToString());
                currSyllable.Clear();
            }
        }
        else
        {
            lastWasVowel = false;
        }

        // Add this character to the current syllable
        currSyllable.Append(c);
    }
    
    if((word.EndsWith("e") || (word.EndsWith("es") || word.EndsWith("ed"))) && !word.EndsWith("le"))
            {
                // Remove the last syllable?
                syllableList.Remove(syllableList.Count - 1);
            }

    return syllableList.Count;
}

The out parm modifer allows you to output another value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM