Goal: Activity to practice formulating algorithms.

Introductory Activity: Introduce pig-latin to students using a couple phrases. Ask them to try to figure out how the language is constructed. If students already know the rules, ask them to not spoil it for others while they figure it out.

Lesson: Have the students write down on paper or in comments in a blank playground the rules for translating English into Pig-Latin. Then have the students encode the rules into a Swift program.

The goal at first is to turn an input string into the corresponding string in Pig-Latin. Once a student is happy that their program is working, they can add the code to have the voice synthesizer speak the resulting Pig-Latin string.

Instructor Notes:

Use the “Answers” template in Swift Playgrounds for this activity since you will 1) be gathering input from the user, and 2) using show() to debug and test the code by printing the translated words and phrases.

Have the students develop their translation code using single words at a time. Then when they have successfully tested their algorithm with single words, guide them to create an array of single words out of the user input sentence. String’s instance method .components(separateBy: ” “) is very handy for this task.

In the code example below, we get the first character of a word by using the .first computed property of a string. The .first property returns an optional character, however, since a string may be empty and not have a first character. However, all our words will have at least one character since our array of words was constructed by splitting sentences into words, so it is fine to ‘force unwrap’ the value returned from .first. This is what the “!” is in the line:

var firstLetter = word.first!

If possible, share a template with your students to get them started. This template might contain the code to launch the voice synthesizer and/or provide your students some helper arrays:

let consonants = ["b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s","t", "v", "w", "x", "y", "z"]
let vowels = ["a", "e", "i", "o", "u"]

One of our student’s solutions:

import AVFoundation // needed for the text-to-speech synthesis

import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true

// define some useful arrays that define the characters that are consonants or vowels
let consonants = ["b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s","t", "v", "w", "x", "y", "z"]
let vowels = ["a", "e", "i", "o", "u"]
var sentenceToTranslate = ask("Please enter some text for translating to pig Latin:")
sentenceToTranslate = sentenceToTranslate.lowercased()
var wordArray = sentenceToTranslate.components(separatedBy:" ")

var translatedSentence = ""  // create a blank string to which we will append all our translated words

//  function that speaks a string using a voice synthesizer
func voiceSynthesizer(wordToSpeak: String) {
    let voice = AVSpeechSynthesisVoice(language: "au-AU")
    let synthesizer = AVSpeechSynthesizer()
    let utterance = AVSpeechUtterance(string: wordToSpeak)
    utterance.voice = voice
    utterance.rate = 0.25

func translate(word: String) -> String {
    if word.count < 3 {
        return word
    var translatedWord = ""
    var firstLetter = word.first!
    var firstLetterRemovedWord = word.dropFirst()
    var secondLetter = firstLetterRemovedWord.first!
    var secondLetterRemovedWord = firstLetterRemovedWord.dropFirst()
    // rules for Pig-Latin: if it starts with a vowel, add "ay" to the end of the word
    for vowel in vowels {
        if word.hasPrefix(vowel) {
            translatedWord = word.lowercased() + "ay"
            //show("Pig latin is \(translatedWord)")
            return translatedWord

    // rules for Pig-Latin: if it starts with a consonant, move the consonant to the end of the word, and append "ay" to word
    for consonant in consonants {
        if word.hasPrefix(consonant) {
            for consonant in consonants {
                if firstLetterRemovedWord.hasPrefix(consonant) {
                    // second letter is a consonant
                    translatedWord = secondLetterRemovedWord.lowercased() + "\(firstLetter)"+ "\(secondLetter)ay"
                    return translatedWord
            if translatedWord == "" {
                // only first letter is consonant
                translatedWord = word.lowercased().dropFirst() + "\(firstLetter)ay"
                return translatedWord
    return ""

// put the array of words back together into a single, long string
for word in wordArray {
    translatedSentence += translate(word: word) + " "

voiceSynthesizer(wordToSpeak: translatedSentence)

Output example from this code:

example output for pig-latin activity

Supplemental Activity: Have the students try different accents when speaking their Pig-Latin results. It’s fun to try British, Australian, German, etc., accents once the program is running. A useful list of accent options is included at the end of this lesson.

Final Activity: Have the students come up with their own rule-based translational language. It could be as simple as replacing “ay” in the above code to some other syllable or string, and/or rearranging the logic so that words that start with vowels are given the more complex translation rules, or something completely different.

Once the framework is in place to 1) split the input into words, 2) recombine the words into phrases, and 3) speak the resulting phrases, creating new rules for a completely new language is an easy, engaging and fun activity.

Have students challenge you, or their peers to try to guess the translation rules by entering some text and listening to or viewing the speech output.

Swift Playgrounds Voice Synthesizer Dialects