sinf/watsonphpsdk

用于Watson的PHP SDK,以便开发者在PHP应用程序中快速应用Watson

dev-master 2018-05-28 18:18 UTC

This package is not auto-updated.

Last update: 2024-09-20 01:48:09 UTC


README

Language: PHP Build Status codecov

IBM Watson开发者云的Watson PHP SDK

安装

安装Composer将更易于管理应用程序的依赖项。

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

composer require sinf/watsonphpsdk

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

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参考

访问我们的wiki

服务

助手

Watson助手是一个企业级人工智能(AI)助手,它通过提供主动和个性化的服务,同时确保数据隐私,帮助企业在品牌忠诚度和客户体验方面进行转型。它是以下AI助手的助手:

您的品牌 增加参与度和忠诚度。您的客户 获得个性化的学习体验。您的数据 安全且个性化的洞察。

以下示例演示了如何通过使用凭证使用助手服务

$assistant = new Assistant( WatsonCredential::initWithCredentials('your_username', 'your_password') );
$result = $assistant->sendMessage('your_message', 'your_workspace_id');
echo $result->getContent();

语调分析器

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知识工作室开发的自定义注释模型,在非结构化文本中识别行业/领域特定的实体和关系。

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

$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 许可协议下。