1
0
mirror of https://github.com/gsi-upm/senpy synced 2025-09-17 20:12:22 +00:00

Compare commits

..

1 Commits
yapsy ... 0.2.9

Author SHA1 Message Date
J. Fernando Sánchez
2834967026 Better jsonld support 2014-11-27 11:27:05 +01:00
16 changed files with 102 additions and 161 deletions

View File

@@ -1,3 +1,4 @@
include requirements.txt
include README.md
include senpy/context.jsonld
recursive-include *.senpy

4
app.py
View File

@@ -22,6 +22,8 @@ This class shows how to use the nif_server module to create custom services.
import config
from flask import Flask
from senpy.extensions import Senpy
import logging
logging.basicConfig(level=logging.DEBUG)
app = Flask(__name__)
@@ -29,7 +31,5 @@ sp = Senpy()
sp.init_app(app)
if __name__ == '__main__':
import logging
logging.basicConfig(level=config.DEBUG)
app.debug = config.DEBUG
app.run(host="0.0.0.0", use_reloader=False)

View File

@@ -36,7 +36,7 @@ class Sentiment140Plugin(SentimentPlugin):
elif polarity_value < 50:
polarity = "marl:Negative"
entry = Entry(text=params["input"])
opinion = Opinion(polarity=polarity, polarity_value=polarity_value)
opinion = Opinion(hasPolarity=polarity, polarityValue=polarity_value)
entry.opinions.append(opinion)
entry.language = lang
response.entries.append(entry)

View File

@@ -1,45 +0,0 @@
import requests
import json
from senpy.plugins import SentimentPlugin
from senpy.models import Response, Opinion, Entry
class Sentiment140Plugin(SentimentPlugin):
EXTRA_PARAMS = {
"language": {"aliases": ["language", "l"],
"required": False,
"options": ["es", "en", "auto"],
}
}
def __init__(self, **kwargs):
super(Sentiment140Plugin, self).__init__(name="sentiment140",
version="2.0",
extraparams=self.EXTRA_PARAMS,
**kwargs)
def analyse(self, **params):
lang = params.get("language", "auto")
res = requests.post("http://www.sentiment140.com/api/bulkClassifyJson",
json.dumps({"language": lang,
"data": [{"text": params["input"]}]
}
)
)
response = Response()
polarity_value = int(res.json()["data"][0]["polarity"]) * 25
polarity = "marl:Neutral"
if polarity_value > 50:
polarity = "marl:Positive"
elif polarity_value < 50:
polarity = "marl:Negative"
entry = Entry(text=params["input"])
opinion = Opinion(polarity=polarity, polarity_value=polarity_value)
entry.opinions.append(opinion)
entry.language = lang
response.entries.append(entry)
return response
plugin = Sentiment140Plugin()

View File

@@ -1,8 +0,0 @@
[Core]
Name = Test plugin of Yapsy
Module = prueba
[Documentation]
Description = What my plugin broadly does
Author = My very own name
Version = 0.1
Website = My very own website

View File

@@ -1,5 +1,4 @@
Flask==0.10.1
gunicorn==19.0.0
requests==2.4.1
GitPython==0.3.2.RC1
Yapsy>=1.10.423
GitPython==0.3.2.RC1

View File

@@ -120,10 +120,10 @@ def plugins(plugin=None, action="list"):
dic = {plug: plugs[plug].jsonable(with_params) for plug in plugs}
return jsonify(dic)
if action == "disable":
current_app.senpy.deactivate_plugin(plugin)
current_app.senpy.disable_plugin(plugin)
return "Ok"
elif action == "enable":
current_app.senpy.activate_plugin(plugin)
current_app.senpy.enable_plugin(plugin)
return "Ok"
elif action == "reload":
current_app.senpy.reload_plugin(plugin)

View File

@@ -8,25 +8,26 @@
"nif": "http://persistence.uni-leipzig.org/nlp2rdf/ontologies/nif-core#",
"onyx": "http://www.gsi.dit.upm.es/ontologies/onyx/ns#",
"emotions": {
"@id": "onyx:hasEmotionSet",
"@type": "onyx:EmotionSet"
"@container": "@set",
"@id": "onyx:hasEmotionSet"
},
"opinions": {
"@container": "@list",
"@id": "marl:hasOpinion",
"@type": "marl:Opinion"
"@container": "@set",
"@id": "marl:hasOpinion"
},
"prov": "http://www.w3.org/ns/prov#",
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
"analysis": {
"@container": "@set",
"@id": "prov:wasInformedBy"
},
"entries": {
"@container": "@set",
"@id": "prov:generated"
},
"strings": {
"@reverse": "nif:hasContext",
"@type": "nif:String"
"@container": "@set",
"@reverse": "nif:hasContext"
},
"date":
{

View File

@@ -8,8 +8,6 @@ import logging
logger = logging.getLogger(__name__)
from .plugins import SentimentPlugin, EmotionPlugin
from yapsy.PluginFileLocator import PluginFileLocator, PluginFileAnalyzerWithInfoFile
from yapsy.PluginManager import PluginManager
try:
from flask import _app_ctx_stack as stack
@@ -52,13 +50,11 @@ class Senpy(object):
app.register_blueprint(nif_blueprint)
def add_folder(self, folder):
logger.debug("Adding folder: %s", folder)
if os.path.isdir(folder):
self._search_folders.add(folder)
self._outdated = True
return True
else:
logger.debug("Not a folder: %s", folder)
return False
def analyse(self, **params):
@@ -68,24 +64,17 @@ class Senpy(object):
algo = params["algorithm"]
elif self.plugins:
algo = self.default_plugin
if algo in self.plugins:
if self.plugins[algo].is_activated:
plug = self.plugins[algo]
resp = plug.analyse(**params)
resp.analysis.append(plug.jsonable())
return resp
logger.debug("Plugin not activated: {}".format(algo))
if algo in self.plugins and self.plugins[algo].enabled:
plug = self.plugins[algo]
resp = plug.analyse(**params)
resp.analysis.append(plug.jsonable())
return resp
else:
logger.debug("The algorithm '{}' is not valid\nValid algorithms: {}".format(algo, self.plugins.keys()))
return {"status": 400, "message": "The algorithm '{}' is not valid".format(algo)}
def activate_all(self):
for plug in self.plugins.values():
plug.activate()
@property
def default_plugin(self):
candidates = self.filter_plugins(is_activated=True)
candidates = self.filter_plugins(enabled=True)
if len(candidates) > 0:
candidate = candidates.keys()[0]
logger.debug("Default: {}".format(candidate))
@@ -96,11 +85,11 @@ class Senpy(object):
def parameters(self, algo):
return getattr(self.plugins.get(algo or self.default_plugin), "params", {})
def activate_plugin(self, plugin):
self.plugins[plugin].activate()
def enable_plugin(self, plugin):
self.plugins[plugin].enable()
def deactivate_plugin(self, plugin):
self.plugins[plugin].deactivate()
def disable_plugin(self, plugin):
self.plugins[plugin].disable()
def reload_plugin(self, plugin):
logger.debug("Reloading {}".format(plugin))
@@ -110,7 +99,7 @@ class Senpy(object):
self.plugins[nplug.name] = nplug
@staticmethod
def _load_plugin(plugin, search_folder, is_activated=True):
def _load_plugin(plugin, search_folder, enabled=True):
logger.debug("Loading plugins")
sys.path.append(search_folder)
(fp, pathname, desc) = imp.find_module(plugin)
@@ -123,8 +112,8 @@ class Senpy(object):
tmp.repo = Repo(repo_path)
except InvalidGitRepositoryError:
tmp.repo = None
if not hasattr(tmp, "is_activated"):
tmp.is_activated = is_activated
if not hasattr(tmp, "enabled"):
tmp.enabled = enabled
tmp.module = plugin
except Exception as ex:
tmp = None
@@ -151,20 +140,7 @@ class Senpy(object):
def enable_all(self):
for plugin in self.plugins:
self.activate_plugin(plugin)
@property
def manager(self):
ctx = stack.top
if ctx is not None:
if not hasattr(ctx, 'senpy_manager'):
logger.debug("Loading manager: %s", self._search_folders)
ctx.senpy_manager = PluginManager(plugin_info_ext="senpy")
ctx.senpy_manager.getPluginLocator().setPluginPlaces(self._search_folders)
ctx.senpy_manager.locatePlugins()
ctx.senpy_manager.loadPlugins()
self.activate_all()
return ctx.senpy_manager
self.enable_plugin(plugin)
@property
def plugins(self):
@@ -172,7 +148,7 @@ class Senpy(object):
ctx = stack.top
if ctx is not None:
if not hasattr(ctx, 'senpy_plugins') or self._outdated:
ctx.senpy_plugins = {p.name:p.plugin_object for p in self.manager.getAllPlugins()}
ctx.senpy_plugins = self._load_plugins()
return ctx.senpy_plugins
def filter_plugins(self, **kwargs):
@@ -193,4 +169,4 @@ class Senpy(object):
def sentiment_plugins(self):
""" Return only the sentiment plugins """
return {p: plugin for p, plugin in self.plugins.items() if
isinstance(plugin, SentimentPlugin)}
isinstance(plugin, SentimentPlugin)}

View File

@@ -4,35 +4,48 @@ from collections import defaultdict
class Leaf(defaultdict):
def __init__(self, ofclass=list):
def __init__(self, context=None, ofclass=list):
super(Leaf, self).__init__(ofclass)
if context:
self.context = context
def __getattr__(self, name):
return super(Leaf, self).__getitem__(name)
if name is not "context":
return super(Leaf, self).__getitem__(name)
return self["@context"]
def __setattr__(self, name, value):
self[name] = value
name = "@context" if name is "context" else name
self[name] = self.get_context(value)
def __delattr__(self, name):
return super(Leaf, self).__delitem__(name)
@staticmethod
def get_context(context):
if isinstance(context, list):
contexts = []
for c in context:
contexts.append(Response.get_context(c))
return contexts
elif isinstance(context, dict):
return context
elif isinstance(context, basestring):
try:
with open(context) as f:
return json.loads(f.read())
except IOError:
return context
class Response(Leaf):
def __init__(self, context=None):
super(Response, self).__init__()
self["analysis"] = []
self["entries"] = []
def __init__(self, context=None, *args, **kwargs):
if context is None:
context = "{}/context.jsonld".format(os.path.dirname(
os.path.realpath(__file__)))
if isinstance(context, dict):
self["@context"] = context
if isinstance(context, str) or isinstance(context, unicode):
try:
with open(context) as f:
self["@context"] = json.loads(f.read())
except IOError:
self["@context"] = context
super(Response, self).__init__(*args, context=context, **kwargs)
self["analysis"] = []
self["entries"] = []
class Entry(Leaf):
@@ -47,17 +60,27 @@ class Entry(Leaf):
class Opinion(Leaf):
def __init__(self, polarity_value=None, polarity=None, **kwargs):
super(Opinion, self).__init__(**kwargs)
if polarity_value is not None:
self.polarity_value = polarity_value
if polarity is not None:
self.polarity = polarity
opinionContext = {
"@vocab": "http://www.gsi.dit.upm.es/ontologies/marl/ns#"
}
def __init__(self, polarityValue=None, hasPolarity=None, *args, **kwargs):
super(Opinion, self).__init__(context=self.opinionContext,
*args,
**kwargs)
if polarityValue is not None:
self.polarityValue = polarityValue
if hasPolarity is not None:
self.hasPolarity = hasPolarity
class EmotionSet(Leaf):
def __init__(self, emotions=None, **kwargs):
emotionContext = {
"@vocab": "http://www.gsi.dit.upm.es/ontologies/onyx/ns#"
}
def __init__(self, emotions=None, *args, **kwargs):
if not emotions:
emotions = []
super(EmotionSet, self).__init__(**kwargs)
super(EmotionSet, self).__init__(context=self.emotionContext,
*args,
**kwargs)
self.emotions = emotions or []

View File

@@ -1,5 +1,4 @@
import logging
from yapsy.IPlugin import IPlugin
logger = logging.getLogger(__name__)
@@ -34,7 +33,7 @@ PARAMS = {"input": {"aliases": ["i", "input"],
}
class SenpyPlugin(IPlugin):
class SenpyPlugin(object):
def __init__(self, name=None, version=None, extraparams=None, params=None):
logger.debug("Initialising {}".format(name))
self.name = name
@@ -46,16 +45,21 @@ class SenpyPlugin(IPlugin):
if extraparams:
self.params.update(extraparams)
self.extraparams = extraparams or {}
self.is_activated = True
self.enabled = True
def analyse(self, *args, **kwargs):
logger.debug("Analysing with: {} {}".format(self.name, self.version))
pass
def enable(self):
self.enabled = True
def disable(self):
self.enabled = False
def jsonable(self, parameters=False):
resp = {
"@id": "{}_{}".format(self.name, self.version),
"is_activated": self.is_activated,
"enabled": self.enabled,
}
if hasattr(self, "repo") and self.repo:
resp["repo"] = self.repo.remotes[0].url

View File

@@ -8,7 +8,7 @@ install_reqs = parse_requirements("requirements.txt")
# e.g. ['django==1.5.1', 'mezzanine==1.4.6']
reqs = [str(ir.req) for ir in install_reqs]
VERSION = "0.2.8"
VERSION = "0.2.9"
print(reqs)

View File

@@ -0,0 +1,3 @@
from senpy.plugins import SenpyPlugin
plugin = SenpyPlugin("dummy")

View File

@@ -1,6 +0,0 @@
from senpy.plugins import SenpyPlugin
class DummyPlugin(SenpyPlugin):
def __init__(self):
super(DummyPlugin, self).__init__("dummy")

View File

@@ -1,8 +0,0 @@
[Core]
Name = dummy
Module = dummy
[Documentation]
Description = What my plugin broadly does
Author = My very own name
Version = 0.1
Website = My very own website

View File

@@ -13,9 +13,10 @@ from flask.ext.testing import TestCase
class ExtensionsTest(TestCase):
def create_app(self):
self.app = Flask("test_extensions")
self.dir = os.path.join(os.path.dirname(__file__), "..")
self.senpy = Senpy(plugin_folder=self.dir)
self.senpy = Senpy()
self.senpy.init_app(self.app)
self.dir = os.path.join(os.path.dirname(__file__), "..")
self.senpy.add_folder(self.dir)
return self.app
def test_init(self):
@@ -34,14 +35,14 @@ class ExtensionsTest(TestCase):
def test_enabling(self):
""" Enabling a plugin """
self.senpy.activate_plugin("dummy")
assert self.senpy.plugins["dummy"].is_activated
self.senpy.enable_plugin("dummy")
assert self.senpy.plugins["dummy"].enabled
def test_disabling(self):
""" Disabling a plugin """
self.senpy.activate_plugin("dummy")
self.senpy.deactivate_plugin("dummy")
assert not self.senpy.plugins["dummy"].is_activated
self.senpy.enable_plugin("dummy")
self.senpy.disable_plugin("dummy")
assert not self.senpy.plugins["dummy"].enabled
def test_default(self):
""" Default plugin should be set """
@@ -56,7 +57,7 @@ class ExtensionsTest(TestCase):
mocked.assert_any_call(input="tupni", output="tuptuo", algorithm="dummy")
mocked.assert_any_call(input="tupni", output="tuptuo")
for plug in self.senpy.plugins:
self.senpy.deactivate_plugin(plug)
self.senpy.disable_plugin(plug)
resp = self.senpy.analyse(input="tupni")
logging.debug("Response: {}".format(resp))
assert resp["status"] == 400
@@ -65,6 +66,6 @@ class ExtensionsTest(TestCase):
""" Filtering plugins """
assert len(self.senpy.filter_plugins(name="dummy")) > 0
assert not len(self.senpy.filter_plugins(name="notdummy"))
assert self.senpy.filter_plugins(name="dummy", is_activated=True)
self.senpy.deactivate_plugin("dummy")
assert not len(self.senpy.filter_plugins(name="dummy", is_activated=True))
assert self.senpy.filter_plugins(name="dummy", enabled=True)
self.senpy.disable_plugin("dummy")
assert not len(self.senpy.filter_plugins(name="dummy", enabled=True))