-
Notifications
You must be signed in to change notification settings - Fork 12
/
TextExtractorForTransformers.java
348 lines (289 loc) · 12.1 KB
/
TextExtractorForTransformers.java
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package de.uni_mannheim.informatik.dws.melt.matching_jena_matchers.util.textExtractors;
import de.uni_mannheim.informatik.dws.melt.matching_jena.TextExtractor;
import de.uni_mannheim.informatik.dws.melt.matching_jena_matchers.util.PropertyVocabulary;
import de.uni_mannheim.informatik.dws.melt.matching_jena_matchers.util.StringProcessing;
import de.uni_mannheim.informatik.dws.melt.matching_jena_matchers.util.URIUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import org.apache.jena.rdf.model.Literal;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.ResourceFactory;
import org.apache.jena.rdf.model.Statement;
import org.apache.jena.rdf.model.StmtIterator;
import org.apache.jena.vocabulary.RDFS;
/**
* A {@link TextExtractor} which extracts texts from a resource which can be used by transformer
* based matchers like {@link de.uni_mannheim.informatik.dws.melt.matching_ml.python.nlptransformers.TransformersFilter}
* or {@link de.uni_mannheim.informatik.dws.melt.matching_ml.python.nlptransformers.TransformersFineTuner}.
*/
public class TextExtractorForTransformers implements TextExtractor {
private int minToken;
public TextExtractorForTransformers(int minToken) {
this.minToken = minToken;
}
public TextExtractorForTransformers() {
this(Integer.MAX_VALUE);
}
/**
* Returns a set of descriptions.
* @param r the jena resource which also allows to traverse the whole rdf graph
* @return Potentially multiple strings. If there are multiple strings but one string is a subset of another string,
* the subset will not be included. For example:
* Found strings: {@code [(label) "heart", (comment) "The heart is..."]} -> only {@code "The heart is..."} will
* be returned.
*/
@Override
public Set<String> extract(Resource r) {
List<ProcessedLiteral> literals = new ArrayList<>(getPossibleLiterals(r));
updateContained(literals);
literals.sort(Comparator.comparing(ProcessedLiteral::getPropertyTypeCourseGrained)
.thenComparing(ProcessedLiteral::getContainedLiteralsSize, Comparator.reverseOrder())
.thenComparing(ProcessedLiteral::getPropertyTypeFineGrained)
.thenComparing(ProcessedLiteral::getLexical)
);
Set<String> extractedLiterals = new HashSet<>();
int countTokens = 0;
while(countTokens < this.minToken){
if(literals.isEmpty())
break;
ProcessedLiteral literal = literals.get(0);
extractedLiterals.add(literal.getLexical());
countTokens += literal.getTokenCount();
literals.remove(0);
literals.removeAll(literal.getContainedLiterals());
}
return extractedLiterals;
}
/*
private static final Set<ProperyType> TYPES = new HashSet<>(Arrays.asList(ProperyType.COMMENT, ProperyType.COMMENT_LIKE,
ProperyType.COMMENT_NAME,ProperyType.LONGEST_LITERAL));
private List<ProcessedLiteral> getCommentOrLongestLiteralSortedByMostContained(List<ProcessedLiteral> literals){
List<ProcessedLiteral> filtered = new ArrayList<>();
for(ProcessedLiteral l : literals){
if(TYPES.contains(l.getPropertyType())){
filtered.add(l);
}
}
filtered.sort(Comparator.comparing(ProcessedLiteral::getContainedLiteralsSize).reversed());
return filtered;
}
*/
private TextExtractorAllAnnotationProperties annotationExtractor = new TextExtractorAllAnnotationProperties();
private Set<ProcessedLiteral> getPossibleLiterals(Resource r){
Set<ProcessedLiteral> literals = new HashSet<>();
String longestLiteral = "";
StmtIterator i = r.listProperties();
while(i.hasNext()){
Statement stmt = i.next();
RDFNode object = stmt.getObject();
if(object.isLiteral()){
Literal literal = object.asLiteral();
if(TextExtractorAllStringLiterals.isLiteralAString(literal)){
String text = literal.getLexicalForm().trim();
if(!text.isEmpty()){
Property p = stmt.getPredicate();
/*
if(PropertyVocabulary.COMMENT_LIKE_PROPERTIES.contains(p) ||
PropertyVocabulary.hasPropertyCommentFragment(p)){
literals.add(new ProcessedLiteral(ProperyType.COMMENT_AND_LONGEST_LITERAL, text));
} else if(PropertyVocabulary.LABEL_LIKE_PROPERTIES.contains(p) ||
PropertyVocabulary.hasPropertyLabelFragment(p)){
literals.add(new ProcessedLiteral(ProperyType.LABELS, text));
}*/
if(p.equals(RDFS.label)){
literals.add(new ProcessedLiteral(ProperyTypeFineGrained.LABEL, text));
} else if(p.equals(RDFS.comment)){
literals.add(new ProcessedLiteral(ProperyTypeFineGrained.COMMENT, text));
} else if(PropertyVocabulary.LABEL_LIKE_PROPERTIES.contains(p)){
literals.add(new ProcessedLiteral(ProperyTypeFineGrained.LABEL_LIKE, text));
} else if(PropertyVocabulary.COMMENT_LIKE_PROPERTIES.contains(p)){
literals.add(new ProcessedLiteral(ProperyTypeFineGrained.COMMENT_LIKE, text));
} else if(PropertyVocabulary.hasPropertyLabelFragment(p)){
literals.add(new ProcessedLiteral(ProperyTypeFineGrained.LABEL_NAME, text));
} else if(PropertyVocabulary.hasPropertyCommentFragment(p)){
literals.add(new ProcessedLiteral(ProperyTypeFineGrained.COMMENT_NAME, text));
}
if(text.length() > longestLiteral.length()){
longestLiteral = text;
}
}
}
}
}
if(longestLiteral.isEmpty() == false)
literals.add(new ProcessedLiteral(ProperyTypeFineGrained.LONGEST_LITERAL, longestLiteral));
//add literal
String uri = r.getURI();
if(uri != null){
String fragment = URIUtil.getUriFragment(uri).trim();
if(StringProcessing.containsMostlyNumbers(fragment) == false){
literals.add(new ProcessedLiteral(ProperyTypeFineGrained.FRAGMENT, fragment));
}
}
//add annotation properties
for(String s : annotationExtractor.extract(r)){
literals.add(new ProcessedLiteral(ProperyTypeFineGrained.ANNOTATION_PROP, s));
}
return literals;
}
private void updateContained(List<ProcessedLiteral> literals){
literals.sort(Comparator.comparingInt(ProcessedLiteral::getNormalizedLength).thenComparing(ProcessedLiteral::getNormalized));
for(int i=0; i<literals.size(); i++){
ProcessedLiteral literal = literals.get(i);
for(int j=i + 1; j<literals.size(); j++){
ProcessedLiteral literalInner = literals.get(j);
if(literalInner.getNormalized().contains(literal.getNormalized())){
literalInner.addLiteralContained(literal);
}
}
}
}
/*
private List<String> removeIfAlreadyContained(List<String> list){
//preprocess
List<String> preprocessed = new ArrayList<>();
// save only indices which are later removed.
list.sort(String::length);
for(int i=0; i < list.size(); i++){
String s = list.get(i);
boolean isContained = false;
for(int j=i; j < list.size(); j++){
if(list.get(j).contains(s))
}
}
}
*/
@Override
public int hashCode() {
int hash = 7;
hash = 47 * hash + this.minToken;
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final TextExtractorForTransformers other = (TextExtractorForTransformers) obj;
if (this.minToken != other.minToken) {
return false;
}
return true;
}
}
class ProcessedLiteral{
private ProperyTypeCourseGrained propertyTypeCourseGrained;
private ProperyTypeFineGrained propertyTypeFineGrained;
private String lexical;
private List<String> tokens;
private String normalized;
private Set<ProcessedLiteral> containedLiterals;
public ProcessedLiteral(ProperyTypeFineGrained propertyTypeFineGrained, String lexical) {
this.propertyTypeFineGrained = propertyTypeFineGrained;
this.lexical = lexical;
this.tokens = StringProcessing.normalize(lexical);
this.normalized = String.join(" ", this.tokens);
this.containedLiterals = new HashSet<>();
switch(this.propertyTypeFineGrained){
case COMMENT:
case COMMENT_LIKE:
case COMMENT_NAME:
case LONGEST_LITERAL:
this.propertyTypeCourseGrained = ProperyTypeCourseGrained.COMMENT_AND_LONGEST_LITERAL;
break;
case LABEL:
case LABEL_LIKE:
case LABEL_NAME:
this.propertyTypeCourseGrained = ProperyTypeCourseGrained.LABELS;
break;
case ANNOTATION_PROP:
this.propertyTypeCourseGrained = ProperyTypeCourseGrained.ANNOTATION_PROP;
break;
case FRAGMENT:
this.propertyTypeCourseGrained = ProperyTypeCourseGrained.FRAGMENT;
break;
default:
throw new IllegalArgumentException("ProperyTypeFineGrained is not valid.");
}
}
@Override
public int hashCode() {
int hash = 5;
hash = 13 * hash + Objects.hashCode(this.normalized);
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final ProcessedLiteral other = (ProcessedLiteral) obj;
if (!Objects.equals(this.normalized, other.normalized)) {
return false;
}
return true;
}
public int getNormalizedLength(){
return this.normalized.length();
}
public String getNormalized() {
return normalized;
}
public void addLiteralContained(ProcessedLiteral literal){
this.containedLiterals.add(literal);
}
public ProperyTypeCourseGrained getPropertyTypeCourseGrained() {
return propertyTypeCourseGrained;
}
public ProperyTypeFineGrained getPropertyTypeFineGrained() {
return propertyTypeFineGrained;
}
public Set<ProcessedLiteral> getContainedLiterals() {
return containedLiterals;
}
public int getContainedLiteralsSize() {
return containedLiterals.size();
}
public String getLexical() {
return lexical;
}
public int getTokenCount(){
return this.tokens.size();
}
}
enum ProperyTypeCourseGrained{
COMMENT_AND_LONGEST_LITERAL,
LABELS,
ANNOTATION_PROP,
FRAGMENT;
}
enum ProperyTypeFineGrained{
COMMENT,
COMMENT_LIKE,
COMMENT_NAME,
LONGEST_LITERAL,
LABEL,
LABEL_LIKE,
LABEL_NAME,
ANNOTATION_PROP,
FRAGMENT;
}