1
|
package prototype;
|
2
|
|
3
|
import java.nio.charset.StandardCharsets;
|
4
|
import java.text.Normalizer;
|
5
|
import java.util.List;
|
6
|
import java.util.Set;
|
7
|
|
8
|
import prototype.utils.Capitalize;
|
9
|
import prototype.utils.DotAbbreviations;
|
10
|
|
11
|
import com.google.common.base.Joiner;
|
12
|
import com.google.common.base.Splitter;
|
13
|
import com.google.common.collect.Iterables;
|
14
|
import com.google.common.collect.Lists;
|
15
|
import com.google.common.hash.Hashing;
|
16
|
|
17
|
//import eu.dnetlib.pace.clustering.NGramUtils;
|
18
|
//import eu.dnetlib.pace.util.Capitalise;
|
19
|
//import eu.dnetlib.pace.util.DotAbbreviations;
|
20
|
|
21
|
public class PersonOrig {
|
22
|
private List<String> name = Lists.newArrayList();
|
23
|
private List<String> surname = Lists.newArrayList();
|
24
|
private List<String> fullname = Lists.newArrayList();
|
25
|
|
26
|
private static Set<String> particles = null;
|
27
|
|
28
|
public PersonOrig(String s) {
|
29
|
s = Normalizer.normalize(s, Normalizer.Form.NFD);
|
30
|
s = s.replaceAll("\\(.+\\)", "");
|
31
|
s = s.replaceAll("\\[.+\\]", "");
|
32
|
s = s.replaceAll("\\{.+\\}", "");
|
33
|
s = s.replaceAll("\\s+-\\s+", "-");
|
34
|
s = s.replaceAll("[\\W&&[^,-]]", " ");
|
35
|
s = s.replaceAll("[\\p{InCombiningDiacriticalMarks}&&[^,-]]", " ");
|
36
|
s = s.replaceAll("[\\p{Punct}&&[^-,]]", " ");
|
37
|
s = s.replaceAll("\\d", " ");
|
38
|
s = s.replaceAll("\\n", " ");
|
39
|
s = s.replaceAll("\\.", " ");
|
40
|
s = s.replaceAll("\\s+", " ");
|
41
|
|
42
|
if (s.contains(",")) {
|
43
|
String[] arr = s.split(",");
|
44
|
if (arr.length == 1) {
|
45
|
fullname = splitTerms(arr[0]);
|
46
|
} else if (arr.length > 1) {
|
47
|
surname = splitTerms(arr[0]);
|
48
|
name = splitTerms(arr[1]);
|
49
|
fullname.addAll(surname);
|
50
|
fullname.addAll(name);
|
51
|
}
|
52
|
} else {
|
53
|
fullname = splitTerms(s);
|
54
|
|
55
|
int lastInitialPosition = fullname.size();
|
56
|
boolean hasSurnameInUpperCase = false;
|
57
|
|
58
|
for (int i = 0; i < fullname.size(); i++) {
|
59
|
String term = fullname.get(i);
|
60
|
if (term.length() == 1) {
|
61
|
lastInitialPosition = i;
|
62
|
} else if (term.equals(term.toUpperCase())) {
|
63
|
hasSurnameInUpperCase = true;
|
64
|
}
|
65
|
}
|
66
|
|
67
|
if (lastInitialPosition < fullname.size() - 1) { // Case: Michele G. Artini
|
68
|
name = fullname.subList(0, lastInitialPosition + 1);
|
69
|
surname = fullname.subList(lastInitialPosition + 1, fullname.size());
|
70
|
} else if (hasSurnameInUpperCase) { // Case: Michele ARTINI
|
71
|
for (String term : fullname) {
|
72
|
if (term.length() > 1 && term.equals(term.toUpperCase())) {
|
73
|
surname.add(term);
|
74
|
} else {
|
75
|
name.add(term);
|
76
|
}
|
77
|
}
|
78
|
}
|
79
|
}
|
80
|
}
|
81
|
|
82
|
private List<String> splitTerms(String s) {
|
83
|
// if (particles == null) {
|
84
|
// particles = NGramUtils.loadFromClasspath("/eu/dnetlib/pace/config/name_particles.txt");
|
85
|
// }
|
86
|
|
87
|
List<String> list = Lists.newArrayList();
|
88
|
for (String part : Splitter.on(" ").omitEmptyStrings().split(s)) {
|
89
|
// if (!particles.contains(part.toLowerCase())) {
|
90
|
list.add(part);
|
91
|
// }
|
92
|
}
|
93
|
return list;
|
94
|
}
|
95
|
|
96
|
public List<String> getName() {
|
97
|
return name;
|
98
|
}
|
99
|
|
100
|
public List<String> getSurname() {
|
101
|
return surname;
|
102
|
}
|
103
|
|
104
|
public List<String> getFullname() {
|
105
|
return fullname;
|
106
|
}
|
107
|
|
108
|
public String hash() {
|
109
|
return Hashing.murmur3_128().hashString(getNormalisedFullname(), StandardCharsets.UTF_8).toString();
|
110
|
}
|
111
|
|
112
|
public String getNormalisedFullname() {
|
113
|
return isAccurate() ?
|
114
|
Joiner.on(" ").join(getCapitalSurname()) + ", " + Joiner.on(" ").join(getNameWithAbbreviations()) :
|
115
|
Joiner.on(" ").join(fullname);
|
116
|
}
|
117
|
|
118
|
public List<String> getCapitalSurname() {
|
119
|
return Lists.newArrayList(Iterables.transform(surname, new Capitalize()));
|
120
|
}
|
121
|
|
122
|
public List<String> getNameWithAbbreviations() {
|
123
|
return Lists.newArrayList(Iterables.transform(name, new DotAbbreviations()));
|
124
|
}
|
125
|
|
126
|
public boolean isAccurate() {
|
127
|
return (name != null && surname != null && !name.isEmpty() && !surname.isEmpty());
|
128
|
}
|
129
|
}
|