From 53138e694203434fd34c47a65a7b53b3c5fc60f0 Mon Sep 17 00:00:00 2001 From: drevicko Date: Tue, 4 Apr 2017 15:37:07 +0100 Subject: [PATCH] Estimate VAD by weighted average Does a weighted average of centroids. If intensity sums to zero for a category, a 'neutral' category is used or 0 if it's not present. I'm not 100% sure this is the best approach, and the name of the "neutral" category perhaps should use some convention? Note that if there are no categories present, then no VAD (or other dimensional) estimate is returned. It may be better to use the neutral centroid if it's present in this case also. --- senpy/plugins/conversion/centroids.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/senpy/plugins/conversion/centroids.py b/senpy/plugins/conversion/centroids.py index 2dd1c97..2c0e735 100644 --- a/senpy/plugins/conversion/centroids.py +++ b/senpy/plugins/conversion/centroids.py @@ -1,5 +1,6 @@ from senpy.plugins import EmotionConversionPlugin from senpy.models import EmotionSet, Emotion, Error +from collections import defaultdict import logging logger = logging.getLogger(__name__) @@ -37,14 +38,22 @@ class CentroidConversion(EmotionConversionPlugin): def _forward_conversion(self, original): """Sum the VAD value of all categories found.""" res = Emotion() + totalIntensities = defaultdict(float) for e in original.onyx__hasEmotion: category = e.onyx__hasEmotionCategory + intensity = e.get("onyx__hasEmotionIntensity",1) if category in self.centroids: + totalIntensities[category] += intensity for dim, value in self.centroids[category].items(): try: - res[dim] += value + res[dim] += value * intensity except Exception: - res[dim] = value + res[dim] = value * intensity + for dim,intensity in totalIntensities.items(): + if intensity != 0: + res[dim] /= intensity + else: + res[dim] = self.centroids.get('neutral', {dim:0})[dim] return res def _backwards_conversion(self, original):