-
Notifications
You must be signed in to change notification settings - Fork 5
/
namestring.go
206 lines (174 loc) · 4.65 KB
/
namestring.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
package gonameparts
import (
"sort"
"strings"
)
type nameString struct {
FullName string
SplitName []string
Nickname string
Aliases []string
}
func (n *nameString) cleaned() []string {
unwanted := []string{",", "."}
cleaned := []string{}
for _, x := range n.split() {
for _, y := range unwanted {
x = strings.Replace(x, y, "", -1)
}
cleaned = append(cleaned, strings.Trim(x, " "))
}
return cleaned
}
func (n *nameString) searchParts(parts []string) int {
for i, x := range n.cleaned() {
for _, y := range parts {
if strings.ToUpper(x) == strings.ToUpper(y) {
return i
}
}
}
return -1
}
func (n *nameString) looksCorporate() bool {
return n.searchParts(corpEntity) > -1
}
func (n *nameString) hasComma() bool {
for _, x := range n.split() {
if strings.ContainsAny(x, ",") {
return true
}
}
return false
}
func (n *nameString) slotNickname() {
var nickNameBoundaries []int
for index, x := range n.split() {
if string(x[0]) == "'" || x[0] == '"' {
nickNameBoundaries = append(nickNameBoundaries, index)
}
if string(x[len(x)-1]) == "'" || x[len(x)-1] == '"' {
nickNameBoundaries = append(nickNameBoundaries, index)
}
}
if len(nickNameBoundaries) > 0 && len(nickNameBoundaries)%2 == 0 {
nickStart := nickNameBoundaries[0]
nickEnd := nickNameBoundaries[1]
nick := n.SplitName[:nickStart]
postNick := n.SplitName[nickEnd+1:]
n.Nickname = strings.Join(n.SplitName[nickStart:nickEnd+1], " ")
nick = append(nick, postNick...)
n.FullName = strings.Join(nick, " ")
}
}
func (n *nameString) fixMisplacedApostrophe() {
var endsWithApostrophe []int
for index, x := range n.split() {
if string(x[len(x)-1]) == "'" {
endsWithApostrophe = append(endsWithApostrophe, index)
}
}
if len(endsWithApostrophe) > 0 {
for _, y := range endsWithApostrophe {
if n.SplitName[y] == n.SplitName[len(n.SplitName)-1] {
tmpName := n.SplitName[:y]
tmpName = append(tmpName, strings.Trim(n.SplitName[y], "'"))
n.FullName = strings.Join(tmpName, " ")
} else {
misplacedStart := y
// Build a new name part composed of the misplaced apostrophe
// plus what it should be attached to (i.e. O' Hurley becomes O'Hurley)
fixedName := []string{n.SplitName[misplacedStart]}
fixedName = append(fixedName, n.SplitName[misplacedStart+1])
fixedPlacement := strings.Join(fixedName, "")
// Rebuild our FullName with our fixedPlacement
tmpName := n.SplitName[:misplacedStart]
tmpName = append(tmpName, fixedPlacement)
partsAfterMisplacedStart := n.SplitName[misplacedStart+2:]
tmpName = append(tmpName, partsAfterMisplacedStart...)
n.FullName = strings.Join(tmpName, " ")
}
}
}
}
func (n *nameString) hasAliases() (bool, string) {
upperName := strings.ToUpper(n.FullName)
for _, x := range nonName {
if strings.Contains(upperName, x) && !strings.HasSuffix(upperName, x) {
return true, x
}
}
return false, ""
}
func (n *nameString) find(part string) int {
switch part {
case "salutation":
return n.searchParts(salutations)
case "generation":
return n.searchParts(generations)
case "suffix":
return n.searchParts(suffixes)
case "lnprefix":
return n.searchParts(lnPrefixes)
case "nonname":
return n.searchParts(nonName)
case "supplemental":
return n.searchParts(supplementalInfo)
default:
}
return -1
}
func (n *nameString) split() []string {
n.SplitName = strings.Fields(n.FullName)
return n.SplitName
}
func (n *nameString) normalize() []string {
// Handle any aliases in our nameString
hasAlias, aliasSep := n.hasAliases()
if hasAlias {
n.splitAliases(aliasSep)
}
// Strip Supplemental info
supplementalIndex := n.find("supplemental")
if supplementalIndex > -1 {
n.FullName = strings.Join(n.SplitName[:supplementalIndex], " ")
}
// Handle quoted Nicknames
n.slotNickname()
// Handle misplaced apostrophes
n.fixMisplacedApostrophe()
// Swap Lastname, Firstname to Firstname Lastname
if n.hasComma() {
commaSplit := strings.SplitN(n.FullName, ",", 2)
sort.StringSlice(commaSplit).Swap(1, 0)
n.FullName = strings.Join(commaSplit, " ")
}
return n.cleaned()
}
func (n *nameString) splitAliases(aliasSep string) {
splitNames := n.split()
for index, part := range splitNames {
if strings.ToUpper(part) == aliasSep {
splitNames[index] = "*|*"
}
}
names := strings.Split(strings.Join(splitNames, " "), "*|*")
n.FullName = names[0]
n.Aliases = names[1:]
}
func (n *nameString) findNotSlotted(slotted []int) []int {
var notSlotted []int
for i := range n.SplitName {
found := false
for _, j := range slotted {
if i == j {
found = true
break
}
}
if !found {
notSlotted = append(notSlotted, i)
}
}
return notSlotted
}