-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.go
136 lines (104 loc) · 2.85 KB
/
gen.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 namegen creates random names based on the probability of characters in a given string.
package namegen
import (
"strings"
"github.com/der-antikeks/namegen/rand"
"github.com/der-antikeks/namegen/ssyll"
)
const (
StopString = " " // The string that is used to separate names.
Vowels = "aeiou" // Simple english vowels for syllable splitting, ignore Y as only-sometimes-vowel
)
// A NameGen creates random names.
type NameGen struct {
dict map[string]map[string]int // [previous syllable][next syllable]amount
length map[int]int // [number of syllables]amount
}
// NewNameGen returns a new NameGen and calculates the probabilities of consecutive syllables based on the passed string slice.
func NewNameGen(names []string) *NameGen {
n := new(NameGen)
n.length = make(map[int]int)
n.dict = make(map[string]map[string]int)
var syllables []string
prev := StopString
for _, name := range names {
name = strings.ToLower(name)
syllables = ssyll.Divide(name, Vowels)
n.length[len(syllables)]++
for _, syllable := range syllables {
if n.dict[prev] == nil {
n.dict[prev] = make(map[string]int)
}
n.dict[prev][syllable]++
prev = syllable
}
if n.dict[prev] == nil {
n.dict[prev] = make(map[string]int)
}
n.dict[prev][StopString]++
prev = StopString
}
return n
}
// GenerateOne generates a single name.
func (n NameGen) GenerateOne() string {
return n.GenerateWithStart(string(StopString))
}
// GenerateMultiple generates multiple names.
func (n NameGen) GenerateMultiple(amount int) []string {
ret := make([]string, amount)
for i := range ret {
ret[i] = n.GenerateOne()
}
return ret
}
// GenerateWithStart generates a single name with a specified start string.
func (n NameGen) GenerateWithStart(start string) string {
var cur string
var name []string
prev := start
length := selectInt(n.length)
for (cur != StopString) && (len(name) <= length) {
if n.dict[prev] == nil {
break
}
cur = selectString(n.dict[prev])
name = append(name, cur)
prev = cur
}
return strings.TrimSpace(strings.Join(name, ""))
}
// selectString chooses a string based on the weight.
func selectString(dict map[string]int) string {
rand.Seed()
data := []string{}
weights := []float64{}
sum := 0.0
for _, w := range dict {
sum += float64(w)
}
for c, w := range dict {
data = append(data, c)
weights = append(weights, float64(w)/sum)
}
index := rand.WeightedChoice(weights, sum)
result := data[index]
return result
}
// selectInt chooses an int based on the weight.
func selectInt(dict map[int]int) int {
rand.Seed()
data := []int{}
weights := []float64{}
sum := 0.0
for _, w := range dict {
sum += float64(w)
}
for c, w := range dict {
data = append(data, c)
weights = append(weights, float64(w)/sum)
}
index := rand.WeightedChoice(weights, sum)
result := data[index]
return result
}