mirror of
https://github.com/gsi-upm/senpy
synced 2025-08-24 02:22:20 +00:00
@@ -18,7 +18,7 @@
|
||||
Blueprints for Senpy
|
||||
"""
|
||||
from flask import Blueprint, request, current_app
|
||||
from .models import Error, Response
|
||||
from .models import Error, Response, Leaf
|
||||
|
||||
import json
|
||||
import logging
|
||||
@@ -158,9 +158,9 @@ def plugins(plugin=None, action="list"):
|
||||
method = "{}_plugin".format(action)
|
||||
if(hasattr(sp, method)):
|
||||
getattr(sp, method)(plugin)
|
||||
return "Ok"
|
||||
return Leaf(message="Ok").flask()
|
||||
else:
|
||||
return "action '{}' not allowed".format(action), 400
|
||||
return Error("action '{}' not allowed".format(action)).flask()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
@@ -1,5 +1,9 @@
|
||||
"""
|
||||
"""
|
||||
import gevent
|
||||
from gevent import monkey
|
||||
monkey.patch_all()
|
||||
|
||||
from .plugins import SenpyPlugin, SentimentPlugin, EmotionPlugin
|
||||
from .models import Error
|
||||
from .blueprints import nif_blueprint
|
||||
@@ -23,15 +27,16 @@ class Senpy(object):
|
||||
|
||||
""" Default Senpy extension for Flask """
|
||||
|
||||
def __init__(self, app=None, plugin_folder="plugins"):
|
||||
def __init__(self, app=None, plugin_folder="plugins", base_plugins=True):
|
||||
self.app = app
|
||||
base_folder = os.path.join(os.path.dirname(__file__), "plugins")
|
||||
|
||||
self._search_folders = set()
|
||||
self._outdated = True
|
||||
|
||||
for folder in (base_folder, plugin_folder):
|
||||
self.add_folder(folder)
|
||||
self.add_folder(plugin_folder)
|
||||
if base_plugins:
|
||||
base_folder = os.path.join(os.path.dirname(__file__), "plugins")
|
||||
self.add_folder(base_folder)
|
||||
|
||||
if app is not None:
|
||||
self.init_app(app)
|
||||
@@ -124,7 +129,13 @@ class Senpy(object):
|
||||
|
||||
def activate_plugin(self, plugin_name, sync=False):
|
||||
plugin = self.plugins[plugin_name]
|
||||
th = gevent.spawn(plugin.activate)
|
||||
def act():
|
||||
try:
|
||||
plugin.activate()
|
||||
except Exception as ex:
|
||||
logger.error("Error activating plugin {}: {}".format(plugin.name,
|
||||
ex))
|
||||
th = gevent.spawn(act)
|
||||
th.link_value(partial(self._set_active_plugin, plugin_name, True))
|
||||
if sync:
|
||||
th.join()
|
||||
|
@@ -239,7 +239,7 @@ class Emotion(Leaf):
|
||||
_context = {}
|
||||
|
||||
|
||||
class Error(Response):
|
||||
class Error(Leaf):
|
||||
# A better pattern would be this:
|
||||
# http://flask.pocoo.org/docs/0.10/patterns/apierrors/
|
||||
_frame = {}
|
||||
|
31
senpy/plugins/rand/rand.py
Normal file
31
senpy/plugins/rand/rand.py
Normal file
@@ -0,0 +1,31 @@
|
||||
import json
|
||||
import random
|
||||
|
||||
from senpy.plugins import SentimentPlugin
|
||||
from senpy.models import Response, Opinion, Entry
|
||||
|
||||
|
||||
class Sentiment140Plugin(SentimentPlugin):
|
||||
def analyse(self, **params):
|
||||
lang = params.get("language", "auto")
|
||||
|
||||
p = params.get("prefix", None)
|
||||
response = Response(prefix=p)
|
||||
polarity_value = max(-1, min(1, random.gauss(0.2, 0.2)))
|
||||
polarity = "marl:Neutral"
|
||||
if polarity_value > 0:
|
||||
polarity = "marl:Positive"
|
||||
elif polarity_value < 0:
|
||||
polarity = "marl:Negative"
|
||||
entry = Entry(id="Entry0",
|
||||
text=params["input"],
|
||||
prefix=p)
|
||||
opinion = Opinion(id="Opinion0",
|
||||
prefix=p,
|
||||
hasPolarity=polarity,
|
||||
polarityValue=polarity_value)
|
||||
opinion["prov:wasGeneratedBy"] = self.id
|
||||
entry.opinions.append(opinion)
|
||||
entry.language = lang
|
||||
response.entries.append(entry)
|
||||
return response
|
18
senpy/plugins/rand/rand.senpy
Normal file
18
senpy/plugins/rand/rand.senpy
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "rand",
|
||||
"module": "rand",
|
||||
"description": "What my plugin broadly does",
|
||||
"author": "@balkian",
|
||||
"version": "0.1",
|
||||
"extra_params": {
|
||||
"language": {
|
||||
"@id": "lang_rand",
|
||||
"aliases": ["language", "l"],
|
||||
"required": false,
|
||||
"options": ["es", "en", "auto"]
|
||||
}
|
||||
},
|
||||
"requirements": {},
|
||||
"marl:maxPolarityValue": "1",
|
||||
"marl:minPolarityValue": "-1"
|
||||
}
|
38
senpy/plugins/sentiment140/sentiment140.py
Normal file
38
senpy/plugins/sentiment140/sentiment140.py
Normal file
@@ -0,0 +1,38 @@
|
||||
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"]}]
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
p = params.get("prefix", None)
|
||||
response = Response(prefix=p)
|
||||
polarity_value = self.maxPolarityValue*int(res.json()["data"][0]
|
||||
["polarity"]) * 0.25
|
||||
polarity = "marl:Neutral"
|
||||
if polarity_value > 50:
|
||||
polarity = "marl:Positive"
|
||||
elif polarity_value < 50:
|
||||
polarity = "marl:Negative"
|
||||
entry = Entry(id="Entry0",
|
||||
text=params["input"],
|
||||
prefix=p)
|
||||
opinion = Opinion(id="Opinion0",
|
||||
prefix=p,
|
||||
hasPolarity=polarity,
|
||||
polarityValue=polarity_value)
|
||||
opinion["prov:wasGeneratedBy"] = self.id
|
||||
entry.opinions.append(opinion)
|
||||
entry.language = lang
|
||||
response.entries.append(entry)
|
||||
return response
|
18
senpy/plugins/sentiment140/sentiment140.senpy
Normal file
18
senpy/plugins/sentiment140/sentiment140.senpy
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "sentiment140",
|
||||
"module": "sentiment140",
|
||||
"description": "What my plugin broadly does",
|
||||
"author": "@balkian",
|
||||
"version": "0.1",
|
||||
"extra_params": {
|
||||
"language": {
|
||||
"@id": "lang_sentiment140",
|
||||
"aliases": ["language", "l"],
|
||||
"required": false,
|
||||
"options": ["es", "en", "auto"]
|
||||
}
|
||||
},
|
||||
"requirements": {},
|
||||
"maxPolarityValue": "1",
|
||||
"minPolarityValue": "0"
|
||||
}
|
Reference in New Issue
Block a user