1
|
package eu.dnetlib.data.mapreduce.hbase.bulktag;
|
2
|
|
3
|
import com.google.common.base.Functions;
|
4
|
import com.google.common.collect.Maps;
|
5
|
import com.google.common.collect.Sets;
|
6
|
import eu.dnetlib.data.bulktag.CommunityConfiguration;
|
7
|
import eu.dnetlib.data.bulktag.Pair;
|
8
|
import eu.dnetlib.data.proto.FieldTypeProtos;
|
9
|
import eu.dnetlib.data.proto.OafProtos;
|
10
|
import eu.dnetlib.data.proto.ResultProtos;
|
11
|
import eu.dnetlib.data.proto.ResultProtos.Result.Context;
|
12
|
import org.apache.commons.lang3.StringUtils;
|
13
|
import org.apache.hadoop.mapreduce.Mapper;
|
14
|
|
15
|
import java.util.*;
|
16
|
import java.util.stream.Collectors;
|
17
|
import java.util.stream.Stream;
|
18
|
|
19
|
/**
|
20
|
* Created by miriam on 02/08/2018.
|
21
|
*/
|
22
|
public class ResultTagger {
|
23
|
private final static String DATA_INFO_TYPE = "bulktagging";
|
24
|
private final static String SCHEMA_NAME = "dnet:provenanceActions";
|
25
|
private final static String CLASS_ID = "bulktagging::community";
|
26
|
private final static String SCHEMA_ID = "dnet:provenanceActions";
|
27
|
private final static String COUNTER_GROUP = "Bulk Tagging";
|
28
|
|
29
|
private final static String ZENODO_COMMUNITY_INDICATOR = "https://zenodo.org/communities/";
|
30
|
|
31
|
private String trust = "0.8";
|
32
|
|
33
|
|
34
|
public OafProtos.Oaf enrichContext(final OafProtos.Oaf oaf, final CommunityConfiguration conf, final Mapper.Context context) {
|
35
|
|
36
|
//context.getCounter(COUNTER_GROUP, "to enrich").increment(1);
|
37
|
final OafProtos.Oaf.Builder builder = OafProtos.Oaf.newBuilder(oaf);
|
38
|
|
39
|
|
40
|
if(oaf.getDataInfo().getDeletedbyinference()){
|
41
|
context.getCounter(COUNTER_GROUP, "deleted by inference").increment(1);
|
42
|
return null;
|
43
|
}
|
44
|
//context.getCounter(COUNTER_GROUP, "not deleted by inference").increment(1);
|
45
|
|
46
|
final List<ResultProtos.Result.Context> contextList = oaf.getEntity().getResult().getMetadata().getContextList();
|
47
|
|
48
|
if(contextList.size()>0){
|
49
|
context.getCounter(COUNTER_GROUP, "exist context list").increment(1);
|
50
|
}else{
|
51
|
context.getCounter(COUNTER_GROUP, "not exist context list").increment(1);
|
52
|
}
|
53
|
//communities contains all the communities to be added as context for the result
|
54
|
final Set<String> communities = new HashSet<>();
|
55
|
|
56
|
oaf.getEntity().getResult().getMetadata().getSubjectList().stream()
|
57
|
.map(subject -> subject.getValue())
|
58
|
.filter(StringUtils::isNotBlank)
|
59
|
.map(String::toLowerCase)
|
60
|
.map(String::trim)
|
61
|
.collect(Collectors.toCollection(HashSet::new))
|
62
|
.forEach(s -> communities.addAll(conf.getCommunityForSubjectValue(s)));
|
63
|
|
64
|
oaf.getEntity().getResult().getInstanceList()
|
65
|
.stream()
|
66
|
.map(i -> new Pair<>(i.getCollectedfrom().getKey(), i.getHostedby().getKey()))
|
67
|
.flatMap(p -> Stream.of(p.getFst(), p.getSnd()))
|
68
|
.map(s -> StringUtils.substringAfter(s, "|"))
|
69
|
.collect(Collectors.toCollection(HashSet::new))
|
70
|
.forEach(dsId -> communities.addAll(conf.getCommunityForDatasourceValue(dsId)));
|
71
|
|
72
|
final ResultProtos.Result.Metadata.Builder mBuilder = builder.getEntityBuilder().getResultBuilder().getMetadataBuilder();
|
73
|
mBuilder.getContextBuilderList().stream().filter(cBuilder -> cBuilder.getId().contains(ZENODO_COMMUNITY_INDICATOR))
|
74
|
.collect(Collectors.toList())
|
75
|
.forEach(c->communities.addAll(conf.getCommunityForZenodoCommunityValue(c.getId().substring(c.getId().lastIndexOf("/")+1).trim())));
|
76
|
|
77
|
|
78
|
|
79
|
List<Context.Builder> clist = mBuilder.getContextBuilderList().stream()
|
80
|
.filter(c -> (!c.getId().contains(ZENODO_COMMUNITY_INDICATOR))).collect(Collectors.toList());
|
81
|
|
82
|
mBuilder.clearContext();
|
83
|
clist.forEach(c->mBuilder.addContext(c));
|
84
|
|
85
|
|
86
|
final Map<String, ResultProtos.Result.Context.Builder> cBuilders = Maps.newHashMap();
|
87
|
|
88
|
|
89
|
if(communities.isEmpty()){
|
90
|
context.getCounter(COUNTER_GROUP, "list of communities empty").increment(1);
|
91
|
}else{
|
92
|
context.getCounter(COUNTER_GROUP, "list of communities has values!").increment(1);
|
93
|
}
|
94
|
|
95
|
mBuilder.getContextBuilderList().forEach(cBuilder -> {
|
96
|
cBuilders.put(cBuilder.getId(), cBuilder);
|
97
|
});
|
98
|
|
99
|
for(String contextId:communities){
|
100
|
|
101
|
final ResultProtos.Result.Context.Builder cBuilder = cBuilders.get(contextId);
|
102
|
if (cBuilder != null) {
|
103
|
|
104
|
if (!cBuilder.getDataInfoBuilderList().stream()
|
105
|
.map(di -> di.getInferenceprovenance())
|
106
|
.anyMatch(s -> DATA_INFO_TYPE.equals(s))) {
|
107
|
|
108
|
cBuilder.addDataInfo(buildDataInfo());
|
109
|
context.getCounter(COUNTER_GROUP, "add provenance").increment(1);
|
110
|
} else {
|
111
|
context.getCounter(COUNTER_GROUP, "provenance already bulk tagged").increment(1);
|
112
|
}
|
113
|
} else {
|
114
|
context.getCounter(COUNTER_GROUP, "add context").increment(1);
|
115
|
mBuilder.addContext(buildContext(contextId));
|
116
|
}
|
117
|
|
118
|
}
|
119
|
|
120
|
return builder.build();
|
121
|
}
|
122
|
|
123
|
private ResultProtos.Result.Context buildContext(final String contextId) {
|
124
|
return ResultProtos.Result.Context.newBuilder()
|
125
|
.setId(contextId)
|
126
|
.addDataInfo(buildDataInfo())
|
127
|
.build();
|
128
|
}
|
129
|
|
130
|
private FieldTypeProtos.DataInfo buildDataInfo() {
|
131
|
FieldTypeProtos.DataInfo.Builder builder = FieldTypeProtos.DataInfo.newBuilder()
|
132
|
.setInferred(true)
|
133
|
.setProvenanceaction(
|
134
|
FieldTypeProtos.Qualifier.newBuilder()
|
135
|
.setClassid(CLASS_ID)
|
136
|
.setClassname("Bulk Tagging for Communities")
|
137
|
.setSchemeid(SCHEMA_ID)
|
138
|
.setSchemename(SCHEMA_NAME))
|
139
|
.setInferenceprovenance(DATA_INFO_TYPE)
|
140
|
.setTrust(trust);
|
141
|
return builder
|
142
|
.build();
|
143
|
}
|
144
|
|
145
|
|
146
|
public void setTrust(String s) {
|
147
|
trust = s;
|
148
|
}
|
149
|
}
|