From 02739ece39797c90c77c64a11df8b1b6c83ca27e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20Fernando=20S=C3=A1nchez?= Date: Sun, 13 Jul 2014 16:28:01 +0200 Subject: [PATCH] Better indexing, added README --- README.md | 19 +++++++++++++++++++ app.py | 15 ++++++++++++++- nif_server.py | 10 +++++++--- 3 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..90af278 --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +![GSI Logo](http://gsi.dit.upm.es/templates/jgsi/images/logo.png) +[EUROSENTIMETN Endpoint example](http://eurosentiment-resources.herokuapp.com) +========================================= +Example endpoint that yields results compatible with the EUROSENTIMENT format and exposes the NIF API. +It can be used as a template to adapt existing services to EUROSENTIMENT or to create new services. + +[DEMO on Heroku](http://eurosentiment-endpoint.herokuapp.com) + +This endpoint serves as bootcampt for any developer wishing to build applications that use the EUROSENTIMENT services. + +Acknowledgement +--------------- +EUROSENTIMENT PROJECT +Grant Agreement no: 296277 +Starting date: 01/09/2012 +Project duration: 24 months + +![Eurosentiment Logo](logo_grande.png) +![FP7 logo](logo_fp7.gif) diff --git a/app.py b/app.py index dd487c5..78e3947 100644 --- a/app.py +++ b/app.py @@ -19,17 +19,21 @@ Simple Sentiment Analysis server for EUROSENTIMENT This class shows how to use the nif_server module to create custom services. ''' +import config +import re from flask import Flask from random import random from nif_server import * -import config app = Flask(__name__) +rgx = re.compile("(\w[\w']*\w|\w)", re.U) + def hard_analysis(params): response = basic_analysis(params) response["analysis"][0]["marl:algorithm"] = "SimpleAlgorithm" for i in response["entries"]: + contextid = i["@id"] polValue = random() if polValue > 0.5: pol = "marl:Positive" @@ -41,6 +45,15 @@ def hard_analysis(params): "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 diff --git a/nif_server.py b/nif_server.py index a6266ec..af9c5eb 100644 --- a/nif_server.py +++ b/nif_server.py @@ -43,7 +43,6 @@ PARAMS = {"input": {"aliases": ["i", "input"], "options": ["json-ld"], }, "language": {"aliases": ["language", "l"], - "default": None, "required": False, "options": ["es", "en"], }, @@ -88,7 +87,11 @@ def get_params(req): return outdict def basic_analysis(params): - response = {"@context": "http://demos.gsi.dit.upm.es/eurosentiment/static/context.jsonld", + response = {"@context": ["http://demos.gsi.dit.upm.es/eurosentiment/static/context.jsonld", + { + "@base": "{}#".format(request.url.encode('utf-8')) + } + ], "analysis": [{ "@type": "marl:SentimentAnalysis" }], @@ -96,8 +99,9 @@ def basic_analysis(params): } if "language" in params: response["language"] = params["language"] - for sentence in params["input"].split("."): + for idx, sentence in enumerate(params["input"].split(".")): response["entries"].append({ + "@id": "Sentence{}".format(idx), "nif:isString": sentence }) return response