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
|
package names
import (
"errors"
"regexp"
"strings"
"unicode"
)
type Parts struct {
First string
Last string
Patronymic string // may be "" or an initial like "F"
}
func ParseLatinName(s string) (Parts, error) {
toks := tokenizeLatin(s) // keeps letters, apostrophes, hyphens, optional trailing dot
if len(toks) < 2 || len(toks) > 3 {
return Parts{}, errors.New("expecting 2 or 3 name parts")
}
type part struct {
raw string
lo string
}
ps := make([]part, 0, len(toks))
for _, t := range toks {
lo := strings.ToLower(strings.TrimSuffix(t, "."))
ps = append(ps, part{raw: t, lo: lo})
}
// fathersname
pIdx := -1
for i, p := range ps {
if isInitial(p.raw) || isFathersnameLatin(p.lo) {
pIdx = i
break
}
}
// Surname
lIdx := -1
for i, p := range ps {
if i == pIdx {
continue
}
if looksLikeSurnameLatin(p.lo) {
lIdx = i
break
}
}
// firs name
rem := make([]int, 0, 2)
for i := range ps {
if i != pIdx && i != lIdx {
rem = append(rem, i)
}
}
// if surname not obvious and we have 2 leftovers, pick the longer one as surname ;)
if lIdx == -1 && len(rem) == 2 {
if runeLen(ps[rem[0]].raw) >= runeLen(ps[rem[1]].raw) {
lIdx = rem[0]
rem = rem[1:]
} else {
lIdx = rem[1]
rem = rem[:1]
}
}
out := Parts{}
if pIdx != -1 {
out.Patronymic = strings.TrimSuffix(ps[pIdx].raw, ".")
}
if lIdx != -1 {
out.Last = ps[lIdx].raw
}
// remaining becomes first name
if len(rem) == 1 {
out.First = ps[rem[0]].raw
} else if len(ps) == 2 {
for i := range ps {
if i != pIdx && i != lIdx {
out.First = ps[i].raw
}
}
}
// normalize to Title Case
out.First = capWord(out.First)
out.Last = capWord(out.Last)
out.Patronymic = strings.ToUpper(out.Patronymic) // keep initials uppercase
// not found ;(
if out.First == "" || out.Last == "" {
return out, errors.New("unable to classify parts")
}
return out, nil
}
func tokenizeLatin(s string) []string {
re := regexp.MustCompile(`(?i)[a-z]+(?:['-][a-z]+)*\.?`)
return re.FindAllString(s, -1)
}
func isInitial(x string) bool {
x = strings.TrimSuffix(x, ".")
r := []rune(x)
return len(r) >= 1 && len(r) <= 2 && allASCIIAlpha(r)
}
func isFathersnameLatin(lo string) bool {
sufs := []string{"ovich", "evich", "ich", "ovna", "evna", "ichna", "ogly", "kyzy"}
for _, s := range sufs {
if strings.HasSuffix(lo, s) && len(lo) >= len(s)+2 {
return true
}
}
return false
}
func looksLikeSurnameLatin(lo string) bool {
sufs := []string{
"ov", "ev", "in", "ina", "ova", "eva",
"sky", "skiy", "skii", "skaya", "ska",
"enko", "ienko",
"uk", "yk", "chuk", "czuk",
"yan", "ian",
"dze", "dze", "shvili",
}
for _, s := range sufs {
if strings.HasSuffix(lo, s) {
return true
}
}
if strings.Contains(lo, "'") {
if strings.HasSuffix(lo, "yanova") || strings.HasSuffix(lo, "yanov") || strings.HasSuffix(lo, "eva") || strings.HasSuffix(lo, "ova") {
return true
}
}
return false
}
func capWord(s string) string {
if s == "" {
return s
}
sep := func(r rune) bool { return r == '-' || r == '\'' }
parts := strings.FieldsFunc(strings.ToLower(s), sep)
i := 0
builder := strings.Builder{}
for _, r := range s {
if r == '-' || r == '\'' {
builder.WriteRune(r)
continue
}
if len(parts) == 0 {
builder.WriteRune(unicode.ToUpper(r))
continue
}
if i == 0 {
builder.WriteRune(unicode.ToUpper(r))
} else {
builder.WriteRune(unicode.ToLower(r))
}
i++
}
return strings.ToUpper(string([]rune(s)[0])) + strings.ToLower(s[1:])
}
func allASCIIAlpha(r []rune) bool {
for _, ch := range r {
if ch < 'A' || (ch > 'Z' && ch < 'a') || ch > 'z' {
return false
}
}
return true
}
func runeLen(s string) int { return len([]rune(s)) }
|