-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
120 lines (103 loc) · 2.4 KB
/
main.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
package main
import (
"encoding/csv"
"flag"
"fmt"
"io"
"log"
"os"
"regexp"
"strconv"
"strings"
"time"
)
var (
name = flag.String("name", "", "Name to find")
last = flag.Int64("last", 0, "Limit to last X years")
csvF = flag.Bool("csv", false, "Use CSV output")
filF = flag.Bool("f", false, "Filter to results with sex 'F'")
filM = flag.Bool("m", false, "Filter to results with sex 'M'")
filR = flag.Bool("r", false, "Filter with regex matching")
)
func main() {
flag.Parse()
//
// Validate data and args
female, err := os.Open("data/ontario_top_baby_names_female_1917-2022_en_fr_clean.csv")
if err != nil {
panic(err)
}
male, err := os.Open("data/ontario_top_baby_names_male_1917-2022_en_fr_clean.csv")
if err != nil {
panic(err)
}
if len(*name) == 0 {
flag.Usage()
return
}
//
//
// Set vars
showF := *filF == true || (*filF == false && *filM == false)
showM := *filM == true || (*filF == false && *filM == false)
var filter *regexp.Regexp = nil
var validName = *name
//
// Check for valid regex
if f, err := regexp.Compile(fmt.Sprintf("(?i)%s", validName)); err != nil || *filF != true {
// don't use regex if it fails to compile
validName = strings.ToUpper(validName)
if err != nil && *csvF != true {
log.Printf("Failed to compile regex, reverting to string compare mode: %v\n", err)
}
} else {
filter = f
validName = filter.String()
}
if *csvF != true {
log.Printf(
"\nMATCHING NAME:\t%s\nUSE REGEX:\t%v\nSHOW MALE:\t%v\nSHOW FEMALE:\t%v\n\n",
validName, filter != nil, showM, showF,
)
}
if showF {
read(female, "F", validName, filter)
}
if showM {
read(male, "M", validName, filter)
}
}
func read(file *os.File, sex string, name string, filter *regexp.Regexp) {
r := csv.NewReader(file)
currentYear := time.Now().Year()
for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
year, err := strconv.Atoi(record[0])
if err != nil {
continue
}
if *last > 0 && int64(currentYear-year) > *last {
continue
}
if matchName(record[1], name, filter) {
if *csvF != true {
fmt.Printf("[%s] %s (%s)\n", sex, record[0], record[2])
} else {
fmt.Printf("%s,%s,%s,%s\n", sex, record[0], record[1], record[2])
}
}
}
}
func matchName(record string, name string, filter *regexp.Regexp) bool {
if filter == nil {
return record == name
} else {
return filter.MatchString(record)
}
}