-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extended QALD Based Web Serivce Implementation
- Loading branch information
TortugaAttack
committed
Jun 15, 2016
1 parent
c39ac0e
commit 286ba78
Showing
1 changed file
with
102 additions
and
0 deletions.
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
src/main/java/org/aksw/gerbil/annotator/impl/qa/ExtendedQALDBasedWebService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |