17
17
package org .aksw .gerbil .annotator .impl .wat ;
18
18
19
19
import java .io .IOException ;
20
+ import java .io .UnsupportedEncodingException ;
21
+ import java .net .URLEncoder ;
20
22
import java .util .List ;
21
23
22
24
import org .aksw .gerbil .annotator .A2KBAnnotator ;
36
38
import org .apache .http .HttpEntity ;
37
39
import org .apache .http .HttpHeaders ;
38
40
import org .apache .http .client .methods .CloseableHttpResponse ;
41
+ import org .apache .http .client .methods .HttpGet ;
39
42
import org .apache .http .client .methods .HttpPost ;
43
+ import org .apache .http .client .methods .HttpRequestBase ;
40
44
import org .apache .http .entity .StringEntity ;
41
45
import org .apache .http .util .EntityUtils ;
42
46
import org .json .JSONArray ;
@@ -51,12 +55,16 @@ public class WATAnnotator extends AbstractHttpBasedAnnotator implements A2KBAnno
51
55
private static final String WAT_CONFIG_FILE_PROPERTY_ENDPOINT = "org.aksw.gerbil.annotators.wat.disambiguateUrl" ;
52
56
private static final String WAT_CONFIG_FILE_PROPERTY_PARAMETERS = "org.aksw.gerbil.annotators.wat.annotateUrl" ;
53
57
54
- private static final String SPANS_REQUEST_PARAMETER_KEY = "spans " ;
58
+ private static final String SPANS_REQUEST_PARAMETER_KEY = "suggested_spans " ;
55
59
private static final String TEXT_REQUEST_PARAMETER_KEY = "text" ;
56
60
private static final String ENTITY_TITLE_KEY = "title" ;
57
61
private static final String ENTITY_CONFIDENCE_KEY = "rho" ;
58
62
private static final String ENTITY_START_KEY = "start" ;
59
63
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
+
60
68
61
69
private final String annotateUrl ;
62
70
private final String disambiguateUrl ;
@@ -72,11 +80,27 @@ public WATAnnotator() throws GerbilException {
72
80
throw new GerbilException ("Couldn't load endpoint (\" " + WAT_CONFIG_FILE_PROPERTY_ENDPOINT + "\" ." ,
73
81
ErrorTypes .ANNOTATOR_LOADING_ERROR );
74
82
}
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
+ }
75
88
}
76
89
77
- public WATAnnotator (String annotateUrl , String disambiguateUrl ) {
90
+ public WATAnnotator (String annotateUrl , String disambiguateUrl ) throws GerbilException {
78
91
this .disambiguateUrl = disambiguateUrl ;
79
92
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 ;
80
104
}
81
105
82
106
@ Override
@@ -114,13 +138,26 @@ private Document requestAnnotations(Document document, boolean disambiguate) thr
114
138
}
115
139
parameters .put (TEXT_REQUEST_PARAMETER_KEY , document .getText ());
116
140
117
- HttpPost request = null ;
141
+ HttpRequestBase request = null ;
142
+ StringBuilder url = null ;
118
143
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 ) {
121
158
throw new GerbilException ("Couldn't create HTTP request." , e , ErrorTypes .UNEXPECTED_EXCEPTION );
122
159
}
123
- request . setEntity ( new StringEntity ( parameters . toString (), "UTF8" ));
160
+
124
161
request .addHeader (HttpHeaders .CONTENT_TYPE , "application/json;charset=UTF-8" );
125
162
request .addHeader (HttpHeaders .ACCEPT , "application/json" );
126
163
request .addHeader (HttpHeaders .ACCEPT_CHARSET , "UTF-8" );
@@ -171,4 +208,6 @@ private Document requestAnnotations(Document document, boolean disambiguate) thr
171
208
}
172
209
return resultDoc ;
173
210
}
211
+
212
+
174
213
}
0 commit comments