-
Notifications
You must be signed in to change notification settings - Fork 0
/
nameset.go
86 lines (73 loc) · 1.44 KB
/
nameset.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
package fakename
import (
"fmt"
"io"
"math/rand"
"time"
)
type Nameset interface {
RandomName() string
Label() string
All() (name []string, count []int, cum int)
}
type nsconf struct {
r *rand.Rand
label string
}
func defaultNamesetConfig() nsconf {
return nsconf{
r: rand.New(rand.NewSource(time.Now().UnixNano())),
label: "no label",
}
}
type NamesetOption func(nsconf) nsconf
func NamesetWithRand(r *rand.Rand) NamesetOption {
return func(n nsconf) nsconf {
n.r = r
return n
}
}
func NamesetWithLabel(label string) NamesetOption {
return func(n nsconf) nsconf {
n.label = label
return n
}
}
func NewSet(r io.Reader, opts ...NamesetOption) (Nameset, error) {
l := NewLoader(r)
_ns, err := l.Read()
ns := _ns.(*nameset)
if err != nil {
return nil, fmt.Errorf("ns: read: %w", err)
}
ns.conf = defaultNamesetConfig()
for _, opt := range opts {
ns.conf = opt(ns.conf)
}
return ns, nil
}
type nameset struct {
conf nsconf
name []string
count []int
cum int
}
func (ns nameset) RandomName() string {
p := ns.conf.r.Float64()
pos := int(p * float64(ns.cum))
var i, count, cur int
for i, count = range ns.count {
cur += count
if cur >= pos &&
(ns.name[i] != "AUTRES NOMS" && ns.name[i] != "_PRENOMS_RARES") {
break
}
}
return ns.name[i]
}
func (ns nameset) Label() string {
return ns.conf.label
}
func (ns nameset) All() (name []string, count []int, cum int) {
return ns.name, ns.count, ns.cum
}