From 92189822d8c9df31861c140ea11d6bd3707ba581 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20Fernando=20S=C3=A1nchez?= Date: Wed, 10 Jan 2018 09:50:52 +0100 Subject: [PATCH] Change Box plugin to mimic a sklearn classifier --- example-plugins/basic_box_plugin.py | 2 +- example-plugins/basic_plugin.py | 2 +- example-plugins/sklearn/pipeline_plugin.py | 2 +- senpy/plugins/__init__.py | 9 +++++---- tests/test_plugins.py | 4 ++-- 5 files changed, 10 insertions(+), 9 deletions(-) diff --git a/example-plugins/basic_box_plugin.py b/example-plugins/basic_box_plugin.py index 23e5cf6..0b85951 100644 --- a/example-plugins/basic_box_plugin.py +++ b/example-plugins/basic_box_plugin.py @@ -18,7 +18,7 @@ class BasicBox(SentimentBox): 'default': 'marl:Neutral' } - def box(self, input, **kwargs): + def predict(self, input): output = basic.get_polarity(input) return self.mappings.get(output, self.mappings['default']) diff --git a/example-plugins/basic_plugin.py b/example-plugins/basic_plugin.py index b2205a0..35af16a 100644 --- a/example-plugins/basic_plugin.py +++ b/example-plugins/basic_plugin.py @@ -18,7 +18,7 @@ class Basic(MappingMixin, SentimentBox): 'default': 'marl:Neutral' } - def box(self, input, **kwargs): + def predict(self, input): return basic.get_polarity(input) test_cases = [{ diff --git a/example-plugins/sklearn/pipeline_plugin.py b/example-plugins/sklearn/pipeline_plugin.py index 1ac1f78..a8eca0d 100644 --- a/example-plugins/sklearn/pipeline_plugin.py +++ b/example-plugins/sklearn/pipeline_plugin.py @@ -18,7 +18,7 @@ class PipelineSentiment(MappingMixin, SentimentBox): -1: 'marl:Negative' } - def box(self, input, *args, **kwargs): + def predict(self, input): return pipeline.predict([input, ])[0] test_cases = [ diff --git a/senpy/plugins/__init__.py b/senpy/plugins/__init__.py index 34d4289..6072597 100644 --- a/senpy/plugins/__init__.py +++ b/senpy/plugins/__init__.py @@ -251,7 +251,7 @@ class Box(AnalysisPlugin): .. code-block:: - entry --> input() --> box() --> output() --> entry' + entry --> input() --> predict() --> output() --> entry' In other words: their ``input`` method convers a query (entry and a set of parameters) into @@ -267,13 +267,13 @@ class Box(AnalysisPlugin): '''Transforms the results of the black box into an entry''' return output - def box(self): + def predict(self, input): raise NotImplementedError('You should define the behavior of this plugin') def analyse_entries(self, entries, params): for entry in entries: input = self.input(entry=entry, params=params) - results = self.box(input=input, params=params) + results = self.predict(input=input) yield self.output(output=results, entry=entry, params=params) @@ -453,7 +453,8 @@ def install_deps(*plugins): return installed -is_plugin_file = re.compile(r'.*\.senpy$|senpy_[a-zA-Z0-9_]+\.py$|[a-zA-Z0-9_]+_plugin.py$') +is_plugin_file = re.compile(r'.*\.senpy$|senpy_[a-zA-Z0-9_]+\.py$|' + '^(?!test_)[a-zA-Z0-9_]+_plugin.py$') def find_plugins(folders): diff --git a/tests/test_plugins.py b/tests/test_plugins.py index cb080c5..1ec08d2 100644 --- a/tests/test_plugins.py +++ b/tests/test_plugins.py @@ -212,7 +212,7 @@ class PluginsTest(TestCase): def input(self, entry, **kwargs): return entry.text - def box(self, input, **kwargs): + def predict(self, input): return 'SIGN' in input def output(self, output, entry, **kwargs): @@ -242,7 +242,7 @@ class PluginsTest(TestCase): mappings = {'happy': 'marl:Positive', 'sad': 'marl:Negative'} - def box(self, input, **kwargs): + def predict(self, input, **kwargs): return 'happy' if ':)' in input else 'sad' test_cases = [