freearhey/knowledge-graph

用于操作 Google 知识图谱 API 的简单 PHP 客户端

2.4.0 2021-01-05 08:50 UTC

This package is auto-updated.

Last update: 2024-09-05 17:34:34 UTC


README

一个简单的 PHP 客户端,用于操作 Google 知识图谱 API

安装

composer require freearhey/knowledge-graph

使用方法

require_once('vendor/autoload.php');

use KnowledgeGraph\Client;
use KnowledgeGraph\KnowledgeGraph;

$client = new Client();

$client->setKey('YOUR_API_KEY'); // More about API Key here: https://developers.google.com/knowledge-graph/how-tos/authorizing

$graph = new KnowledgeGraph($client);

可用方法

search()

search 方法提供了一种通过实体名称在知识图谱中搜索的方式。

$results = $graph->search($query, $type, $lang, $limit);

参数

  • $query: 要搜索的词(必需)
  • $type: 指定结果的类型(例如,“Book”)(可用类型的列表: https://developers.google.com/knowledge-graph/
  • $lang: 指定结果的语种
  • $limit: 设置自定义限制(默认:20)

示例

$results = $graph->search('taylor swift');

/*
  Collection {
    #items: array:20 [
      0 => SearchResult {
        id: "/m/0dl567"
        score: 1466.352905
        name: "Taylor Swift"
        type: array:2 [
          0 => "Thing"
          1 => "Person"
        ]
        description: "American singer"
        image: array:2 [
          "contentUrl" => "http://t0.gstatic.com/images?q=tbn:ANd9GcST848UJ0u31E6aoQfb2nnKZFyad7rwNF0ZLOCACGpu4jnboEzV"
          "url" => "https://en.wikipedia.org/wiki/Begin_Again_(Taylor_Swift_song)"
        ]
        detailedDescription: array:3 [
          "articleBody" => "Taylor Alison Swift is an American singer-songwriter. As one of the world's leading contemporary recording artists, she is known for narrative songs about her p ▶"
          "url" => "https://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/"
      }
      ...
    ]
  }
 */

search 方法总是返回带有结果的 Illuminate\Support\Collection 类。这意味着您可以使用 Laravel 集合中所有可用的 方法

find()

find 方法提供了一种通过实体 ID 搜索的方式。

$results = $graph->find($id, $lang);

参数

  • $id: 要搜索的 ID(例如,"/m/0dl567")
  • $lang: 指定结果的语种

示例

$results = $graph->find('/m/02j81');

/*
  SearchResult { 
    id: "/m/02j81"
    score: 26.98455
    name: "Eiffel Tower"
    type: array:5 [
      0 => "Thing"
      1 => "CivicStructure"
      2 => "Place"
      3 => "TouristAttraction"
      4 => "Organization"
    ]
    description: "Tower in Paris, France"
    image: array:2 [
      "contentUrl" => "http://t1.gstatic.com/images?q=tbn:ANd9GcSao5YmaJqJVcSi60m9ypkaIC6bjKVJdoocuGBzgyTIu4MaMJ-t"
      "url" => "https://commons.wikimedia.org/wiki/File:Eiffel_tower.svg"
    ]
    detailedDescription: array:3 [
      "articleBody" => "The Eiffel Tower is a wrought-iron lattice tower on the Champ de Mars in Paris, France. It is named after the engineer Gustave Eiffel, whose company designed an ▶"
      "url" => "https://en.wikipedia.org/wiki/Eiffel_Tower"
      "license" => "https://en.wikipedia.org/wiki/Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_License"
    ]
    url: "http://www.eiffel-tower.com/"
  }
 */

测试

API_KEY=YOUR_API_KEY vendor/bin/phpunit

贡献

如果您发现错误或想为代码或文档做出贡献,您可以通过提交 问题拉取请求 来帮助我们。

许可证

知识图谱采用 MIT 许可证