Innovative Techniques for Pig Latin Translation in JavaScript
Written on
Chapter 1 Understanding Pig Latin
The topic for today’s DevAdvent 2022 revolves around Pig Latin. For those unfamiliar, Pig Latin is a whimsical language often used among children in English-speaking countries. As a native Italian speaker, I find it reminiscent of the Farfallino Alphabet. However, the real challenge lies in processing individual words within a sentence, which may not always be logically structured.
Understanding the Rules for Pig Latin Translation
According to the problem statement, the rules for translating to Pig Latin are as follows:
- Move the first letter of each word to the end.
- Append "ay" to the word.
- Punctuation remains unchanged.
For instance:
pigIt('Pig latin is cool'); // igPay atinlay siay oolcay
pigIt('Hello world !'); // elloHay orldway !
My Approach: Dividing and Transforming Words
To tackle this problem, I approached it in three stages:
- Convert the sentence into an array of words by splitting at spaces.
- Adjust each word by moving its first letter to the end and appending "ay".
- Combine the modified words back into a single string.
This resulted in a function like the following:
export const pigIt = (a: string): string =>
a
.split(" ")
.map((w) => w.slice(1, w.length) + w[0] + "ay")
.join(" ");
However, this function also modifies words that consist solely of punctuation, which violates the problem's stipulation. To rectify this, I incorporated a regex check: /W+/ to identify non-alphanumeric characters.
Upon refining this solution, I discovered another issue: the function fails when the sentence ends with a whitespace. To address this, I added a condition for when w == ''. Though this feels like a workaround, the function ultimately works as intended.
export const pigIt = (a: string): string =>
a
.split(" ")
.map((w) =>
!!w.match(/W+/) || w == "" ? w : w.slice(1, w.length) + w[0] + "ay")
.join(" ");
Fortunately, I later encountered a more elegant solution from another developer.
The Optimal Method for Pig Latin Translation
The superior solution can be expressed as follows:
export const pigIt = (a: string) => a.replace(/(w)(w+)*/g, "$2$1ay");
Upon reviewing this, I realized my initial approach was overly simplistic. Instead of viewing the string as an array, treating it as a cohesive string is far more effective. This solution utilizes groups in regular expressions, specifically:
- The expression /(w)/ captures the first letter of each word.
- The second group /(w+)/ captures all subsequent letters.
By applying the String.replace() method with these groups, we can rearrange them as needed and append "ay" to form the Pig Latin equivalent.
To apply this to all words in a sentence, the inclusion of an asterisk (*) simplifies the process greatly. It is a straightforward and elegant solution, one I wish I had discovered on my own.
Chapter 2 Comparing Human and AI Solutions
Recently, I have been experimenting with ChatGPT, an AI designed to tackle various programming challenges. I posed the Pig Latin problem to it, and its initial response was:
function pigIt(a: string): string {
return a
.split(" ")
.map((word) => {
const firstLetter = word[0];
if (firstLetter.match(/[a-zA-Z]/)) {
return word.slice(1) + firstLetter + "ay";} else {
return word;}
})
.join(" ");
}
Unfortunately, this solution was incorrect.
After rephrasing my question to include edge cases, ChatGPT provided a different answer:
function pigIt(a: string): string {
return a
.split(/b/)
.map((word) => {
const firstLetter = word[0];
if (firstLetter.match(/[a-zA-Z]/)) {
return word.slice(1) + firstLetter + "ay";} else {
return word;}
})
.join("");
}
This time, the solution was accurate, albeit not the most optimal. However, it still effectively addressed the problem.
Thank you for reading! Stay tuned for more insights. Don’t forget to subscribe to my Medium email list for updates!
The first video titled "Using Pattern Matching & Regular Expressions to solve Pig Latin on Exercism" provides an insightful overview of employing regex for this translation.
In the second video, "How to Build a Pig Latin Translator with Vanilla JavaScript," you can learn to create a Pig Latin translator using basic JavaScript techniques.