2014-07-13 13:36:47 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
2014-11-20 18:29:49 +00:00
|
|
|
# Copyright 2014 J. Fernando Sánchez Rada - Grupo de Sistemas Inteligentes
|
|
|
|
# DIT, UPM
|
2014-07-13 13:36:47 +00:00
|
|
|
#
|
2014-11-20 18:29:49 +00:00
|
|
|
# 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
|
2014-07-13 13:36:47 +00:00
|
|
|
#
|
2014-11-20 18:29:49 +00:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2014-07-13 13:36:47 +00:00
|
|
|
#
|
2014-11-20 18:29:49 +00:00
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
2014-07-13 13:36:47 +00:00
|
|
|
# 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.
|
2014-11-20 18:29:49 +00:00
|
|
|
"""
|
|
|
|
Blueprints for Senpy
|
|
|
|
"""
|
2015-02-24 06:15:25 +00:00
|
|
|
from flask import Blueprint, request, current_app
|
|
|
|
from .models import Error, Response
|
|
|
|
|
2014-07-13 13:36:47 +00:00
|
|
|
import json
|
2014-11-07 18:12:21 +00:00
|
|
|
import logging
|
2014-11-20 18:29:49 +00:00
|
|
|
|
2014-11-07 18:12:21 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2014-07-13 13:36:47 +00:00
|
|
|
|
2014-10-17 10:47:17 +00:00
|
|
|
nif_blueprint = Blueprint("NIF Sentiment Analysis Server", __name__)
|
2014-07-13 13:36:47 +00:00
|
|
|
|
2014-11-04 20:31:41 +00:00
|
|
|
BASIC_PARAMS = {
|
2015-02-24 06:15:25 +00:00
|
|
|
"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"
|
|
|
|
},
|
2014-11-04 20:31:41 +00:00
|
|
|
}
|
2014-07-13 13:36:47 +00:00
|
|
|
|
2014-11-20 18:29:49 +00:00
|
|
|
|
2014-11-04 20:31:41 +00:00
|
|
|
def get_params(req, params=BASIC_PARAMS):
|
2014-07-13 13:36:47 +00:00
|
|
|
if req.method == 'POST':
|
|
|
|
indict = req.form
|
2014-07-13 14:38:13 +00:00
|
|
|
elif req.method == 'GET':
|
2014-07-13 13:36:47 +00:00
|
|
|
indict = req.args
|
|
|
|
else:
|
|
|
|
raise ValueError("Invalid data")
|
|
|
|
|
|
|
|
outdict = {}
|
2014-11-20 18:29:49 +00:00
|
|
|
wrong_params = {}
|
2014-10-17 17:06:19 +00:00
|
|
|
for param, options in params.iteritems():
|
2015-02-24 06:15:25 +00:00
|
|
|
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"]
|
2014-07-13 13:36:47 +00:00
|
|
|
else:
|
2015-02-24 06:15:25 +00:00
|
|
|
if "options" in params[param] and \
|
|
|
|
outdict[param] not in params[param]["options"]:
|
|
|
|
wrong_params[param] = params[param]
|
2014-11-20 18:29:49 +00:00
|
|
|
if wrong_params:
|
2015-02-24 06:15:25 +00:00
|
|
|
message = Error({"status": 404,
|
|
|
|
"message": "Missing or invalid parameters",
|
|
|
|
"parameters": outdict,
|
|
|
|
"errors": {param: error for param, error in
|
|
|
|
wrong_params.iteritems()}
|
|
|
|
})
|
2015-02-23 01:13:31 +00:00
|
|
|
raise ValueError(message)
|
2014-07-13 13:36:47 +00:00
|
|
|
return outdict
|
|
|
|
|
2014-11-20 18:29:49 +00:00
|
|
|
|
2014-07-13 13:36:47 +00:00
|
|
|
def basic_analysis(params):
|
2015-02-24 06:15:25 +00:00
|
|
|
response = {"@context":
|
|
|
|
[("http://demos.gsi.dit.upm.es/"
|
|
|
|
"eurosentiment/static/context.jsonld"),
|
|
|
|
{
|
|
|
|
"@base": "{}#".format(request.url.encode('utf-8'))
|
|
|
|
}
|
|
|
|
],
|
2014-11-20 18:29:49 +00:00
|
|
|
"analysis": [{"@type": "marl:SentimentAnalysis"}],
|
2014-07-13 13:36:47 +00:00
|
|
|
"entries": []
|
|
|
|
}
|
|
|
|
if "language" in params:
|
|
|
|
response["language"] = params["language"]
|
2014-07-13 14:28:01 +00:00
|
|
|
for idx, sentence in enumerate(params["input"].split(".")):
|
2014-07-13 13:36:47 +00:00
|
|
|
response["entries"].append({
|
2014-07-13 14:28:01 +00:00
|
|
|
"@id": "Sentence{}".format(idx),
|
2014-07-13 13:36:47 +00:00
|
|
|
"nif:isString": sentence
|
|
|
|
})
|
|
|
|
return response
|
|
|
|
|
2014-11-20 18:29:49 +00:00
|
|
|
|
2014-10-17 10:47:17 +00:00
|
|
|
@nif_blueprint.route('/', methods=['POST', 'GET'])
|
2014-11-20 18:29:49 +00:00
|
|
|
def home():
|
2014-07-13 13:36:47 +00:00
|
|
|
try:
|
2015-02-23 01:13:31 +00:00
|
|
|
params = get_params(request)
|
|
|
|
algo = params.get("algorithm", None)
|
2014-11-04 20:31:41 +00:00
|
|
|
specific_params = current_app.senpy.parameters(algo)
|
2015-02-24 06:15:25 +00:00
|
|
|
logger.debug(
|
|
|
|
"Specific params: %s", json.dumps(specific_params, indent=4))
|
2015-02-23 01:13:31 +00:00
|
|
|
params.update(get_params(request, specific_params))
|
2014-11-07 18:12:21 +00:00
|
|
|
response = current_app.senpy.analyse(**params)
|
2015-02-24 06:15:25 +00:00
|
|
|
in_headers = params["inHeaders"] != "0"
|
|
|
|
return response.flask(in_headers=in_headers)
|
2014-07-13 13:36:47 +00:00
|
|
|
except ValueError as ex:
|
2015-02-24 06:15:25 +00:00
|
|
|
return ex.message.flask()
|
2014-07-13 13:36:47 +00:00
|
|
|
|
2014-11-20 18:29:49 +00:00
|
|
|
|
2014-11-04 20:31:41 +00:00
|
|
|
@nif_blueprint.route("/default")
|
|
|
|
def default():
|
2015-02-24 06:15:25 +00:00
|
|
|
# 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()
|
2014-11-04 20:31:41 +00:00
|
|
|
|
2014-11-20 18:29:49 +00:00
|
|
|
|
2014-10-17 10:47:17 +00:00
|
|
|
@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"):
|
2014-11-04 20:31:41 +00:00
|
|
|
filt = {}
|
2014-12-01 17:27:20 +00:00
|
|
|
sp = current_app.senpy
|
2014-10-17 10:47:17 +00:00
|
|
|
if plugin:
|
2014-11-04 20:31:41 +00:00
|
|
|
filt["name"] = plugin
|
2014-12-01 17:27:20 +00:00
|
|
|
plugs = sp.filter_plugins(**filt)
|
2014-11-04 20:31:41 +00:00
|
|
|
if plugin and not plugs:
|
|
|
|
return "Plugin not found", 400
|
2014-10-17 10:47:17 +00:00
|
|
|
if action == "list":
|
2015-02-24 06:15:25 +00:00
|
|
|
with_params = get_params(request, LIST_PARAMS)["params"] == "1"
|
|
|
|
in_headers = get_params(request, BASIC_PARAMS)["inHeaders"] != "0"
|
2014-12-01 17:27:20 +00:00
|
|
|
if plugin:
|
2015-02-24 06:15:25 +00:00
|
|
|
dic = plugs[plugin]
|
2014-12-01 17:27:20 +00:00
|
|
|
else:
|
2015-02-24 06:15:25 +00:00
|
|
|
dic = Response(
|
|
|
|
{plug: plugs[plug].jsonld(with_params) for plug in plugs},
|
|
|
|
frame={})
|
|
|
|
return dic.flask(in_headers=in_headers)
|
2014-12-01 17:27:20 +00:00
|
|
|
method = "{}_plugin".format(action)
|
|
|
|
if(hasattr(sp, method)):
|
|
|
|
getattr(sp, method)(plugin)
|
2014-10-17 10:47:17 +00:00
|
|
|
return "Ok"
|
|
|
|
else:
|
2014-11-04 20:31:41 +00:00
|
|
|
return "action '{}' not allowed".format(action), 400
|
2014-10-17 10:47:17 +00:00
|
|
|
|
2014-11-20 18:29:49 +00:00
|
|
|
|
2014-07-13 13:36:47 +00:00
|
|
|
if __name__ == '__main__':
|
2014-10-17 10:47:17 +00:00
|
|
|
import config
|
2014-07-13 13:36:47 +00:00
|
|
|
from flask import Flask
|
2014-11-20 18:29:49 +00:00
|
|
|
|
2014-07-13 13:36:47 +00:00
|
|
|
app = Flask(__name__)
|
2014-10-17 10:47:17 +00:00
|
|
|
app.register_blueprint(nif_blueprint)
|
2014-07-13 13:36:47 +00:00
|
|
|
app.debug = config.DEBUG
|
|
|
|
app.run()
|