forked from ironarachne/world
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwords.go
136 lines (116 loc) · 3.77 KB
/
words.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package language
import (
"bytes"
"fmt"
"text/template"
)
// ConjugationRules is a set of templates to be applied to a word depending on its tense
type ConjugationRules struct {
SimplePresent string
SimplePast string
SimpleFuture string
PresentContinuous string
PastContinuous string
FutureContinuous string
PresentPerfect string
PastPerfect string
FuturePerfect string
PresentPerfectContinuous string
PastPerfectContinuous string
FuturePerfectContinuous string
}
// Word is a simple wrapper for use in conjugations
type Word struct {
Root string
}
// Conjugate returns the conjugated form of a root word given a tense
func (language Language) Conjugate(root string, tense string) string {
template := ""
if tense == "simple present" {
template = language.VerbConjugationRules.SimplePresent
} else if tense == "simple past" {
template = language.VerbConjugationRules.SimplePast
} else if tense == "simple future" {
template = language.VerbConjugationRules.SimpleFuture
} else if tense == "present continuous" {
template = language.VerbConjugationRules.PresentContinuous
} else if tense == "past continuous" {
template = language.VerbConjugationRules.PastContinuous
} else if tense == "future continuous" {
template = language.VerbConjugationRules.FutureContinuous
} else if tense == "present perfect" {
template = language.VerbConjugationRules.PresentPerfect
} else if tense == "past perfect" {
template = language.VerbConjugationRules.PastPerfect
} else if tense == "future perfect" {
template = language.VerbConjugationRules.FuturePerfect
} else if tense == "present perfect continuous" {
template = language.VerbConjugationRules.PresentPerfectContinuous
} else if tense == "past perfect continuous" {
template = language.VerbConjugationRules.PastPerfectContinuous
} else if tense == "future perfect continuous" {
template = language.VerbConjugationRules.FuturePerfectContinuous
}
word := renderWord(root, template)
return word
}
// GenerateWordList creates a word list for a language
func (language Language) GenerateWordList() (map[string]string, error) {
wordList := make(map[string]string)
wordTypes := getAllWordTypes()
for _, t := range wordTypes {
for _, w := range t.WordList {
for {
newWord, err := language.randomWord(t.MaxSyllables)
if err != nil {
err = fmt.Errorf("Could not generate word list: %w", err)
return wordList, err
}
if !IsInWordList(newWord, wordList) {
wordList[w], err = language.randomWord(t.MaxSyllables)
if err != nil {
err = fmt.Errorf("Could not generate word list: %w", err)
return wordList, err
}
break
}
}
}
}
return wordList, nil
}
// AddNounToWordList adds a new noun to an existing word list for a language
func (language Language) AddNounToWordList(word string) (map[string]string, error) {
wordList := language.WordList
for {
newWord, err := language.randomWord(3)
if err != nil {
err = fmt.Errorf("Could not add noun to word list: %w", err)
return wordList, err
}
if !IsInWordList(newWord, wordList) {
wordList[word], err = language.randomWord(3)
if err != nil {
err = fmt.Errorf("Could not add noun to word list: %w", err)
return wordList, err
}
break
}
}
return wordList, nil
}
// MakePractice changes a word into an "-ism" form word
func (language Language) MakePractice(word string) string {
practice := word + language.PracticeSuffix
return practice
}
func renderWord(word string, wordTemplate string) string {
var tplOutput bytes.Buffer
data := Word{
Root: word,
}
tmpl, _ := template.New(word).Parse(wordTemplate)
tmpl.Execute(&tplOutput, data)
templateOutput := tplOutput.String()
return templateOutput
}