1717package org .aksw .gerbil .annotator .impl .wat ;
1818
1919import java .io .IOException ;
20+ import java .io .UnsupportedEncodingException ;
21+ import java .net .URLEncoder ;
2022import java .util .List ;
2123
2224import org .aksw .gerbil .annotator .A2KBAnnotator ;
3638import org .apache .http .HttpEntity ;
3739import org .apache .http .HttpHeaders ;
3840import org .apache .http .client .methods .CloseableHttpResponse ;
41+ import org .apache .http .client .methods .HttpGet ;
3942import org .apache .http .client .methods .HttpPost ;
43+ import org .apache .http .client .methods .HttpRequestBase ;
4044import org .apache .http .entity .StringEntity ;
4145import org .apache .http .util .EntityUtils ;
4246import 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}
0 commit comments