Skip to content

Commit d3bfdc4

Browse files
author
TortugaAttack
committed
fixed #207
1 parent c371ee7 commit d3bfdc4

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

src/main/java/org/aksw/gerbil/annotator/impl/wat/WATAnnotator.java

+45-6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.aksw.gerbil.annotator.impl.wat;
1818

1919
import java.io.IOException;
20+
import java.io.UnsupportedEncodingException;
21+
import java.net.URLEncoder;
2022
import java.util.List;
2123

2224
import org.aksw.gerbil.annotator.A2KBAnnotator;
@@ -36,7 +38,9 @@
3638
import org.apache.http.HttpEntity;
3739
import org.apache.http.HttpHeaders;
3840
import org.apache.http.client.methods.CloseableHttpResponse;
41+
import org.apache.http.client.methods.HttpGet;
3942
import org.apache.http.client.methods.HttpPost;
43+
import org.apache.http.client.methods.HttpRequestBase;
4044
import org.apache.http.entity.StringEntity;
4145
import org.apache.http.util.EntityUtils;
4246
import org.json.JSONArray;
@@ -51,12 +55,16 @@ public class WATAnnotator extends AbstractHttpBasedAnnotator implements A2KBAnno
5155
private static final String WAT_CONFIG_FILE_PROPERTY_ENDPOINT = "org.aksw.gerbil.annotators.wat.disambiguateUrl";
5256
private static final String WAT_CONFIG_FILE_PROPERTY_PARAMETERS = "org.aksw.gerbil.annotators.wat.annotateUrl";
5357

54-
private static final String SPANS_REQUEST_PARAMETER_KEY = "spans";
58+
private static final String SPANS_REQUEST_PARAMETER_KEY = "suggested_spans";
5559
private static final String TEXT_REQUEST_PARAMETER_KEY = "text";
5660
private static final String ENTITY_TITLE_KEY = "title";
5761
private static final String ENTITY_CONFIDENCE_KEY = "rho";
5862
private static final String ENTITY_START_KEY = "start";
5963
private static final String ENTITY_END_KEY = "end";
64+
private static final String TAGME_KEY_PARAMETER_KEY = "org.aksw.gerbil.annotators.TagMe.key";
65+
66+
private String key;
67+
6068

6169
private final String annotateUrl;
6270
private final String disambiguateUrl;
@@ -72,11 +80,27 @@ public WATAnnotator() throws GerbilException {
7280
throw new GerbilException("Couldn't load endpoint (\"" + WAT_CONFIG_FILE_PROPERTY_ENDPOINT + "\".",
7381
ErrorTypes.ANNOTATOR_LOADING_ERROR);
7482
}
83+
key = GerbilConfiguration.getInstance().getString(TAGME_KEY_PARAMETER_KEY);
84+
if (key == null) {
85+
throw new GerbilException("Couldn't load key from configuration (\"" + TAGME_KEY_PARAMETER_KEY + "\").",
86+
ErrorTypes.ANNOTATOR_LOADING_ERROR);
87+
}
7588
}
7689

77-
public WATAnnotator(String annotateUrl, String disambiguateUrl) {
90+
public WATAnnotator(String annotateUrl, String disambiguateUrl) throws GerbilException {
7891
this.disambiguateUrl = disambiguateUrl;
7992
this.annotateUrl = annotateUrl;
93+
key = GerbilConfiguration.getInstance().getString(TAGME_KEY_PARAMETER_KEY);
94+
if (key == null) {
95+
throw new GerbilException("Couldn't load key from configuration (\"" + TAGME_KEY_PARAMETER_KEY + "\").",
96+
ErrorTypes.ANNOTATOR_LOADING_ERROR);
97+
}
98+
}
99+
100+
public WATAnnotator(String annotateUrl, String disambiguateUrl, String key) {
101+
this.disambiguateUrl = disambiguateUrl;
102+
this.annotateUrl = annotateUrl;
103+
this.key = key;
80104
}
81105

82106
@Override
@@ -114,13 +138,26 @@ private Document requestAnnotations(Document document, boolean disambiguate) thr
114138
}
115139
parameters.put(TEXT_REQUEST_PARAMETER_KEY, document.getText());
116140

117-
HttpPost request = null;
141+
HttpRequestBase request = null;
142+
StringBuilder url= null;
118143
try {
119-
request = createPostRequest(disambiguate ? disambiguateUrl : annotateUrl);
120-
} catch (IllegalArgumentException e) {
144+
if(disambiguate){
145+
url=new StringBuilder(disambiguateUrl);
146+
url.append("?gcube-token=").append(key);
147+
url.append("&document=").append(URLEncoder.encode(parameters.toString(), "UTF-8"));
148+
}
149+
else{
150+
url = new StringBuilder(annotateUrl);
151+
url.append("?gcube-token=").append(key);
152+
url.append("&text=").append(URLEncoder.encode(document.getText(), "UTF-8"));
153+
}
154+
request = createGetRequest(url.toString());
155+
156+
157+
} catch (IllegalArgumentException | UnsupportedEncodingException e) {
121158
throw new GerbilException("Couldn't create HTTP request.", e, ErrorTypes.UNEXPECTED_EXCEPTION);
122159
}
123-
request.setEntity(new StringEntity(parameters.toString(), "UTF8"));
160+
124161
request.addHeader(HttpHeaders.CONTENT_TYPE, "application/json;charset=UTF-8");
125162
request.addHeader(HttpHeaders.ACCEPT, "application/json");
126163
request.addHeader(HttpHeaders.ACCEPT_CHARSET, "UTF-8");
@@ -171,4 +208,6 @@ private Document requestAnnotations(Document document, boolean disambiguate) thr
171208
}
172209
return resultDoc;
173210
}
211+
212+
174213
}

src/main/properties/annotators.properties

+2-2
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,8 @@ org.aksw.gerbil.annotators.definition.TagMe.class=org.aksw.gerbil.annotator.impl
205205
org.aksw.gerbil.annotators.definition.TagMe.constructorArgs=${org.aksw.gerbil.annotators.TagmeAnnotator.annotateUrl},${org.aksw.gerbil.annotators.TagmeAnnotator.spotUrl}
206206

207207
### WAT
208-
org.aksw.gerbil.annotators.wat.annotateUrl=http://wikisense.mkapp.it/tag/tag
209-
org.aksw.gerbil.annotators.wat.disambiguateUrl=http://wikisense.mkapp.it/tag/disambiguate
208+
org.aksw.gerbil.annotators.wat.annotateUrl=https://wat.d4science.org/wat/tag/tag
209+
org.aksw.gerbil.annotators.wat.disambiguateUrl=https://wat.d4science.org/wat/tag/json
210210
org.aksw.gerbil.annotators.definition.WAT.name=WAT
211211
org.aksw.gerbil.annotators.definition.WAT.experimentType=A2KB
212212
org.aksw.gerbil.annotators.definition.WAT.cacheable=true

0 commit comments

Comments
 (0)