mirror of
https://github.com/gsi-upm/senpy
synced 2025-08-23 18:12:20 +00:00
Last batch of big changes
* Add Box plugin (i.e. black box) * Add SentimentBox, EmotionBox and MappingMixin * Refactored CustomDict
This commit is contained in:
@@ -15,7 +15,8 @@ from senpy.models import (Emotion,
|
||||
SentimentPlugin,
|
||||
Plugins,
|
||||
from_string,
|
||||
from_dict)
|
||||
from_dict,
|
||||
subtypes)
|
||||
from senpy import plugins
|
||||
from pprint import pprint
|
||||
|
||||
@@ -134,6 +135,11 @@ class ModelsTest(TestCase):
|
||||
s = str(r)
|
||||
assert "_testing" not in s
|
||||
|
||||
def test_serialize(self):
|
||||
for k, v in subtypes().items():
|
||||
e = v()
|
||||
e.serialize()
|
||||
|
||||
def test_turtle(self):
|
||||
"""Any model should be serializable as a turtle file"""
|
||||
ana = EmotionAnalysis()
|
||||
|
@@ -13,6 +13,10 @@ from senpy.plugins.conversion.emotion.centroids import CentroidConversion
|
||||
|
||||
class ShelfDummyPlugin(plugins.SentimentPlugin, plugins.ShelfMixin):
|
||||
'''Dummy plugin for tests.'''
|
||||
name = 'Shelf'
|
||||
version = 0
|
||||
author = 'the senpy community'
|
||||
|
||||
def activate(self, *args, **kwargs):
|
||||
if 'counter' not in self.sh:
|
||||
self.sh['counter'] = 0
|
||||
@@ -41,6 +45,16 @@ class PluginsTest(TestCase):
|
||||
self.shelf_dir = tempfile.mkdtemp()
|
||||
self.shelf_file = os.path.join(self.shelf_dir, "shelf")
|
||||
|
||||
def test_serialize(self):
|
||||
'''A plugin should be serializable and de-serializable'''
|
||||
dummy = ShelfDummyPlugin()
|
||||
dummy.serialize()
|
||||
|
||||
def test_jsonld(self):
|
||||
'''A plugin should be serializable and de-serializable'''
|
||||
dummy = ShelfDummyPlugin()
|
||||
dummy.jsonld()
|
||||
|
||||
def test_shelf_file(self):
|
||||
a = ShelfDummyPlugin(
|
||||
info={'name': 'default_shelve_file',
|
||||
@@ -187,6 +201,61 @@ class PluginsTest(TestCase):
|
||||
})
|
||||
assert 'example' in a.extra_params
|
||||
|
||||
def test_box(self):
|
||||
|
||||
class MyBox(plugins.Box):
|
||||
''' Vague description'''
|
||||
|
||||
author = 'me'
|
||||
version = 0
|
||||
|
||||
def input(self, entry, **kwargs):
|
||||
return entry.text
|
||||
|
||||
def box(self, input, **kwargs):
|
||||
return 'SIGN' in input
|
||||
|
||||
def output(self, output, entry, **kwargs):
|
||||
if output:
|
||||
entry.myAnnotation = 'DETECTED'
|
||||
return entry
|
||||
|
||||
test_cases = [
|
||||
{
|
||||
'input': "nothing here",
|
||||
'expected': {'myAnnotation': 'DETECTED'},
|
||||
'should_fail': True
|
||||
}, {
|
||||
'input': "SIGN",
|
||||
'expected': {'myAnnotation': 'DETECTED'}
|
||||
}]
|
||||
|
||||
MyBox().test()
|
||||
|
||||
def test_sentimentbox(self):
|
||||
|
||||
class SentimentBox(plugins.MappingMixin, plugins.SentimentBox):
|
||||
''' Vague description'''
|
||||
|
||||
author = 'me'
|
||||
version = 0
|
||||
|
||||
mappings = {'happy': 'marl:Positive', 'sad': 'marl:Negative'}
|
||||
|
||||
def box(self, input, **kwargs):
|
||||
return 'happy' if ':)' in input else 'sad'
|
||||
|
||||
test_cases = [
|
||||
{
|
||||
'input': 'a happy face :)',
|
||||
'polarity': 'marl:Positive'
|
||||
}, {
|
||||
'input': "Nothing",
|
||||
'polarity': 'marl:Negative'
|
||||
}]
|
||||
|
||||
SentimentBox().test()
|
||||
|
||||
def test_conversion_centroids(self):
|
||||
info = {
|
||||
"name": "CentroidTest",
|
||||
|
Reference in New Issue
Block a user