python OpenRefine / Jython sparql查询(查找令牌中可能的位置和人员)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python OpenRefine / Jython sparql查询(查找令牌中可能的位置和人员)相关的知识,希望对你有一定的参考价值。

import sys
sys.path.append(r'D:\jython2.7.0\Lib\site-packages')
from SPARQLWrapper import SPARQLWrapper, JSON
from langdetect import detect

dbpedia_version = "http://dbpedia.org/sparql"

#TEST
value = "comptoir"

#detect language (useless with short tokens)
lang_query = detect(value)

if lang_query == "fr":
    dbpedia = "http://fr.dbpedia.org/sparql"
elif lang_query == "nl":
    dbpedia = "http://nl.dbpedia.org/sparql"
else:
    dbpedia = "http://dbpedia.org/sparql"


def get_sparql_label(value, dbpedia_version):
    dbpedia_version = dbpedia
    sparql = SPARQLWrapper(dbpedia_version)
    sparql.setQuery("""
SELECT DISTINCT ?entity ?score1 ?type
    WHERE{
        ?entity ?p ?label.
                ?entity ?q ?abstract.
                Filter langMatches(lang(?label),"%s").
                Filter langMatches(lang(?abstract),"%s").
        ?label <bif:contains> "'%s'" OPTION(score ?score1).
        FILTER (?p=<http://www.w3.org/2000/01/rdf-schema#label> ||
                ?p=<http://www.w3.org/2004/02/skos/core#prefLabel>).
                FILTER (?q=<http://dbpedia.org/ontology/abstract>).
        ?entity a ?type.
        FILTER (?type IN (<http://dbpedia.org/ontology/Place>,
                           <http://dbpedia.org/ontology/Agent>)).
        FILTER isIRI(?entity).
    } ORDER BY desc(?score1) LIMIT 5
""" % (lang_query, lang_query, value))

    sparql.setReturnFormat(JSON)
    results=sparql.query().convert()
    return results

results=get_sparql_label(value, dbpedia)
liste=[]
for result in results["results"]["bindings"]:
    liste.append(result["type"]["value"] + "||" + result["entity"]["value"])


if not liste:
    dbpedia_version="http://nl.dbpedia.org/sparql"
    lang_query = "NL"
    results=get_sparql_label(value, dbpedia_version)
    for result in results["results"]["bindings"]:
        liste.append(result["type"]["value"] +
                     "||" + result["entity"]["value"])

print(liste)

以上是关于python OpenRefine / Jython sparql查询(查找令牌中可能的位置和人员)的主要内容,如果未能解决你的问题,请参考以下文章

python OpenRefine / Jython sparql查询(查找令牌中可能的位置和人员)

python Jython天真的方法,用于根据名字列表检测OpenRefine中的潜在人名

python 基于gazeeter在OpenRefine中检测比利时城市名称的Jython天真方法

python 计算在OpenRefine中与维基数据重新调整的术语及其候选者之间的Levensthein编辑距离的方法(https:// gi

openrefine优缺点

OpenRefine:用增加的计数器填充