/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package at.illecker.storm.examples.util.bolt;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import at.illecker.storm.examples.util.dictionaries.SentimentResult;
import at.illecker.storm.examples.util.dictionaries.SentimentWordLists;
import at.illecker.storm.examples.util.tweet.Tweet;
import backtype.storm.task.OutputCollector;
import backtype.storm.task.TopologyContext;
import backtype.storm.topology.OutputFieldsDeclarer;
import backtype.storm.topology.base.BaseRichBolt;
import backtype.storm.tuple.Fields;
import backtype.storm.tuple.Tuple;

public class SentimentDetectionBolt extends BaseRichBolt {
  public static final String ID = "sentiment-detection-bolt";
  private static final long serialVersionUID = -3279220626656829348L;
  private static final Logger LOG = LoggerFactory
      .getLogger(SentimentDetectionBolt.class);
  private String[] m_inputFields;
  private String[] m_outputFields;
  private OutputCollector m_collector;
  private SentimentWordLists m_sentimentWordLists;

  public SentimentDetectionBolt(String[] inputFields, String[] outputFields) {
    this.m_inputFields = inputFields;
    this.m_outputFields = outputFields;
  }

  public void declareOutputFields(OutputFieldsDeclarer declarer) {
    // key of output tuples
    if (m_outputFields != null) {
      declarer.declare(new Fields(m_outputFields));
    }
  }

  public void prepare(Map config, TopologyContext context,
      OutputCollector collector) {
    this.m_collector = collector;
    this.m_sentimentWordLists = SentimentWordLists.getInstance();
  }

  public void cleanup() {
    m_sentimentWordLists.close();
  }

  public void execute(Tuple tuple) {
    Tweet tweet = (Tweet) tuple.getValueByField(m_inputFields[0]);
    // LOG.info(tweet.toString());

    Map<Integer, SentimentResult> tweetSentiments = m_sentimentWordLists
        .getTweetSentiment(tweet);

    // Debug
    LOG.info("Tweet: " + tweet.toString() + " TweetSentiment: "
        + tweetSentiments);

    this.m_collector.ack(tuple);
  }
}

@@ -2704,89 +2704,705 @@
     
-// Debug%0A    LOG.info(%22Tweet: %22 + tweet.toString() + %22 TweetSentiment: %22%0A
+double totalSentimentScore = Double.MIN_VALUE;%0A    if (tweetSentiments != null) %7B%0A      for (SentimentResult tweetSentiment : tweetSentiments.values()) %7B%0A        totalSentimentScore += tweetSentiment.getAvgSum();%0A      %7D%0A      totalSentimentScore /= tweetSentiments.size();%0A    %7D%0A%0A    // Debug%0A    if (totalSentimentScore != Double.MIN_VALUE) %7B%0A      if (totalSentimentScore %3E SentimentResult.POSITIVE_THRESHOLD) %7B%0A        LOG.info(%22Tweet: %22 + tweet.toString() + %22 Sentiment: POSITIVE Score: %22%0A            + totalSentimentScore);%0A      %7D else if (totalSentimentScore %3C SentimentResult.NEGATIVE_THRESHOLD) %7B%0A        LOG.info(%22Tweet: %22 + tweet.toString() + %22 Sentiment: NEGATIVE Score: %22%0A   
         
 + tw
@@ -3401,27 +3401,257 @@
     
+ 
 + t
-weetSentiments);
+otalSentimentScore);%0A      %7D else %7B%0A        LOG.info(%22Tweet: %22 + tweet.toString() + %22 Sentiment: NEUTAL Score: %22%0A            + totalSentimentScore);%0A      %7D%0A    %7D else %7B%0A      LOG.info(%22Tweet: %22 + tweet.toString() + %22 Sentiment: UNKNOWN%22);%0A    %7D
 %0A%0A  

Update SentimentDetectionBolt