This repository has been archived by the owner on Oct 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcomponents.go
198 lines (168 loc) · 5.36 KB
/
components.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package naminglanguage
import (
"math/rand"
)
func isUnique(word string, wordList []Word) bool {
for _, entry := range wordList {
if word == entry.Word {
return false
}
}
return true
}
func nounFromVerb(word string) string {
lastCharacter := string(word[len(word)-1:])
if lastCharacter == "e" {
return word + "r"
}
return word + "er"
}
func pluralizeNoun(word string) string {
lastCharacter := string(word[len(word)-1:])
if lastCharacter == "s" {
return word + "ses"
}
return word + "s"
}
func generateSyllable(consonants string, vowels string, sibilants string, glides string, finals string, order []string) string {
syllable := ""
component := ""
for _, orderItem := range order {
component = randomCharacterFromString(consonants)
if orderItem == "V" {
component = randomCharacterFromString(vowels)
} else if orderItem == "S?" {
component = optionalCharacter(randomCharacterFromString(sibilants))
} else if orderItem == "G" {
component = randomCharacterFromString(glides)
} else if orderItem == "F" {
component = randomCharacterFromString(finals)
}
syllable += component
}
return syllable
}
func generateSyllables() []string {
syllable := ""
consonants := randomConsonantSet()
vowels := randomVowelSet()
sibilants := randomSibilantSet()
glides := randomGlideSet()
finals := randomFinalSet()
order := randomSyllableOrder()
var syllables []string
badCombinations := []string{"sd", "bx", "cx", "zd"}
doubleLetter := "aa"
for _, letter := range "abcdefghijklmnopqrstuvwyxz" {
doubleLetter = string(letter + letter)
badCombinations = append(badCombinations, doubleLetter)
}
for i := 0; i < 150; i++ {
syllable = generateSyllable(consonants, vowels, sibilants, glides, finals, order)
for stringInSlice(syllable, badCombinations) {
syllable = generateSyllable(consonants, vowels, sibilants, glides, finals, order)
}
syllables = append(syllables, syllable)
}
return syllables
}
func generateWordsForType(wordType string, wordList []string, existingWords []Word, maxSyllables int, syllables []string) []Word {
var words []Word
wordString := ""
word := Word{}
for _, wordMeaning := range wordList {
unique := false
for !unique {
wordString = ""
for j := 0; j < maxSyllables; j++ {
wordString += randomSyllable(syllables)
}
if maxSyllables == 1 || isUnique(wordString, existingWords) {
unique = true
}
}
word = Word{wordString, wordMeaning, wordType}
words = append(words, word)
}
return words
}
func generateWords(syllables []string) []Word {
var words []Word
words = append(words, generateWordsForType("article", articles, words, 1, syllables)...)
words = append(words, generateWordsForType("adjective", adjectives, words, randomWordSyllableLength(2), syllables)...)
words = append(words, generateWordsForType("adverb", adverbs, words, randomWordSyllableLength(2), syllables)...)
words = append(words, generateWordsForType("noun", nouns, words, randomWordSyllableLength(3), syllables)...)
words = append(words, generateWordsForType("verb", verbs, words, randomWordSyllableLength(3), syllables)...)
words = append(words, generateWordsForType("conjunction", conjunctions, words, 1, syllables)...)
words = append(words, generateWordsForType("pronoun", pronouns, words, 1, syllables)...)
return words
}
func getWordsByType(language Language, wordType string) []Word {
var words []Word
for _, word := range language.Dictionary {
if word.Part == wordType {
words = append(words, word)
}
}
return words
}
func optionalCharacter(character string) string {
i := rand.Intn(10)
if i >= 6 {
return character
}
return ""
}
func randomConsonantSet() string {
consonantSets := []string{"ptkmnsl", "ptkbdgmnlrsz", "ptkqvsgrmn", "tkdgmns", "tksdbqxmnlrwj"}
return consonantSets[rand.Intn(len(consonantSets))]
}
func randomCharacterFromString(items string) string {
characters := []byte(items)
return string(characters[rand.Intn(len(characters))])
}
func randomFinalSet() string {
finalSets := []string{"mn", "sk", "sz"}
return finalSets[rand.Intn(len(finalSets))]
}
func randomGlideSet() string {
glideSets := []string{"l", "r", "lr", "lrw", "lrwj"}
return glideSets[rand.Intn(len(glideSets))]
}
func randomSibilantSet() string {
sibilantSets := []string{"s", "sf"}
return sibilantSets[rand.Intn(len(sibilantSets))]
}
func randomVowelSet() string {
vowelSets := []string{"aeiou", "aiu"}
return vowelSets[rand.Intn(len(vowelSets))]
}
func randomAdjective(language Language) Word {
words := getWordsByType(language, "adjective")
return words[rand.Intn(len(words))]
}
func randomNoun(language Language) Word {
words := getWordsByType(language, "noun")
return words[rand.Intn(len(words))]
}
func randomVerb(language Language) Word {
words := getWordsByType(language, "verb")
return words[rand.Intn(len(words))]
}
func randomWord(language Language) Word {
return language.Dictionary[rand.Intn(len(language.Dictionary))]
}
func randomSyllable(syllables []string) string {
return syllables[rand.Intn(len(syllables))]
}
func randomSyllableOrder() []string {
syllableOrders := [][]string{{"C", "V", "C"}, {"S?", "C", "V", "C"}, {"S?", "C", "V", "F"}, {"C", "V"}}
return syllableOrders[rand.Intn(len(syllableOrders))]
}
func randomWordOrder() []string {
wordOrders := [][]string{{"S", "V", "O"}}
return wordOrders[rand.Intn(len(wordOrders))]
}
func randomWordSyllableLength(max int) int {
return rand.Intn(max) + 1
}