Iggy Latin Translator – String Manipulation

IggyLatin

IggyLatinInterface

The following program is called an Iggy Lating translator. You’ve heard of Pig Latin right? Well think of this as an off-shoot of Pig Latin. Basically you’re adding the following “ig”, “ggy”, “iggi”, “t”, or “tt” to either the beginning of the word or the end depending on if the word has a vowel or consonant. The letter replacement is done 3 different ways for string manipulation. The 1st way is a regular string method, the 2nd way is using the StringBuilder method, and the 3rd way is using a Regular Expression to add the characters. Call me crazy I actually had more fun messing with the Regular Expressions. I can see how you can use this to make sure only certain information is entered into a WPF form. You will also notice that I used a static class for the conversions. This way I don’t don’t have to create and instance of the class. I can just call the class directly. Now onto the code. Or you can download the source.

Cheers
Wierdguy


MainForm Class


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;

namespace IggyLatin
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void convertButton_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(originalTextBox.Text))
            {
                int loops = (int)iterationsNumericUpDown.Value;
                Stopwatch stopWatch = new Stopwatch();
                string result = "";
                long elapsedTicks = 0;

                //Run through stop watch the first time (per Microsoft) to ensure more accurate measurements.
                stopWatch.Start();
                stopWatch.Stop();
                stopWatch.Reset();

                //Using String methods.
                stopWatch.Start();
                for (int i = 0; i < loops; i++)
                    result = IggyConversion.ConvertUsingString(originalTextBox.Text);
                stopWatch.Stop();
                elapsedTicks = stopWatch.ElapsedTicks;
                stringIggyLabel.Text = string.Format("{0} - {1}", elapsedTicks, result);

                //Using StringBuilder methods.
                stopWatch.Reset();
                stopWatch.Start();
                for (int i = 0; i < loops; i++)
                    result = IggyConversion.ConvertUsingStringBuilder(originalTextBox.Text);
                stopWatch.Stop();
                elapsedTicks = stopWatch.ElapsedTicks;
                builderIggyLabel.Text = string.Format("{0} - {1}", elapsedTicks, result);

                //Using Regular Expressions.
                stopWatch.Reset();
                stopWatch.Start();
                for (int i = 0; i < loops; i++)
                {
                    result = IggyConversion.ConvertUsingRegularExpressions(originalTextBox.Text);
                    stopWatch.Stop();
                    elapsedTicks = stopWatch.ElapsedTicks;
                    expressionIggyLabel.Text = string.Format("{0} - {1}", elapsedTicks, result);
                }
            }
        }
    }
}

 

IggyConversion Class


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace IggyLatin
{
    public static class IggyConversion
    {
        //Private class variables.
        private static readonly char[] _prependList = "yiaeou".ToCharArray();
        private static readonly char[] _appendList = "aeiou".ToCharArray();
        private static readonly char[] _appendSpecialList = "y".ToCharArray();
        private static readonly string _prependText = "ig";
        private static readonly string _appendText = "ggy";
        private static readonly string _appendSpecialText = "iggi";

        private static readonly char[] _prependProjectLetter = "t".ToCharArray(); //Char array for the "t" character. DS
        private static readonly string _appendProjectLetter = "tt";  //Replacing the "t" character with double "t's". DS

        //Using String methods.
        public static string ConvertUsingString(string sentence)
        {
            string convertedSentence = string.Empty;

            //Making sure there is something to convert.
            if (!string.IsNullOrWhiteSpace(sentence))
            {
                //Parse out each word.
                string word = "";
                for (int position = 0; position < sentence.Length; position++)
                {
                    string character = sentence.Substring(position, 1);
                    if (!char.IsWhiteSpace(character, 0))
                        word += character;

                    if (char.IsWhiteSpace(character, 0) || position == sentence.Length - 1)
                    {
                        convertedSentence += ConvertWordUsingString(word);
                        if (position < sentence.Length - 1)
                        {
                            convertedSentence += character;
                            word = string.Empty;
                        }
                    }
                }
            }
            return convertedSentence;
        }

        private static string ConvertWordUsingString(string word)
        {
            string convertedWord = string.Empty;

            //Make sure we have something to convert.
            if (!string.IsNullOrWhiteSpace(word))
            {
                //Convert each character, with special hadling for the last character.
                for (int position = 0; position < word.Length; position++)                 {                     string character = word.Substring(position, 1);                     if (character.ToLower().IndexOfAny(_prependList) >= 0)
                    {
                        //Vowels.
                        if (position < word.Length - 1)                             //Prepend.                             convertedWord += _prependText + character;                         else if (character.ToLower().IndexOfAny(_appendSpecialList) >= 0)
                            //Append Special.
                            convertedWord += _appendSpecialText;
                        else if (character.ToLower().IndexOfAny(_appendList) >= 0)
                            //Append
                            convertedWord += character + _appendText;
                    }
                    //Looking for the "t" character. DS
                    else if (character.ToLower().IndexOfAny(_prependProjectLetter) >= 0)
                    {
                        convertedWord += character + _appendProjectLetter;
                    }

                    else
                    {
                        //Consonants
                        if (position < word.Length - 1)
                            //Append
                            convertedWord += character;
                        else
                            //Append special.
                            convertedWord += character + _appendSpecialText;
                    }
                }
                //Fix first character case.
                if (word.Substring(0, 1).Equals(word.Substring(0, 1).ToUpper(), StringComparison.Ordinal))
                    convertedWord = char.ToUpper(convertedWord[0]) + convertedWord.Substring(1);
            }
            return convertedWord;
        }

        //Using StringBuilder methods.
        public static string ConvertUsingStringBuilder(string sentence)
        {
            StringBuilder convertedSentence = new StringBuilder(sentence.Length);

            // Making sure we have something to convert
            if (!string.IsNullOrWhiteSpace(sentence))
            {
                // Parse out each word
                StringBuilder word = new StringBuilder();
                for (int position = 0; position < sentence.Length; position++)
                {
                    string character = sentence.Substring(position, 1);
                    if (!char.IsWhiteSpace(character, 0))
                        word.Append(character);

                    if (char.IsWhiteSpace(character, 0) || position == sentence.Length - 1)
                    {
                        convertedSentence.Append(ConvertWordUsingStringBuilder(word.ToString()));
                        if (position < sentence.Length - 1) convertedSentence.Append(character);                         word.Clear();                     }                 }             }             return convertedSentence.ToString();         }         private static string ConvertWordUsingStringBuilder(string word)         {             string convertedWord = string.Empty;             // Making sure we have something to convert             if (!string.IsNullOrWhiteSpace(word))             {                 StringBuilder workingWord = new StringBuilder(word.Substring(0, word.Length - 1));                 // Replace all "vowels" except skip last character (special handling)                 foreach (char ch in _prependList)                     workingWord.Replace(ch.ToString(), _prependText + ch);                 //Replace all the "t" characters and skip the last character. DS                 foreach (char tee in _prependProjectLetter)                     workingWord.Replace(tee.ToString(), tee + _appendProjectLetter);                 // Replace last character                 string lastCharacter = word.Substring(word.Length - 1, 1);                 if (lastCharacter.IndexOfAny(_appendSpecialList) >= 0)
                    workingWord.Append(_appendSpecialText);
                else if (lastCharacter.IndexOfAny(_appendList) >= 0)
                    workingWord.Append(lastCharacter + _appendText);

                //Replace the last "t" character with double tt's. DS
                else if (lastCharacter.IndexOfAny(_prependProjectLetter) >= 0)
                    workingWord.Append(lastCharacter + _appendProjectLetter);

                else
                    workingWord.Append(lastCharacter + _appendSpecialText);

                // Fix first character case
                if (word.Substring(0, 1).Equals(word.Substring(0, 1).ToUpper(), StringComparison.Ordinal))
                    workingWord[0] = char.ToUpper(workingWord[0]);

                convertedWord = workingWord.ToString();
            }
            return convertedWord;
        }

        //Convert using regular expressions.
        public static string ConvertUsingRegularExpressions(string sentence)
        {
            //making sure we have something to convert.
            if (!string.IsNullOrWhiteSpace(sentence))
            {
                //Step 1: Prepend all the vowels and y with ig not at the end of the word.
                string matchPattern = @"[aeiouy](?!\b)";
                string replacePattern = _prependText + @"$&";
                sentence = Regex.Replace(sentence, matchPattern, replacePattern, RegexOptions.IgnoreCase);

                //Step 2: Replace all word ending in y with iggy.
                matchPattern = @"[y]\b";
                replacePattern = _appendSpecialText;
                sentence = Regex.Replace(sentence, matchPattern, replacePattern, RegexOptions.IgnoreCase);

                //Step 3: Replace all words ending in a vowel with vowel+iggy.
                matchPattern = @"[aeiou]\b";
                replacePattern = @"$&" + _appendText;
                sentence = Regex.Replace(sentence, matchPattern, replacePattern, RegexOptions.IgnoreCase);

                //Step 4: Replace all words not ending in vowel or y with iggy.
                matchPattern = @"[^aeiouy\W]\b";
                replacePattern = @"$&" + _appendSpecialText;
                sentence = Regex.Replace(sentence, matchPattern, replacePattern, RegexOptions.IgnoreCase);

                //Step 5: Translate any t character and replace with 2 tt (Two t's) letters. DS
                matchPattern = @"[t]";
                replacePattern = @"$&" + _appendProjectLetter;
                sentence = Regex.Replace(sentence, matchPattern, replacePattern, RegexOptions.IgnoreCase);
            }
            return sentence;
        }
    }
}

 

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s