1
0
mirror of https://github.com/gsi-upm/senpy synced 2024-11-22 00:02:28 +00:00
This commit is contained in:
J. Fernando Sánchez 2017-06-16 17:53:42 +02:00
parent 312e7f7f12
commit 8e4578dc25
9 changed files with 136 additions and 5 deletions

View File

@ -218,11 +218,16 @@ class BaseModel(SenpyMixin, dict):
super(BaseModel, self).__init__(temp)
def _get_key(self, key):
if key is 'id':
key = '@id'
key = key.replace("__", ":", 1)
return key
def __getitem__(self, key):
return dict.__getitem__(self, self._get_key(key))
def __setitem__(self, key, value):
dict.__setitem__(self, key, value)
dict.__setitem__(self, self._get_key(key), value)
def __delitem__(self, key):
dict.__delitem__(self, key)
@ -244,8 +249,6 @@ class BaseModel(SenpyMixin, dict):
def _plain_dict(self):
d = {k: v for (k, v) in self.items() if k[0] != "_"}
if 'id' in d:
d["@id"] = d.pop('id')
return d

View File

@ -14,6 +14,30 @@ from ..api import API_PARAMS
logger = logging.getLogger(__name__)
def check_template(indict, template):
if isinstance(template, dict) and isinstance(indict, dict):
for k, v in template.items():
if k not in indict:
return '{} not in {}'.format(k, indict)
check_template(indict[k], v)
elif isinstance(template, list) and isinstance(indict, list):
if len(indict) != len(template):
raise models.Error('Different size for {} and {}'.format(indict, template))
for e in template:
found = False
for i in indict:
try:
check_template(i, e)
found = True
except models.Error as ex:
continue
if not found:
raise models.Error('{} not found in {}'.format(e, indict))
else:
if indict != template:
raise models.Error('{} and {} are different'.format(indict, template))
class Plugin(models.Plugin):
def __init__(self, info=None):
"""
@ -37,6 +61,15 @@ class Plugin(models.Plugin):
def deactivate(self):
pass
def test(self):
for case in self.test_cases:
res = list(self.analyse_entry(models.Entry(case['entry']),
case['params']))
exp = case['expected']
if not isinstance(exp, list):
exp = [exp]
check_template(res, exp)
SenpyPlugin = Plugin

View File

@ -100,3 +100,54 @@ class CentroidConversion(EmotionConversionPlugin):
else:
raise Error('EMOTION MODEL NOT KNOWN')
yield e
def test(self, info=None):
if not info:
info = {
"name": "CentroidTest",
"description": "Centroid test",
"version": 0,
"centroids": {
"c1": {"V1": 0.5,
"V2": 0.5},
"c2": {"V1": -0.5,
"V2": 0.5},
"c3": {"V1": -0.5,
"V2": -0.5},
"c4": {"V1": 0.5,
"V2": -0.5}},
"aliases": {
"V1": "X-dimension",
"V2": "Y-dimension"
},
"centroids_direction": ["emoml:big6", "emoml:fsre-dimensions"]
}
c = CentroidConversion(info)
es1 = EmotionSet()
e1 = Emotion()
e1.onyx__hasEmotionCategory = "c1"
es1.onyx__hasEmotion.append(e1)
res = c._forward_conversion(es1)
assert res["X-dimension"] == 0.5
assert res["Y-dimension"] == 0.5
e2 = Emotion()
e2.onyx__hasEmotionCategory = "c2"
es1.onyx__hasEmotion.append(e2)
res = c._forward_conversion(es1)
assert res["X-dimension"] == 0
assert res["Y-dimension"] == 1
e = Emotion()
e["X-dimension"] = -0.2
e["Y-dimension"] = -0.3
res = c._backwards_conversion(e)
assert res["onyx:hasEmotionCategory"] == "c3"
e = Emotion()
e["X-dimension"] = -0.2
e["Y-dimension"] = 0.3
res = c._backwards_conversion(e)
assert res["onyx:hasEmotionCategory"] == "c2"

View File

@ -1,7 +1,7 @@
import random
from senpy.plugins import EmotionPlugin
from senpy.models import EmotionSet, Emotion
from senpy.models import EmotionSet, Emotion, Entry
class RmoRandPlugin(EmotionPlugin):
@ -16,3 +16,11 @@ class RmoRandPlugin(EmotionPlugin):
emotionSet.prov__wasGeneratedBy = self.id
entry.emotions.append(emotionSet)
yield entry
def test(self):
params = dict()
results = list()
for i in range(100):
res = next(self.analyse_entry(Entry(text="Hello"), params))
res.validate()
results.append(res.emotions[0]['onyx:hasEmotion'][0]['onyx:hasEmotionCategory'])

View File

@ -1,7 +1,7 @@
import random
from senpy.plugins import SentimentPlugin
from senpy.models import Sentiment
from senpy.models import Sentiment, Entry
class RandPlugin(SentimentPlugin):
@ -22,3 +22,13 @@ class RandPlugin(SentimentPlugin):
entry.sentiments.append(sentiment)
entry.language = lang
yield entry
def test(self):
params = dict()
results = list()
for i in range(100):
res = next(self.analyse_entry(Entry(text="Hello"), params))
res.validate()
results.append(res.sentiments[0]['marl:hasPolarity'])
assert 'marl:Positive' in results
assert 'marl:Negative' in results

View File

@ -34,3 +34,20 @@ class Sentiment140Plugin(SentimentPlugin):
entry.sentiments.append(sentiment)
entry.language = lang
yield entry
test_cases = [
{
'entry': {
'text': 'I love Titanic'
},
'params': {},
'expected': {
"text": "I love Titanic",
'sentiments': [
{
'marl:hasPolarity': 'marl:Positive',
}
]
}
}
]

View File

@ -21,3 +21,6 @@ class AsyncPlugin(AnalysisPlugin):
values = self._do_async(2)
entry.async_values = values
yield entry
def test(self):
pass

View File

@ -6,3 +6,6 @@ class DummyPlugin(SentimentPlugin):
entry.text = entry.text[::-1]
entry.reversed = entry.get('reversed', 0) + 1
yield entry
def test(self):
pass

View File

@ -9,3 +9,6 @@ class SleepPlugin(AnalysisPlugin):
def analyse_entry(self, entry, params):
sleep(float(params.get("timeout", self.timeout)))
yield entry
def test(self):
pass