diff --git a/sentiment/model/model.go b/sentiment/model/model.go index b433bbf..ec774b6 100644 --- a/sentiment/model/model.go +++ b/sentiment/model/model.go @@ -20,5 +20,19 @@ func init() { func Analyze(text string) float64 { an := model.SentimentAnalysis(text, sentiment.English) - return float64(an.Score) + + // no words, just return whats scored + if len(an.Words) == 0 { + return float64(an.Score) + } + + // take each word score then divide by num words + var total float64 + + for _, word := range an.Words { + total += float64(word.Score) + } + + // get the overall score + return total / float64(len(an.Words)) } diff --git a/sentiment/subscriber/subscriber.go b/sentiment/subscriber/subscriber.go index 073c1ec..7a5cd09 100644 --- a/sentiment/subscriber/subscriber.go +++ b/sentiment/subscriber/subscriber.go @@ -26,7 +26,7 @@ func EnrichPost(ctx context.Context, post *pb.Post) error { score := model.Analyze(post.Title) post.Metadata["sentiment"] = fmt.Sprintf("%.1f", score) - logger.Infof("Setting score %.1f for post %v", score, post.Title) + logger.Infof("Setting score %.1f for post '%v'", score, post.Title) // now save the post PostsClient.Save(ctx, &pb.SaveRequest{