def get_sentiment(self, cumulative=False): ''' Calculates the sentiment of the cluster. It returns a list of tuples (date, value) where date value is the accumulated sentiment of that date. ''' emotional_rollercoaster = [] tsa = TwitterSemanticAnalyser() for document in self.document_dict.values(): sentiment = tsa.extract_sentiment(' '.join(token for token in document.tokens)) emotional_rollercoaster.append( (document.date, sentiment) ) #It's important to sort this list otherwise itertools will npt work. #The we group emotion scores by date. t[1][1] is the score of a document at time d. x = sorted(emotional_rollercoaster) grouped_emotions = [(d, sum([float(t[1][1]) for t in g])) for d, g in itertools.groupby(x, lambda x: x[0])] return grouped_emotions
def test_sentiment_extraction(self): tsa = TwitterSemanticAnalyser() calculated_sad = tsa.extract_sentiment(tweet6) calculated_happy = tsa.extract_sentiment(tweet7) self.assertEqual(('negative', '-0.200008'), calculated_sad) self.assertEqual(('positive', '0.61373'), calculated_happy)