Pig latin translator(c++ and luau)

Ever wanted to speak pig latin but you don’t want to learn it?
Talk with other distinguished gentlemen that wield the power of pig latin?

Well don’t fret, I have just what you need!

A english to pig latin translator! This translator helps you translate weird and non-understandable english eughhhh, into the distinguished and world-renowned(not really) pig latin!

Heres a brief description of pig latin for the mere mortals that DON’T know:

In the curious art of Pig Latin, words that begin with vowels—those noble letters a, e, i, o, and u—are gently adorned with the suffix “hay” or “ay”,
thereby maintaining their original grace whilst adding a playful flourish. Thus, apple doth become applehay or appleay.

When words commence with consonants, the initial cluster of these harsh sounds is severed and placed at the end of the word, followed by the customary “ay” or “hay”. Forsooth, brave transformeth to avebray, and strong to ongstray.

The pair “qu”, bound tightly as lovers, is treated as a single entity, ever moved together to the word’s rear. Hence, quiet changeth to ietquay.

Thus, this tongue, both playful and cryptic, rearranges the familiar into the strange, cloaking speech in merry disguise.

chatgpt written :>/

Anyway, enough of that, heres the two implementations in c++ and luau:

Luau
-- Sentence to translate
local sentence = "I walked a lonely road"

-- Check if the letter is a vowel
function isVowelChar(c : string) : boolean
	local vowels = {"a", "e", "i", "o", "u"} :: {string}
	
	for _, vowel in vowels do
		if string.lower(c) == vowel then return true end
	end
	
	return false
end

-- Translate text into pig latin
function toPigLatin(word : string) : string
	local endings = {"ay", "hay"} :: {string}
	
	if string.len(word) == 0 then return end
	
	-- Check if the first letter is a vowel, if it is, just add the ending
	local firstLetter = string.lower(string.sub(word, 1, 1)) :: string
	if isVowelChar(firstLetter) then return word .. endings[math.random(1, 2)] end
	
	--[[
		if it is not a vowel, continue until the letter is a vowel, then
		take the consonant cluster up to the vowel, move it to the front of the word,
		and add the ending.
	]]
	local stringCluster = "" :: string
	local index = 0 :: number
	
	while index < string.len(word) and not isVowelChar(string.sub(word, index, index)) do
		local letter = "" :: string
		if string.sub(word, index, index + 1) == "qu" then
			letter = string.sub(word, index, index + 1)
			stringCluster = stringCluster.. letter
			index += 2
		else
			stringCluster = stringCluster..letter
			index += 1
		end
	end
	
	return string.sub(word, index) .. stringCluster .. endings[math.random(1, 2)] 
end

-- Parse whole sentences
function parseSentence(sentance : string) : string
	-- Split words from sentence
	local words = string.split(sentance, " ")
	
	-- Construct and return new sentence with translated words
		local newSentence = "" :: string
		
		for _, word in words do
			newSentence = newSentence .. (toPigLatin(word) .. " ")
		end
	
	return newSentence
end

print(parseSentence(sentence))
c++
#include <iostream>
#include <vector>
#include <random>

// Check if character is a vowel
bool isVowelChar(char c) {
        const char vowels[5] = {'a', 'e', 'i', 'o', 'u'};
        for (char vowel : vowels) {
            if (tolower(c) == vowel) return true;
        }
        return false;
    }

// Translate text into pig latin
std::string toPigLatin(std::string word) {
    static const std::string endings[2] = {"ay", "hay"};

    std::random_device dev;
    std::mt19937 rng(dev());
    std::uniform_int_distribution<std::mt19937::result_type> randomness(0, 1);

    if (word.empty()) {
    return word;
    }

    char firstLetter = tolower(word[0]);

    // Check if the first letter is a vowel, if it is, just add the ending
    if (isVowelChar(firstLetter)) {
        return word + endings[randomness(rng)];
    }
    /*
        if it is not a vowel, continue until the letter is a vowel, then
	    take the consonant cluster up to the vowel, move it to the front of the word,
	    and add the ending.
    */
    else {
        std::string stringCluster;
        int index = 0;
        while (index < word.length() && !isVowelChar(word[index])) {
            if (word.substr(index, 2) == "qu") {
                stringCluster += "qu";
                index += 2;
                continue;
            } else {
                stringCluster += word[index];
                index ++;
            }
        }
        return word.erase(0, index) + stringCluster + endings[randomness(rng)];
    }
}

// Main function/Runtime
int main() {
    // Parse Whole sentances

    // Sentence
    std::string sentence = "I walked a lonely road";

    // Split words from sentence
    std::vector<std::string> words;

    std::string word;

    for (char letter : sentence) {
        if (letter == ' ') {
            if (!word.empty()) {
                words.push_back(word);
                word.clear();
            }
        }
        else if (std::isalpha(letter)) {
            word += letter;
        }
    }
    if (!word.empty()) words.push_back(word);

    // Construct and return new sentence with translated words
    std::string newSentence;

    for (const std::string& word : words) {
        newSentence += (toPigLatin(word) + " ");
    }

    std::cout << newSentence << std::endl;
    return 0;
}

I wanted to make this in 2 different languages, just so people can learn from this a little, Im experimenting with things done outside of roblox inside of roblox, so thats why I made this, also to probably show how the two languages differ from each other ig? idk.

It should work most of the time, let me know if theres any issues, or things I can improve upon with this.

Im guessing this is not new, and people definitely made this countless times, I think, in luau and c++.

Take away what you want from it, learn from it, idk.

Anyway, thats all, thank you.

5 Likes