-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrig.go
299 lines (261 loc) · 7.13 KB
/
grig.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
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
// The MIT License (MIT)
// Copyright (c) 2014 Claes Mogren
// http://opensource.org/licenses/MIT
// Code to generate a weighted random identity based on weighted input files
// http://www.keithschwarz.com/darts-dice-coins/
package main
import (
"bufio"
"encoding/json"
"encoding/xml"
"flag"
"fmt"
"github.com/mogren/grig/vose"
"math/rand"
"os"
"strconv"
"strings"
"sync"
"time"
)
var (
langFlag string
listLangFlag bool
verbose bool
nrLoopsFlag int
jsonFlag bool
xmlFlag bool
// Global RNG with mutex protection
globalRNG = struct {
sync.Mutex
rng *rand.Rand
}{rng: rand.New(rand.NewSource(time.Now().UnixNano()))}
)
// Rig holds a randomly generated identity
type Rig struct {
Firstname string `json:"firstname" xml:"firstname"`
Lastname string `json:"lastname" xml:"lastname"`
Street string `json:"street" xml:"street"`
StreetNumber int `json:"nr" xml:"nr"`
Zipcode int `json:"zip" xml:"zip"`
City string `json:"city" xml:"city"`
}
// AsText prints the Rig as text
func (r Rig) AsText() string {
var sb strings.Builder
sb.WriteString(r.Firstname + " " + r.Lastname + "\n")
if langFlag == "en_us" {
fmt.Fprintf(&sb, "%d %s\n", r.StreetNumber, r.Street)
} else {
fmt.Fprintf(&sb, "%s %d\n", r.Street, r.StreetNumber)
}
fmt.Fprintf(&sb, "%d %s\n", r.Zipcode, r.City)
return sb.String()
}
// AsJSON will output the Rig as JSON
func (r Rig) AsJSON(isLast bool) string {
var sb strings.Builder
b, err := json.Marshal(r) // Use Marshal instead of MarshalIndent for performance
if err != nil {
fmt.Println("error:", err)
}
sb.Write(b)
if !isLast {
sb.WriteString(",")
}
return sb.String()
}
// AsXML will output the Rig as XML
func (r Rig) AsXML() string {
var sb strings.Builder
b, err := xml.Marshal(r) // Use Marshal instead of MarshalIndent for performance
if err != nil {
fmt.Println("error:", err)
}
sb.Write(b)
return sb.String()
}
// RigFile contains all the randomly generated identities
type RigFile struct {
total float64
texts [][]string
vose *vose.Vose
}
// RigDict is the dictionary for the given language
type RigDict struct {
fnames, mnames, lnames, streets, zipcodes RigFile
}
func init() {
flag.StringVar(&langFlag, "lang", "en_us", "Select ISO 639-1 language code, defaults to USA")
flag.BoolVar(&listLangFlag, "l", false, "List available ISO language codes")
flag.BoolVar(&verbose, "v", false, "Verbose output")
flag.BoolVar(&jsonFlag, "j", false, "Print as JSON")
flag.BoolVar(&xmlFlag, "x", false, "Print as XML")
flag.IntVar(&nrLoopsFlag, "n", 1, "Number of identities to output")
}
func main() {
flag.Parse()
if listLangFlag {
listLangs()
os.Exit(0)
}
dict := loadData(langFlag)
// Pre-allocate identities for better performance when generating multiple
identities := make([]Rig, 0, nrLoopsFlag)
// Generate all identities
for i := 0; i < nrLoopsFlag; i++ {
identities = append(identities, getNext(dict))
}
// Output in requested format
if jsonFlag {
if nrLoopsFlag > 1 {
fmt.Println("\"list\": [")
}
for i, rig := range identities {
fmt.Println(rig.AsJSON(i == (nrLoopsFlag - 1)))
}
if nrLoopsFlag > 1 {
fmt.Println("]")
}
} else if xmlFlag {
for _, rig := range identities {
fmt.Println(rig.AsXML())
}
} else {
for _, rig := range identities {
fmt.Println(rig.AsText())
}
}
}
func listLangs() {
// for all dirs in data
files, _ := os.ReadDir("./data/")
for _, f := range files {
if f.IsDir() && !strings.HasPrefix(f.Name(), ".") {
if validateDir(f.Name()) {
fmt.Println(f.Name())
}
}
}
}
func validateDir(iso string) bool {
// Check for fnames, mnames, lnames, zipcodes and Streets
srcFileNames := []string{"fnames.grig", "lnames.grig", "mnames.grig", "streets.grig", "zipcodes.grig"}
valid := true
for _, srcFile := range srcFileNames {
filePath := "./data/" + iso + "/" + srcFile
_, err := os.Stat(filePath)
if err != nil {
if os.IsNotExist(err) {
if verbose {
fmt.Println("Data file", srcFile, "missing for", iso)
}
valid = false
} else {
// Handle other errors (permissions, etc.)
if verbose {
fmt.Printf("Error accessing %s: %v\n", filePath, err)
}
valid = false
}
}
}
return valid
}
func loadData(iso string) RigDict {
dict := RigDict{}
var wg sync.WaitGroup
wg.Add(5)
// Load files in parallel
go func() { dict.fnames = loadFile(iso, "fnames.grig"); wg.Done() }()
go func() { dict.mnames = loadFile(iso, "mnames.grig"); wg.Done() }()
go func() { dict.lnames = loadFile(iso, "lnames.grig"); wg.Done() }()
go func() { dict.streets = loadFile(iso, "streets.grig"); wg.Done() }()
go func() { dict.zipcodes = loadFile(iso, "zipcodes.grig"); wg.Done() }()
wg.Wait()
if verbose {
fmt.Printf("fname total: %.f\n", dict.fnames.total)
fmt.Printf("mname total: %.f\n", dict.mnames.total)
fmt.Printf("lname total: %.f\n", dict.lnames.total)
fmt.Printf("streets total: %.f\n", dict.streets.total)
fmt.Printf("zipcodes total: %.f\n", dict.zipcodes.total)
}
return dict
}
func loadFile(iso string, srcFile string) RigFile {
// Initial capacity estimates based on typical file sizes
const initialCapacity = 1000
file, err := os.Open("data/" + iso + "/" + srcFile)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
fmt.Println("Error closing file:", err)
os.Exit(1)
}
}(file)
rigFile := RigFile{}
rigFile.texts = make([][]string, 0, initialCapacity)
scanner := bufio.NewScanner(file)
sum := 0.0
weights := make([]float64, 0, initialCapacity)
for scanner.Scan() {
scanText := scanner.Text()
if strings.HasPrefix(scanText, "#") {
continue
}
dataStr := strings.Split(scanText, "\t")
// string to float
f, err := strconv.ParseFloat(dataStr[0], 64)
if err != nil {
fmt.Printf("Error parsing float in %s: %v\n", srcFile, err)
continue
}
sum += f
weights = append(weights, f)
rigFile.texts = append(rigFile.texts, dataStr[1:])
}
if err := scanner.Err(); err != nil {
fmt.Printf("Error reading file %s: %v\n", srcFile, err)
}
rigFile.total = sum
// Use the global RNG with mutex protection
globalRNG.Lock()
rigFile.vose, err = vose.NewVose(weights, globalRNG.rng)
globalRNG.Unlock()
if err != nil {
fmt.Printf("Vose error with %s: %v\n", srcFile, err)
panic(1)
}
return rigFile
}
func getNext(dict RigDict) Rig {
rig := Rig{}
// Use bit mask for coin flip (faster than rand.Intn(2))
globalRNG.Lock()
coinFlip := globalRNG.rng.Uint32() & 1
globalRNG.Unlock()
if coinFlip == 0 {
rig.Firstname = dict.fnames.texts[dict.fnames.vose.Next()][0]
} else {
rig.Firstname = dict.mnames.texts[dict.mnames.vose.Next()][0]
}
rig.Lastname = dict.lnames.texts[dict.lnames.vose.Next()][0]
rig.Street = dict.streets.texts[dict.streets.vose.Next()][0]
// Use global RNG with mutex protection
globalRNG.Lock()
rig.StreetNumber = globalRNG.rng.Intn(150) + 1
globalRNG.Unlock()
zip := dict.zipcodes.texts[dict.zipcodes.vose.Next()]
rig.City = zip[1]
zipcode, err := strconv.Atoi(zip[0])
if err != nil {
// Fallback to default value if conversion fails
zipcode = 10000
}
rig.Zipcode = zipcode
return rig
}