Project

General

Profile

1
package prototype;
2

    
3
import java.text.Normalizer;
4
import java.util.List;
5
import java.util.Set;
6

    
7
import prototype.utils.Capitalize;
8
import prototype.utils.DotAbbreviations;
9

    
10
import com.google.common.base.Joiner;
11
import com.google.common.base.Splitter;
12
import com.google.common.collect.Iterables;
13
import com.google.common.collect.Lists;
14
import com.google.common.hash.Hashing;
15

    
16
//import eu.dnetlib.pace.clustering.NGramUtils;
17
//import eu.dnetlib.pace.util.Capitalise;
18
//import eu.dnetlib.pace.util.DotAbbreviations;
19

    
20
public class PersonOrig {
21
	private List<String> name = Lists.newArrayList();
22
	private List<String> surname = Lists.newArrayList();
23
	private List<String> fullname = Lists.newArrayList();
24

    
25
	private static Set<String> particles = null;
26

    
27
	public PersonOrig(String s) {
28
		s = Normalizer.normalize(s, Normalizer.Form.NFD);
29
		s = s.replaceAll("\\(.+\\)", "");
30
		s = s.replaceAll("\\[.+\\]", "");
31
		s = s.replaceAll("\\{.+\\}", "");
32
		s = s.replaceAll("\\s+-\\s+", "-");
33
		s = s.replaceAll("[\\W&&[^,-]]", " ");
34
		s = s.replaceAll("[\\p{InCombiningDiacriticalMarks}&&[^,-]]", " ");
35
		s = s.replaceAll("[\\p{Punct}&&[^-,]]", " ");
36
		s = s.replaceAll("\\d", " ");
37
		s = s.replaceAll("\\n", " ");
38
		s = s.replaceAll("\\.", " ");
39
		s = s.replaceAll("\\s+", " ");
40

    
41
		if (s.contains(",")) {
42
			String[] arr = s.split(",");
43
			if (arr.length == 1) {
44
				fullname = splitTerms(arr[0]);
45
			} else if (arr.length > 1) {
46
				surname = splitTerms(arr[0]);
47
				name = splitTerms(arr[1]);
48
				fullname.addAll(surname);
49
				fullname.addAll(name);
50
			}
51
		} else {
52
			fullname = splitTerms(s);
53

    
54
			int lastInitialPosition = fullname.size();
55
			boolean hasSurnameInUpperCase = false;
56

    
57
			for (int i = 0; i < fullname.size(); i++) {
58
				String term = fullname.get(i);
59
				if (term.length() == 1) {
60
					lastInitialPosition = i;
61
				} else if (term.equals(term.toUpperCase())) {
62
					hasSurnameInUpperCase = true;
63
				}
64
			}
65

    
66
			if (lastInitialPosition < fullname.size() - 1) { // Case: Michele G. Artini
67
				name = fullname.subList(0, lastInitialPosition + 1);
68
				surname = fullname.subList(lastInitialPosition + 1, fullname.size());
69
			} else if (hasSurnameInUpperCase) { // Case: Michele ARTINI
70
				for (String term : fullname) {
71
					if (term.length() > 1 && term.equals(term.toUpperCase())) {
72
						surname.add(term);
73
					} else {
74
						name.add(term);
75
					}
76
				}
77
			}
78
		}
79
	}
80

    
81
	private List<String> splitTerms(String s) {
82
//		if (particles == null) {
83
//			particles = NGramUtils.loadFromClasspath("/eu/dnetlib/pace/config/name_particles.txt");
84
//		}
85

    
86
		List<String> list = Lists.newArrayList();
87
		for (String part : Splitter.on(" ").omitEmptyStrings().split(s)) {
88
//			if (!particles.contains(part.toLowerCase())) {
89
				list.add(part);
90
//			}
91
		}
92
		return list;
93
	}
94

    
95
	public List<String> getName() {
96
		return name;
97
	}
98

    
99
	public List<String> getSurname() {
100
		return surname;
101
	}
102

    
103
	public List<String> getFullname() {
104
		return fullname;
105
	}
106
	
107
	public String hash() {
108
		return Hashing.murmur3_128().hashString(getNormalisedFullname()).toString();
109
	}
110
	
111
	public String getNormalisedFullname() {
112
		return isAccurate() ? 
113
				Joiner.on(" ").join(getCapitalSurname()) + ", " + Joiner.on(" ").join(getNameWithAbbreviations()) : 
114
				Joiner.on(" ").join(fullname);
115
	}
116
	
117
	public List<String> getCapitalSurname() {
118
		return Lists.newArrayList(Iterables.transform(surname, new Capitalize()));
119
	}
120
	
121
	public List<String> getNameWithAbbreviations() {
122
		return Lists.newArrayList(Iterables.transform(name, new DotAbbreviations()));
123
	}	
124

    
125
	public boolean isAccurate() {
126
		return (name != null && surname != null && !name.isEmpty() && !surname.isEmpty());
127
	}
128
}
(2-2/2)