1
0
mirror of https://github.com/gsi-upm/senpy synced 2024-11-25 01:22:28 +00:00

Fixed bugs in Ekman2VAD

This commit is contained in:
J. Fernando Sánchez 2017-02-28 04:01:05 +01:00
parent 5fb858f5fc
commit 453b9f3257
6 changed files with 58 additions and 16 deletions

View File

@ -13,6 +13,7 @@ from .api import API_PARAMS, NIF_PARAMS, parse_params
from threading import Thread from threading import Thread
import os import os
import copy
import fnmatch import fnmatch
import inspect import inspect
import sys import sys
@ -180,7 +181,7 @@ class Senpy(object):
newentries = [] newentries = []
for i in resp.entries: for i in resp.entries:
if output == "full": if output == "full":
newemotions = i.emotions.copy() newemotions = copy.copy(i.emotions)
else: else:
newemotions = [] newemotions = []
for j in i.emotions: for j in i.emotions:

View File

@ -10,24 +10,26 @@ import math
class WNA2VAD(EmotionConversionPlugin): class WNA2VAD(EmotionConversionPlugin):
def _ekman_to_vad(self, ekmanSet): def _ekman_to_vad(self, ekmanSet):
potency = 0 """Sum the VAD value of all categories found."""
valence = 0
arousal = 0 arousal = 0
dominance = 0 dominance = 0
for e in ekmanSet.onyx__hasEmotion: for e in ekmanSet.onyx__hasEmotion:
category = e.onyx__hasEmotionCategory category = e.onyx__hasEmotionCategory
centroid = self.centroids[category] centroid = self.centroids[category]
potency += centroid['V'] valence += centroid['V']
arousal += centroid['A'] arousal += centroid['A']
dominance += centroid['D'] dominance += centroid['D']
e = Emotion({'emoml:potency': potency, e = Emotion({'emoml:valence': valence,
'emoml:arousal': arousal, 'emoml:arousal': arousal,
'emoml:dominance': dominance}) 'emoml:potency': dominance})
return e return e
def _vad_to_ekman(self, VADEmotion): def _vad_to_ekman(self, VADEmotion):
"""Find the closest category"""
V = VADEmotion['emoml:valence'] V = VADEmotion['emoml:valence']
A = VADEmotion['emoml:potency'] A = VADEmotion['emoml:arousal']
D = VADEmotion['emoml:dominance'] D = VADEmotion['emoml:potency']
emotion = '' emotion = ''
value = 10000000000000000000000.0 value = 10000000000000000000000.0
for state in self.centroids: for state in self.centroids:
@ -50,7 +52,7 @@ class WNA2VAD(EmotionConversionPlugin):
e.onyx__hasEmotion.append(self._ekman_to_vad(emotionSet)) e.onyx__hasEmotion.append(self._ekman_to_vad(emotionSet))
elif fromModel == 'emoml:fsre-dimensions': elif fromModel == 'emoml:fsre-dimensions':
for i in emotionSet.onyx__hasEmotion: for i in emotionSet.onyx__hasEmotion:
e.onyx__hasEmotion.append(self._vad_to_ekman(e)) e.onyx__hasEmotion.append(self._vad_to_ekman(i))
else: else:
raise Error('EMOTION MODEL NOT KNOWN') raise Error('EMOTION MODEL NOT KNOWN')
yield e yield e

View File

@ -7,7 +7,7 @@ onyx:doesConversion:
- onyx:conversionFrom: emoml:big6 - onyx:conversionFrom: emoml:big6
onyx:conversionTo: emoml:fsre-dimensions onyx:conversionTo: emoml:fsre-dimensions
- onyx:conversionFrom: emoml:fsre-dimensions - onyx:conversionFrom: emoml:fsre-dimensions
onyx:conversionTo: wna:WNAModel onyx:conversionTo: emoml:big6
centroids: centroids:
emoml:big6anger: emoml:big6anger:
A: 6.95 A: 6.95
@ -31,5 +31,5 @@ centroids:
V: 2.21 V: 2.21
aliases: aliases:
A: emoml:arousal A: emoml:arousal
V: emoml:potency V: emoml:valence
D: emoml:dominance D: emoml:dominance

View File

@ -9,4 +9,6 @@ test=pytest
ignore = E402 ignore = E402
max-line-length = 100 max-line-length = 100
[bdist_wheel] [bdist_wheel]
universal=1 universal=1
[tool:pytest]
addopts = --cov=senpy --cov-report term-missing

View File

@ -1,5 +1,6 @@
from __future__ import print_function from __future__ import print_function
import os import os
from copy import deepcopy
import logging import logging
try: try:
@ -9,7 +10,7 @@ except ImportError:
from functools import partial from functools import partial
from senpy.extensions import Senpy from senpy.extensions import Senpy
from senpy.models import Error from senpy.models import Error, Results, Entry, EmotionSet, Emotion
from flask import Flask from flask import Flask
from unittest import TestCase from unittest import TestCase
@ -52,6 +53,7 @@ class ExtensionsTest(TestCase):
assert module assert module
import noop import noop
dir(noop) dir(noop)
self.senpy.install_deps()
def test_installing(self): def test_installing(self):
""" Enabling a plugin """ """ Enabling a plugin """
@ -120,3 +122,42 @@ class ExtensionsTest(TestCase):
def test_load_default_plugins(self): def test_load_default_plugins(self):
senpy = Senpy(plugin_folder=self.dir, default_plugins=True) senpy = Senpy(plugin_folder=self.dir, default_plugins=True)
assert len(senpy.plugins) > 1 assert len(senpy.plugins) > 1
def test_convert_emotions(self):
self.senpy.activate_all()
plugin = {
'id': 'imaginary',
'onyx:usesEmotionModel': 'emoml:fsre-dimensions'
}
eSet1 = EmotionSet()
eSet1['onyx:hasEmotion'].append(Emotion({
'emoml:arousal': 1,
'emoml:potency': 0,
'emoml:valence': 0
}))
response = Results({
'entries': [Entry({
'text': 'much ado about nothing',
'emotions': [eSet1]
})]
})
params = {'emotionModel': 'emoml:big6',
'conversion': 'full'}
r1 = deepcopy(response)
self.senpy.convert_emotions(r1,
plugin,
params)
assert len(r1.entries[0].emotions) == 2
params['conversion'] = 'nested'
r2 = deepcopy(response)
self.senpy.convert_emotions(r2,
plugin,
params)
assert len(r2.entries[0].emotions) == 1
assert r2.entries[0].emotions[0]['prov:wasDerivedFrom'] == eSet1
params['conversion'] = 'filtered'
r3 = deepcopy(response)
self.senpy.convert_emotions(r3,
plugin,
params)
assert len(r3.entries[0].emotions) == 1

View File

@ -143,7 +143,3 @@ class ModelsTest(TestCase):
print(t) print(t)
g = rdflib.Graph().parse(data=t, format='turtle') g = rdflib.Graph().parse(data=t, format='turtle')
assert len(g) == len(triples) assert len(g) == len(triples)
def test_convert_emotions(self):
"""It should be possible to convert between different emotion models"""
pass