Commit 00da7515 authored by J. Fernando Sánchez's avatar J. Fernando Sánchez
Browse files

Change conversion to Euclidean distance

* Added neutral point (if present)

Closes !gsi-upm/senpy#37 (Ian's)
parent 6b843a43
...@@ -32,8 +32,17 @@ class CentroidConversion(EmotionConversionPlugin): ...@@ -32,8 +32,17 @@ class CentroidConversion(EmotionConversionPlugin):
nv1[aliases.get(k2, k2)] = v2 nv1[aliases.get(k2, k2)] = v2
ncentroids[aliases.get(k1, k1)] = nv1 ncentroids[aliases.get(k1, k1)] = nv1
info['centroids'] = ncentroids info['centroids'] = ncentroids
super(CentroidConversion, self).__init__(info) super(CentroidConversion, self).__init__(info)
self.dimensions = set()
for c in self.centroids.values():
self.neutralPoints = self.get("neutralPoints", dict())
if not self.neutralPoints:
for i in self.dimensions:
self.neutralPoints[i] = self.get("neutralValue", 0)
def _forward_conversion(self, original): def _forward_conversion(self, original):
"""Sum the VAD value of all categories found.""" """Sum the VAD value of all categories found."""
res = Emotion() res = Emotion()
...@@ -49,14 +58,18 @@ class CentroidConversion(EmotionConversionPlugin): ...@@ -49,14 +58,18 @@ class CentroidConversion(EmotionConversionPlugin):
def _backwards_conversion(self, original): def _backwards_conversion(self, original):
"""Find the closest category""" """Find the closest category"""
dimensions = set(c.keys() for c in centroids.values()) centroids = self.centroids
neutralPoint = self.get("origin", None) neutralPoints = self.neutralPoints
neutralPoint = {k:neutralPoint[k] if k in neturalPoint else 0} dimensions = self.dimensions
def distance_k(centroid, original, k):
# k component of the distance between the value and a given centroid
return (centroid.get(k, neutralPoints[k]) - original.get(k, neutralPoints[k]))**2
def distance(centroid): def distance(centroid):
return sum((centroid.get(k, neutralPoint[k]) - original.get(k, neutralPoint[k]))**2 for k in dimensions) return sum(distance_k(centroid, original, k) for k in dimensions)
emotion = min(centroids, key=lambda x: distance(centroids[x]) emotion = min(centroids, key=lambda x: distance(centroids[x]))
result = Emotion(onyx__hasEmotionCategory=emotion) result = Emotion(onyx__hasEmotionCategory=emotion)
return result return result
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment