mirror of
https://github.com/gsi-upm/senpy
synced 2025-09-17 12:02:21 +00:00
Compare commits
19 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
d1006bbc92 | ||
|
d58137e8f9 | ||
|
79c83e34a3 | ||
|
37a098109f | ||
|
ff14925056 | ||
|
10f4782ad7 | ||
|
4351f76b60 | ||
|
86f45f8147 | ||
|
2834967026 | ||
|
2f7a8d7267 | ||
|
2b68838514 | ||
|
eaf65f0c6b | ||
|
a5e79bead3 | ||
|
54492472ba | ||
|
ff8d12074b | ||
|
bdf1992775 | ||
|
e06fc2e671 | ||
|
8405e5deef | ||
|
d0aa889124 |
4
MANIFEST.in
Normal file
4
MANIFEST.in
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
include requirements.txt
|
||||||
|
include README.md
|
||||||
|
include senpy/context.jsonld
|
||||||
|
recursive-include *.senpy
|
@@ -1,4 +1,4 @@
|
|||||||

|

|
||||||
[Senpy](http://senpy.herokuapp.com)
|
[Senpy](http://senpy.herokuapp.com)
|
||||||
=========================================
|
=========================================
|
||||||
Example endpoint that yields results compatible with the EUROSENTIMENT format and exposes the NIF API.
|
Example endpoint that yields results compatible with the EUROSENTIMENT format and exposes the NIF API.
|
||||||
|
62
app.py
62
app.py
@@ -14,54 +14,30 @@
|
|||||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
'''
|
"""
|
||||||
Simple Sentiment Analysis server for EUROSENTIMENT
|
This is a helper for development. If you want to run Senpy use:
|
||||||
|
|
||||||
This class shows how to use the nif_server module to create custom services.
|
python -m senpy
|
||||||
'''
|
"""
|
||||||
|
from gevent.monkey import patch_all patch_all()
|
||||||
|
import gevent
|
||||||
import config
|
import config
|
||||||
import re
|
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
import random
|
from senpy.extensions import Senpy
|
||||||
from senpy.nif_server import *
|
import logging
|
||||||
|
import os
|
||||||
|
from gevent.wsgi import WSGIServer
|
||||||
|
|
||||||
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
mypath = os.path.dirname(os.path.realpath(__file__))
|
||||||
rgx = re.compile("(\w[\w']*\w|\w)", re.U)
|
sp = Senpy(app, os.path.join(mypath, "plugins"))
|
||||||
|
sp.activate_all()
|
||||||
def hard_analysis(params):
|
|
||||||
response = basic_analysis(params)
|
|
||||||
response["analysis"][0].update({ "marl:algorithm": "SimpleAlgorithm",
|
|
||||||
"marl:minPolarityValue": -1,
|
|
||||||
"marl:maxPolarityValue": 1})
|
|
||||||
for i in response["entries"]:
|
|
||||||
contextid = i["@id"]
|
|
||||||
random.seed(str(params))
|
|
||||||
polValue = 2*random.random()-1
|
|
||||||
if polValue > 0:
|
|
||||||
pol = "marl:Positive"
|
|
||||||
elif polValue == 0:
|
|
||||||
pol = "marl:Neutral"
|
|
||||||
else:
|
|
||||||
pol = "marl:Negative"
|
|
||||||
i["opinions"] = [{"marl:polarityValue": polValue,
|
|
||||||
"marl:hasPolarity": pol
|
|
||||||
|
|
||||||
}]
|
|
||||||
i["strings"] = []
|
|
||||||
for m in rgx.finditer(i["nif:isString"]):
|
|
||||||
i["strings"].append({
|
|
||||||
"@id": "{}#char={},{}".format(contextid, m.start(), m.end()),
|
|
||||||
"nif:beginIndex": m.start(),
|
|
||||||
"nif:endIndex": m.end(),
|
|
||||||
"nif:anchorOf": m.group(0)
|
|
||||||
})
|
|
||||||
|
|
||||||
return response
|
|
||||||
|
|
||||||
app.analyse = hard_analysis
|
|
||||||
app.register_blueprint(nif_server)
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
import logging
|
||||||
|
logging.basicConfig(level=config.DEBUG)
|
||||||
app.debug = config.DEBUG
|
app.debug = config.DEBUG
|
||||||
app.run()
|
http_server = WSGIServer(('', config.SERVER_PORT), app)
|
||||||
|
http_server.serve_forever()
|
||||||
|
1
dev-requirements.txt
Normal file
1
dev-requirements.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
mock
|
31
plugins/rand/rand.py
Normal file
31
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
plugins/rand/rand.senpy
Normal file
18
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
plugins/sentiment140/sentiment140.py
Normal file
38
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
plugins/sentiment140/sentiment140.senpy
Normal file
18
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"
|
||||||
|
}
|
@@ -1,3 +1,7 @@
|
|||||||
Flask==0.10.1
|
Flask==0.10.1
|
||||||
gunicorn==19.0.0
|
gunicorn==19.0.0
|
||||||
requests==2.4.1
|
requests==2.4.1
|
||||||
|
GitPython==0.3.2.RC1
|
||||||
|
Yapsy>=1.10.423
|
||||||
|
gevent>=1.0.1
|
||||||
|
PyLD>=0.6.5
|
@@ -0,0 +1,23 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# 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
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# 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.
|
||||||
|
"""
|
||||||
|
Sentiment analysis server in Python
|
||||||
|
"""
|
||||||
|
|
||||||
|
import extensions
|
||||||
|
import blueprints
|
||||||
|
import plugins
|
||||||
|
75
senpy/__main__.py
Normal file
75
senpy/__main__.py
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# 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
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# 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.
|
||||||
|
"""
|
||||||
|
Senpy is a modular sentiment analysis server. This script runs an instance of
|
||||||
|
the server.
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
from flask import Flask
|
||||||
|
from senpy.extensions import Senpy
|
||||||
|
from gevent.wsgi import WSGIServer
|
||||||
|
from gevent.monkey import patch_all
|
||||||
|
import gevent
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
import argparse
|
||||||
|
|
||||||
|
patch_all(thread=False)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
parser = argparse.ArgumentParser(description='Run a Senpy server')
|
||||||
|
parser.add_argument('--level',
|
||||||
|
"-l",
|
||||||
|
metavar="logging_level",
|
||||||
|
type=str,
|
||||||
|
default="INFO",
|
||||||
|
help='Logging level')
|
||||||
|
parser.add_argument('--debug',
|
||||||
|
"-d",
|
||||||
|
action='store_true',
|
||||||
|
default=False,
|
||||||
|
help='Run the application in debug mode')
|
||||||
|
parser.add_argument('--host',
|
||||||
|
type=str,
|
||||||
|
default="127.0.0.1",
|
||||||
|
help='Use 0.0.0.0 to accept requests from any host.')
|
||||||
|
parser.add_argument('--port',
|
||||||
|
'-p',
|
||||||
|
type=int,
|
||||||
|
default=5000,
|
||||||
|
help='Port to listen on.')
|
||||||
|
parser.add_argument('--plugins-folder',
|
||||||
|
'-f',
|
||||||
|
type=str,
|
||||||
|
default="plugins",
|
||||||
|
help='Where to look for plugins.')
|
||||||
|
args = parser.parse_args()
|
||||||
|
logging.basicConfig(level=getattr(logging, args.level))
|
||||||
|
app = Flask(__name__)
|
||||||
|
app.debug = args.debug
|
||||||
|
sp = Senpy(app, args.plugins_folder)
|
||||||
|
sp.activate_all()
|
||||||
|
import logging
|
||||||
|
http_server = WSGIServer((args.host, args.port), app)
|
||||||
|
try:
|
||||||
|
print "Server running on port %s:%d. Ctrl+C to quit" % (args.host,
|
||||||
|
args.port)
|
||||||
|
http_server.serve_forever()
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
http_server.stop()
|
||||||
|
print "Bye!"
|
173
senpy/blueprints.py
Normal file
173
senpy/blueprints.py
Normal file
@@ -0,0 +1,173 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# 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
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# 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.
|
||||||
|
"""
|
||||||
|
Blueprints for Senpy
|
||||||
|
"""
|
||||||
|
from flask import Blueprint, request, current_app
|
||||||
|
from .models import Error, Response
|
||||||
|
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
nif_blueprint = Blueprint("NIF Sentiment Analysis Server", __name__)
|
||||||
|
|
||||||
|
BASIC_PARAMS = {
|
||||||
|
"algorithm": {
|
||||||
|
"aliases": ["algorithm", "a", "algo"],
|
||||||
|
"required": False,
|
||||||
|
},
|
||||||
|
"inHeaders": {
|
||||||
|
"aliases": ["inHeaders", "headers"],
|
||||||
|
"required": True,
|
||||||
|
"default": "0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
LIST_PARAMS = {
|
||||||
|
"params": {
|
||||||
|
"aliases": ["params", "with_params"],
|
||||||
|
"required": False,
|
||||||
|
"default": "0"
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def get_params(req, params=BASIC_PARAMS):
|
||||||
|
if req.method == 'POST':
|
||||||
|
indict = req.form
|
||||||
|
elif req.method == 'GET':
|
||||||
|
indict = req.args
|
||||||
|
else:
|
||||||
|
raise ValueError("Invalid data")
|
||||||
|
|
||||||
|
outdict = {}
|
||||||
|
wrong_params = {}
|
||||||
|
for param, options in params.iteritems():
|
||||||
|
if param[0] != "@": # Exclude json-ld properties
|
||||||
|
logger.debug("Param: %s - Options: %s", param, options)
|
||||||
|
for alias in options["aliases"]:
|
||||||
|
if alias in indict:
|
||||||
|
outdict[param] = indict[alias]
|
||||||
|
if param not in outdict:
|
||||||
|
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"]:
|
||||||
|
wrong_params[param] = params[param]
|
||||||
|
if wrong_params:
|
||||||
|
message = Error({"status": 404,
|
||||||
|
"message": "Missing or invalid parameters",
|
||||||
|
"parameters": outdict,
|
||||||
|
"errors": {param: error for param, error in
|
||||||
|
wrong_params.iteritems()}
|
||||||
|
})
|
||||||
|
raise ValueError(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"}],
|
||||||
|
"entries": []
|
||||||
|
}
|
||||||
|
if "language" in params:
|
||||||
|
response["language"] = params["language"]
|
||||||
|
for idx, sentence in enumerate(params["input"].split(".")):
|
||||||
|
response["entries"].append({
|
||||||
|
"@id": "Sentence{}".format(idx),
|
||||||
|
"nif:isString": sentence
|
||||||
|
})
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
@nif_blueprint.route('/', methods=['POST', 'GET'])
|
||||||
|
def home():
|
||||||
|
try:
|
||||||
|
params = get_params(request)
|
||||||
|
algo = params.get("algorithm", None)
|
||||||
|
specific_params = current_app.senpy.parameters(algo)
|
||||||
|
logger.debug(
|
||||||
|
"Specific params: %s", json.dumps(specific_params, indent=4))
|
||||||
|
params.update(get_params(request, specific_params))
|
||||||
|
response = current_app.senpy.analyse(**params)
|
||||||
|
in_headers = params["inHeaders"] != "0"
|
||||||
|
return response.flask(in_headers=in_headers)
|
||||||
|
except ValueError as ex:
|
||||||
|
return ex.message.flask()
|
||||||
|
|
||||||
|
|
||||||
|
@nif_blueprint.route("/default")
|
||||||
|
def default():
|
||||||
|
# return current_app.senpy.default_plugin
|
||||||
|
plug = current_app.senpy.default_plugin
|
||||||
|
if plug:
|
||||||
|
return plugins(action="list", plugin=plug.name)
|
||||||
|
else:
|
||||||
|
error = Error(status=404, message="No plugins found")
|
||||||
|
return error.flask()
|
||||||
|
|
||||||
|
|
||||||
|
@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 = sp.filter_plugins(**filt)
|
||||||
|
if plugin and not plugs:
|
||||||
|
return "Plugin not found", 400
|
||||||
|
if action == "list":
|
||||||
|
with_params = get_params(request, LIST_PARAMS)["params"] == "1"
|
||||||
|
in_headers = get_params(request, BASIC_PARAMS)["inHeaders"] != "0"
|
||||||
|
if plugin:
|
||||||
|
dic = plugs[plugin]
|
||||||
|
else:
|
||||||
|
dic = Response(
|
||||||
|
{plug: plugs[plug].jsonld(with_params) for plug in plugs},
|
||||||
|
frame={})
|
||||||
|
return dic.flask(in_headers=in_headers)
|
||||||
|
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
|
||||||
|
app.run()
|
42
senpy/context.jsonld
Normal file
42
senpy/context.jsonld
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
{
|
||||||
|
"dc": "http://purl.org/dc/terms/",
|
||||||
|
"dc:subject": {
|
||||||
|
"@type": "@id"
|
||||||
|
},
|
||||||
|
"xsd": "http://www.w3.org/2001/XMLSchema#",
|
||||||
|
"marl": "http://www.gsi.dit.upm.es/ontologies/marl/ns#",
|
||||||
|
"nif": "http://persistence.uni-leipzig.org/nlp2rdf/ontologies/nif-core#",
|
||||||
|
"onyx": "http://www.gsi.dit.upm.es/ontologies/onyx/ns#",
|
||||||
|
"emotions": {
|
||||||
|
"@container": "@set",
|
||||||
|
"@id": "onyx:hasEmotionSet"
|
||||||
|
},
|
||||||
|
"opinions": {
|
||||||
|
"@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": {
|
||||||
|
"@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#",
|
||||||
|
"senpy": "http://www.gsi.dit.upm.es/ontologies/senpy/ns#",
|
||||||
|
"@vocab": "http://www.gsi.dit.upm.es/ontologies/senpy/ns#"
|
||||||
|
}
|
226
senpy/extensions.py
Normal file
226
senpy/extensions.py
Normal file
@@ -0,0 +1,226 @@
|
|||||||
|
"""
|
||||||
|
"""
|
||||||
|
from .plugins import SenpyPlugin, SentimentPlugin, EmotionPlugin
|
||||||
|
from .models import Error
|
||||||
|
from .blueprints import nif_blueprint
|
||||||
|
|
||||||
|
from git import Repo, InvalidGitRepositoryError
|
||||||
|
from functools import partial
|
||||||
|
|
||||||
|
import os
|
||||||
|
import fnmatch
|
||||||
|
import inspect
|
||||||
|
import sys
|
||||||
|
import imp
|
||||||
|
import logging
|
||||||
|
import gevent
|
||||||
|
import json
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
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 = 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.
|
||||||
|
"""
|
||||||
|
app.senpy = self
|
||||||
|
# Use the newstyle teardown_appcontext if it's available,
|
||||||
|
# otherwise fall back to the request context
|
||||||
|
if hasattr(app, 'teardown_appcontext'):
|
||||||
|
app.teardown_appcontext(self.teardown)
|
||||||
|
else:
|
||||||
|
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
|
||||||
|
logger.debug("analysing with params: {}".format(params))
|
||||||
|
if "algorithm" in params:
|
||||||
|
algo = params["algorithm"]
|
||||||
|
elif self.plugins:
|
||||||
|
algo = self.default_plugin and self.default_plugin.name
|
||||||
|
if not algo:
|
||||||
|
return Error(status=404,
|
||||||
|
message=("No plugins found."
|
||||||
|
" Please install one.").format(algo))
|
||||||
|
if algo in self.plugins:
|
||||||
|
if self.plugins[algo].is_activated:
|
||||||
|
plug = self.plugins[algo]
|
||||||
|
resp = plug.analyse(**params)
|
||||||
|
resp.analysis.append(plug)
|
||||||
|
logger.debug("Returning analysis result: {}".format(resp))
|
||||||
|
return resp
|
||||||
|
else:
|
||||||
|
logger.debug("Plugin not activated: {}".format(algo))
|
||||||
|
return Error(status=400,
|
||||||
|
message=("The algorithm '{}'"
|
||||||
|
" is not activated yet").format(algo))
|
||||||
|
else:
|
||||||
|
logger.debug(("The algorithm '{}' is not valid\n"
|
||||||
|
"Valid algorithms: {}").format(algo,
|
||||||
|
self.plugins.keys()))
|
||||||
|
return Error(status=400,
|
||||||
|
message="The algorithm '{}' is not valid"
|
||||||
|
.format(algo))
|
||||||
|
|
||||||
|
@property
|
||||||
|
def default_plugin(self):
|
||||||
|
candidates = self.filter_plugins(is_activated=True)
|
||||||
|
if len(candidates) > 0:
|
||||||
|
candidate = candidates.values()[0]
|
||||||
|
logger.debug("Default: {}".format(candidate))
|
||||||
|
return candidate
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def parameters(self, algo):
|
||||||
|
return getattr(self.plugins.get(algo) or self.default_plugin,
|
||||||
|
"params",
|
||||||
|
{})
|
||||||
|
|
||||||
|
def activate_all(self, sync=False):
|
||||||
|
ps = []
|
||||||
|
for plug in self.plugins.keys():
|
||||||
|
ps.append(self.activate_plugin(plug, sync=sync))
|
||||||
|
return ps
|
||||||
|
|
||||||
|
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):
|
||||||
|
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
|
||||||
|
|
||||||
|
@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:
|
||||||
|
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 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
|
||||||
|
|
||||||
|
@property
|
||||||
|
def plugins(self):
|
||||||
|
""" 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())
|
||||||
|
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)}
|
||||||
|
|
||||||
|
def sentiment_plugins(self):
|
||||||
|
""" Return only the sentiment plugins """
|
||||||
|
return {p: plugin for p, plugin in self.plugins.items() if
|
||||||
|
isinstance(plugin, SentimentPlugin)}
|
249
senpy/models.py
Normal file
249
senpy/models.py
Normal file
@@ -0,0 +1,249 @@
|
|||||||
|
import json
|
||||||
|
import os
|
||||||
|
from collections import defaultdict
|
||||||
|
from pyld import jsonld
|
||||||
|
import logging
|
||||||
|
from flask import Response as FlaskResponse
|
||||||
|
|
||||||
|
|
||||||
|
class Leaf(dict):
|
||||||
|
_prefix = None
|
||||||
|
_frame = {}
|
||||||
|
_context = {}
|
||||||
|
|
||||||
|
def __init__(self,
|
||||||
|
*args,
|
||||||
|
**kwargs):
|
||||||
|
|
||||||
|
id = kwargs.pop("id", None)
|
||||||
|
context = kwargs.pop("context", self._context)
|
||||||
|
vocab = kwargs.pop("vocab", None)
|
||||||
|
prefix = kwargs.pop("prefix", None)
|
||||||
|
frame = kwargs.pop("frame", None)
|
||||||
|
super(Leaf, self).__init__(*args, **kwargs)
|
||||||
|
if context is not None:
|
||||||
|
self.context = context
|
||||||
|
if frame is not None:
|
||||||
|
self._frame = frame
|
||||||
|
self._prefix = prefix
|
||||||
|
self.id = id
|
||||||
|
|
||||||
|
def __getattr__(self, key):
|
||||||
|
try:
|
||||||
|
return object.__getattr__(self, key)
|
||||||
|
except AttributeError:
|
||||||
|
try:
|
||||||
|
return super(Leaf, self).__getitem__(self._get_key(key))
|
||||||
|
except KeyError:
|
||||||
|
raise AttributeError()
|
||||||
|
|
||||||
|
def __setattr__(self, key, value):
|
||||||
|
try:
|
||||||
|
object.__getattr__(self, key)
|
||||||
|
object.__setattr__(self, key, value)
|
||||||
|
except AttributeError:
|
||||||
|
key = self._get_key(key)
|
||||||
|
if key == "@context":
|
||||||
|
value = self.get_context(value)
|
||||||
|
elif key == "@id":
|
||||||
|
value = self.get_id(value)
|
||||||
|
if key[0] == "_":
|
||||||
|
object.__setattr__(self, key, value)
|
||||||
|
else:
|
||||||
|
if value is None:
|
||||||
|
try:
|
||||||
|
super(Leaf, self).__delitem__(key)
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
super(Leaf, self).__setitem__(key, value)
|
||||||
|
|
||||||
|
def get_id(self, id):
|
||||||
|
"""
|
||||||
|
Get id, dealing with prefixes
|
||||||
|
"""
|
||||||
|
# This is not the most elegant solution to change the @id attribute,
|
||||||
|
# but it is the quickest way to have it included in the dictionary
|
||||||
|
# without extra boilerplate.
|
||||||
|
if id and self._prefix and ":" not in id:
|
||||||
|
return "{}{}".format(self._prefix, id)
|
||||||
|
else:
|
||||||
|
return id
|
||||||
|
|
||||||
|
def __delattr__(self, key):
|
||||||
|
return super(Leaf, self).__delitem__(self._get_key(key))
|
||||||
|
|
||||||
|
def _get_key(self, key):
|
||||||
|
if key[0] == "_":
|
||||||
|
return key
|
||||||
|
elif key in ["context", "id"]:
|
||||||
|
return "@{}".format(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:
|
||||||
|
return json.loads(f.read())
|
||||||
|
except IOError:
|
||||||
|
return context
|
||||||
|
|
||||||
|
def compact(self):
|
||||||
|
return jsonld.compact(self, self.get_context(self.context))
|
||||||
|
|
||||||
|
def frame(self, frame=None, options=None):
|
||||||
|
if frame is None:
|
||||||
|
frame = self._frame
|
||||||
|
if options is None:
|
||||||
|
options = {}
|
||||||
|
return jsonld.frame(self, frame, options)
|
||||||
|
|
||||||
|
def jsonld(self, frame=None, options=None,
|
||||||
|
context=None, removeContext=None):
|
||||||
|
if removeContext is None:
|
||||||
|
removeContext = Response._context # Loop?
|
||||||
|
if frame is None:
|
||||||
|
frame = self._frame
|
||||||
|
if context is None:
|
||||||
|
context = self.context
|
||||||
|
else:
|
||||||
|
context = self.get_context(context)
|
||||||
|
# For some reason, this causes errors with pyld
|
||||||
|
# if options is None:
|
||||||
|
# options = {"expandContext": context.copy() }
|
||||||
|
js = self
|
||||||
|
if frame:
|
||||||
|
logging.debug("Framing: %s", json.dumps(self, indent=4))
|
||||||
|
logging.debug("Framing with %s", json.dumps(frame, indent=4))
|
||||||
|
js = jsonld.frame(js, frame, options)
|
||||||
|
logging.debug("Result: %s", json.dumps(js, indent=4))
|
||||||
|
logging.debug("Compacting with %s", json.dumps(context, indent=4))
|
||||||
|
js = jsonld.compact(js, context, options)
|
||||||
|
logging.debug("Result: %s", json.dumps(js, indent=4))
|
||||||
|
if removeContext == context:
|
||||||
|
del js["@context"]
|
||||||
|
return js
|
||||||
|
|
||||||
|
def to_JSON(self, removeContext=None):
|
||||||
|
return json.dumps(self.jsonld(removeContext=removeContext),
|
||||||
|
default=lambda o: o.__dict__,
|
||||||
|
sort_keys=True, indent=4)
|
||||||
|
|
||||||
|
def flask(self,
|
||||||
|
in_headers=False,
|
||||||
|
url="http://demos.gsi.dit.upm.es/senpy/senpy.jsonld"):
|
||||||
|
"""
|
||||||
|
Return the values and error to be used in flask
|
||||||
|
"""
|
||||||
|
js = self.jsonld()
|
||||||
|
headers = None
|
||||||
|
if in_headers:
|
||||||
|
ctx = js["@context"]
|
||||||
|
headers = {
|
||||||
|
"Link": ('<%s>;'
|
||||||
|
'rel="http://www.w3.org/ns/json-ld#context";'
|
||||||
|
' type="application/ld+json"' % url)
|
||||||
|
}
|
||||||
|
del js["@context"]
|
||||||
|
return FlaskResponse(json.dumps(js),
|
||||||
|
status=self.get("status", 200),
|
||||||
|
headers=headers,
|
||||||
|
mimetype="application/json")
|
||||||
|
|
||||||
|
|
||||||
|
class Response(Leaf):
|
||||||
|
_context = Leaf.get_context("{}/context.jsonld".format(
|
||||||
|
os.path.dirname(os.path.realpath(__file__))))
|
||||||
|
_frame = {
|
||||||
|
"@context": _context,
|
||||||
|
"analysis": {
|
||||||
|
"@explicit": True,
|
||||||
|
"maxPolarityValue": {},
|
||||||
|
"minPolarityValue": {},
|
||||||
|
"name": {},
|
||||||
|
"version": {},
|
||||||
|
},
|
||||||
|
"entries": {}
|
||||||
|
}
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
context = kwargs.pop("context", None)
|
||||||
|
frame = kwargs.pop("frame", None)
|
||||||
|
if context is None:
|
||||||
|
context = self._context
|
||||||
|
self.context = context
|
||||||
|
super(Response, self).__init__(
|
||||||
|
*args, context=context, frame=frame, **kwargs)
|
||||||
|
if self._frame is not None and "entries" in self._frame:
|
||||||
|
self.analysis = []
|
||||||
|
self.entries = []
|
||||||
|
|
||||||
|
def jsonld(self, frame=None, options=None, context=None, removeContext={}):
|
||||||
|
return super(Response, self).jsonld(frame,
|
||||||
|
options,
|
||||||
|
context,
|
||||||
|
removeContext)
|
||||||
|
|
||||||
|
|
||||||
|
class Entry(Leaf):
|
||||||
|
_context = {
|
||||||
|
"@vocab": ("http://persistence.uni-leipzig.org/"
|
||||||
|
"nlp2rdf/ontologies/nif-core#")
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
def __init__(self, text=None, emotion_sets=None, opinions=None, **kwargs):
|
||||||
|
super(Entry, self).__init__(**kwargs)
|
||||||
|
if text:
|
||||||
|
self.text = text
|
||||||
|
self.emotionSets = emotion_sets if emotion_sets else []
|
||||||
|
self.opinions = opinions if opinions else []
|
||||||
|
|
||||||
|
|
||||||
|
class Opinion(Leaf):
|
||||||
|
_context = {
|
||||||
|
"@vocab": "http://www.gsi.dit.upm.es/ontologies/marl/ns#"
|
||||||
|
}
|
||||||
|
|
||||||
|
def __init__(self, polarityValue=None, hasPolarity=None, *args, **kwargs):
|
||||||
|
super(Opinion, self).__init__(*args,
|
||||||
|
**kwargs)
|
||||||
|
if polarityValue is not None:
|
||||||
|
self.hasPolarityValue = polarityValue
|
||||||
|
if hasPolarity is not None:
|
||||||
|
self.hasPolarity = hasPolarity
|
||||||
|
|
||||||
|
|
||||||
|
class EmotionSet(Leaf):
|
||||||
|
_context = {}
|
||||||
|
|
||||||
|
def __init__(self, emotions=None, *args, **kwargs):
|
||||||
|
if not emotions:
|
||||||
|
emotions = []
|
||||||
|
super(EmotionSet, self).__init__(context=EmotionSet._context,
|
||||||
|
*args,
|
||||||
|
**kwargs)
|
||||||
|
self.emotions = emotions or []
|
||||||
|
|
||||||
|
|
||||||
|
class Emotion(Leaf):
|
||||||
|
_context = {}
|
||||||
|
|
||||||
|
|
||||||
|
class Error(Response):
|
||||||
|
# A better pattern would be this:
|
||||||
|
# http://flask.pocoo.org/docs/0.10/patterns/apierrors/
|
||||||
|
_frame = {}
|
||||||
|
_context = {}
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super(Error, self).__init__(*args, **kwargs)
|
@@ -1,126 +0,0 @@
|
|||||||
#!/usr/bin/python
|
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
# 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
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# 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
|
|
||||||
import config
|
|
||||||
import json
|
|
||||||
|
|
||||||
nif_server = Blueprint("NIF Sentiment Analysis Server", __name__)
|
|
||||||
|
|
||||||
PARAMS = {"input": {"aliases": ["i", "input"],
|
|
||||||
"required": True,
|
|
||||||
"help": "Input text"
|
|
||||||
},
|
|
||||||
"informat": {"aliases": ["f", "informat"],
|
|
||||||
"required": False,
|
|
||||||
"default": "text",
|
|
||||||
"options": ["turtle", "text"],
|
|
||||||
},
|
|
||||||
"intype": {"aliases": ["intype", "t"],
|
|
||||||
"required": False,
|
|
||||||
"default": "direct",
|
|
||||||
"options": ["direct", "url", "file"],
|
|
||||||
},
|
|
||||||
"outformat": {"aliases": ["outformat", "o"],
|
|
||||||
"default": "json-ld",
|
|
||||||
"required": False,
|
|
||||||
"options": ["json-ld"],
|
|
||||||
},
|
|
||||||
"algorithm": {"aliases": ["algorithm", "a"],
|
|
||||||
"required": False,
|
|
||||||
},
|
|
||||||
"language": {"aliases": ["language", "l"],
|
|
||||||
"required": False,
|
|
||||||
"options": ["es", "en"],
|
|
||||||
},
|
|
||||||
"urischeme": {"aliases": ["urischeme", "u"],
|
|
||||||
"required": False,
|
|
||||||
"default": "RFC5147String",
|
|
||||||
"options": "RFC5147String"
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def get_params(req):
|
|
||||||
indict = None
|
|
||||||
if req.method == 'POST':
|
|
||||||
indict = req.form
|
|
||||||
elif req.method == 'GET':
|
|
||||||
indict = req.args
|
|
||||||
else:
|
|
||||||
raise ValueError("Invalid data")
|
|
||||||
|
|
||||||
outdict = {}
|
|
||||||
wrongParams = {}
|
|
||||||
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]
|
|
||||||
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()}
|
|
||||||
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"
|
|
||||||
}],
|
|
||||||
"entries": []
|
|
||||||
}
|
|
||||||
if "language" in params:
|
|
||||||
response["language"] = params["language"]
|
|
||||||
for idx, sentence in enumerate(params["input"].split(".")):
|
|
||||||
response["entries"].append({
|
|
||||||
"@id": "Sentence{}".format(idx),
|
|
||||||
"nif:isString": sentence
|
|
||||||
})
|
|
||||||
return response
|
|
||||||
|
|
||||||
@nif_server.route('/', methods=['POST', 'GET'])
|
|
||||||
def home(entries=None):
|
|
||||||
try:
|
|
||||||
params = get_params(request)
|
|
||||||
except ValueError as ex:
|
|
||||||
return ex.message
|
|
||||||
response = current_app.analyse(params)
|
|
||||||
return jsonify(response)
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
from flask import Flask
|
|
||||||
app = Flask(__name__)
|
|
||||||
app.register_blueprint(nif_server)
|
|
||||||
app.debug = config.DEBUG
|
|
||||||
app.run()
|
|
123
senpy/plugins.py
Normal file
123
senpy/plugins.py
Normal file
@@ -0,0 +1,123 @@
|
|||||||
|
|
||||||
|
import logging
|
||||||
|
import ConfigParser
|
||||||
|
from .models import Response, Leaf
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
PARAMS = {
|
||||||
|
"input": {
|
||||||
|
"@id": "input",
|
||||||
|
"aliases": ["i", "input"],
|
||||||
|
"required": True,
|
||||||
|
"help": "Input text"
|
||||||
|
},
|
||||||
|
"informat": {
|
||||||
|
"@id": "informat",
|
||||||
|
"aliases": ["f", "informat"],
|
||||||
|
"required": False,
|
||||||
|
"default": "text",
|
||||||
|
"options": ["turtle", "text"],
|
||||||
|
},
|
||||||
|
"intype": {
|
||||||
|
"@id": "intype",
|
||||||
|
"aliases": ["intype", "t"],
|
||||||
|
"required": False,
|
||||||
|
"default": "direct",
|
||||||
|
"options": ["direct", "url", "file"],
|
||||||
|
},
|
||||||
|
"outformat": {
|
||||||
|
"@id": "outformat",
|
||||||
|
"aliases": ["outformat", "o"],
|
||||||
|
"default": "json-ld",
|
||||||
|
"required": False,
|
||||||
|
"options": ["json-ld"],
|
||||||
|
},
|
||||||
|
"language": {
|
||||||
|
"@id": "language",
|
||||||
|
"aliases": ["language", "l"],
|
||||||
|
"required": False,
|
||||||
|
},
|
||||||
|
"prefix": {
|
||||||
|
"@id": "prefix",
|
||||||
|
"aliases": ["prefix", "p"],
|
||||||
|
"required": True,
|
||||||
|
"default": "",
|
||||||
|
},
|
||||||
|
"urischeme": {
|
||||||
|
"@id": "urischeme",
|
||||||
|
"aliases": ["urischeme", "u"],
|
||||||
|
"required": False,
|
||||||
|
"default": "RFC5147String",
|
||||||
|
"options": "RFC5147String"
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class SenpyPlugin(Leaf):
|
||||||
|
_context = Leaf.get_context(Response._context)
|
||||||
|
_frame = {"@context": _context,
|
||||||
|
"name": {},
|
||||||
|
"extra_params": {"@container": "@index"},
|
||||||
|
"@explicit": True,
|
||||||
|
"version": {},
|
||||||
|
"repo": None,
|
||||||
|
"is_activated": {},
|
||||||
|
"params": None,
|
||||||
|
}
|
||||||
|
|
||||||
|
def __init__(self, info=None):
|
||||||
|
if not info:
|
||||||
|
raise ValueError(("You need to provide configuration"
|
||||||
|
"information for the plugin."))
|
||||||
|
logger.debug("Initialising {}".format(info))
|
||||||
|
super(SenpyPlugin, self).__init__()
|
||||||
|
self.name = info["name"]
|
||||||
|
self.version = info["version"]
|
||||||
|
self.id = "{}_{}".format(self.name, self.version)
|
||||||
|
self.params = info.get("params", PARAMS.copy())
|
||||||
|
if "@id" not in self.params:
|
||||||
|
self.params["@id"] = "params_%s" % self.id
|
||||||
|
self.extra_params = info.get("extra_params", {})
|
||||||
|
self.params.update(self.extra_params.copy())
|
||||||
|
if "@id" not in self.extra_params:
|
||||||
|
self.extra_params["@id"] = "extra_params_%s" % self.id
|
||||||
|
self.is_activated = False
|
||||||
|
self._info = info
|
||||||
|
|
||||||
|
def analyse(self, *args, **kwargs):
|
||||||
|
logger.debug("Analysing with: {} {}".format(self.name, self.version))
|
||||||
|
pass
|
||||||
|
|
||||||
|
def activate(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def deactivate(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def jsonld(self, parameters=False, *args, **kwargs):
|
||||||
|
nframe = kwargs.pop("frame", self._frame)
|
||||||
|
if parameters:
|
||||||
|
nframe = nframe.copy()
|
||||||
|
nframe["params"] = {}
|
||||||
|
return super(SenpyPlugin, self).jsonld(frame=nframe, *args, **kwargs)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def id(self):
|
||||||
|
return "{}_{}".format(self.name, self.version)
|
||||||
|
|
||||||
|
|
||||||
|
class SentimentPlugin(SenpyPlugin):
|
||||||
|
|
||||||
|
def __init__(self, info, *args, **kwargs):
|
||||||
|
super(SentimentPlugin, self).__init__(info, *args, **kwargs)
|
||||||
|
self.minPolarityValue = float(info.get("minPolarityValue", 0))
|
||||||
|
self.maxPolarityValue = float(info.get("maxPolarityValue", 1))
|
||||||
|
|
||||||
|
|
||||||
|
class EmotionPlugin(SenpyPlugin):
|
||||||
|
|
||||||
|
def __init__(self, info, *args, **kwargs):
|
||||||
|
resp = super(EmotionPlugin, self).__init__(info, *args, **kwargs)
|
||||||
|
self.minEmotionValue = float(info.get("minEmotionValue", 0))
|
||||||
|
self.maxEmotionValue = float(info.get("maxEmotionValue", 0))
|
@@ -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()
|
|
38
setup.py
38
setup.py
@@ -1,16 +1,32 @@
|
|||||||
from distutils.core import setup
|
from setuptools import setup
|
||||||
|
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.4.0"
|
||||||
|
|
||||||
|
print(reqs)
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
name = 'senpy',
|
name='senpy',
|
||||||
packages = ['senpy'], # this must be the same as the name above
|
packages=['senpy'], # this must be the same as the name above
|
||||||
version = '0.1',
|
version=VERSION,
|
||||||
description = '''
|
description='''
|
||||||
A sentiment analysis server implementation. Designed to be \
|
A sentiment analysis server implementation. Designed to be \
|
||||||
extendable, so new algorithms and sources can be used.
|
extendable, so new algorithms and sources can be used.
|
||||||
''',
|
''',
|
||||||
author = 'J. Fernando Sanchez',
|
author='J. Fernando Sanchez',
|
||||||
author_email = 'balkian@gmail.com',
|
author_email='balkian@gmail.com',
|
||||||
url = 'https://github.com/balkian/senpy', # use the URL to the github repo
|
url='https://github.com/gsi-upm/senpy', # use the URL to the github repo
|
||||||
download_url = 'https://github.com/balkian/senpy/tarball/0.1', # I'll explain this in a second
|
download_url='https://github.com/gsi-upm/senpy/archive/{}.tar.gz'
|
||||||
keywords = ['eurosentiment', 'sentiment', 'emotions', 'nif'], # arbitrary keywords
|
.format(VERSION),
|
||||||
classifiers = [],
|
keywords=['eurosentiment', 'sentiment', 'emotions', 'nif'],
|
||||||
|
classifiers=[],
|
||||||
|
install_requires=reqs,
|
||||||
|
include_package_data=True,
|
||||||
)
|
)
|
||||||
|
20
supervisord.conf
Normal file
20
supervisord.conf
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
[unix_http_server]
|
||||||
|
file=/tmp/senpy.sock ; path to your socket file
|
||||||
|
|
||||||
|
[supervisord]
|
||||||
|
logfile = %(here)s/logs/supervisor.log
|
||||||
|
childlogdir = %(here)s/logs/
|
||||||
|
|
||||||
|
[rpcinterface:supervisor]
|
||||||
|
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
|
||||||
|
|
||||||
|
[supervisorctl]
|
||||||
|
logfile = %(here)s/logs/supervisorctl.log
|
||||||
|
serverurl=unix:///tmp/senpy.sock ; use a unix:// URL for a unix socket
|
||||||
|
|
||||||
|
|
||||||
|
[program:senpy]
|
||||||
|
command = venv/bin/gunicorn -w 1 -b 0.0.0.0:6666 --log-file %(here)s/logs/gunicorn.log --access-logfile - app:app
|
||||||
|
directory = %(here)s
|
||||||
|
environment = PATH=%(here)s/venv/bin/
|
||||||
|
logfile = %(here)s/logs/senpy.log
|
114
tests/blueprints_test/__init__.py
Normal file
114
tests/blueprints_test/__init__.py
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
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
|
||||||
|
from itertools import product
|
||||||
|
|
||||||
|
|
||||||
|
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.assert404(resp)
|
||||||
|
logging.debug(resp.json)
|
||||||
|
assert resp.json["status"] == 404
|
||||||
|
atleast = {
|
||||||
|
"status": 404,
|
||||||
|
"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("Got response: %s", 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
|
||||||
|
assert "@context" in resp.json
|
||||||
|
|
||||||
|
def test_headers(self):
|
||||||
|
for i, j in product(["/plugins/?nothing=", "/?i=test&"],
|
||||||
|
["headers", "inHeaders"]):
|
||||||
|
resp = self.client.get("%s" % (i))
|
||||||
|
assert "@context" in resp.json
|
||||||
|
resp = self.client.get("%s&%s=0" % (i, j))
|
||||||
|
assert "@context" in resp.json
|
||||||
|
resp = self.client.get("%s&%s=1" % (i, j))
|
||||||
|
assert "@context" not in resp.json
|
||||||
|
resp = self.client.get("%s&%s=true" % (i, j))
|
||||||
|
assert "@context" not 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
|
||||||
|
|
||||||
|
def test_default(self):
|
||||||
|
""" Show only one plugin"""
|
||||||
|
resp = self.client.get("/default")
|
||||||
|
self.assert200(resp)
|
||||||
|
logging.debug(resp.json)
|
||||||
|
assert "@id" in resp.json
|
||||||
|
assert resp.json["@id"] == "Dummy_0.1"
|
||||||
|
resp = self.client.get("/plugins/Dummy/deactivate")
|
||||||
|
self.assert200(resp)
|
||||||
|
sleep(0.5)
|
||||||
|
resp = self.client.get("/default")
|
||||||
|
self.assert404(resp)
|
40
tests/context.jsonld
Normal file
40
tests/context.jsonld
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
{
|
||||||
|
"dc": "http://purl.org/dc/terms/",
|
||||||
|
"dc:subject": {
|
||||||
|
"@type": "@id"
|
||||||
|
},
|
||||||
|
"xsd": "http://www.w3.org/2001/XMLSchema#",
|
||||||
|
"marl": "http://www.gsi.dit.upm.es/ontologies/marl/ns#",
|
||||||
|
"nif": "http://persistence.uni-leipzig.org/nlp2rdf/ontologies/nif-core#",
|
||||||
|
"onyx": "http://www.gsi.dit.upm.es/ontologies/onyx/ns#",
|
||||||
|
"emotions": {
|
||||||
|
"@container": "@set",
|
||||||
|
"@id": "onyx:hasEmotionSet"
|
||||||
|
},
|
||||||
|
"opinions": {
|
||||||
|
"@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": {
|
||||||
|
"@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#"
|
||||||
|
}
|
8
tests/dummy_plugin/dummy.py
Normal file
8
tests/dummy_plugin/dummy.py
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
from senpy.plugins import SentimentPlugin
|
||||||
|
from senpy.models import Response
|
||||||
|
|
||||||
|
|
||||||
|
class DummyPlugin(SentimentPlugin):
|
||||||
|
|
||||||
|
def analyse(self, *args, **kwargs):
|
||||||
|
return Response()
|
7
tests/dummy_plugin/dummy.senpy
Normal file
7
tests/dummy_plugin/dummy.senpy
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"name": "Dummy",
|
||||||
|
"module": "dummy",
|
||||||
|
"description": "I am dummy",
|
||||||
|
"author": "@balkian",
|
||||||
|
"version": "0.1"
|
||||||
|
}
|
86
tests/extensions_test/__init__.py
Normal file
86
tests/extensions_test/__init__.py
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
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 not self.senpy.plugins["Dummy"].is_activated
|
||||||
|
assert not self.senpy.plugins["Sleep"].is_activated
|
||||||
|
|
||||||
|
def test_default(self):
|
||||||
|
""" Default plugin should be set """
|
||||||
|
assert self.senpy.default_plugin
|
||||||
|
assert self.senpy.default_plugin.name == "Dummy"
|
||||||
|
self.senpy.deactivate_all(sync=True)
|
||||||
|
logging.debug("Default: {}".format(self.senpy.default_plugin))
|
||||||
|
assert self.senpy.default_plugin is None
|
||||||
|
|
||||||
|
def test_noplugin(self):
|
||||||
|
""" Don't analyse if there isn't any plugin installed """
|
||||||
|
self.senpy.deactivate_all(sync=True)
|
||||||
|
resp = self.senpy.analyse(input="tupni")
|
||||||
|
logging.debug("Response: {}".format(resp))
|
||||||
|
assert resp["status"] == 404
|
||||||
|
|
||||||
|
def test_analyse(self):
|
||||||
|
""" Using a plugin """
|
||||||
|
# I was using mock until plugin started inheriting
|
||||||
|
# Leaf (defaultdict with __setattr__ and __getattr__.
|
||||||
|
r1 = self.senpy.analyse(
|
||||||
|
algorithm="Dummy", input="tupni", output="tuptuo")
|
||||||
|
r2 = self.senpy.analyse(input="tupni", output="tuptuo")
|
||||||
|
assert r1.analysis[0].id[:5] == "Dummy"
|
||||||
|
assert r2.analysis[0].id[:5] == "Dummy"
|
||||||
|
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"] == 404
|
||||||
|
|
||||||
|
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))
|
81
tests/models_test/__init__.py
Normal file
81
tests/models_test/__init__.py
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
import os
|
||||||
|
import logging
|
||||||
|
|
||||||
|
try:
|
||||||
|
import unittest.mock as mock
|
||||||
|
except ImportError:
|
||||||
|
import mock
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
from unittest import TestCase
|
||||||
|
from senpy.models import Response, Entry
|
||||||
|
from senpy.plugins import SenpyPlugin
|
||||||
|
|
||||||
|
|
||||||
|
class ModelsTest(TestCase):
|
||||||
|
|
||||||
|
def test_response(self):
|
||||||
|
r = Response(context=os.path.normpath(
|
||||||
|
os.path.join(__file__, "..", "..", "context.jsonld")))
|
||||||
|
assert("@context" in r)
|
||||||
|
assert(r._frame)
|
||||||
|
logging.debug("Default frame: %s", r._frame)
|
||||||
|
assert("marl" in r.context)
|
||||||
|
assert("entries" in r.context)
|
||||||
|
|
||||||
|
r2 = Response(context=json.loads('{"test": "roger"}'))
|
||||||
|
assert("test" in r2.context)
|
||||||
|
|
||||||
|
r3 = Response(context=None)
|
||||||
|
del r3.context
|
||||||
|
assert("@context" not in r3)
|
||||||
|
assert("entries" in r3)
|
||||||
|
assert("analysis" in r3)
|
||||||
|
|
||||||
|
r4 = Response()
|
||||||
|
assert("@context" in r4)
|
||||||
|
assert("entries" in r4)
|
||||||
|
assert("analysis" in r4)
|
||||||
|
|
||||||
|
dummy = SenpyPlugin({"name": "dummy", "version": 0})
|
||||||
|
r5 = Response({"dummy": dummy}, context=None, frame=None)
|
||||||
|
logging.debug("Response 5: %s", r5)
|
||||||
|
assert("dummy" in r5)
|
||||||
|
assert(r5["dummy"].name == "dummy")
|
||||||
|
js = r5.jsonld(context={}, frame={})
|
||||||
|
logging.debug("jsonld 5: %s", js)
|
||||||
|
assert("dummy" in js)
|
||||||
|
assert(js["dummy"].name == "dummy")
|
||||||
|
|
||||||
|
r6 = Response()
|
||||||
|
r6.entries.append(Entry(text="Just testing"))
|
||||||
|
logging.debug("Reponse 6: %s", r6)
|
||||||
|
assert("@context" in r6)
|
||||||
|
assert("marl" in r6.context)
|
||||||
|
assert("entries" in r6.context)
|
||||||
|
js = r6.jsonld()
|
||||||
|
logging.debug("jsonld: %s", js)
|
||||||
|
assert("entries" in js)
|
||||||
|
assert("entries" in js)
|
||||||
|
assert("analysis" in js)
|
||||||
|
resp = r6.flask()
|
||||||
|
received = json.loads(resp.data)
|
||||||
|
logging.debug("Response: %s", js)
|
||||||
|
assert(received["entries"])
|
||||||
|
assert(received["entries"][0]["text"] == "Just testing")
|
||||||
|
assert(received["entries"][0]["text"] != "Not testing")
|
||||||
|
|
||||||
|
def test_opinions(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def test_plugins(self):
|
||||||
|
p = SenpyPlugin({"name": "dummy", "version": 0})
|
||||||
|
c = p.jsonld()
|
||||||
|
assert "info" not in c
|
||||||
|
assert "repo" not in c
|
||||||
|
assert "params" not in c
|
||||||
|
logging.debug("Framed: %s", c)
|
||||||
|
assert "extra_params" in c
|
||||||
|
|
||||||
|
def test_frame_response(self):
|
||||||
|
pass
|
16
tests/sleep_plugin/sleep.py
Normal file
16
tests/sleep_plugin/sleep.py
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
from senpy.plugins import SenpyPlugin
|
||||||
|
from senpy.models import Response
|
||||||
|
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)
|
||||||
|
|
||||||
|
def analyse(self, *args, **kwargs):
|
||||||
|
return Response()
|
8
tests/sleep_plugin/sleep.senpy
Normal file
8
tests/sleep_plugin/sleep.senpy
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"name": "Sleep",
|
||||||
|
"module": "sleep",
|
||||||
|
"description": "I am dummy",
|
||||||
|
"author": "@balkian",
|
||||||
|
"version": "0.1",
|
||||||
|
"timeout": "2"
|
||||||
|
}
|
Reference in New Issue
Block a user