0

编辑:当我使用...

print("Result", result)

...我得到这个输出:

Result <sparql._ResultsParser object at 0x7f05adbc9668>

...但我不知道这是否只是意味着格式错误。


编辑 2:在对 wikidata 的另一个请求之后,由于该线程中的评论,我得出结论,为每个关系查询 Wikidata 是不可行的。所以我最终下载了所有属性的列表及其英文标签、描述和 altLabels,并执行“离线”搜索。如果需要,倒排索引将进一步提高性能。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 中使用这个 SPARQL 客户端/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

这意味着,我没有检索任何东西。我试图在这里执行查询,它按预期工作,所以查询很好。我尝试在我的程序中执行示例查询之一,它按预期工作,按预期打印多行。

我能想到的唯一可能的原因是,当从我的程序执行时查询需要更长的时间才能达到超时,而查询是通过第二个链接及时评估的。但我没有收到警告或任何东西。我的假设正确吗?如果是这样,我的查询可以改进吗?可能有一个我不知道的性能杀手。

谢谢!

4

0 回答 0