-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.go
121 lines (112 loc) · 2.94 KB
/
generator.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
package slug_generator
import (
"fmt"
"math/rand"
"strings"
)
type SlugGenerator struct {
random *rand.Rand
adjectives []string
nouns []string
}
func (sg *SlugGenerator) getRandomAdjective() string {
return sg.adjectives[rand.Intn(len(sg.adjectives))]
}
func (sg *SlugGenerator) getRandomNoun() string {
return sg.nouns[rand.Intn(len(sg.nouns))]
}
func NewSlugGenerator(seed int64, adjectives, nouns []string) *SlugGenerator {
src := rand.NewSource(seed)
return &SlugGenerator{
random: rand.New(src),
adjectives: adjectives,
nouns: nouns,
}
}
func (sg *SlugGenerator) Generate(length int) string {
if length < 1 {
return ""
}
if length == 1 {
return sg.getRandomNoun()
}
if length == 2 {
return fmt.Sprintf("%v-%v", sg.getRandomAdjective(), sg.getRandomNoun())
}
if length == 3 {
return fmt.Sprintf("%v-%v-%v", sg.getRandomAdjective(), sg.getRandomAdjective(), sg.getRandomNoun())
}
slugArr := make([]string, length)
adjectiveCount := 0
previousWordWasNoun := false
previousWordWasOf := false
for i := 0; i < length; i++ {
if i == length-3 {
if previousWordWasNoun {
slugArr[i] = "of"
previousWordWasOf = true
previousWordWasNoun = false
adjectiveCount = 0
} else if previousWordWasOf {
// randomly pick noun or adjective
if sg.random.Intn(2) == 0 {
slugArr[i] = sg.getRandomNoun()
previousWordWasNoun = true
previousWordWasOf = false
adjectiveCount = 0
} else {
slugArr[i] = sg.getRandomAdjective()
adjectiveCount++
previousWordWasNoun = false
previousWordWasOf = false
}
} else {
slugArr[i] = sg.getRandomNoun()
previousWordWasNoun = true
previousWordWasOf = false
adjectiveCount = 0
}
} else if i == length-2 {
if previousWordWasNoun {
slugArr[i] = "of"
previousWordWasOf = true
previousWordWasNoun = false
adjectiveCount = 0
} else if previousWordWasOf || adjectiveCount == 1 {
// 2nd to last word is an adjective, to make room for a noun
slugArr[i] = sg.getRandomAdjective()
previousWordWasNoun = false
previousWordWasOf = false
adjectiveCount++
} else {
// theoretically should never reach here
// but if it does, slug will be 1 short, which will be caught in tests
continue
}
} else if i == length-1 || adjectiveCount == 2 {
// Add a noun if two adjectives or last word
slugArr[i] = sg.getRandomNoun()
previousWordWasNoun = true
previousWordWasOf = false
adjectiveCount = 0
} else if previousWordWasNoun {
slugArr[i] = "of"
previousWordWasOf = true
previousWordWasNoun = false
adjectiveCount = 0
} else {
if sg.random.Intn(2) == 0 {
slugArr[i] = sg.getRandomNoun()
previousWordWasNoun = true
previousWordWasOf = false
adjectiveCount = 0
} else {
slugArr[i] = sg.getRandomAdjective()
adjectiveCount++
previousWordWasNoun = false
previousWordWasOf = false
}
}
}
return strings.Join(slugArr, "-")
}