How to get Dbpedia Spotlight annotations and candidates with filter sparql in python?

眉间皱痕 提交于 2021-02-05 09:36:04

问题


I have this sql in Sparql.

SELECT DISTINCT ?issue WHERE {
VALUES ?issue
    {
       <http://dbpedia.org/resource/Deforestation>
       <http://dbpedia.org/resource/Pollution>
       <http://dbpedia.org/resource/Wast>
    }
 ?issue ?a ?b}

I encode the query on this website https://urldecode.org/ and it looks like this

%20%20%20%20SELECT%20DISTINCT%20%3Fissue%20WHERE%20%7B%20%20%20%20%20VALUES%20%3Fissue%20%20%20%20%20%20%20%20%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%3Chttp%3A%2F%2Fdbpedia.org%2Fresource%2FDeforestation%3E%20%20%20%20%20%20%20%20%20%20%20%20%3Chttp%3A%2F%2Fdbpedia.org%2Fresource%2FPollution%3E%20%20%20%20%20%20%20%20%20%20%20%20%3Chttp%3A%2F%2Fdbpedia.org%2Fresource%2FWast%3E%20%20%20%20%20%20%20%20%20%7D%20%20%20%20%20%20%3Fissue%20%3Fa%20%3Fb%7D

I want to use dbpedia Spotlight with annotate and candidates

http://api.dbpedia-spotlight.org/en/annotate?text=....?sparql

http://api.dbpedia-spotlight.org/en/candidates?text=...?sparql

import json
from SPARQLWrapper import SPARQLWrapper, JSON
import requests
import urllib.parse

## initial consts
BASE_URL = 'http://api.dbpedia-spotlight.org/en/annotate?text={text}&confidence={confidence}&support={support}&sparql={SPARQL}'

TEXT = 'uviría first used the pen name Hugo Wast for his 1911 novel, Flor de Durazno (Peach Blossom) - his first commercial'
CONFIDENCE = '0.5'
SUPPORT = '10'
SPARQL = '%20%20%20%20SELECT%20DISTINCT%20%3Fissue%20WHERE%20%7B%20%20%20%20%20VALUES%20%3Fissue%20%20%20%20%20%20`%20%20%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%3Chttp%3A%2F%2Fdbpedia.org%2Fresource%2FDeforestation%3E%20%20%20%20%20%20%20%20%20%20%20%20%3Chttp%3A%2F%2Fdbpedia.org%2Fresource%2FPollution%3E%20%20%20%20%20%20%20%20%20%20%20%20%3Chttp%3A%2F%2Fdbpedia.org%2Fresource%2FWast%3E%20%20%20%20%20%20%20%20%20%7D%20%20%20%20%20%20%3Fissue%20%3Fa%20%3Fb%7D'`
REQUEST = BASE_URL.format(
    text=urllib.parse.quote_plus(TEXT), 
    confidence=CONFIDENCE, 
    support=SUPPORT,
    sparql = SPARQL
HEADERS = {'Accept': 'application/json'}
#sparql = SPARQLWrapper("http://dbpedia.org/sparql")
all_urls = []

r = requests.get(url=REQUEST, headers=HEADERS)
if r.status_code == requests.codes['ok']:
        response = r.json()
        resources = response.get('Resources', [])
        for res in resources:
            all_urls.append(res['@URI'])

How can I make it come out the same as this example and save it to a file? Paste in url.

http://api.dbpedia-spotlight.org/en/annotate?text=Deforestation is a big problem, pollution smells.&confidence=0.2&support=20&sparql=SELECT%20DISTINCT%20%3Fissue%20WHERE%20%7B%20%20VALUES%20%3Fissue%20%20%20%20%20%7B%20%20%20%20%20%20%20%20%3Chttp%3A%2F%2Fdbpedia.org%2Fresource%2FDeforestation%3E%20%20%20%20%20%20%20%20%3Chttp%3A%2F%2Fdbpedia.org%2Fresource%2FPollution%3E%20%20%20%20%20%7D%20%20%20%3Fissue%20%3Fa%20%3Fb%7D%20 

Ouput example annotate: Deforestation is a big problem, pollution smells.

http://api.dbpedia-spotlight.org/en/candidates?text=Deforestation is a big problem, pollution smells.&confidence=0.2&support=20&sparql=SELECT%20DISTINCT%20%3Fissue%20WHERE%20%7B%20%20VALUES%20%3Fissue%20%20%20%20%20%7B%20%20%20%20%20%20%20%20%3Chttp%3A%2F%2Fdbpedia.org%2Fresource%2FDeforestation%3E%20%20%20%20%20%20%20%20%3Chttp%3A%2F%2Fdbpedia.org%2Fresource%2FPollution%3E%20%20%20%20%20%7D%20%20%20%3Fissue%20%3Fa%20%3Fb%7D%20

Ouput example candidate:

<annotation text="Deforestation is a big problem, pollution smells.">
<surfaceForm name="Deforestation" offset="0">
<resource label="Deforestation" uri="Deforestation" contextualScore="0.17739563221027313" percentageOfSecondRank="0.001283655804029094" support="3031" priorScore="1.5419102159467388E-5" finalScore="0.9987119466194989" types=""/>
</surfaceForm>
<surfaceForm name="pollution" offset="32">
<resource label="Pollution" uri="Pollution" contextualScore="0.3303241788728437" percentageOfSecondRank="0.0435287048726528" support="3817" priorScore="1.9417589225564835E-5" finalScore="0.9554673152457217" types=""/>
</surfaceForm>
</annotation>

来源:https://stackoverflow.com/questions/61758873/how-to-get-dbpedia-spotlight-annotations-and-candidates-with-filter-sparql-in-py

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!