5

我目前正在使用Wikidata 查询服务来运行我的 Wikidata 查询。

例如,我的一个 Wikidata 查询如下所示。

SELECT ?sLabel {
    SERVICE wikibase:mwapi {
        bd:serviceParam wikibase:api "EntitySearch".
        bd:serviceParam wikibase:endpoint "www.wikidata.org".
        bd:serviceParam mwapi:search "natural language processing".
        bd:serviceParam mwapi:language "en".
        ?item wikibase:apiOutputItem mwapi:item.
        ?num wikibase:apiOrdinal true.
    }
    ?s wdt:P279|wdt:P31 ?item .
    SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
}
ORDER BY ?num
LIMIT 10

我想知道我们是否可以在 python 程序中使用这些查询?如果是这样,我们如何在 python 中集成查询?

如果需要,我很乐意提供更多详细信息。

4

3 回答 3

8

如果您想在没有 SPARQL 特定库的情况下执行此操作:

import requests

url = 'https://query.wikidata.org/sparql'
query = '''
SELECT ?item ?itemLabel ?linkcount WHERE {
    ?item wdt:P31/wdt:P279* wd:Q35666 .
    ?item wikibase:sitelinks ?linkcount .
FILTER (?linkcount >= 1) .
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" . }
}
GROUP BY ?item ?itemLabel ?linkcount
ORDER BY DESC(?linkcount)
'''
r = requests.get(url, params = {'format': 'json', 'query': query})
data = r.json()
于 2020-11-18T19:11:17.663 回答
5

sparqlwrapper可以处理。你可以在这里找到更多信息

于 2019-05-03T00:04:26.607 回答
-2

作为http://wiki.bitplan.com/index.php/PyLoDStorage的提交者,我推荐这个库,因为它包装了 sparqlwrapper,您可以立即获得正确的 python 类型。因此,您将能够将数据获取到其他库中,例如 pandas 或使用 csv、json、xml、sql 或任何您的后续需求。

我已经修改并扩展了您的查询

# WF 2021-10-30
# see https://stackoverflow.com/questions/55961615/how-to-integrate-wikidata-query-in-python/69771615#69771615
SELECT ?s ?sLabel ?item ?itemLabel ?sourceCode ?webSite ?stackexchangeTag  {
    SERVICE wikibase:mwapi {
        bd:serviceParam wikibase:api "EntitySearch".
        bd:serviceParam wikibase:endpoint "www.wikidata.org".
        bd:serviceParam mwapi:search "natural language processing".
        bd:serviceParam mwapi:language "en".
        ?item wikibase:apiOutputItem mwapi:item.
        ?num wikibase:apiOrdinal true.
    }
    ?s wdt:P279|wdt:P31 ?item .
    OPTIONAL { 
      ?s wdt:P1324 ?sourceCode.
    }
    OPTIONAL {    
      ?s wdt:P856 ?webSite.
    }
    OPTIONAL {    
      ?s wdt:P1482 ?stackexchangeTag.
    }
    SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
}
ORDER BY ?itemLabel ?sLabel

试试看!

然后将其用作 pyLoDStorage 库的展示:

def testStackoverflow55961615Query(self):
        '''
        see 
        https://stackoverflow.com/questions/55961615/how-to-integrate-wikidata-query-in-python
        https://stackoverflow.com/a/69771615/1497139
        '''
        endpoint="https://query.wikidata.org/sparql"
        wd=SPARQL(endpoint)
        queryString="""SELECT ?s ?sLabel ?item ?itemLabel ?sourceCode ?webSite ?stackexchangeTag  {
    SERVICE wikibase:mwapi {
        bd:serviceParam wikibase:api "EntitySearch".
        bd:serviceParam wikibase:endpoint "www.wikidata.org".
        bd:serviceParam mwapi:search "natural language processing".
        bd:serviceParam mwapi:language "en".
        ?item wikibase:apiOutputItem mwapi:item.
        ?num wikibase:apiOrdinal true.
    }
    ?s wdt:P279|wdt:P31 ?item .
    OPTIONAL { 
      ?s wdt:P1324 ?sourceCode.
    }
    OPTIONAL {    
      ?s wdt:P856 ?webSite.
    }
    OPTIONAL {    
      ?s wdt:P1482 ?stackexchangeTag.
    }
    SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
}
ORDER BY ?itemLabel ?sLabel"""
        qlod=wd.queryAsListOfDicts(queryString,fixNone=True)
        query=Query(name="EntitySearch",query=queryString,lang='sparql')
        debug=self.debug
        for tablefmt in ["github","mediawiki","latex"]:
            lod=copy.deepcopy(qlod)
            qdoc=query.documentQueryResult(lod,tablefmt=tablefmt)
            if debug:
                print (qdoc)

输出可以立即粘贴到此处,如下所示。输出也可以以表格库支持的任何格式提供,例如 mediawiki、latex 等。

实体搜索

询问

SELECT ?s ?sLabel ?item ?itemLabel ?sourceCode ?webSite ?stackexchangeTag  {
    SERVICE wikibase:mwapi {
        bd:serviceParam wikibase:api "EntitySearch".
        bd:serviceParam wikibase:endpoint "www.wikidata.org".
        bd:serviceParam mwapi:search "natural language processing".
        bd:serviceParam mwapi:language "en".
        ?item wikibase:apiOutputItem mwapi:item.
        ?num wikibase:apiOrdinal true.
    }
    ?s wdt:P279|wdt:P31 ?item .
    OPTIONAL { 
      ?s wdt:P1324 ?sourceCode.
    }
    OPTIONAL {    
      ?s wdt:P856 ?webSite.
    }
    OPTIONAL {    
      ?s wdt:P1482 ?stackexchangeTag.
    }
    SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
}
ORDER BY ?itemLabel ?sLabel

结果

s s标签 物品 物品标签 stackexchange标签 源代码 网站
http://www.wikidata.org/entity/Q24841819 Q24841819 http://www.wikidata.org/entity/Q30642 自然语言处理
http://www.wikidata.org/entity/Q1898737 形态分析 http://www.wikidata.org/entity/Q30642 自然语言处理
http://www.wikidata.org/entity/Q6913444 形态解析 http://www.wikidata.org/entity/Q30642 自然语言处理
http://www.wikidata.org/entity/Q104415642 维基化 http://www.wikidata.org/entity/Q30642 自然语言处理
http://www.wikidata.org/entity/Q51751772 生物医学自然语言处理 http://www.wikidata.org/entity/Q30642 自然语言处理
http://www.wikidata.org/entity/Q46346005 基于计算机的问题分类 http://www.wikidata.org/entity/Q30642 自然语言处理
http://www.wikidata.org/entity/Q1513879 自然语言生成 http://www.wikidata.org/entity/Q30642 自然语言处理 https://stackoverflow.com/tags/nlg
http://www.wikidata.org/entity/Q1078276 自然语言理解 http://www.wikidata.org/entity/Q30642 自然语言处理
http://www.wikidata.org/entity/Q1271424 词性标注 http://www.wikidata.org/entity/Q30642 自然语言处理
http://www.wikidata.org/entity/Q105171570 说话人验证 http://www.wikidata.org/entity/Q30642 自然语言处理
http://www.wikidata.org/entity/Q189436 语音识别 http://www.wikidata.org/entity/Q30642 自然语言处理 https://ai.stackexchange.com/tags/speech-recognition
http://www.wikidata.org/entity/Q189436 语音识别 http://www.wikidata.org/entity/Q30642 自然语言处理 https://cs.stackexchange.com/tags/speech-recognition
http://www.wikidata.org/entity/Q189436 语音识别 http://www.wikidata.org/entity/Q30642 自然语言处理 https://dsp.stackexchange.com/tags/speech-recognition
http://www.wikidata.org/entity/Q189436 语音识别 http://www.wikidata.org/entity/Q30642 自然语言处理 https://linguistics.stackexchange.com/tags/speech-recognition
http://www.wikidata.org/entity/Q189436 语音识别 http://www.wikidata.org/entity/Q30642 自然语言处理 https://stackoverflow.com/tags/speech-recognition
http://www.wikidata.org/entity/Q189436 语音识别 http://www.wikidata.org/entity/Q30642 自然语言处理 https://unix.stackexchange.com/tags/speech-recognition
http://www.wikidata.org/entity/Q1948408 文本分割 http://www.wikidata.org/entity/Q30642 自然语言处理
http://www.wikidata.org/entity/Q3484781 文字简化 http://www.wikidata.org/entity/Q30642 自然语言处理
http://www.wikidata.org/entity/Q2438971 标记化 http://www.wikidata.org/entity/Q30642 自然语言处理
http://www.wikidata.org/entity/Q105330879 分词 http://www.wikidata.org/entity/Q30642 自然语言处理
http://www.wikidata.org/entity/Q7095836 Apache OpenNLP http://www.wikidata.org/entity/Q21129801 自然语言处理工具包 https://stackoverflow.com/tags/opennlp http://svn.apache.org/repos/asf/opennlp http://opennlp.apache.org
http://www.wikidata.org/entity/Q7095836 Apache OpenNLP http://www.wikidata.org/entity/Q21129801 自然语言处理工具包 https://stackoverflow.com/tags/opennlp https://github.com/apache/opennlp http://opennlp.apache.org
http://www.wikidata.org/entity/Q7095836 Apache OpenNLP http://www.wikidata.org/entity/Q21129801 自然语言处理工具包 https://stackoverflow.com/tags/opennlp https://github.com/apache/opennlp.git http://opennlp.apache.org
http://www.wikidata.org/entity/Q7095836 Apache OpenNLP http://www.wikidata.org/entity/Q21129801 自然语言处理工具包 https://stackoverflow.com/tags/opennlp http://svn.apache.org/repos/asf/opennlp https://opennlp.apache.org/
http://www.wikidata.org/entity/Q7095836 Apache OpenNLP http://www.wikidata.org/entity/Q21129801 自然语言处理工具包 https://stackoverflow.com/tags/opennlp https://github.com/apache/opennlp https://opennlp.apache.org/
http://www.wikidata.org/entity/Q7095836 Apache OpenNLP http://www.wikidata.org/entity/Q21129801 自然语言处理工具包 https://stackoverflow.com/tags/opennlp https://github.com/apache/opennlp.git https://opennlp.apache.org/
http://www.wikidata.org/entity/Q975453 文本工程的通用架构 http://www.wikidata.org/entity/Q21129801 自然语言处理工具包 https://github.com/GateNLP http://gate.ac.uk
http://www.wikidata.org/entity/Q5533567 根西姆 http://www.wikidata.org/entity/Q21129801 自然语言处理工具包 https://github.com/RaRe-Technologies/gensim https://radimrehurek.com/gensim/
http://www.wikidata.org/entity/Q48688669 灵管 http://www.wikidata.org/entity/Q21129801 自然语言处理工具包 https://stackoverflow.com/tags/lingpipe http://alias-i.com/lingpipe/
http://www.wikidata.org/entity/Q6553971 语言流 http://www.wikidata.org/entity/Q21129801 自然语言处理工具包
http://www.wikidata.org/entity/Q5396532 http://www.wikidata.org/entity/Q21129801 自然语言处理工具包 http://mallet.cs.umass.edu/
http://www.wikidata.org/entity/Q6906675 蒙蒂语言 http://www.wikidata.org/entity/Q21129801 自然语言处理工具包 http://web.media.mit.edu/~hugo/montylingua/
http://www.wikidata.org/entity/Q3630063 摩西 http://www.wikidata.org/entity/Q21129801 自然语言处理工具包 https://github.com/moses-smt/mosesdecoder http://www.statmt.org/moses
http://www.wikidata.org/entity/Q1635410 自然语言工具包 http://www.wikidata.org/entity/Q21129801 自然语言处理工具包 https://stackoverflow.com/tags/nltk https://github.com/nltk/nltk http://nltk.org/
http://www.wikidata.org/entity/Q17071870 牛运 http://www.wikidata.org/entity/Q21129801 自然语言处理工具包 http://www.nlplab.com/NiuPlan/NiuTrans.html
http://www.wikidata.org/entity/Q47405152 图案 http://www.wikidata.org/entity/Q21129801 自然语言处理工具包 https://github.com/clips/pattern http://www.clips.ua.ac.be/pages/pattern
http://www.wikidata.org/entity/Q32998961 斯坦福 CoreNLP http://www.wikidata.org/entity/Q21129801 自然语言处理工具包 https://stanfordnlp.github.io/CoreNLP/
http://www.wikidata.org/entity/Q104840874 http://www.wikidata.org/entity/Q21129801 自然语言处理工具包 https://stanfordnlp.github.io/stanza/ https://stanfordnlp.github.io/stanza/
http://www.wikidata.org/entity/Q2593410 文字史密斯 http://www.wikidata.org/entity/Q21129801 自然语言处理工具包 http://www.lexically.net
test testStackoverflow55961615Query, debug=False took   0.5 s
----------------------------------------------------------------------
Ran 1 test in 0.503s

OK
于 2021-10-29T15:46:12.613 回答