I've been recently fooling around with Go(lang) and ended up doing a (very) simple pig Latin parser. I'm pretty sure this is not the best solution out there (no punctuation and only lowercase letters) but it does its job.
However, I'd be delighted to learn how this could be improved, as I'm still pretty new to Go.
Here's the code and at the bottom some test cases.
package igpay
import "strings"
const pigTail = "ay"
func PigIt(input string) string {
if len(input) == 0 {
return ""
}
words := strings.Fields(strings.ToLower(input))
result := make([]string, len(words))
for _, word := range words {
if len(word) < 3 {
result = append(result, piggify(word, 1))
continue
}
switch word[:3] {
case "squ", "sch", "thr":
result = append(result, piggify(word, 3))
continue
}
switch word[:2] {
case "ch", "qu", "th", "rh":
result = append(result, piggify(word, 2))
continue
case "ye", "xe":
result = append(result, piggify(word, 1))
continue
}
switch word[:1] {
case "a", "e", "o", "u", "i", "y", "x":
result = append(result, word+pigTail)
continue
}
result = append(result, piggify(word, 1))
}
return strings.TrimLeft(strings.Join(result, " "), " ")
}
func piggify(word string, index int) string {
return word[index:]+word[:index]+pigTail
}
Test cases:
package igpay
var testCases = []struct {
description string
input string
expected string
}{
{
description: "word beginning with a",
input: "apple",
expected: "appleay",
},
{
description: "word beginning with e",
input: "ear",
expected: "earay",
},
{
description: "word beginning with i",
input: "igloo",
expected: "iglooay",
},
{
description: "word beginning with o",
input: "object",
expected: "objectay",
},
{
description: "word beginning with u",
input: "under",
expected: "underay",
},
{
description: "word beginning with a vowel and followed by a qu",
input: "equal",
expected: "equalay",
},
{
description: "word beginning with p",
input: "pig",
expected: "igpay",
},
{
description: "word beginning with k",
input: "koala",
expected: "oalakay",
},
{
description: "word beginning with x",
input: "xenon",
expected: "enonxay",
},
{
description: "word beginning with q without a following u",
input: "qat",
expected: "atqay",
},
{
description: "word beginning with ch",
input: "chair",
expected: "airchay",
},
{
description: "word beginning with qu",
input: "queen",
expected: "eenquay",
},
{
description: "word beginning with qu and a preceding consonant",
input: "square",
expected: "aresquay",
},
{
description: "word beginning with th",
input: "therapy",
expected: "erapythay",
},
{
description: "word beginning with thr",
input: "thrush",
expected: "ushthray",
},
{
description: "word beginning with sch",
input: "school",
expected: "oolschay",
},
{
description: "word beginning with yt",
input: "yttria",
expected: "yttriaay",
},
{
description: "word beginning with xr",
input: "xray",
expected: "xrayay",
},
{
description: "y is treated like a consonant at the beginning of a word",
input: "yellow",
expected: "ellowyay",
},
{
description: "y is treated like a vowel at the end of a consonant cluster",
input: "rhythm",
expected: "ythmrhay",
},
{
description: "y as second letter in two letter word",
input: "my",
expected: "ymay",
},
{
description: "a whole phrase",
input: "quick fast run",
expected: "ickquay astfay unray",
},
}
Test:
package igpay
import (
"fmt"
"testing"
)
func TestPigLatin(t *testing.T) {
for _, test := range test cases {
if pl := PigIt(test.input); pl != test.expected {
t.Fatalf("FAIL: Sentence(%q) = %q, want %q.", test.input, pl, test.expected)
}
fmt.Printf("PASS: %s\n", test.description)
}
}
thr
treated as a consonant cluster but nottr
,str
,chr
, etc.? \$\endgroup\$