mirror of
https://github.com/gsi-upm/senpy
synced 2025-09-16 19:42:21 +00:00
Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
dabf444607 | ||
|
2f7a8d7267 | ||
|
2b68838514 |
3
MANIFEST.in
Normal file
3
MANIFEST.in
Normal file
@@ -0,0 +1,3 @@
|
||||
include requirements.txt
|
||||
include README.md
|
||||
include senpy/context.jsonld
|
6
app.py
6
app.py
@@ -14,11 +14,11 @@
|
||||
# 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.
|
||||
'''
|
||||
"""
|
||||
import config
|
||||
from flask import Flask
|
||||
from senpy.extensions import Senpy
|
||||
@@ -29,5 +29,7 @@ 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)
|
||||
|
1
dev-requirements.txt
Normal file
1
dev-requirements.txt
Normal file
@@ -0,0 +1 @@
|
||||
mock
|
@@ -1,9 +0,0 @@
|
||||
from senpy.plugins import SenpyPlugin
|
||||
|
||||
class Prueba(SenpyPlugin):
|
||||
def __init__(self, **kwargs):
|
||||
super(Prueba, self).__init__(name="prueba",
|
||||
version="4.0",
|
||||
**kwargs)
|
||||
|
||||
plugin = Prueba()
|
@@ -1,11 +1,10 @@
|
||||
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"],
|
||||
@@ -13,6 +12,7 @@ class Sentiment140Plugin(SentimentPlugin):
|
||||
"options": ["es", "en", "auto"],
|
||||
}
|
||||
}
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super(Sentiment140Plugin, self).__init__(name="sentiment140",
|
||||
version="2.0",
|
||||
@@ -22,23 +22,21 @@ 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"]}]}
|
||||
))
|
||||
|
||||
json.dumps({"language": lang,
|
||||
"data": [{"text": params["input"]}]
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
response = Response()
|
||||
polarityValue = int(res.json()["data"][0]["polarity"]) * 25
|
||||
polarity_value = int(res.json()["data"][0]["polarity"]) * 25
|
||||
polarity = "marl:Neutral"
|
||||
if polarityValue > 50:
|
||||
if polarity_value > 50:
|
||||
polarity = "marl:Positive"
|
||||
elif polarityValue < 50:
|
||||
elif polarity_value < 50:
|
||||
polarity = "marl:Negative"
|
||||
|
||||
|
||||
entry = Entry(text=params["input"])
|
||||
opinion = Opinion(polarity=polarity, polarityValue=polarityValue)
|
||||
opinion = Opinion(polarity=polarity, polarity_value=polarity_value)
|
||||
entry.opinions.append(opinion)
|
||||
entry.language = lang
|
||||
response.entries.append(entry)
|
||||
|
45
plugins/yapsy_plugin/prueba.py
Normal file
45
plugins/yapsy_plugin/prueba.py
Normal file
@@ -0,0 +1,45 @@
|
||||
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()
|
8
plugins/yapsy_plugin/prueba.senpy
Normal file
8
plugins/yapsy_plugin/prueba.senpy
Normal file
@@ -0,0 +1,8 @@
|
||||
[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
|
@@ -1,5 +1,5 @@
|
||||
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
|
@@ -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.6"
|
||||
"""
|
||||
|
||||
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"
|
||||
|
@@ -14,14 +14,15 @@
|
||||
# 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
|
||||
'''
|
||||
"""
|
||||
Blueprints for Senpy
|
||||
"""
|
||||
import json
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
from flask import Blueprint, render_template, request, jsonify, current_app
|
||||
from flask import Blueprint, request, jsonify, current_app
|
||||
|
||||
nif_blueprint = Blueprint("NIF Sentiment Analysis Server", __name__)
|
||||
|
||||
@@ -31,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':
|
||||
@@ -41,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]
|
||||
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:
|
||||
@@ -83,8 +84,9 @@ 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)
|
||||
@@ -96,11 +98,13 @@ def home(entries=None):
|
||||
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'])
|
||||
@@ -116,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.disable_plugin(plugin)
|
||||
current_app.senpy.deactivate_plugin(plugin)
|
||||
return "Ok"
|
||||
elif action == "enable":
|
||||
current_app.senpy.enable_plugin(plugin)
|
||||
current_app.senpy.activate_plugin(plugin)
|
||||
return "Ok"
|
||||
elif action == "reload":
|
||||
current_app.senpy.reload_plugin(plugin)
|
||||
@@ -127,9 +131,11 @@ def plugins(plugin=None, action="list"):
|
||||
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
|
||||
|
@@ -1,3 +1,5 @@
|
||||
"""
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
import imp
|
||||
@@ -6,6 +8,8 @@ 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
|
||||
@@ -48,14 +52,15 @@ 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):
|
||||
algo = None
|
||||
logger.debug("analysing with params: {}".format(params))
|
||||
@@ -63,18 +68,25 @@ class Senpy(object):
|
||||
algo = params["algorithm"]
|
||||
elif self.plugins:
|
||||
algo = self.default_plugin
|
||||
if algo in self.plugins and self.plugins[algo].enabled:
|
||||
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:
|
||||
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(enabled=True)
|
||||
if len(candidates)>1:
|
||||
candidates = self.filter_plugins(is_activated=True)
|
||||
if len(candidates) > 0:
|
||||
candidate = candidates.keys()[0]
|
||||
logger.debug("Default: {}".format(candidate))
|
||||
return candidate
|
||||
@@ -84,11 +96,11 @@ class Senpy(object):
|
||||
def parameters(self, algo):
|
||||
return getattr(self.plugins.get(algo or self.default_plugin), "params", {})
|
||||
|
||||
def enable_plugin(self, plugin):
|
||||
self.plugins[plugin].enable()
|
||||
def activate_plugin(self, plugin):
|
||||
self.plugins[plugin].activate()
|
||||
|
||||
def disable_plugin(self, plugin):
|
||||
self.plugins[plugin].disable()
|
||||
def deactivate_plugin(self, plugin):
|
||||
self.plugins[plugin].deactivate()
|
||||
|
||||
def reload_plugin(self, plugin):
|
||||
logger.debug("Reloading {}".format(plugin))
|
||||
@@ -97,7 +109,8 @@ class Senpy(object):
|
||||
del self.plugins[plugin]
|
||||
self.plugins[nplug.name] = nplug
|
||||
|
||||
def _load_plugin(self, plugin, search_folder, enabled=True):
|
||||
@staticmethod
|
||||
def _load_plugin(plugin, search_folder, is_activated=True):
|
||||
logger.debug("Loading plugins")
|
||||
sys.path.append(search_folder)
|
||||
(fp, pathname, desc) = imp.find_module(plugin)
|
||||
@@ -110,22 +123,22 @@ class Senpy(object):
|
||||
tmp.repo = Repo(repo_path)
|
||||
except InvalidGitRepositoryError:
|
||||
tmp.repo = None
|
||||
if not hasattr(tmp, "enabled"):
|
||||
tmp.enabled = enabled
|
||||
if not hasattr(tmp, "is_activated"):
|
||||
tmp.is_activated = is_activated
|
||||
tmp.module = plugin
|
||||
except Exception as ex:
|
||||
tmp = None
|
||||
logger.debug("Exception importing {}: {}".format(plugin, ex))
|
||||
return tmp
|
||||
|
||||
|
||||
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")):
|
||||
and os.path.exists(os.path.join(search_folder,
|
||||
item,
|
||||
"__init__.py")):
|
||||
plugin = self._load_plugin(item, search_folder)
|
||||
if plugin:
|
||||
plugins[plugin.name] = plugin
|
||||
@@ -138,13 +151,20 @@ class Senpy(object):
|
||||
|
||||
def enable_all(self):
|
||||
for plugin in self.plugins:
|
||||
self.enable_plugin(plugin)
|
||||
self.activate_plugin(plugin)
|
||||
|
||||
def enable_plugin(self, item):
|
||||
self.plugins[item].enabled = True
|
||||
|
||||
def disable_plugin(self, item):
|
||||
self.plugins[item].enabled = False
|
||||
@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
|
||||
|
||||
@property
|
||||
def plugins(self):
|
||||
@@ -152,17 +172,19 @@ class Senpy(object):
|
||||
ctx = stack.top
|
||||
if ctx is not None:
|
||||
if not hasattr(ctx, 'senpy_plugins') or self._outdated:
|
||||
ctx.senpy_plugins = self._load_plugins()
|
||||
ctx.senpy_plugins = {p.name:p.plugin_object for p in self.manager.getAllPlugins()}
|
||||
return ctx.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())
|
||||
logger.debug("matching {} with {}: {}".format(plug.name,
|
||||
kwargs,
|
||||
res))
|
||||
return res
|
||||
|
||||
if not kwargs:
|
||||
return self.plugins
|
||||
else:
|
||||
@@ -172,12 +194,3 @@ class Senpy(object):
|
||||
""" Return only the sentiment plugins """
|
||||
return {p: plugin for p, plugin in self.plugins.items() 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()
|
||||
|
@@ -2,6 +2,7 @@ import json
|
||||
import os
|
||||
from collections import defaultdict
|
||||
|
||||
|
||||
class Leaf(defaultdict):
|
||||
def __init__(self, ofclass=list):
|
||||
super(Leaf, self).__init__(ofclass)
|
||||
@@ -15,6 +16,7 @@ class Leaf(defaultdict):
|
||||
def __delattr__(self, name):
|
||||
return super(Leaf, self).__delitem__(name)
|
||||
|
||||
|
||||
class Response(Leaf):
|
||||
def __init__(self, context=None):
|
||||
super(Response, self).__init__()
|
||||
@@ -25,7 +27,7 @@ class Response(Leaf):
|
||||
os.path.realpath(__file__)))
|
||||
if isinstance(context, dict):
|
||||
self["@context"] = context
|
||||
if isinstance(context, basestring):
|
||||
if isinstance(context, str) or isinstance(context, unicode):
|
||||
try:
|
||||
with open(context) as f:
|
||||
self["@context"] = json.loads(f.read())
|
||||
@@ -34,26 +36,28 @@ class Response(Leaf):
|
||||
|
||||
|
||||
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):
|
||||
def __init__(self, polarity_value=None, polarity=None, **kwargs):
|
||||
super(Opinion, self).__init__(**kwargs)
|
||||
if polarityValue is not None:
|
||||
self.polarityValue = polarityValue
|
||||
if polarity_value is not None:
|
||||
self.polarity_value = polarity_value
|
||||
if polarity is not None:
|
||||
self.polarity = polarity
|
||||
|
||||
|
||||
|
||||
class EmotionSet(Leaf):
|
||||
def __init__(self, emotions=[], **kwargs):
|
||||
def __init__(self, emotions=None, **kwargs):
|
||||
if not emotions:
|
||||
emotions = []
|
||||
super(EmotionSet, self).__init__(**kwargs)
|
||||
self.emotions = emotions or []
|
||||
|
@@ -1,4 +1,5 @@
|
||||
import logging
|
||||
from yapsy.IPlugin import IPlugin
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -32,7 +33,8 @@ PARAMS = {"input": {"aliases": ["i", "input"],
|
||||
},
|
||||
}
|
||||
|
||||
class SenpyPlugin(object):
|
||||
|
||||
class SenpyPlugin(IPlugin):
|
||||
def __init__(self, name=None, version=None, extraparams=None, params=None):
|
||||
logger.debug("Initialising {}".format(name))
|
||||
self.name = name
|
||||
@@ -44,23 +46,18 @@ class SenpyPlugin(object):
|
||||
if extraparams:
|
||||
self.params.update(extraparams)
|
||||
self.extraparams = extraparams or {}
|
||||
self.enabled = True
|
||||
self.is_activated = 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),
|
||||
"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
|
||||
@@ -68,14 +65,15 @@ class SenpyPlugin(object):
|
||||
resp["extra_parameters"] = self.extraparams
|
||||
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)
|
||||
@@ -83,16 +81,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)
|
||||
|
19
setup.py
19
setup.py
@@ -1,10 +1,21 @@
|
||||
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.2.8"
|
||||
|
||||
print(reqs)
|
||||
|
||||
setup(
|
||||
name='senpy',
|
||||
packages=['senpy'], # this must be the same as the name above
|
||||
version = senpy.VERSION,
|
||||
version=VERSION,
|
||||
description='''
|
||||
A sentiment analysis server implementation. Designed to be \
|
||||
extendable, so new algorithms and sources can be used.
|
||||
@@ -12,7 +23,9 @@ 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),
|
||||
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,0 +1 @@
|
||||
|
||||
|
@@ -1,5 +1,7 @@
|
||||
|
||||
import os
|
||||
import logging
|
||||
|
||||
try:
|
||||
import unittest.mock as mock
|
||||
except ImportError:
|
||||
@@ -8,10 +10,12 @@ from senpy.extensions import Senpy
|
||||
from flask import Flask
|
||||
from flask.ext.testing import TestCase
|
||||
|
||||
|
||||
def check_dict(indic, template):
|
||||
return all(item in indic.items() for item in template.items())
|
||||
|
||||
class Blueprints_Test(TestCase):
|
||||
|
||||
class BlueprintsTest(TestCase):
|
||||
def create_app(self):
|
||||
self.app = Flask("test_extensions")
|
||||
self.senpy = Senpy()
|
||||
@@ -20,7 +24,6 @@ class Blueprints_Test(TestCase):
|
||||
self.senpy.add_folder(self.dir)
|
||||
return self.app
|
||||
|
||||
|
||||
def test_home(self):
|
||||
""" Calling with no arguments should ask the user for more arguments """
|
||||
resp = self.client.get("/")
|
||||
|
@@ -1,3 +0,0 @@
|
||||
from senpy.plugins import SenpyPlugin
|
||||
|
||||
plugin = SenpyPlugin("dummy")
|
6
tests/dummy_plugin/dummy.py
Normal file
6
tests/dummy_plugin/dummy.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from senpy.plugins import SenpyPlugin
|
||||
|
||||
class DummyPlugin(SenpyPlugin):
|
||||
def __init__(self):
|
||||
super(DummyPlugin, self).__init__("dummy")
|
||||
|
8
tests/dummy_plugin/dummy.senpy
Normal file
8
tests/dummy_plugin/dummy.senpy
Normal file
@@ -0,0 +1,8 @@
|
||||
[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
|
@@ -1,5 +1,6 @@
|
||||
import os
|
||||
import logging
|
||||
|
||||
try:
|
||||
import unittest.mock as mock
|
||||
except ImportError:
|
||||
@@ -9,39 +10,38 @@ from flask import Flask
|
||||
from flask.ext.testing import TestCase
|
||||
|
||||
|
||||
class Extensions_Test(TestCase):
|
||||
class ExtensionsTest(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 = Senpy(plugin_folder=self.dir)
|
||||
self.senpy.init_app(self.app)
|
||||
return self.app
|
||||
|
||||
def test_init(self):
|
||||
""" Initialising the app with the extension. """
|
||||
assert hasattr(self.app, "senpy")
|
||||
tapp = Flask("temp app")
|
||||
tsen = Senpy(tapp)
|
||||
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.enable_plugin("dummy")
|
||||
assert self.senpy.plugins["dummy"].enabled
|
||||
self.senpy.activate_plugin("dummy")
|
||||
assert self.senpy.plugins["dummy"].is_activated
|
||||
|
||||
def test_disabling(self):
|
||||
""" Disabling a plugin """
|
||||
self.senpy.enable_plugin("dummy")
|
||||
self.senpy.disable_plugin("dummy")
|
||||
assert self.senpy.plugins["dummy"].enabled == False
|
||||
self.senpy.activate_plugin("dummy")
|
||||
self.senpy.deactivate_plugin("dummy")
|
||||
assert not self.senpy.plugins["dummy"].is_activated
|
||||
|
||||
def test_default(self):
|
||||
""" Default plugin should be set """
|
||||
@@ -56,7 +56,7 @@ class Extensions_Test(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.disable_plugin(plug)
|
||||
self.senpy.deactivate_plugin(plug)
|
||||
resp = self.senpy.analyse(input="tupni")
|
||||
logging.debug("Response: {}".format(resp))
|
||||
assert resp["status"] == 400
|
||||
@@ -65,6 +65,6 @@ class Extensions_Test(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", enabled=True)
|
||||
self.senpy.disable_plugin("dummy")
|
||||
assert not len(self.senpy.filter_plugins(name="dummy", enabled=True))
|
||||
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))
|
||||
|
Reference in New Issue
Block a user