*/
@Override
public void computeEnhancements(ContentItem ci) throws EngineException {
AnalysedText analysedText = getAnalysedText(this,ci, true);
String language = getLanguage(this, ci, true);
SentimentClassifier classifier = classifiers.get(language);
if(classifier == null){
throw new IllegalStateException("Sentiment Classifier for language '"
+ language +"' not available. As this is also checked in "
+ " canEnhance this may indicate an Bug in the used "
+ "EnhancementJobManager!");
}
//TODO: locking for AnalysedText not yet defined
// ci.getLock().writeLock().lock();
// try {
Iterator<Token> tokens = analysedText.getTokens();
while(tokens.hasNext()){
Token token = tokens.next();
Set<LexicalCategory> cats = null;
boolean process = false;
if(!adjectivesOnly){
process = true;
Value<PosTag> posTag = token.getAnnotation(NlpAnnotations.POS_ANNOTATION);
if(posTag != null && posTag.probability() == Value.UNKNOWN_PROBABILITY
|| posTag.probability() >= (minPOSConfidence/2.0)){
cats = classifier.getCategories(posTag.value());
} else { //no POS tags or probability to low
cats = Collections.emptySet();
}
} else { //check PosTags if we need to lookup this word
Iterator<Value<PosTag>> posTags = token.getAnnotations(NlpAnnotations.POS_ANNOTATION).iterator();
boolean ignore = false;
while(!ignore && !process && posTags.hasNext()) {
Value<PosTag> value = posTags.next();
PosTag tag = value.value();
cats = classifier.getCategories(tag);
boolean state = cats.contains(LexicalCategory.Adjective)
|| cats.contains(LexicalCategory.Noun);
ignore = !state && (value.probability() == Value.UNKNOWN_PROBABILITY ||
value.probability() >= minPOSConfidence);
process = state && (value.probability() == Value.UNKNOWN_PROBABILITY ||
value.probability() >= (minPOSConfidence/2.0));
}
} //else process all tokens ... no POS tag checking needed
if(process){
String word = token.getSpan();
double sentiment = 0.0;
if(cats.isEmpty()){
sentiment = classifier.classifyWord(null, word);
} else { //in case of multiple Lexical Cats
//we build the average over NOT NULL sentiments for the word
int catSentNum = 0;
for(LexicalCategory cat : cats){
double catSent = classifier.classifyWord(cat, word);
if(catSent != 0.0){
catSentNum++;
sentiment = sentiment + catSent;
}
}