Skip to content

Pig Latin

Translate English words to Pig Latin by moving each word’s leading consonants to its end and adding “ay”.

Adapted from Exercism’s problem specification (MIT). Listed under Strings & Text.

// FILE: pig_latin.nomi
//# Translates English words to Pig Latin.

once vowels = Set["a", "e", "i", "o", "u", "y"]

/// `phrase` in Pig Latin, word by word: a word's leading consonants move to
/// its end, and every word ends in `ay`.
pub fn translate(phrase: String) -> String
    phrase
    |> String.words()
    |> Iter.map(translate_word)
    |> String.join(" ")
end

fn translate_word(word: String) -> String
    onset = word |> Iter.to_list() |> onset_length()
    String.drop(word, onset) + String.take(word, onset) + "ay"
end

/// How many letters at the start of a word move to its end: the consonants
/// before its first vowel, with a `u` after a `q` counted with the `q`. A
/// `y` is a consonant only as the first letter, and a word that starts with
/// `xr` or `yt` keeps its start, as one that starts with a vowel does.
fn onset_length(letters: List<String>) -> Int
    case letters
        ["x", "r", ..] or ["y", "t", ..] -> 0
        ["y", ..rest]                    -> 1 + consonants(rest)
        _                                -> consonants(letters)
    end
end

/// How many consonants `letters` starts with, where `y` is a vowel and `qu`
/// is two consonants.
fn consonants(letters: List<String>) -> Int
    case letters
        ["q", "u", ..] -> 2
        [letter, ..rest] when not letter in vowels -> 1 + consonants(rest)
        _ -> 0
    end
end

// FILE: pig_latin_test.nomi
import pig_latin

test "ay is added to words that start with vowels"
    [
        ("word beginning with a",                            "apple",  "appleay"),
        ("word beginning with e",                            "ear",    "earay"),
        ("word beginning with i",                            "igloo",  "iglooay"),
        ("word beginning with o",                            "object", "objectay"),
        ("word beginning with u",                            "under",  "underay"),
        ("word beginning with a vowel and followed by a qu", "equal",  "equalay"),
    ]
    |> subtests (description, phrase, expected), description
        assert pig_latin.translate(phrase) == expected
    end
end

test "first letter and ay are moved to the end of words that start with consonants"
    [
        ("word beginning with p",                                 "pig",    "igpay"),
        ("word beginning with k",                                 "koala",  "oalakay"),
        ("word beginning with x",                                 "xenon",  "enonxay"),
        ("word beginning with q without a following u",           "qat",    "atqay"),
        ("word beginning with consonant and vowel containing qu", "liquid", "iquidlay"),
    ]
    |> subtests (description, phrase, expected), description
        assert pig_latin.translate(phrase) == expected
    end
end

test "some letter clusters are treated like a single consonant"
    [
        ("word beginning with ch",                           "chair",   "airchay"),
        ("word beginning with qu",                           "queen",   "eenquay"),
        ("word beginning with qu and a preceding consonant", "square",  "aresquay"),
        ("word beginning with th",                           "therapy", "erapythay"),
        ("word beginning with thr",                          "thrush",  "ushthray"),
        ("word beginning with sch",                          "school",  "oolschay"),
    ]
    |> subtests (description, phrase, expected), description
        assert pig_latin.translate(phrase) == expected
    end
end

test "some letter clusters are treated like a single vowel"
    [
        ("word beginning with yt", "yttria", "yttriaay"),
        ("word beginning with xr", "xray",   "xrayay"),
    ]
    |> subtests (description, phrase, expected), description
        assert pig_latin.translate(phrase) == expected
    end
end

test "position of y in a word determines if it is a consonant or a vowel"
    [
        ("y is treated like a consonant at the beginning of a word",    "yellow", "ellowyay"),
        ("y is treated like a vowel at the end of a consonant cluster", "rhythm", "ythmrhay"),
        ("y as second letter in two letter word",                       "my",     "ymay"),
    ]
    |> subtests (description, phrase, expected), description
        assert pig_latin.translate(phrase) == expected
    end
end

test "phrases are translated"
    assert pig_latin.translate("quick fast run") == "ickquay astfay unray"
end
  • The four rules become one count, onset_length: how many letters at the start of the word move. The word is then String.drop(word, onset), String.take(word, onset) and "ay".
  • The count walks the word’s letters with list patterns. ["q", "u", ..] is rule 3, ["x", "r", ..] or ["y", "t", ..] is the xr/yt part of rule 1, and a leading ["y", ..rest] makes y a consonant only as the first letter, which is rule 4 without a flag parameter.
  • The vowels are a once Set, looked up with in in a guard; y is among them because after the first letter it acts as one.
  • Each canonical group is a test named by the group’s description, holding one subtests table; the one-case “phrases are translated” group is a plain test.