'''
Created on 6 Nov 2013

@author: cmarat
'''

from flask import request, jsonify
from flask.ext.login import login_required

from nltk import clean_html
import requests

from app import app

SPOTLIGHT_URL = 'http://spotlight.dbpedia.org/rest/annotate'


@app.route('/spotlight', methods=['POST'])
@login_required
def link_to_spotlight():
    # Retrieve the article from the post
    article = request.get_json()
    article_id = article['article_id']
    original_qname = "figshare_{}".format(article_id)
    
    app.logger.debug("Running DBpedia Spotlight plugin for article {}".format(article_id))
    
    # Get article description
    description = clean_html(article['description'])
    confidence = 0.2
    response = requests.post(   SPOTLIGHT_URL,
                                data={'text': description, 'confidence': confidence},
                                headers={'Accept': 'application/json'})
    resources = response.json().get('Resources', [])

    matches = {}

    for resource in resources:
        dbpedia_uri = resource['@URI']
        score = 'score: {}'.format(resource['@similarityScore'][:4])
        types = " ,".join(resource['@types'].split(',')[:5])
        if types == '':
            types = None

        # offset = int(resource['@offset'])
        # fragment = "...{}...".format(description[offset-30 : offset+30])

        match = {'type': "reference",
                 'uri': dbpedia_uri,
                 'web': dbpedia_uri,
                 'show': dbpedia_uri,
                 'extra': types,
                 'subscript': score,
                 # 'description': fragment,
                 'original': original_qname}
        
            # Append it to all matches
        matches[dbpedia_uri] = match

    if matches == {} :
        matches = None
    
    # Return the matches
    return jsonify({'title':'DBpedia Spotlight','urls': matches})

'''
Created on 6 Nov 2013

@author: cmarat
'''

from flask import request, jsonify
from flask.ext.login import login_required

from nltk import clean_html
import requests

from app import app

from app.util.baseplugin import plugin
from app.util.provenance import provenance

SPOTLIGHT_URL = 'http://spotlight.dbpedia.org/rest/annotate'
SPOTLIGHT_CONFIDENCE = 0.2
SIMILARITY_CUTOUT = 0.1

@app.route('/spotlight', methods=['POST'])
@login_required
@plugin(fields=[('tags','id','name'),('categories','id','name')], link='mapping')
@provenance()
def link_to_spotlight(*args, **kwargs):

    article_id = kwargs['article']['id']

    app.logger.debug("Running DBpedia Spotlight plugin for article {}".format(article_id))

    original_qname = "figshare_{}".format(article_id)

    match_items = kwargs['inputs']
    labels = ", ".join([item['label'] for item in kwargs['inputs'] + [kwargs['article']]])
    
    # Get article description, the wrapper does not provide it yet
    article = request.get_json()
    description = clean_html(article['description'])

    text = "{}\n\n{}".format(description, labels)
    # app.logger.debug("Query text:\n {}".format(text))

    response = requests.post(   SPOTLIGHT_URL,
                                data={'text': text, 'confidence': SPOTLIGHT_CONFIDENCE},
                                headers={'Accept': 'application/json'})
    resources = response.json().get('Resources', [])

    matches = {}

    for resource in resources:
        dbpedia_uri = resource['@URI']
        similarity_score = float(resource['@similarityScore'])
        if similarity_score < SIMILARITY_CUTOUT:
            continue
        score = 'score: {:.2g}'.format(similarity_score)

        types = " ,".join(resource['@types'].split(','))
        if types == '':
            types = None

        match = {'type': "reference",
                 'uri': dbpedia_uri,
                 'web': dbpedia_uri,
                 'show': dbpedia_uri,
                 'extra': types,
                 'subscript': score,
                 'original': original_qname}
        
            # Append it to all matches
        matches[dbpedia_uri] = match

    # Return the matches
    return matches

Use new decorators in spotlight plugin