Googel knowledge graph API

Freebase

本想用freebase的API,结果打开发现google已经将freebase的API关闭了:On 16 December 2014, Knowledge Graph announced that it would shut down Freebase over the succeeding six months and help with the move of the data from Freebase to Wikidata.On 16 December 2015, Google officially announced the Knowledge Graph API, which is meant to be a replacement to the Freebase API. Freebase.com was officially shut down on 2 May 2016.,现在只留下Freebase Triples可供下载,网址:https://developers.google.com/freebase/
Googel knowledge graph API

google knowledge graph

于是打开google knowledge graph 。网址:https://developers.google.com/knowledge-graph/

Sample request

开始是一个Sample request:https://kgsearch.googleapis.com/v1/entities:search?query=taylor+swift&key=API_KEY&limit=1&indent=True, 图中的查询语句直接填入搜索引擎的网址栏即可查询,但是需要申请一个google API key,申请好之后替换图中的API key,填入搜索引擎的网址栏,返回的是一个格式为JSON-LD的结构化数据。
Googel knowledge graph APIGoogel knowledge graph API

Python

官网上sample request接下来是各种语言的示例,我选择了python,下面是示例代码

"""Example of Python client calling Knowledge Graph Search API."""
import json
import urllib

api_key = open('.api_key').read()
query = 'Taylor Swift'
service_url = 'https://kgsearch.googleapis.com/v1/entities:search'
params = {
    'query': query,
    'limit': 10,
    'indent': True,
    'key': api_key,
}
url = service_url + '?' + urllib.urlencode(params)
response = json.loads(urllib.urlopen(url).read())
for element in response['itemListElement']:
  print element['result']['name'] + ' (' + str(element['resultScore']) + ')'`

api_key = open(’.api_key’).read()意为打开一个文件,这个文件里面是你的api key,此时只需改为api_key = “你申请到的api key”,在jupyter(python3.6)下运行报了module ‘urllib’ has no attribute 'urlencode’的错,是因为python2和python3的不同的问题(python0基础猜的)。将

url = service_url + '?' + urllib.urlencode(params)
response = json.loads(urllib.urlopen(url).read())

改为

url = service_url + '?'+urllib.parse.urlencode(params)
response = json.loads(urllib.request.urlopen(url).read())

即可得到下图的查询结果,代码就是用查询生成一个网址,再解析返回的jsonld格式的,后续如果要做一个应用,可能要学习一下jsonld格式的解析。但是有时候由于网络的错误所以会产生一些连接上的错误。这里还有一个差不多的例子:https://medium.com/@benjburkholder/python-google-knowledge-graph-api-for-seo-c85c68d2fadd 可以借鉴一下。
Googel knowledge graph API

google knowledge graph api的局限:

Googel knowledge graph API
即:知识图搜索API仅返回单个匹配实体,而不是互连实体的图。 如果您需要后者,我们建议您使用维基数据中的数据转储。

Wikidata

这里有一篇wikidata的详解http://www.doc88.com/p-7018677933942.html,可以阅读一下。

如何访问Wikidata的数据

Linked Data interface

其实这一种方法和google knowledge graph的sample request差不多,都是用URL来访问

For example, the concept URI of Douglas Adams is http://www.wikidata.org/entity/Q42. Note that this URI refers to the real-world person, not Wikidata’s description of Douglas Adams.

意为用这种方法来访问并不是连接到*的某个词条,而是一个单独的网页。这个不需要自己的apikey。感觉比较方便。并且这时返回的是一个结构化的网页,阅读性较好。如下图:Googel knowledge graph API

For cases in which it is inconvenient to use content negotiation (e.g. to view non-HTML content in a web browser), you can also access data about an entity in a specific format by extending the data URL with an extension suffix to indicate the content format that you want, such as .json, .rdf, .ttl or .nt.

并且可以直接返回各种格式的数据,如:在网址栏输入https://www.wikidata.org/wiki/Special:EntityData/Q42.rdf, 可以直接下载该实体的rdf压缩包,压缩包里面包含各个国家地区文字的rdf,截取了部分中文介绍(lang=“zh-cn”):

<schema:name xml:lang="zh-cn">道格拉斯·亚当斯</schema:name>
<schema:name xml:lang="zh-cn">人类</schema:name>
<schema:description xml:lang="zh-cn">英国作家</schema:description>
<rdfs:label xml:lang="zh-cn">男性</rdfs:label>
<rdfs:label xml:lang="zh-cn">剧作家</rdfs:label>

发现该条目rdf里面不仅有对该条目的介绍,还有对条目下各个条目的介绍(物理位置不在一起,只是截取):

<schema:name xml:lang="zh-cn">人类</schema:name>
<schema:description xml:lang="zh-cn">灵长目人科人属的物种</schema:description>
<schema:name xml:lang="zh-cn">墓地</schema:name>
<schema:description xml:lang="zh-cn">一个人的坟墓、埋葬地、骨灰撒放地点,等 (例:城市或坟场)。</schema:description>
<schema:name xml:lang="zh-cn">男性</schema:name>
<schema:description xml:lang="zh-cn">人类性别,只用于属性P21</schema:description>
<schema:name xml:lang="zh-cn">母语人名</schema:name>
<schema:description xml:lang="zh-cn">某人在母语中的名称</schema:description>

输入https://www.wikidata.org/wiki/Special:EntityData/Q42.json, 可以在网页中直接得到json格式的数据。

SPARQL endpoints

下次再补

https://www.wikidata.org/wiki/Special:MyLanguage/Wikidata:Tools/External_tools