Commit 1a582c08 authored by J. Fernando Sánchez's avatar J. Fernando Sánchez

Filter conversion plugins

Closes #12

* Shows only analysis plugins by default on /api/plugins
* Adds a plugin_type parameter to get other types of plugins
* default_plugin chosen from analysis plugins
parent 0394bcd6
...@@ -26,6 +26,13 @@ API_PARAMS = { ...@@ -26,6 +26,13 @@ API_PARAMS = {
"aliases": ["emotionModel", "emoModel"], "aliases": ["emotionModel", "emoModel"],
"required": False "required": False
}, },
"plugin_type": {
"@id": "pluginType",
"description": 'What kind of plugins to list',
"aliases": ["pluginType", "plugin_type"],
"required": True,
"default": "analysisPlugin"
},
"conversion": { "conversion": {
"@id": "conversion", "@id": "conversion",
"description": "How to show the elements that have (not) been converted", "description": "How to show the elements that have (not) been converted",
......
...@@ -121,7 +121,9 @@ def api(): ...@@ -121,7 +121,9 @@ def api():
@basic_api @basic_api
def plugins(): def plugins():
sp = current_app.senpy sp = current_app.senpy
dic = Plugins(plugins=list(sp.plugins.values())) ptype = request.params.get('plugin_type')
plugins = sp.filter_plugins(plugin_type=ptype)
dic = Plugins(plugins=list(plugins.values()))
return dic return dic
......
...@@ -5,7 +5,8 @@ It orchestrates plugin (de)activation and analysis. ...@@ -5,7 +5,8 @@ It orchestrates plugin (de)activation and analysis.
from future import standard_library from future import standard_library
standard_library.install_aliases() standard_library.install_aliases()
from .plugins import SentimentPlugin, SenpyPlugin from . import plugins
from .plugins import SenpyPlugin
from .models import Error, Entry, Results from .models import Error, Entry, Results
from .blueprints import api_blueprint, demo_blueprint, ns_blueprint from .blueprints import api_blueprint, demo_blueprint, ns_blueprint
from .api import API_PARAMS, NIF_PARAMS, parse_params from .api import API_PARAMS, NIF_PARAMS, parse_params
...@@ -367,6 +368,22 @@ class Senpy(object): ...@@ -367,6 +368,22 @@ class Senpy(object):
def filter_plugins(self, **kwargs): def filter_plugins(self, **kwargs):
""" Filter plugins by different criteria """ """ Filter plugins by different criteria """
ptype = kwargs.pop('plugin_type', None)
logger.debug('#' * 100)
logger.debug('ptype {}'.format(ptype))
if ptype:
try:
ptype = ptype[0].upper() + ptype[1:]
pclass = getattr(plugins, ptype)
logger.debug('Class: {}'.format(pclass))
candidates = filter(lambda x: isinstance(x, pclass),
self.plugins.values())
except AttributeError:
raise Error('{} is not a valid type'.format(ptype))
else:
candidates = self.plugins.values()
logger.debug(candidates)
def matches(plug): def matches(plug):
res = all(getattr(plug, k, None) == v for (k, v) in kwargs.items()) res = all(getattr(plug, k, None) == v for (k, v) in kwargs.items())
...@@ -374,15 +391,11 @@ class Senpy(object): ...@@ -374,15 +391,11 @@ class Senpy(object):
"matching {} with {}: {}".format(plug.name, kwargs, res)) "matching {} with {}: {}".format(plug.name, kwargs, res))
return res return res
if not kwargs: if kwargs:
return self.plugins candidates = filter(matches, candidates)
else: return {p.name: p for p in candidates}
return {n: p for n, p in self.plugins.items() if matches(p)}
@property
def sentiment_plugins(self): def analysis_plugins(self):
""" Return only the sentiment plugins """ """ Return only the analysis plugins """
return { return self.filter_plugins(plugin_type='analysisPlugin')
p: plugin
for p, plugin in self.plugins.items()
if isinstance(plugin, SentimentPlugin)
}
...@@ -30,6 +30,15 @@ class SenpyPlugin(models.Plugin): ...@@ -30,6 +30,15 @@ class SenpyPlugin(models.Plugin):
def get_folder(self): def get_folder(self):
return os.path.dirname(inspect.getfile(self.__class__)) return os.path.dirname(inspect.getfile(self.__class__))
def activate(self):
pass
def deactivate(self):
pass
class AnalysisPlugin(SenpyPlugin):
def analyse(self, *args, **kwargs): def analyse(self, *args, **kwargs):
raise NotImplemented( raise NotImplemented(
'Your method should implement either analyse or analyse_entry') 'Your method should implement either analyse or analyse_entry')
...@@ -48,30 +57,27 @@ class SenpyPlugin(models.Plugin): ...@@ -48,30 +57,27 @@ class SenpyPlugin(models.Plugin):
for i in results.entries: for i in results.entries:
yield i yield i
def activate(self):
pass
def deactivate(self): class ConversionPlugin(SenpyPlugin):
pass pass
class SentimentPlugin(models.SentimentPlugin, SenpyPlugin): class SentimentPlugin(models.SentimentPlugin, AnalysisPlugin):
def __init__(self, info, *args, **kwargs): def __init__(self, info, *args, **kwargs):
super(SentimentPlugin, self).__init__(info, *args, **kwargs) super(SentimentPlugin, self).__init__(info, *args, **kwargs)
self.minPolarityValue = float(info.get("minPolarityValue", 0)) self.minPolarityValue = float(info.get("minPolarityValue", 0))
self.maxPolarityValue = float(info.get("maxPolarityValue", 1)) self.maxPolarityValue = float(info.get("maxPolarityValue", 1))
class EmotionPlugin(models.EmotionPlugin, SenpyPlugin): class EmotionPlugin(models.EmotionPlugin, AnalysisPlugin):
def __init__(self, info, *args, **kwargs): def __init__(self, info, *args, **kwargs):
super(EmotionPlugin, self).__init__(info, *args, **kwargs) super(EmotionPlugin, self).__init__(info, *args, **kwargs)
self.minEmotionValue = float(info.get("minEmotionValue", -1)) self.minEmotionValue = float(info.get("minEmotionValue", -1))
self.maxEmotionValue = float(info.get("maxEmotionValue", 1)) self.maxEmotionValue = float(info.get("maxEmotionValue", 1))
class EmotionConversionPlugin(models.EmotionConversionPlugin, SenpyPlugin): class EmotionConversionPlugin(models.EmotionConversionPlugin, ConversionPlugin):
def __init__(self, info, *args, **kwargs): pass
super(EmotionConversionPlugin, self).__init__(info, *args, **kwargs)
class ShelfMixin(object): class ShelfMixin(object):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment