From 286ba78dfb36ec954bf3c366a8b9808190fbd7ad Mon Sep 17 00:00:00 2001 From: TortugaAttack Date: Wed, 15 Jun 2016 12:50:50 +0200 Subject: [PATCH] Extended QALD Based Web Serivce Implementation --- .../impl/qa/ExtendedQALDBasedWebService.java | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 src/main/java/org/aksw/gerbil/annotator/impl/qa/ExtendedQALDBasedWebService.java diff --git a/src/main/java/org/aksw/gerbil/annotator/impl/qa/ExtendedQALDBasedWebService.java b/src/main/java/org/aksw/gerbil/annotator/impl/qa/ExtendedQALDBasedWebService.java new file mode 100644 index 000000000..8bbacf6c1 --- /dev/null +++ b/src/main/java/org/aksw/gerbil/annotator/impl/qa/ExtendedQALDBasedWebService.java @@ -0,0 +1,102 @@ +package org.aksw.gerbil.annotator.impl.qa; + +import java.io.IOException; +import java.util.List; + +import org.aksw.gerbil.annotator.QASystem; +import org.aksw.gerbil.annotator.http.AbstractHttpBasedAnnotator; +import org.aksw.gerbil.annotator.impl.nif.NIFBasedAnnotatorWebservice; +import org.aksw.gerbil.datatypes.ErrorTypes; +import org.aksw.gerbil.exceptions.GerbilException; +import org.aksw.gerbil.qa.QAUtils; +import org.aksw.gerbil.transfer.nif.Document; +import org.aksw.gerbil.transfer.nif.Marking; +import org.aksw.gerbil.utils.ClosePermitionGranter; +import org.aksw.qa.commons.datastructure.IQuestion; +import org.aksw.qa.commons.load.QALD_Loader; +import org.apache.commons.io.IOUtils; +import org.apache.http.HttpEntity; +import org.apache.http.HttpHeaders; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.util.EntityUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ExtendedQALDBasedWebService extends AbstractHttpBasedAnnotator implements QASystem{ + + private String name; + private String url; + private static final Logger LOGGER = LoggerFactory.getLogger(ExtendedQALDBasedWebService.class); + + + public ExtendedQALDBasedWebService(String url) { + super(); + this.url = url; + } + + public ExtendedQALDBasedWebService(String url, String name) { + super(name); + this.url = url; + } + + @Override + public String getName() { + return name; + } + + @Override + public void setName(String name) { + this.name = name; + } + + @Override + public List answerQuestion(Document document) + throws GerbilException { + + HttpEntity entity = new StringEntity("query="+document.getText(), "UTF-8"); + HttpPost request = null; + try { + request = createPostRequest(url); + } catch (IllegalArgumentException e) { + throw new GerbilException("Couldn't create HTTP request.", e, ErrorTypes.UNEXPECTED_EXCEPTION); + } + request.setEntity(entity); + request.addHeader(HttpHeaders.CONTENT_TYPE, ContentType.TEXT_PLAIN + ";charset=UTF-8"); + request.addHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.toString()); + request.addHeader(HttpHeaders.ACCEPT_CHARSET, "UTF-8"); + + entity = null; + CloseableHttpResponse response = null; + List ret; + try { + response = sendRequest(request); + // receive NIF document + entity = response.getEntity(); + // read response and parse NIF + try { + List questions = QALD_Loader.loadJSON(entity.getContent()); + ret = QAUtils.translateQuestion(questions.get(0), questions.get(0).getId()+"").getMarkings(); + + } catch (Exception e) { + LOGGER.error("Couldn't parse the response.", e); + throw new GerbilException("Couldn't parse the response.", e, ErrorTypes.UNEXPECTED_EXCEPTION); + } + } finally { + closeRequest(request); + if (entity != null) { + try { + EntityUtils.consume(entity); + } catch (IOException e1) { + } + } + IOUtils.closeQuietly(response); + } + + return ret; + } + +}