*/
@Override
public void computeEnhancements(final ContentItem ci) throws EngineException {
checkRESTfulNlpAnalysisService(); //validate that the service is active
//get/create the AnalysedText
final AnalysedText at = NlpEngineHelper.initAnalysedText(this, analysedTextFactory, ci);
final Blob blob = at.getBlob();
//send the text to the server
final String language = getLanguage(this, ci, true);
final HttpPost request = new HttpPost(analysisServiceUrl);
request.addHeader(HttpHeaders.CONTENT_LANGUAGE, language);
request.setEntity(new InputStreamEntity(
blob.getStream(), blob.getContentLength(),
ContentType.create(blob.getMimeType(),
blob.getParameter().get("charset"))));
//execute the request
try {
AccessController.doPrivileged(new PrivilegedExceptionAction<AnalysedText>() {
public AnalysedText run() throws ClientProtocolException, IOException {
return httpClient.execute(request, new AnalysisResponseHandler(at));
}
});
} catch (PrivilegedActionException pae) {
Exception e = pae.getException();
if(e instanceof ClientProtocolException) {
//force re-initialisation upon error
serviceInitialised = false;
throw new EngineException(this, ci, "Exception while executing Request "
+ "on RESTful NLP Analysis Service at "+analysisServiceUrl, e);
} else if(e instanceof IOException) {
//force re-initialisation upon error
serviceInitialised = false;
throw new EngineException(this, ci, "Exception while executing Request "
+ "on RESTful NLP Analysis Service at "+analysisServiceUrl, e);
} else {
throw RuntimeException.class.cast(e);
}
}
if(writeTextAnnotations){
Iterator<Span> spans = at.getEnclosed(EnumSet.of(SpanTypeEnum.Sentence,SpanTypeEnum.Chunk));
Sentence context = null;
MGraph metadata = ci.getMetadata();
Language lang = new Language(language);
LiteralFactory lf = LiteralFactory.getInstance();
ci.getLock().writeLock().lock();
try { //write TextAnnotations for Named Entities
while(spans.hasNext()){
Span span = spans.next();
switch (span.getType()) {
case Sentence:
context = (Sentence)context;
break;
default:
Value<NerTag> nerAnno = span.getAnnotation(NER_ANNOTATION);
if(nerAnno != null){
UriRef ta = EnhancementEngineHelper.createTextEnhancement(ci, this);
//add span related data
metadata.add(new TripleImpl(ta, ENHANCER_SELECTED_TEXT,
new PlainLiteralImpl(span.getSpan(), lang)));
metadata.add(new TripleImpl(ta, ENHANCER_START,
lf.createTypedLiteral(span.getStart())));
metadata.add(new TripleImpl(ta, ENHANCER_END,
lf.createTypedLiteral(span.getEnd())));
metadata.add(new TripleImpl(ta, ENHANCER_SELECTION_CONTEXT,
new PlainLiteralImpl(context == null ?
getDefaultSelectionContext(at.getSpan(), span.getSpan(), span.getStart()) :
context.getSpan(), lang)));
//add the NER type
if(nerAnno.value().getType() != null){
metadata.add(new TripleImpl(ta,DC_TYPE,nerAnno.value().getType()));
}