mauricioschmitz/watson-php-sdk

PHP SDK 用于 Watson,以便开发者能够快速将 Watson 应用于 PHP 应用程序

安装: 24

依赖项: 0

建议者: 0

安全性: 0

星级: 0

关注者: 2

分支: 17

语言:JavaScript

dev-master 2017-11-23 17:56 UTC

This package is not auto-updated.

Last update: 2024-09-21 17:20:24 UTC


README

Language: PHP Build Status codecov

IBM Watson 开发者云的 Watson PHP SDK

安装

安装 Composer 将更容易管理应用程序的依赖关系。

运行 Composer 命令以安装 Watson PHP SDK 的最新版本

composer require mauricioschmitz/watsonphpsdk:dev-master

如果 Watson PHP SDK 已经从 GitHub 下载,运行更新命令

composer update

在您的应用程序中包含 autoload.php

require 'vendor/autoload.php';

命名空间

对于常用,WatsonCredentialSimpleTokenProvider 中的一个命名空间是可选的,具体取决于如何调用 Watson 服务,您可以像这样引用类:

use WatsonSDK\Common\WatsonCredential;
use WatsonSDK\Common\SimpleTokenProvider;
use WatsonSDK\Services\ToneAnalyzer;
use WatsonSDK\Services\ToneAnalyzer\ToneModel;

API 参考

访问我们的维基

服务

语调分析器

IBM Watson 语调分析器服务可用于发现、理解和修订文本中的语言语调。该服务使用语言分析从书面文本中检测三种类型的语调:情绪、社会倾向和写作风格。

识别的情绪包括愤怒、恐惧、快乐、悲伤和厌恶等。识别的社会倾向包括一些心理学家使用的五大人格特质。这些包括开放性、责任心、外向性、宜人性以及情绪范围。识别的写作风格包括自信、分析和试探性。

以下示例演示了如何使用凭据使用语调分析器服务

$analyzer = new ToneAnalyzer( WatsonCredential::initWithCredentials('your_username', 'your_password') );

或使用令牌调用语调分析器 API,SimpleTokenProvider 是 TokenProvider 的示例,我们建议您实现自己的 Token Provider,通过实现 TokenProviderInterface

$tokenProvider = new SimpleTokenProvider('https://your-token-factory-url');
$analyzer = new ToneAnalyzer( WatsonCredential::initWithTokenProvider( $tokenProvider ) );

放置要分析的内容,调用 Tone API 并检查结果

$model = new ToneModel();
$model->setText('your text to be analyzed.');
$result = $analyzer->getTone($model);
echo $result->getContent();

自然语言分类器

IBM Watson™ 自然语言分类器服务使用机器学习算法为简短文本输入返回最匹配的预定义类。您创建并训练一个分类器将预定义类与示例文本连接起来,以便服务可以将这些类应用于新的输入。

以下示例演示了如何使用自然语言分类器

$nlc = new NaturalLanguageClassifier( WatsonCredential::initWithCredentials('your_username', 'your_password') );
$result = $nlc->classify('your text to be classified.', 'your classifier id');
echo $result->getContent();

自然语言理解

使用自然语言理解分析文本,从内容中提取元数据,如概念、实体、关键词、类别、情感、情绪、关系、语义角色。使用 Watson Knowledge Studio 开发的自定义注释模型,在非结构化文本中识别行业/领域特定实体和关系。

以下示例演示了如何使用自然语言理解

$nlu = new NaturalLanguageUnderstanding( WatsonCredential::initWithCredentials('your_username', 'your_password') );
$model = new AnalyzeModel('Watson PHP SDK for IBM Watson Developer Cloud.', [ 'keywords' => [ 'limit' => 5 ] ]);
$result = $nlu->analyze($model);
echo $result->getContent();

个性洞察

个性洞察根据一个人的写作方式提取个性特征。您可以使用该服务将个人与另一个人、机会和产品相匹配,或通过个性化消息和推荐来定制他们的体验。特征包括五大人格特质、价值观和需求。使用此服务时建议至少输入 1200 个字。

以下示例演示了如何使用个性洞察服务

$insights = new PersonalityInsights( WatsonCredential::initWithCredentials('your_username', 'your_password') );
$model = new ProfileModel( new ContentItemModel('Enter more than 100 unique words here...'));
$mode->setConsumptionPreferences(TRUE);
$result = $insights->getProfile($model);
echo $result->getContent();

API 响应

所有服务响应都封装在 HttpResponse 类中,而不是原始响应格式,因此如果您需要,将更容易使用自己的 HttpClient。一旦从 Watson API 获得结果,您可以利用三个常见的方法:

// Get HTTP status code
public function getStatusCode();
// Get actual size of response content
public function getSize();
// Get the content of the response
public function getContent();

语调分析器 为例

$analyzer = new ToneAnalyzer(...);
// getTone will return HttpResponse type
$result = $analyzer->getTone('your text to be analyzed.');

var_dump($result->getStatusCode());
var_dump($result->getSize());
var_dump($result->getContent());

// Also there is another way under string context
// You can use $result as the response content instead of using $result->getContent();.
echo $result;

基于令牌的认证

有关如何使用 TokenProvider 调用服务的示例,请参阅语调分析器

许可

版权所有 2017 GCG GBS CTO 办公室,遵循Apache 2.0 许可协议