Search code examples
c#language-translation

C# translator from Pig Latin to English


Obviously I'm new to this, hence the content of this project. I have written some code that will translate English into Pig Latin. Easy enough. The problem is I want to find a way to translate the Pig Latin back into English using a logic block. The clone string just seems like a cheap way to do it. Any suggestions? Here's my code:

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

namespace FunctionTest
{
    public class PigLatinClass
    {

        public static void pigTalk(string sentence)
        {
            try
            {
                while (sentence != "exit")
                {
                    string firstLetter;
                    string afterFirst;
                    string pigLatinOut = "";
                    int x;
                    string vowel = "AEIOUaeiou";

                    Console.WriteLine("Enter a sentence to convert into PigLatin");

                    sentence = Console.ReadLine();

                    string[] pieces = sentence.Split();

                    foreach (string piece in pieces)
                    {
                        afterFirst = piece.Substring(1);
                        firstLetter = piece.Substring(0, 1);
                        x = vowel.IndexOf(firstLetter);

                        if (x == -1)
                        {
                            pigLatinOut = (afterFirst + firstLetter + "ay ");
                        }
                        else
                        {
                            pigLatinOut = (firstLetter + afterFirst + "way ");
                        }

                        Console.Write(pigLatinOut);
                    }

                    Console.WriteLine("Press Enter to flip the sentence back.");
                    Console.ReadKey(true);
                    string clonedString = null;
                    clonedString = (String)sentence.Clone();
                    Console.WriteLine(clonedString);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

        }
    }
}

The problem is there's no real rule that would work. For example: If the 3rd letter from the last was "w" you might want to say this is a vowel word but, a consonant word starting with a "w" could also fit this rule. If the first letter was a vowel again you might want to say that this is a vowel word but, a consonant word could also fit this rule since the first letter is moved to the back (pat = atpay). The only way I think this is possible is to have an if statement that checks if w is in the 3rd position and the word starts with a vowel which would call for && operator and Visual Studio yells at you if you use it with strings.


Solution

  • The problem is that Pig Latin/English translation is not a bijective function.

    For example, imagine to have 2 English words like "all" and "wall", the corresponding Pig Latin words will be always "allway".

    This suggest you that if you get a word like "allway" you can't give a unique translation in English, but (at least) two.