1
0
mirror of https://github.com/gsi-upm/senpy synced 2025-09-15 19:12:21 +00:00

Compare commits

..

8 Commits
0.2.5 ... 0.3.0

Author SHA1 Message Date
J. Fernando Sánchez
ff14925056 Improved plugins, better tests, gevent
Moved from Yapsy again (it is not flexible enough), now we use a
custom solution.
The activation and deactivation of plugins is asynchronous, so
that plugins that take a long time don't interfere with the rest.
2014-12-01 18:27:20 +01:00
J. Fernando Sánchez
10f4782ad7 Better NIF compliance 2014-12-01 09:38:23 +01:00
J. Fernando Sánchez
4351f76b60 Removed unnecessary contexts 2014-11-27 17:43:19 +01:00
J. Fernando Sánchez
86f45f8147 JSON-LD contexts and prefixes 2014-11-27 17:39:36 +01:00
J. Fernando Sánchez
2834967026 Better jsonld support 2014-11-27 11:27:05 +01:00
J. Fernando Sánchez
2f7a8d7267 Fixed setup.py and pip 2014-11-20 20:54:57 +01:00
J. Fernando Sánchez
2b68838514 PEP8 compliance 2014-11-20 19:29:49 +01:00
J. Fernando Sánchez
eaf65f0c6b First tests 2014-11-07 19:12:21 +01:00
22 changed files with 599 additions and 305 deletions

4
MANIFEST.in Normal file
View File

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

22
app.py
View File

@@ -14,20 +14,30 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
'''
"""
Simple Sentiment Analysis server for EUROSENTIMENT
This class shows how to use the nif_server module to create custom services.
'''
"""
from gevent.monkey import patch_all; patch_all()
import gevent
import config
from flask import Flask
from senpy.extensions import Senpy
import logging
import os
from gevent.wsgi import WSGIServer
logging.basicConfig(level=logging.DEBUG)
app = Flask(__name__)
sp = Senpy()
sp.init_app(app)
mypath = os.path.dirname(os.path.realpath(__file__))
sp = Senpy(app, os.path.join(mypath, "plugins"))
sp.activate_all()
if __name__ == '__main__':
import logging
logging.basicConfig(level=config.DEBUG)
app.debug = config.DEBUG
app.run(host="0.0.0.0", use_reloader=False)
http_server = WSGIServer(('', 5000), app)
http_server.serve_forever()

1
dev-requirements.txt Normal file
View File

@@ -0,0 +1 @@
mock

View File

@@ -0,0 +1,34 @@
import requests
import json
from senpy.plugins import SentimentPlugin
from senpy.models import Response, Opinion, Entry
class Sentiment140Plugin(SentimentPlugin):
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"],
prefix=params.get("prefix", ""))
opinion = Opinion(hasPolarity=polarity,
polarityValue=polarity_value,
prefix=params.get("prefix", ""))
opinion["prov:wasGeneratedBy"] = self.id
entry.opinions.append(opinion)
entry.language = lang
response.entries.append(entry)
return response

View File

@@ -0,0 +1,15 @@
{
"name": "sentiment140",
"module": "sentiment140",
"description": "What my plugin broadly does",
"author": "@balkian",
"version": "0.1",
"extra_params": {
"language": {
"aliases": ["language", "l"],
"required": false,
"options": ["es", "en", "auto"]
}
},
"requirements": {}
}

View File

@@ -1,5 +1,6 @@
Flask==0.10.1
gunicorn==19.0.0
requests==2.4.1
Flask-Plugins==1.4
GitPython==0.3.2.RC1
Yapsy>=1.10.423
gevent>=1.0.1

View File

@@ -14,21 +14,12 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
'''
"""
Sentiment analysis server in Python
'''
VERSION = "0.2.5"
"""
import extensions
import blueprints
import plugins
if __name__ == '__main__':
from flask import Flask
app = Flask(__name__)
sp = extensions.Senpy()
sp.init_app(app)
app.debug = config.DEBUG
app.run()
__version__ = "0.2.8"

View File

@@ -1,24 +1,28 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright 2014 J. Fernando Sánchez Rada - Grupo de Sistemas Inteligentes
# DIT, UPM
# Copyright 2014 J. Fernando Sánchez Rada - Grupo de Sistemas Inteligentes
# DIT, UPM
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
'''
Simple Sentiment Analysis server
'''
from flask import Blueprint, render_template, request, jsonify, current_app
"""
Blueprints for Senpy
"""
import json
import logging
logger = logging.getLogger(__name__)
from flask import Blueprint, request, jsonify, current_app
nif_blueprint = Blueprint("NIF Sentiment Analysis Server", __name__)
@@ -28,8 +32,8 @@ BASIC_PARAMS = {
},
}
def get_params(req, params=BASIC_PARAMS):
indict = None
if req.method == 'POST':
indict = req.form
elif req.method == 'GET':
@@ -38,37 +42,37 @@ def get_params(req, params=BASIC_PARAMS):
raise ValueError("Invalid data")
outdict = {}
wrongParams = {}
wrong_params = {}
for param, options in params.iteritems():
for alias in options["aliases"]:
if alias in indict:
outdict[param] = indict[alias]
if param not in outdict:
if options.get("required", False):
wrongParams[param] = params[param]
if options.get("required", False) and "default" not in options:
wrong_params[param] = params[param]
else:
if "default" in options:
outdict[param] = options["default"]
else:
if "options" in params[param] and \
outdict[param] not in params[param]["options"]:
wrongParams[param] = params[param]
if wrongParams:
message = {"status": "failed", "message": "Missing or invalid parameters"}
message["parameters"] = outdict
message["errors"] = {param:error for param, error in wrongParams.iteritems()}
if "options" in params[param] and outdict[param] not in params[param]["options"]:
wrong_params[param] = params[param]
if wrong_params:
message = {"status": "failed",
"message": "Missing or invalid parameters",
"parameters": outdict,
"errors": {param: error for param, error in wrong_params.iteritems()}
}
raise ValueError(json.dumps(message))
return outdict
def basic_analysis(params):
response = {"@context": ["http://demos.gsi.dit.upm.es/eurosentiment/static/context.jsonld",
{
"@base": "{}#".format(request.url.encode('utf-8'))
}
],
"analysis": [{
"@type": "marl:SentimentAnalysis"
}],
"analysis": [{"@type": "marl:SentimentAnalysis"}],
"entries": []
}
if "language" in params:
@@ -80,51 +84,57 @@ def basic_analysis(params):
})
return response
@nif_blueprint.route('/', methods=['POST', 'GET'])
def home(entries=None):
def home():
try:
algo = get_params(request).get("algorithm", None)
specific_params = current_app.senpy.parameters(algo)
params = get_params(request, specific_params)
response = current_app.senpy.analyse(**params)
return jsonify(response)
except ValueError as ex:
return ex.message
response = current_app.senpy.analyse(**params)
return jsonify(response)
except Exception as ex:
return jsonify(status="400", message=ex.message)
@nif_blueprint.route("/default")
def default():
return current_app.senpy.default_plugin
#return plugins(action="list", plugin=current_app.senpy.default_algorithm)
@nif_blueprint.route('/plugins/', methods=['POST', 'GET'])
@nif_blueprint.route('/plugins/<plugin>', methods=['POST', 'GET'])
@nif_blueprint.route('/plugins/<plugin>/<action>', methods=['POST', 'GET'])
def plugins(plugin=None, action="list"):
filt = {}
sp = current_app.senpy
if plugin:
filt["name"] = plugin
plugs = current_app.senpy.filter_plugins(**filt)
plugs = sp.filter_plugins(**filt)
if plugin and not plugs:
return "Plugin not found", 400
if action == "list":
with_params = request.args.get("params", "") == "1"
dic = {plug:plugs[plug].jsonable(with_params) for plug in plugs}
if plugin:
dic = plugs[plugin].jsonable(with_params)
else:
dic = {plug: plugs[plug].jsonable(with_params) for plug in plugs}
return jsonify(dic)
if action == "disable":
current_app.senpy.disable_plugin(plugin)
return "Ok"
elif action == "enable":
current_app.senpy.enable_plugin(plugin)
return "Ok"
elif action == "reload":
current_app.senpy.reload_plugin(plugin)
method = "{}_plugin".format(action)
if(hasattr(sp, method)):
getattr(sp, method)(plugin)
return "Ok"
else:
return "action '{}' not allowed".format(action), 400
if __name__ == '__main__':
import config
from flask import Flask
app = Flask(__name__)
app.register_blueprint(nif_blueprint)
app.debug = config.DEBUG

View File

@@ -8,31 +8,33 @@
"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":
{
"@id": "dc:date",
"@type": "xsd:dateTime"
},
"text": { "@id": "nif:isString" },
"wnaffect": "http://www.gsi.dit.upm.es/ontologies/wnaffect#",
"xsd": "http://www.w3.org/2001/XMLSchema#"
}

View File

@@ -1,39 +1,46 @@
"""
"""
import os
import fnmatch
import inspect
import sys
import imp
import logging
import gevent
import json
from flask import current_app
from collections import defaultdict
from .plugins import SentimentPlugin, EmotionPlugin
logger = logging.getLogger(__name__)
try:
from flask import _app_ctx_stack as stack
except ImportError:
from flask import _request_ctx_stack as stack
from .plugins import SenpyPlugin, SentimentPlugin, EmotionPlugin
from blueprints import nif_blueprint
from .blueprints import nif_blueprint
from git import Repo, InvalidGitRepositoryError
from functools import partial
class Senpy(object):
""" Default Senpy extension for Flask """
def __init__(self, app=None, plugin_folder="plugins"):
self.app = app
base_folder = os.path.join(os.path.dirname(__file__), "plugins")
self.search_folders = (folder for folder in (base_folder, plugin_folder, '/tmp/plugins')
if folder and os.path.isdir(folder))
self._search_folders = set()
self._outdated = True
for folder in (base_folder, plugin_folder):
self.add_folder(folder)
if app is not None:
self.init_app(app)
"""
def init_app(self, app):
""" Initialise a flask app to add plugins to its context """
"""
Note: I'm not particularly fond of adding self.app and app.senpy, but
I can't think of a better way to do it.
"""
def init_app(self, app, plugin_folder="plugins"):
"""
app.senpy = self
#app.config.setdefault('SQLITE3_DATABASE', ':memory:')
# Use the newstyle teardown_appcontext if it's available,
# otherwise fall back to the request context
if hasattr(app, 'teardown_appcontext'):
@@ -42,114 +49,159 @@ class Senpy(object):
app.teardown_request(self.teardown)
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):
algo = None
print("analysing with params: {}".format(params))
logger.debug("analysing with params: {}".format(params))
if "algorithm" in params:
algo = params["algorithm"]
elif self.plugins:
algo = self.default_plugin
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
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))
else:
return {"status": 500, "message": "No valid algorithm"}
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)}
@property
def default_plugin(self):
if self.plugins:
candidate = self.filter_plugins(enabled=True).keys()[0]
print("Default: {}".format(candidate))
candidates = self.filter_plugins(is_activated=True)
if len(candidates) > 0:
candidate = candidates.keys()[0]
logger.debug("Default: {}".format(candidate))
return candidate
else:
return Exception("No algorithm")
return None
def parameters(self, algo):
return getattr(self.plugins.get(algo or self.default_plugin), "params", {})
def enable_plugin(self, plugin):
self.plugins[plugin].disable()
def activate_all(self, sync=False):
ps = []
for plug in self.plugins.keys():
ps.append(self.activate_plugin(plug, sync=sync))
return ps
def disable_plugin(self, plugin):
self.plugins[plugin].disable()
def deactivate_all(self, sync=False):
ps = []
for plug in self.plugins.keys():
ps.append(self.deactivate_plugin(plug, sync=sync))
return ps
def _set_active_plugin(self, plugin_name, active=True, *args, **kwargs):
self.plugins[plugin_name].is_activated = active
def activate_plugin(self, plugin_name, sync=False):
plugin = self.plugins[plugin_name]
th = gevent.spawn(plugin.activate)
th.link_value(partial(self._set_active_plugin, plugin_name, True))
if sync:
th.join()
else:
return th
def deactivate_plugin(self, plugin_name, sync=False):
plugin = self.plugins[plugin_name]
th = gevent.spawn(plugin.deactivate)
th.link_value(partial(self._set_active_plugin, plugin_name, False))
if sync:
th.join()
else:
return th
def reload_plugin(self, plugin):
print("Reloading {}".format(plugin))
logger.debug("Reloading {}".format(plugin))
plug = self.plugins[plugin]
nplug = self._load_plugin(plug.module, plug.path)
del self.plugins[plugin]
self.plugins[nplug.name] = nplug
def _load_plugin(self, plugin, search_folder, enabled=True):
sys.path.append(search_folder)
(fp, pathname, desc) = imp.find_module(plugin)
tmp = imp.load_module(plugin, fp, pathname, desc).plugin
sys.path.remove(search_folder)
tmp.path = search_folder
@staticmethod
def _load_plugin(root, filename):
logger.debug("Loading plugin: {}".format(filename))
fpath = os.path.join(root, filename)
with open(fpath,'r') as f:
info = json.load(f)
logger.debug("Info: {}".format(info))
sys.path.append(root)
module = info["module"]
name = info["name"]
(fp, pathname, desc) = imp.find_module(module, [root,])
try:
repo_path = os.path.join(search_folder, plugin)
tmp.repo = Repo(repo_path)
except InvalidGitRepositoryError:
tmp.repo = None
if not hasattr(tmp, "enabled"):
tmp.enabled = enabled
tmp.module = plugin
return tmp
tmp = imp.load_module(module, fp, pathname, desc)
sys.path.remove(root)
candidate = None
for _, obj in inspect.getmembers(tmp):
if inspect.isclass(obj) and inspect.getmodule(obj) == tmp:
logger.debug("Found plugin class: {}@{}".format(obj, inspect.getmodule(obj)))
candidate = obj
break
if not candidate:
logger.debug("No valid plugin for: {}".format(filename))
return
module = candidate(info=info)
try:
repo_path = root
module.repo = Repo(repo_path)
except InvalidGitRepositoryError:
module.repo = None
except Exception as ex:
logger.debug("Exception importing {}: {}".format(filename, ex))
return None, None
return name, module
def _load_plugins(self):
plugins = {}
for search_folder in self.search_folders:
for item in os.listdir(search_folder):
if os.path.isdir(os.path.join(search_folder, item)) \
and os.path.exists(
os.path.join(search_folder, item, "__init__.py")):
plugin = self._load_plugin(item, search_folder)
plugins[plugin.name] = plugin
for search_folder in self._search_folders:
for root, dirnames, filenames in os.walk(search_folder):
for filename in fnmatch.filter(filenames, '*.senpy'):
name, plugin = self._load_plugin(root, filename)
if plugin:
plugins[name] = plugin
self._outdated = False
return plugins
def teardown(self, exception):
pass
def enable_all(self):
for plugin in self.plugins:
self.enable_plugin(plugin)
def enable_plugin(self, item):
self.plugins[item].enabled = True
def disable_plugin(self, item):
self.plugins[item].enabled = False
@property
def plugins(self):
ctx = stack.top
if ctx is not None:
if not hasattr(self, '_plugins'):
self._plugins = self._load_plugins()
return self._plugins
""" Return the plugins registered for a given application. """
if not hasattr(self, 'senpy_plugins') or self._outdated:
self.senpy_plugins = self._load_plugins()
return self.senpy_plugins
def filter_plugins(self, **kwargs):
""" Filter plugins by different criteria """
def matches(plug):
res = all(getattr(plug, k, None)==v for (k,v) in kwargs.items())
print("matching {} with {}: {}".format(plug.name, kwargs, res))
res = all(getattr(plug, k, None) == v for (k, v) in kwargs.items())
logger.debug("matching {} with {}: {}".format(plug.name,
kwargs,
res))
return res
if not kwargs:
return self.plugins
else:
return {n:p for n,p in self.plugins.items() if matches(p)}
return {n: p for n, p in self.plugins.items() if matches(p)}
def sentiment_plugins(self):
return (plugin for plugin in self.plugins if
isinstance(plugin, SentimentPlugin))
if __name__ == '__main__':
from flask import Flask
app = Flask(__name__)
sp = Senpy()
sp.init_app(app)
with app.app_context():
sp._load_plugins()
""" Return only the sentiment plugins """
return {p: plugin for p, plugin in self.plugins.items() if
isinstance(plugin, SentimentPlugin)}

View File

@@ -2,58 +2,105 @@ import json
import os
from collections import defaultdict
class Leaf(defaultdict):
def __init__(self, ofclass=list):
_prefix = None
def __init__(self, context=None, prefix=None, ofclass=list):
super(Leaf, self).__init__(ofclass)
if context:
self.context = context
self._prefix = prefix
def __getattr__(self, name):
return super(Leaf, self).__getitem__(name)
def __getattr__(self, key):
return super(Leaf, self).__getitem__(self._get_key(key))
def __setattr__(self, name, value):
self[name] = value
def __setattr__(self, key, value):
try:
object.__getattr__(self, key)
object.__setattr__(self, key, value)
except AttributeError:
key = self._get_key(key)
value = self.get_context(value) if key == "@context" else value
if key[0] == "_":
object.__setattr__(self, key, value)
else:
super(Leaf, self).__setitem__(key, value)
def __delattr__(self, name):
return super(Leaf, self).__delitem__(name)
def __delattr__(self, key):
return super(Leaf, self).__delitem__(self._get_key(key))
class Response(Leaf):
def __init__(self, context=None):
super(Response, self).__init__()
self["analysis"] = []
self["entries"] = []
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, basestring):
def _get_key(self, key):
if key is "context":
return "@context"
elif self._prefix:
return "{}:{}".format(self._prefix, key)
else:
return key
@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:
self["@context"] = json.loads(f.read())
return json.loads(f.read())
except IOError:
self["@context"] = context
return context
class Response(Leaf):
def __init__(self, context=None, *args, **kwargs):
if context is None:
context = "{}/context.jsonld".format(os.path.dirname(
os.path.realpath(__file__)))
super(Response, self).__init__(*args, context=context, **kwargs)
self["analysis"] = []
self["entries"] = []
class Entry(Leaf):
def __init__(self, text=None, emotionSets=None, opinions=None, **kwargs):
def __init__(self, text=None, emotion_sets=None, opinions=None, **kwargs):
super(Entry, self).__init__(**kwargs)
if text:
self.text = text
if emotionSets:
self.emotionSets = emotionSets
if emotion_sets:
self.emotionSets = emotion_sets
if opinions:
self.opinions = opinions
class Opinion(Leaf):
def __init__(self, polarityValue=None, polarity=None, **kwargs):
super(Opinion, self).__init__(**kwargs)
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 polarity is not None:
self.polarity = polarity
if hasPolarity is not None:
self.hasPolarity = hasPolarity
class EmotionSet(Leaf):
def __init__(self, emotions=[], **kwargs):
super(EmotionSet, self).__init__(**kwargs)
emotionContext = {}
def __init__(self, emotions=None, *args, **kwargs):
if not emotions:
emotions = []
super(EmotionSet, self).__init__(context=EmotionSet.emotionContext,
*args,
**kwargs)
self.emotions = emotions or []
class Emotion(Leaf):
emotionContext = {}
def __init__(self, emotions=None, *args, **kwargs):
super(EmotionSet, self).__init__(context=Emotion.emotionContext,
*args,
**kwargs)

View File

@@ -1,3 +1,8 @@
import logging
import ConfigParser
logger = logging.getLogger(__name__)
PARAMS = {"input": {"aliases": ["i", "input"],
"required": True,
"help": "Input text"
@@ -21,6 +26,10 @@ PARAMS = {"input": {"aliases": ["i", "input"],
"required": False,
"options": ["es", "en"],
},
"prefix": {"aliases": ["prefix", "p"],
"required": True,
"default": "",
},
"urischeme": {"aliases": ["urischeme", "u"],
"required": False,
"default": "RFC5147String",
@@ -28,50 +37,57 @@ PARAMS = {"input": {"aliases": ["i", "input"],
},
}
class SenpyPlugin(object):
def __init__(self, name=None, version=None, extraparams=None, params=None):
print("Initing {}".format(name))
self.name = name
self.version = version
if params:
self.params = params
else:
self.params = PARAMS.copy()
if extraparams:
self.params.update(extraparams)
self.extraparams = extraparams or {}
self.enabled = True
def __init__(self, info=None):
if not info:
raise ValueError("You need to provide configuration information for the plugin.")
logger.debug("Initialising {}".format(info))
self.name = info["name"]
self.version = info["version"]
self.params = info.get("params", PARAMS.copy())
self.extra_params = info.get("extra_params", {})
self.params.update(self.extra_params)
self.is_activated = False
self.info = info
def analyse(self, *args, **kwargs):
logger.debug("Analysing with: {} {}".format(self.name, self.version))
pass
def enable(self):
self.enabled = True
def activate(self):
pass
def disable(self):
self.enabled = False
def deactivate(self):
pass
@property
def id(self):
return "{}_{}".format(self.name, self.version)
def jsonable(self, parameters=False):
resp = {
resp = {
"@id": "{}_{}".format(self.name, self.version),
"enabled": self.enabled,
"is_activated": self.is_activated,
}
if self.repo:
if hasattr(self, "repo") and self.repo:
resp["repo"] = self.repo.remotes[0].url
if parameters:
resp["parameters"] = self.params
elif self.extraparams:
resp["extra_parameters"] = self.extraparams
elif self.extra_params:
resp["extra_parameters"] = self.extra_params
return resp
class SentimentPlugin(SenpyPlugin):
def __init__(self,
minPolarityValue=0,
maxPolarityValue=1,
min_polarity_value=0,
max_polarity_value=1,
**kwargs):
super(SentimentPlugin, self).__init__(**kwargs)
self.minPolarityValue = minPolarityValue
self.maxPolarityValue = maxPolarityValue
self.minPolarityValue = min_polarity_value
self.maxPolarityValue = max_polarity_value
def jsonable(self, *args, **kwargs):
resp = super(SentimentPlugin, self).jsonable(*args, **kwargs)
@@ -79,16 +95,17 @@ class SentimentPlugin(SenpyPlugin):
resp["marl:minPolarityValue"] = self.minPolarityValue
return resp
class EmotionPlugin(SenpyPlugin):
def __init__(self,
minEmotionValue=0,
maxEmotionValue=1,
emotionCategory=None,
min_emotion_value=0,
max_emotion_value=1,
emotion_category=None,
**kwargs):
super(EmotionPlugin, self).__init__(**kwargs)
self.minEmotionValue = minEmotionValue
self.maxEmotionValue = maxEmotionValue
self.emotionCategory = emotionCategory
self.minEmotionValue = min_emotion_value
self.maxEmotionValue = max_emotion_value
self.emotionCategory = emotion_category
def jsonable(self, *args, **kwargs):
resp = super(EmotionPlugin, self).jsonable(*args, **kwargs)

View File

@@ -1,47 +0,0 @@
import requests
import json
import sys
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()
polarityValue = int(res.json()["data"][0]["polarity"]) * 25
polarity = "marl:Neutral"
if polarityValue > 50:
polarity = "marl:Positive"
elif polarityValue < 50:
polarity = "marl:Negative"
entry = Entry(text=params["input"])
opinion = Opinion(polarity=polarity, polarityValue=polarityValue)
entry.opinions.append(opinion)
entry.language = lang
response.entries.append(entry)
return response
plugin = Sentiment140Plugin()

View File

@@ -1,41 +0,0 @@
'''
SENTIMENT140
=============
* http://www.sentiment140.com/api/bulkClassifyJson
* Method: POST
* Parameters: JSON Object (that is copied to the result)
* text
* query
* language
* topic
* Example response:
```json
{"data": [{"text": "I love Titanic.", "id":1234, "polarity": 4},
{"text": "I hate Titanic.", "id":4567, "polarity": 0}]}
```
'''
import requests
import json
ENDPOINT_URI = "http://www.sentiment140.com/api/bulkClassifyJson"
def analyse(texts):
parameters = {"data": []}
if isinstance(texts, list):
for text in texts:
parameters["data"].append({"text": text})
else:
parameters["data"].append({"text": texts})
res = requests.post(ENDPOINT_URI, json.dumps(parameters))
res.json()
return res.json()
def test():
print analyse("I love Titanic")
print analyse(["I love Titanic", "I hate Titanic"])
if __name__ == "__main__":
test()

View File

@@ -1,18 +1,31 @@
from setuptools import setup
import senpy
from pip.req import parse_requirements
# parse_requirements() returns generator of pip.req.InstallRequirement objects
install_reqs = parse_requirements("requirements.txt")
# reqs is a list of requirement
# e.g. ['django==1.5.1', 'mezzanine==1.4.6']
reqs = [str(ir.req) for ir in install_reqs]
VERSION = "0.3.0"
print(reqs)
setup(
name = 'senpy',
packages = ['senpy'], # this must be the same as the name above
version = senpy.VERSION,
description = '''
name='senpy',
packages=['senpy'], # this must be the same as the name above
version=VERSION,
description='''
A sentiment analysis server implementation. Designed to be \
extendable, so new algorithms and sources can be used.
''',
author = 'J. Fernando Sanchez',
author_email = 'balkian@gmail.com',
url = 'https://github.com/balkian/senpy', # use the URL to the github repo
download_url = 'https://github.com/balkian/senpy/archive/{}.tar.gz'.format(senpy.VERSION),
keywords = ['eurosentiment', 'sentiment', 'emotions', 'nif'], # arbitrary keywords
classifiers = [],
author='J. Fernando Sanchez',
author_email='balkian@gmail.com',
url='https://github.com/balkian/senpy', # use the URL to the github repo
download_url='https://github.com/balkian/senpy/archive/{}.tar.gz'.format(VERSION),
keywords=['eurosentiment', 'sentiment', 'emotions', 'nif'], # arbitrary keywords
classifiers=[],
install_requires=reqs,
include_package_data = True,
)

0
tests/__init__.py Normal file
View File

View File

@@ -0,0 +1,80 @@
import os
import logging
try:
import unittest.mock as mock
except ImportError:
import mock
from senpy.extensions import Senpy
from flask import Flask
from flask.ext.testing import TestCase
from gevent import sleep
def check_dict(indic, template):
return all(item in indic.items() for item in template.items())
class BlueprintsTest(TestCase):
def create_app(self):
self.app = Flask("test_extensions")
self.senpy = Senpy()
self.senpy.init_app(self.app)
self.dir = os.path.join(os.path.dirname(__file__), "..")
self.senpy.add_folder(self.dir)
self.senpy.activate_plugin("Dummy", sync=True)
return self.app
def test_home(self):
""" Calling with no arguments should ask the user for more arguments """
resp = self.client.get("/")
self.assert200(resp)
logging.debug(resp.json)
assert resp.json["status"] == "failed"
atleast = {
"status": "failed",
"message": "Missing or invalid parameters",
}
assert check_dict(resp.json, atleast)
def test_analysis(self):
""" The dummy plugin returns an empty response, it should contain the context """
resp = self.client.get("/?i=My aloha mohame")
self.assert200(resp)
logging.debug(resp.json)
assert "@context" in resp.json
assert check_dict(resp.json["@context"], {"marl": "http://www.gsi.dit.upm.es/ontologies/marl/ns#"})
assert "entries" in resp.json
def test_list(self):
""" List the plugins """
resp = self.client.get("/plugins/")
self.assert200(resp)
logging.debug(resp.json)
assert "Dummy" in resp.json
def test_detail(self):
""" Show only one plugin"""
resp = self.client.get("/plugins/Dummy")
self.assert200(resp)
logging.debug(resp.json)
assert "@id" in resp.json
assert resp.json["@id"] == "Dummy_0.1"
def test_activate(self):
""" Activate and deactivate one plugin """
resp = self.client.get("/plugins/Dummy/deactivate")
self.assert200(resp)
sleep(0.5)
resp = self.client.get("/plugins/Dummy")
self.assert200(resp)
assert "is_activated" in resp.json
assert resp.json["is_activated"] == False
resp = self.client.get("/plugins/Dummy/activate")
self.assert200(resp)
sleep(0.5)
resp = self.client.get("/plugins/Dummy")
self.assert200(resp)
assert "is_activated" in resp.json
assert resp.json["is_activated"] == True

View File

@@ -0,0 +1,8 @@
from senpy.plugins import SentimentPlugin
from senpy.models import Response
class DummyPlugin(SentimentPlugin):
pass
def analyse(self, *args, **kwargs):
return Response()

View File

@@ -0,0 +1,7 @@
{
"name": "Dummy",
"module": "dummy",
"description": "I am dummy",
"author": "@balkian",
"version": "0.1"
}

View File

@@ -0,0 +1,72 @@
import os
import logging
try:
import unittest.mock as mock
except ImportError:
import mock
from senpy.extensions import Senpy
from flask import Flask
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.init_app(self.app)
self.senpy.activate_plugin("Dummy", sync=True)
return self.app
def test_init(self):
""" Initialising the app with the extension. """
assert hasattr(self.app, "senpy")
tapp = Flask("temp app")
self.senpy.init_app(tapp)
assert hasattr(tapp, "senpy")
def test_discovery(self):
""" Discovery of plugins in given folders. """
# noinspection PyProtectedMember
assert self.dir in self.senpy._search_folders
print self.senpy.plugins
assert "Dummy" in self.senpy.plugins
def test_enabling(self):
""" Enabling a plugin """
self.senpy.activate_all(sync=True)
assert len(self.senpy.plugins) == 2
assert self.senpy.plugins["Sleep"].is_activated
def test_disabling(self):
""" Disabling a plugin """
self.senpy.deactivate_all(sync=True)
assert self.senpy.plugins["Dummy"].is_activated == False
assert self.senpy.plugins["Sleep"].is_activated == False
def test_default(self):
""" Default plugin should be set """
assert self.senpy.default_plugin
assert self.senpy.default_plugin == "Dummy"
def test_analyse(self):
""" Using a plugin """
with mock.patch.object(self.senpy.plugins["Dummy"], "analyse") as mocked:
self.senpy.analyse(algorithm="Dummy", input="tupni", output="tuptuo")
self.senpy.analyse(input="tupni", output="tuptuo")
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, sync=True)
resp = self.senpy.analyse(input="tupni")
logging.debug("Response: {}".format(resp))
assert resp["status"] == 400
def test_filtering(self):
""" 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", sync=True)
assert not len(self.senpy.filter_plugins(name="Dummy", is_activated=True))

View File

@@ -0,0 +1,10 @@
from senpy.plugins import SenpyPlugin
from time import sleep
class SleepPlugin(SenpyPlugin):
def __init__(self, info, *args, **kwargs):
super(SleepPlugin, self).__init__(info, *args, **kwargs)
self.timeout = int(info["timeout"])
def activate(self, *args, **kwargs):
sleep(self.timeout)

View File

@@ -0,0 +1,8 @@
{
"name": "Sleep",
"module": "sleep",
"description": "I am dummy",
"author": "@balkian",
"version": "0.1",
"timeout": "2"
}