Skip to content

Commit

Permalink
Extended QALD Based Web Serivce Implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
TortugaAttack committed Jun 15, 2016
1 parent c39ac0e commit 286ba78
Showing 1 changed file with 102 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<Marking> 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<Marking> ret;
try {
response = sendRequest(request);
// receive NIF document
entity = response.getEntity();
// read response and parse NIF
try {
List<IQuestion> 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;
}

}

0 comments on commit 286ba78

Please sign in to comment.