forked from ironarachne/world
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate.go
86 lines (67 loc) · 1.85 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package language
import (
"strings"
"github.com/ironarachne/world/pkg/words"
)
// RosettaStone generates a version of a common phrase in the language
func (language Language) RosettaStone() string {
phrase := "Hello! It is good to see you, friend."
translation := words.CapitalizeFirst(language.TranslatePhrase(phrase))
return translation
}
// TranslatePhrase parses a phrase and returns the translated version
func (language Language) TranslatePhrase(phrase string) string {
punctuation := ""
translation := ""
translatedWord := ""
wordOnly := ""
skipNext := false
titleNext := false
phrase = strings.ToLower(phrase)
words := strings.Split(phrase, " ")
for i, w := range words {
if !skipNext {
if strings.ContainsAny(w, "!?.,") {
wordOnly = strings.TrimRight(w, "!?.,")
punctuation = string(w[strings.IndexAny(w, "!?.,")])
} else {
wordOnly = w
punctuation = ""
}
if wordOnly == "is" {
wordOnly = "to be"
}
if wordOnly == "to" && i < len(words)-2 && language.WordList["to "+words[i+1]] != "" {
wordOnly = "to " + words[i+1]
skipNext = true
}
wordOnly = getSingular(wordOnly)
translatedWord = language.WordList[wordOnly]
if titleNext {
translatedWord = strings.Title(translatedWord)
}
translation += translatedWord + punctuation + " "
if strings.ContainsAny(w, "!?.") {
titleNext = true
} else {
titleNext = false
}
} else {
skipNext = false
}
}
return translation
}
// TranslateWord returns the translated version of the word
func (language Language) TranslateWord(word string) string {
word = strings.ToLower(word)
return language.WordList[word]
}
func getSingular(word string) string {
if strings.HasSuffix(word, "es") {
word = strings.TrimSuffix(word, "es")
} else if strings.HasSuffix(word, "s") {
word = strings.TrimSuffix(word, "s")
}
return word
}