-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate.go
57 lines (47 loc) · 1.11 KB
/
translate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package igpay
import "strings"
const (
pigLatinSuffix string = "ay"
vowels string = "aeiou"
vowelsSuffix string = "d" + pigLatinSuffix
)
func isCapitalized(word string) bool {
first := word[0:1]
return !(strings.ToLower(first) == first)
}
func capitalize(word string) string {
return strings.ToUpper(word[0:1]) + word[1:]
}
func startsWithVowel(word string) bool {
first := strings.ToLower(word[0:1])
return strings.Contains(vowels, first)
}
func suffix(word string) (suffix string) {
if startsWithVowel(word) {
suffix = vowelsSuffix
} else {
suffix = strings.ToLower(word[0:1]) + pigLatinSuffix
}
return
}
func base(word string) (base string) {
if startsWithVowel(word) {
base = word[0:1] + strings.ToLower(word[1:])
} else {
base = strings.ToLower(word[1:])
if isCapitalized(word) {
base = capitalize(base)
}
}
return
}
func Translate(in string) (out string) {
inWords := strings.Fields(in)
outWords := make([]string, 0)
for _, inWord := range inWords {
outWord := base(inWord) + suffix(inWord)
outWords = append(outWords, outWord)
}
out = strings.Join(outWords, " ")
return
}