-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwords.go
More file actions
394 lines (343 loc) · 13.6 KB
/
words.go
File metadata and controls
394 lines (343 loc) · 13.6 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package gofaker
import (
"bytes"
"errors"
"strings"
"unicode"
)
type paragrapOptions struct {
paragraphCount int
sentenceCount int
wordCount int
separator string
}
const bytesPerWordEstimation = 6
type sentenceGenerator func(wordCount int) string
type wordGenerator func() string
// Noun will generate a random noun
func Noun() string {
return getRandValue([]string{"word", "noun"})
}
// Verb will generate a random verb
func Verb() string {
return getRandValue([]string{"word", "verb"})
}
// Adverb will generate a random adverb
func Adverb() string {
return getRandValue([]string{"word", "adverb"})
}
// Preposition will generate a random preposition
func Preposition() string {
return getRandValue([]string{"word", "preposition"})
}
// Adjective will generate a random adjective
func Adjective() string {
return getRandValue([]string{"word", "adjective"})
}
// Word will generate a random word
func Word() string {
if Bool() {
return getRandValue([]string{"word", "noun"})
}
return getRandValue([]string{"word", "verb"})
}
// Sentence will generate a random sentence
func Sentence(wordCount int) string {
return sentence(wordCount, Word)
}
// Paragraph will generate a random paragraphGenerator
func Paragraph(paragraphCount int, sentenceCount int, wordCount int, separator string) string {
return paragraphGenerator(paragrapOptions{paragraphCount, sentenceCount, wordCount, separator}, Sentence)
}
// LoremIpsumWord will generate a random word
func LoremIpsumWord() string {
return getRandValue([]string{"lorem", "word"})
}
// LoremIpsumSentence will generate a random sentence
func LoremIpsumSentence(wordCount int) string {
return sentence(wordCount, LoremIpsumWord)
}
// LoremIpsumParagraph will generate a random paragraphGenerator
func LoremIpsumParagraph(paragraphCount int, sentenceCount int, wordCount int, separator string) string {
return paragraphGenerator(paragrapOptions{paragraphCount, sentenceCount, wordCount, separator}, LoremIpsumSentence)
}
func sentence(wordCount int, word wordGenerator) string {
if wordCount <= 0 {
return ""
}
wordSeparator := ' '
sentence := bytes.Buffer{}
sentence.Grow(wordCount * bytesPerWordEstimation)
for i := 0; i < wordCount; i++ {
word := word()
if i == 0 {
runes := []rune(word)
runes[0] = unicode.ToTitle(runes[0])
word = string(runes)
}
sentence.WriteString(word)
if i < wordCount-1 {
sentence.WriteRune(wordSeparator)
}
}
sentence.WriteRune('.')
return sentence.String()
}
func paragraphGenerator(opts paragrapOptions, sentecer sentenceGenerator) string {
if opts.paragraphCount <= 0 || opts.sentenceCount <= 0 || opts.wordCount <= 0 {
return ""
}
//to avoid making Go 1.10 dependency, we cannot use strings.Builder
paragraphs := bytes.Buffer{}
//we presume the length
paragraphs.Grow(opts.paragraphCount * opts.sentenceCount * opts.wordCount * bytesPerWordEstimation)
wordSeparator := ' '
for i := 0; i < opts.paragraphCount; i++ {
for e := 0; e < opts.sentenceCount; e++ {
paragraphs.WriteString(sentecer(opts.wordCount))
if e < opts.sentenceCount-1 {
paragraphs.WriteRune(wordSeparator)
}
}
if i < opts.paragraphCount-1 {
paragraphs.WriteString(opts.separator)
}
}
return paragraphs.String()
}
// Question will return a random question
func Question() string {
return strings.Replace(HipsterSentence(Number(3, 10)), ".", "?", 1)
}
// Quote will return a random quote from a random person
func Quote() string {
return `"` + HipsterSentence(Number(3, 10)) + `" - ` + FirstName() + " " + LastName()
}
// Phrase will return a random dictionary phrase
func Phrase() string {
return getRandValue([]string{"word", "phrase"})
}
func addWordLookup() {
AddFuncLookup("noun", Info{
Display: "Noun",
Category: "word",
Description: "Random noun",
Example: "foot",
Output: "string",
Call: func(m *map[string][]string, info *Info) (interface{}, error) {
return Noun(), nil
},
})
AddFuncLookup("verb", Info{
Display: "Verb",
Category: "word",
Description: "Random verb",
Example: "release",
Output: "string",
Call: func(m *map[string][]string, info *Info) (interface{}, error) {
return Verb(), nil
},
})
AddFuncLookup("adverb", Info{
Display: "Adverb",
Category: "word",
Description: "Random adverb",
Example: "smoothly",
Output: "string",
Call: func(m *map[string][]string, info *Info) (interface{}, error) {
return Adverb(), nil
},
})
AddFuncLookup("preposition", Info{
Display: "Preposition",
Category: "word",
Description: "Random preposition",
Example: "down",
Output: "string",
Call: func(m *map[string][]string, info *Info) (interface{}, error) {
return Preposition(), nil
},
})
AddFuncLookup("adjective", Info{
Display: "Adjective",
Category: "word",
Description: "Random adjective",
Example: "genuine",
Output: "string",
Call: func(m *map[string][]string, info *Info) (interface{}, error) {
return Adjective(), nil
},
})
AddFuncLookup("word", Info{
Display: "Word",
Category: "word",
Description: "Random word",
Example: "man",
Output: "string",
Call: func(m *map[string][]string, info *Info) (interface{}, error) {
return Word(), nil
},
})
AddFuncLookup("sentence", Info{
Display: "Sentence",
Category: "word",
Description: "Random sentence",
Example: "Interpret context record river mind.",
Output: "string",
Params: []Param{
{Field: "wordcount", Display: "Word Count", Type: "int", Default: "5", Description: "Number of words in a sentence"},
},
Call: func(m *map[string][]string, info *Info) (interface{}, error) {
wordCount, err := info.GetInt(m, "wordcount")
if err != nil {
return nil, err
}
if wordCount <= 0 || wordCount > 50 {
return nil, errors.New("Invalid word count, must be greater than 0, less than 50")
}
return Sentence(wordCount), nil
},
})
AddFuncLookup("paragraph", Info{
Display: "Paragraph",
Category: "word",
Description: "Random paragraph",
Example: "Interpret context record river mind press self should compare property outcome divide. Combine approach sustain consult discover explanation direct address church husband seek army. Begin own act welfare replace press suspect stay link place manchester specialist. Arrive price satisfy sign force application hair train provide basis right pay. Close mark teacher strengthen information attempt head touch aim iron tv take. Handle wait begin look speech trust cancer visit capacity disease chancellor clean. Race aim function gain couple push faith enjoy admit ring attitude develop. Edge game prevent cast mill favour father star live search aim guess. West heart item adopt compete equipment miss output report communicate model cabinet. Seek worker variety step argue air improve give succeed relief artist suffer. Hide finish insist knowledge thatcher make research chance structure proportion husband implement. Town crown restaurant cost material compete lady climb football region discussion order. Place lee market ice like display mind stress compete weather station raise. Democracy college major recall struggle use cut intention accept period generation strike. Benefit defend recommend conclude justify result depend succeed address owner fill interpret.",
Output: "string",
Params: []Param{
{Field: "paragraphcount", Display: "Paragraph Count", Type: "int", Default: "2", Description: "Number of paragraphs"},
{Field: "sentencecount", Display: "Sentence Count", Type: "int", Default: "2", Description: "Number of sentences in a paragraph"},
{Field: "wordcount", Display: "Word Count", Type: "int", Default: "5", Description: "Number of words in a sentence"},
{Field: "paragraphseparator", Display: "Paragraph Separator", Type: "string", Default: "<br />", Description: "String value to add between paragraphs"},
},
Call: func(m *map[string][]string, info *Info) (interface{}, error) {
paragraphCount, err := info.GetInt(m, "paragraphcount")
if err != nil {
return nil, err
}
if paragraphCount <= 0 || paragraphCount > 20 {
return nil, errors.New("Invalid paragraph count, must be greater than 0, less than 20")
}
sentenceCount, err := info.GetInt(m, "sentencecount")
if err != nil {
return nil, err
}
if sentenceCount <= 0 || sentenceCount > 20 {
return nil, errors.New("Invalid sentence count, must be greater than 0, less than 20")
}
wordCount, err := info.GetInt(m, "wordcount")
if err != nil {
return nil, err
}
if wordCount <= 0 || wordCount > 50 {
return nil, errors.New("Invalid word count, must be greater than 0, less than 50")
}
paragraphSeparator, err := info.GetString(m, "paragraphseparator")
if err != nil {
return nil, err
}
return Paragraph(paragraphCount, sentenceCount, wordCount, paragraphSeparator), nil
},
})
AddFuncLookup("loremipsumword", Info{
Display: "Lorem Ipsum Word",
Category: "word",
Description: "Random lorem ipsum word",
Example: "quia",
Output: "string",
Call: func(m *map[string][]string, info *Info) (interface{}, error) {
return LoremIpsumWord(), nil
},
})
AddFuncLookup("loremipsumsentence", Info{
Display: "Lorem Ipsum Sentence",
Category: "word",
Description: "Random lorem ipsum sentence",
Example: "Quia quae repellat consequatur quidem.",
Output: "string",
Params: []Param{
{Field: "wordcount", Display: "Word Count", Type: "int", Default: "5", Description: "Number of words in a sentence"},
},
Call: func(m *map[string][]string, info *Info) (interface{}, error) {
wordCount, err := info.GetInt(m, "wordcount")
if err != nil {
return nil, err
}
if wordCount <= 0 || wordCount > 50 {
return nil, errors.New("Invalid word count, must be greater than 0, less than 50")
}
return LoremIpsumSentence(wordCount), nil
},
})
AddFuncLookup("loremipsumparagraph", Info{
Display: "Lorem Ipsum Paragraph",
Category: "word",
Description: "Random lorem ipsum paragraph",
Example: "Quia quae repellat consequatur quidem nisi quo qui voluptatum accusantium quisquam amet. Quas et ut non dolorem ipsam aut enim assumenda mollitia harum ut. Dicta similique veniam nulla voluptas at excepturi non ad maxime at non. Eaque hic repellat praesentium voluptatem qui consequuntur dolor iusto autem velit aut. Fugit tempore exercitationem harum consequatur voluptatum modi minima aut eaque et et.<br />Aut ea voluptatem dignissimos expedita odit tempore quod aut beatae ipsam iste. Minus voluptatibus dolorem maiores eius sed nihil vel enim odio voluptatem accusamus. Natus quibusdam temporibus tenetur cumque sint necessitatibus dolorem ex ducimus iusto ex. Voluptatem neque dicta explicabo officiis et ducimus sit ut ut praesentium pariatur. Illum molestias nisi at dolore ut voluptatem accusantium et fugiat et ut.<br />Explicabo incidunt reprehenderit non quia dignissimos recusandae vitae soluta quia et quia. Aut veniam voluptas consequatur placeat sapiente non eveniet voluptatibus magni velit eum. Nobis vel repellendus sed est qui autem laudantium quidem quam ullam consequatur. Aut iusto ut commodi similique quae voluptatem atque qui fugiat eum aut. Quis distinctio consequatur voluptatem vel aliquid aut laborum facere officiis iure tempora.",
Output: "string",
Params: []Param{
{Field: "paragraphcount", Display: "Paragraph Count", Type: "int", Default: "2", Description: "Number of paragraphs"},
{Field: "sentencecount", Display: "Sentence Count", Type: "int", Default: "2", Description: "Number of sentences in a paragraph"},
{Field: "wordcount", Display: "Word Count", Type: "int", Default: "5", Description: "Number of words in a sentence"},
{Field: "paragraphseparator", Display: "Paragraph Separator", Type: "string", Default: "<br />", Description: "String value to add between paragraphs"},
},
Call: func(m *map[string][]string, info *Info) (interface{}, error) {
paragraphCount, err := info.GetInt(m, "paragraphcount")
if err != nil {
return nil, err
}
if paragraphCount <= 0 || paragraphCount > 20 {
return nil, errors.New("Invalid paragraph count, must be greater than 0, less than 20")
}
sentenceCount, err := info.GetInt(m, "sentencecount")
if err != nil {
return nil, err
}
if sentenceCount <= 0 || sentenceCount > 20 {
return nil, errors.New("Invalid sentence count, must be greater than 0, less than 20")
}
wordCount, err := info.GetInt(m, "wordcount")
if err != nil {
return nil, err
}
if wordCount <= 0 || wordCount > 50 {
return nil, errors.New("Invalid word count, must be greater than 0, less than 50")
}
paragraphSeparator, err := info.GetString(m, "paragraphseparator")
if err != nil {
return nil, err
}
return LoremIpsumParagraph(paragraphCount, sentenceCount, wordCount, paragraphSeparator), nil
},
})
AddFuncLookup("question", Info{
Display: "Question",
Category: "word",
Description: "Random question",
Example: "Roof chia echo?",
Output: "string",
Call: func(m *map[string][]string, info *Info) (interface{}, error) {
return Question(), nil
},
})
AddFuncLookup("quote", Info{
Display: "Qoute",
Category: "word",
Description: "Random quote",
Example: `"Roof chia echo." - Lura Lockman`,
Output: "string",
Call: func(m *map[string][]string, info *Info) (interface{}, error) {
return Quote(), nil
},
})
AddFuncLookup("phrase", Info{
Display: "Phrase",
Category: "word",
Description: "Random phrase",
Example: "time will tell",
Output: "string",
Call: func(m *map[string][]string, info *Info) (interface{}, error) {
return Phrase(), nil
},
})
}