1
|
package eu.dnetlib.pace.common;
|
2
|
|
3
|
import java.text.Normalizer;
|
4
|
import java.util.Collection;
|
5
|
import java.util.List;
|
6
|
import java.util.Set;
|
7
|
import java.util.StringTokenizer;
|
8
|
|
9
|
import org.apache.commons.io.IOUtils;
|
10
|
import org.apache.commons.lang.StringUtils;
|
11
|
|
12
|
import com.google.common.base.Joiner;
|
13
|
import com.google.common.collect.Iterables;
|
14
|
import com.google.common.collect.Lists;
|
15
|
import com.google.common.collect.Sets;
|
16
|
|
17
|
import eu.dnetlib.pace.clustering.NGramUtils;
|
18
|
import eu.dnetlib.pace.model.Field;
|
19
|
import eu.dnetlib.pace.model.FieldList;
|
20
|
import eu.dnetlib.pace.model.FieldListImpl;
|
21
|
|
22
|
/**
|
23
|
* Set of common functions
|
24
|
*
|
25
|
* @author claudio
|
26
|
*
|
27
|
*/
|
28
|
public abstract class AbstractPaceFunctions {
|
29
|
|
30
|
protected static Set<String> stopwords = loadFromClasspath("/eu/dnetlib/pace/config/stopwords_en.txt");
|
31
|
|
32
|
protected static Set<String> ngramBlacklist = loadFromClasspath("/eu/dnetlib/pace/config/ngram_blacklist.txt");
|
33
|
|
34
|
private static final String alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ";
|
35
|
private static final String aliases_from = "⁰¹²³⁴⁵⁶⁷⁸⁹⁺⁻⁼⁽⁾ⁿ₀₁₂₃₄₅₆₇₈₉₊₋₌₍₎";
|
36
|
private static final String aliases_to = "0123456789+-=()n0123456789+-=()";
|
37
|
|
38
|
protected final static FieldList EMPTY_FIELD = new FieldListImpl();
|
39
|
|
40
|
protected String concat(final List<String> l) {
|
41
|
return Joiner.on(" ").skipNulls().join(l);
|
42
|
}
|
43
|
|
44
|
protected String cleanup(final String s) {
|
45
|
final String s1 = nfd(s);
|
46
|
final String s2 = fixAliases(s1);
|
47
|
final String s3 = s2.replaceAll("–", " ");
|
48
|
final String s4 = s3.replaceAll("&", " ");
|
49
|
final String s5 = s4.replaceAll("−", " ");
|
50
|
final String s6 = s5.replaceAll("([0-9]+)", " $1 ");
|
51
|
final String s7 = s6.replaceAll("[^\\p{ASCII}]|\\p{Punct}", " ");
|
52
|
final String s8 = s7.replaceAll("\\n", " ");
|
53
|
final String s9 = s8.replaceAll("(?m)\\s+", " ");
|
54
|
final String s10 = s9.trim();
|
55
|
return s10;
|
56
|
}
|
57
|
|
58
|
protected String finalCleanup(final String s) {
|
59
|
return s.toLowerCase();
|
60
|
}
|
61
|
|
62
|
protected boolean checkNumbers(final String a, final String b) {
|
63
|
final String numbersA = getNumbers(a);
|
64
|
final String numbersB = getNumbers(b);
|
65
|
final String romansA = getRomans(a);
|
66
|
final String romansB = getRomans(b);
|
67
|
return !numbersA.equals(numbersB) || !romansA.equals(romansB);
|
68
|
}
|
69
|
|
70
|
protected String getRomans(final String s) {
|
71
|
final StringBuilder sb = new StringBuilder();
|
72
|
for (final String t : s.split(" ")) {
|
73
|
sb.append(isRoman(t) ? t : "");
|
74
|
}
|
75
|
return sb.toString();
|
76
|
}
|
77
|
|
78
|
protected boolean isRoman(final String s) {
|
79
|
return s.replaceAll("^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$", "qwertyuiop").equals("qwertyuiop");
|
80
|
}
|
81
|
|
82
|
protected String getNumbers(final String s) {
|
83
|
return s.replaceAll("\\D", "");
|
84
|
}
|
85
|
|
86
|
protected String fixAliases(final String s) {
|
87
|
final StringBuilder sb = new StringBuilder();
|
88
|
for (final char ch : Lists.charactersOf(s)) {
|
89
|
final int i = StringUtils.indexOf(aliases_from, ch);
|
90
|
sb.append(i >= 0 ? aliases_to.charAt(i) : ch);
|
91
|
}
|
92
|
return sb.toString();
|
93
|
}
|
94
|
|
95
|
protected String removeSymbols(final String s) {
|
96
|
final StringBuilder sb = new StringBuilder();
|
97
|
|
98
|
for (final char ch : Lists.charactersOf(s)) {
|
99
|
sb.append(StringUtils.contains(alpha, ch) ? ch : " ");
|
100
|
}
|
101
|
return sb.toString().replaceAll("\\s+", " ");
|
102
|
}
|
103
|
|
104
|
protected String getFirstValue(final Field values) {
|
105
|
return (values != null) && !Iterables.isEmpty(values) ? Iterables.getFirst(values, EMPTY_FIELD).stringValue() : "";
|
106
|
}
|
107
|
|
108
|
protected boolean notNull(final String s) {
|
109
|
return s != null;
|
110
|
}
|
111
|
|
112
|
// ///////////////////////
|
113
|
|
114
|
protected String normalize(final String s) {
|
115
|
return nfd(s).toLowerCase()
|
116
|
// do not compact the regexes in a single expression, would cause StackOverflowError in case of large input strings
|
117
|
.replaceAll("(\\W)+", " ")
|
118
|
.replaceAll("(\\p{InCombiningDiacriticalMarks})+", " ")
|
119
|
.replaceAll("(\\p{Punct})+", " ")
|
120
|
.replaceAll("(\\d)+", " ")
|
121
|
.replaceAll("(\\n)+", " ")
|
122
|
.trim();
|
123
|
}
|
124
|
|
125
|
private String nfd(final String s) {
|
126
|
return Normalizer.normalize(s, Normalizer.Form.NFD);
|
127
|
}
|
128
|
|
129
|
protected String filterStopWords(final String s, final Set<String> stopwords) {
|
130
|
final StringTokenizer st = new StringTokenizer(s);
|
131
|
final StringBuilder sb = new StringBuilder();
|
132
|
while (st.hasMoreTokens()) {
|
133
|
final String token = st.nextToken();
|
134
|
if (!stopwords.contains(token)) {
|
135
|
sb.append(token);
|
136
|
sb.append(" ");
|
137
|
}
|
138
|
}
|
139
|
return sb.toString().trim();
|
140
|
}
|
141
|
|
142
|
protected Collection<String> filterBlacklisted(final Collection<String> set, final Set<String> ngramBlacklist) {
|
143
|
final Set<String> newset = Sets.newLinkedHashSet();
|
144
|
for (final String s : set) {
|
145
|
if (!ngramBlacklist.contains(s)) {
|
146
|
newset.add(s);
|
147
|
}
|
148
|
}
|
149
|
return newset;
|
150
|
}
|
151
|
|
152
|
// ////////////////////
|
153
|
|
154
|
public static Set<String> loadFromClasspath(final String classpath) {
|
155
|
final Set<String> h = Sets.newHashSet();
|
156
|
try {
|
157
|
for (final String s : IOUtils.readLines(NGramUtils.class.getResourceAsStream(classpath))) {
|
158
|
h.add(s);
|
159
|
}
|
160
|
} catch (final Throwable e) {
|
161
|
return Sets.newHashSet();
|
162
|
}
|
163
|
return h;
|
164
|
}
|
165
|
|
166
|
}
|