Googleがナレッジグラフのデータを使えるAPIを公開しています。ユーザーはこのAPIを利用し、クエリを投げることでナレッジグラフのデータベースにアクセスし、データを取得することができます。このAPIは標準的なschema.orgのタイプであり、JSON -LD仕様に準拠しています。
Google Launches Knowledge Graph Search API, Promises To Close Freebase API In Future
ナレッジグラフAPIの使用例
このAPIの使用例は以下のようなものです。
- 特定の基準に沿ったエンティティのランク付けされたリストの取得
- 予測検索ボックスにエンティティを表示
- ナレッジグラフエンティティを使用した注釈/編成コンテンツの作成
詳しい情報については、下記を参照してください。
Method entities.search | Knowledge Graph Search API | Google Developers
サンプルコード
ナレッジグラフAPIのサンプルコードがありました。JSON-LDの仕様に準拠しており、これまで同様の仕様でAPIを利用したことのある方には、それほど難易度は高くないのではないでしょうか?
APIキー
https://kgsearch.googleapis.com/v1/entities:search?query=taylor+swift&key=API_KEY&limit=1&indent=True
まずAPIを使用するには、APIキーを発行する必要があります。APIキーの発行はこちらのURLからになります。
Prerequisites | Knowledge Graph Search API | Google Developers
サンプルコードはこちらになります。
{
"@context": {
"@vocab": "http://schema.org/",
"goog": "http://schema.googleapis.com/",
"resultScore": "goog:resultScore",
"detailedDescription": "goog:detailedDescription",
"EntitySearchResult": "goog:EntitySearchResult",
"kg": "http://g.co/kg"
},
"@type": "ItemList",
"itemListElement": [
{
"@type": "EntitySearchResult",
"result": {
"@id": "kg:/m/0dl567",
"name": "Taylor Swift",
"@type": [
"Thing",
"Person"
],
"description": "Singer-songwriter",
"image": {
"contentUrl": "https://t1.gstatic.com/images?q=tbn:ANd9GcQmVDAhjhWnN2OWys2ZMO3PGAhupp5tN2LwF_BJmiHgi19hf8Ku",
"url": "https://en.wikipedia.org/wiki/Taylor_Swift",
"license": "http://creativecommons.org/licenses/by-sa/2.0"
},
"detailedDescription": {
"articleBody": "Taylor Alison Swift is an American singer-songwriter and actress. Raised in Wyomissing, Pennsylvania, she moved to Nashville, Tennessee, at the age of 14 to pursue a career in country music. ",
"url": "http://en.wikipedia.org/wiki/Taylor_Swift",
"license": "https://en.wikipedia.org/wiki/Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_License"
},
"url": "http://taylorswift.com/"
},
"resultScore": 896.576599
}
]
}
次にプログラミング言語で記載した際のサンプルコードをご紹介します。実際のリファレンスにはPython、JAVA、JavaScript、PHPのサンプルコードがありますが、ここではPythonのみ記載します。「Taylor Swift」のデータを取得します。
"""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で取得できるエンティティの例一覧を記載します。あくまで例であり、実際には100万以上のエンティティが存在しています。
- Book
- BookSeries
- EducationalOrganization
- Event
- GovernmentOrganization
- LocalBusiness
- Movie
- MovieSeries
- MusicAlbum
- MusicGroup
- MusicRecording
- Organization
- Periodical
- Person
- Place
- SportsTeam
- TVEpisode
- TVSeries
- VideoGame
- VideoGameSeries
- WebSite
最後に
いかがでしたでしょうか?ナレッジグラフAPIはナレッジグラフのデータベースにアクセスできるAPIです。このAPIは、Freebase APIに取って代わる存在であるとGoolgeは公言しており、数ヶ月後にはFreebase APIを廃止する予定とのことです。
※この記事の内容は『Google Knowledge Graph Search API | Knowledge Graph Search API | Google Developers』の内容をもとに作成しています。より詳細なサンプルコードなどをお探しの場合は、こちらのページをご確認ください。