假设正确的SPARQL查询Wikidata在Python中未产生任何结果

问题描述

编辑:当我使用...

print("Result",result)

...我得到以下输出

Result <sparql._ResultsParser object at 0x7f05adbc9668>

...但是我不知道这是否表示格式不正确。


编辑2 :在对Wikidata提出了另一个请求之后,并且由于该线程中的注释,我得出结论,为每个关系查询Wikidata都是不可行的。因此,我最终下载了所有属性及其英文标签,描述和altLabel的列表,然后执行“离线”搜索。如果需要,反向索引将进一步提高性能。 Wikidata中的属性数量相对较少。这是您可以在官方SPARQL API中运行的查询,以查看结果如下:

SELECT ?property ?propertyLabel ?propertyDescription (GROUP_CONCAT(disTINCT(?altLabel); separator = ",") AS ?altLabel_list) WHERE {
    ?property a wikibase:Property .
    OPTIONAL { ?property skos:altLabel ?altLabel . FILTER (lang(?altLabel) = "en") }
    SERVICE wikibase:label { bd:serviceParam wikibase:language "en" .}
 }
GROUP BY ?property ?propertyLabel ?propertyDescription

这是我的Python程序内部的样子,包括解析以满足我的需要。我知道查询中的大多数前缀都是不必要的,但是它们也不会受到损害:

from SPARQLWrapper import SPARQLWrapper,JSON
from datetime import datetime

File_object = open(r"/home/YOUR_NAME/PycharmProjects/Proj/data_files/wikidata_relation_labels.txt","r+")

# https://stackoverflow.com/questions/30755625/urlerror-with-sparqlwrapper-at-sparql-query-convert
sparql = SPARQLWrapper("https://query.wikidata.org/sparql",agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 "
                                                                  "(KHTML,like Gecko) Chrome/23.0.1271.64 "
                                                                  "Safari/537.11")
sparql.setQuery("""PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wds: <http://www.wikidata.org/entity/statement/>
PREFIX wdv: <http://www.wikidata.org/value/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX bd: <http://www.bigdata.com/rdf#>
PREFIX skos:  <http://www.w3.org/2004/02/skos/core#>
PREFIX rdf:   <http://www.w3.org/1999/02/22-rdf-Syntax-ns#>
SELECT ?property ?propertyLabel ?propertyDescription (GROUP_CONCAT(disTINCT(?altLabel); separator = ",") AS ?altLabel_list) WHERE {
    ?property a wikibase:Property .
    OPTIONAL { ?property skos:altLabel ?altLabel . FILTER (lang(?altLabel) = "en") }
    SERVICE wikibase:label { bd:serviceParam wikibase:language "en" .}
 }
GROUP BY ?property ?propertyLabel ?propertyDescription
""")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
dateTimeObj = datetime.Now()
print("timestamp: ",print(dateTimeObj))
for result in results["results"]["bindings"]:
    p_id = p_label = p_description = p_alt_labels = ""
    if result["property"]["value"]:
        p_id = result["property"]["value"].rsplit('/',1)[1]
    if result["propertyLabel"]["value"]:
        p_label = result['propertyLabel']['value']
    # why all these "if"s? Because some properties have no description.
    if "propertyDescription" in result:
        if result["propertyDescription"]["value"]:
            p_description = result['propertyDescription']['value']
    if result["altLabel_list"]["value"]:
        p_alt_labels = result["altLabel_list"]["value"]
    File_object.write(p_id + " | " + p_label + " | " + p_description + " | " + p_alt_labels + "\n")

# simple way to check if Wikidata decided to include a pipe somewhere
for line in File_object:
    if line.count('|') > 4:
        print("Too many pipes: ",line)

lines = File_object.readlines()
lines.sort()

# Todo: sort through terminal: 'sort wikidata_relation_labels.txt -o wikidata_relation_labels.txt'

File_object.close()

我正在使用管道作为分隔符。在许多情况下,这可能被视为不良做法。


我正在尝试获取所有Wikidata属性的ID和标签,其中该属性标签或其“也称为”(替代)标签之一等于/包含给定的字符串(relation.label)。

我在Python 3.x中使用this SPARQL client/API(描述有些矛盾)。

这是我的代码段:

import sparql

endpoint = 'https://query.wikidata.org/sparql'

def is_simple_relation(relation):
    s = sparql.Service(endpoint,"utf-8","GET")
    q = """SELECT disTINCT ?property ?propertyLabel WHERE {
         ?property rdf:type wikibase:Property;
         rdfs:label ?propertyLabel;
         skos:altLabel ?altLabel.
         FILTER(LANG(?propertyLabel) = "[AUTO_LANGUAGE]").
         FILTER(CONTAINS(?propertyLabel,"replace_me") || CONTAINS(?altLabel,"replace_me")).
         }
         LIMIT 100"""
    q = q.replace('replace_me',relation.label)
    print("Query: ",q)
    print("Querying")
    result = sparql.query(endpoint,q)
    print("Finished query")
    for row in result.fetchone():
        print("row: ",row)

我的输出是:

Query:  SELECT disTINCT ?property ?propertyLabel WHERE {
         ?property rdf:type wikibase:Property;
         rdfs:label ?propertyLabel;
         skos:altLabel ?altLabel.
         FILTER(LANG(?propertyLabel) = "[AUTO_LANGUAGE]").
         FILTER(CONTAINS(?propertyLabel,"has effect") || CONTAINS(?altLabel,"has effect")).
         }
         LIMIT 100
Querying
Finished query

这意味着,我没有检索任何东西。 我尝试执行查询here,它按预期方式工作,因此查询很好。 我尝试在程序内执行示例查询之一,它可以按预期工作,并按预期打印多行。

我能想到的唯一可能的原因是,从我的程序执行时,查询花费的时间要长得多,而达到了超时,而查询是通过第二个链接及时进行评估的。但是我没有得到任何警告。我的假设正确吗?如果可以,我的查询可以改善吗?可能是我不了解的性能杀手。

谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...