From 8e4578dc25a0e7b720dcacf1914a28ad67f2bb59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20Fernando=20S=C3=A1nchez?= Date: Fri, 16 Jun 2017 17:53:42 +0200 Subject: [PATCH] Closes #40 --- senpy/models.py | 9 ++-- senpy/plugins/__init__.py | 33 ++++++++++++ senpy/plugins/conversion/emotion/centroids.py | 51 +++++++++++++++++++ senpy/plugins/example/emoRand/emoRand.py | 10 +++- senpy/plugins/example/rand/rand.py | 12 ++++- .../sentiment/sentiment140/sentiment140.py | 17 +++++++ tests/plugins/async_plugin/asyncplugin.py | 3 ++ tests/plugins/dummy_plugin/dummy.py | 3 ++ tests/plugins/sleep_plugin/sleep.py | 3 ++ 9 files changed, 136 insertions(+), 5 deletions(-) diff --git a/senpy/models.py b/senpy/models.py index b47947f..19314b0 100644 --- a/senpy/models.py +++ b/senpy/models.py @@ -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 diff --git a/senpy/plugins/__init__.py b/senpy/plugins/__init__.py index 63bb66e..04299bf 100644 --- a/senpy/plugins/__init__.py +++ b/senpy/plugins/__init__.py @@ -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 diff --git a/senpy/plugins/conversion/emotion/centroids.py b/senpy/plugins/conversion/emotion/centroids.py index fe400cd..d8b5b67 100644 --- a/senpy/plugins/conversion/emotion/centroids.py +++ b/senpy/plugins/conversion/emotion/centroids.py @@ -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" diff --git a/senpy/plugins/example/emoRand/emoRand.py b/senpy/plugins/example/emoRand/emoRand.py index 8de8e81..327c869 100644 --- a/senpy/plugins/example/emoRand/emoRand.py +++ b/senpy/plugins/example/emoRand/emoRand.py @@ -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']) diff --git a/senpy/plugins/example/rand/rand.py b/senpy/plugins/example/rand/rand.py index aa92c70..2287651 100644 --- a/senpy/plugins/example/rand/rand.py +++ b/senpy/plugins/example/rand/rand.py @@ -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 diff --git a/senpy/plugins/sentiment/sentiment140/sentiment140.py b/senpy/plugins/sentiment/sentiment140/sentiment140.py index b8e6d6f..a06d9f9 100644 --- a/senpy/plugins/sentiment/sentiment140/sentiment140.py +++ b/senpy/plugins/sentiment/sentiment140/sentiment140.py @@ -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', + } + ] + } + } + ] diff --git a/tests/plugins/async_plugin/asyncplugin.py b/tests/plugins/async_plugin/asyncplugin.py index 976e6c8..a37f2cb 100644 --- a/tests/plugins/async_plugin/asyncplugin.py +++ b/tests/plugins/async_plugin/asyncplugin.py @@ -21,3 +21,6 @@ class AsyncPlugin(AnalysisPlugin): values = self._do_async(2) entry.async_values = values yield entry + + def test(self): + pass diff --git a/tests/plugins/dummy_plugin/dummy.py b/tests/plugins/dummy_plugin/dummy.py index b6b3966..e020acc 100644 --- a/tests/plugins/dummy_plugin/dummy.py +++ b/tests/plugins/dummy_plugin/dummy.py @@ -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 diff --git a/tests/plugins/sleep_plugin/sleep.py b/tests/plugins/sleep_plugin/sleep.py index 30ff9db..770dd3b 100644 --- a/tests/plugins/sleep_plugin/sleep.py +++ b/tests/plugins/sleep_plugin/sleep.py @@ -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