-
Notifications
You must be signed in to change notification settings - Fork 3
/
ranking.go
105 lines (97 loc) · 2.47 KB
/
ranking.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
package main
import (
"sort"
"strings"
"unicode"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
)
func getMostFrequent(names []string) string {
counter := make(map[string]int)
for _, name := range names {
if _, ok := counter[name]; ok {
counter[name] = counter[name] + 1
} else if strings.TrimSpace(name) != "" {
counter[name] = 1
}
}
maxcount := 0
lastname := ""
for name, counter := range counter {
if counter > maxcount {
lastname = name
maxcount = counter
} else if counter == maxcount {
if name < lastname {
lastname = name
}
}
}
lastname = strings.TrimSpace(lastname)
lastname = strings.Replace(lastname, "\"", "", -1)
return lastname
}
func isMn(r rune) bool {
return unicode.Is(unicode.Mn, r) // Mn: nonspacing marks
}
func normalizeAddressNames(aD AddressData) AddressData {
if aD.Name != "" {
return aD
}
aD.Name = getMostFrequent(aD.Names)
t := transform.Chain(norm.NFD, transform.RemoveFunc(isMn), norm.NFC)
normStr, _, _ := transform.String(t, aD.Name)
aD.NormalizedName = normStr
return aD
}
func calculateRanks(data map[string]AddressData) map[int]map[string]AddressData {
type KeyValue struct {
Key string
Value AddressData
}
classedData := map[int]map[string]AddressData{
2: {},
1: {},
0: {},
}
for addr, value := range data {
classedData[value.Class][addr] = normalizeAddressNames(value)
}
for class := 2; class >= 0; class-- {
thisclass, _ := classedData[class]
s := make([]KeyValue, 0, len(thisclass))
for k, v := range thisclass {
s = append(s, KeyValue{k, v})
}
sort.SliceStable(s, func(i, j int) bool {
if s[i].Value.ClassCount[class] == s[j].Value.ClassCount[class] {
return s[i].Value.Address < s[j].Value.Address
} else {
return s[i].Value.ClassCount[class] > s[j].Value.ClassCount[class]
}
})
for rank, kv := range s {
thisval, _ := thisclass[kv.Key]
thisval.FrequencyRank = rank
thisclass[kv.Key] = thisval
}
sort.SliceStable(s, func(i, j int) bool {
if s[i].Value.ClassDate[class] == s[j].Value.ClassDate[class] {
return s[i].Value.Address < s[j].Value.Address
} else {
return s[i].Value.ClassDate[class] > s[j].Value.ClassDate[class]
}
})
for rank, kv := range s {
thisval, _ := thisclass[kv.Key]
thisval.RecencyRank = rank
thisclass[kv.Key] = thisval
}
for k, v := range thisclass {
v.TotalRank = v.FrequencyRank + v.RecencyRank
thisclass[k] = v
}
classedData[class] = thisclass
}
return classedData
}