Skip to content

adam-paterson/watson-tone-analyzer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

IBM Watson - Tone Analyzer

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

The IBM Watson™ Tone Analyzer service uses linguistic analysis to detect emotional and language tones in written text. The service can analyze tone at both the document and sentence levels. You can use the service to understand how your written communications are perceived and then to improve the tone of your communications. Businesses can use the service to learn the tone of their customers' communications and to respond to each customer appropriately, or to understand and improve their customer conversations.

Note: Request logging is disabled for the Tone Analyzer service. The service neither logs nor retains data from requests and responses, regardless of whether the X-Watson-Learning-Opt-Out request header is set.

Install

Via Composer

$ composer require adam-paterson/watson-tone-analyzer php-http/guzzle6-adapter

Note: This library uses an abstraction layer called HTTPlug which decouples it from any HTTP messaging client. To see which adapters are available and learn more visit: http://httplug.io/.

Usage

General Tone

/** @var $service IBM\Watson\ToneAnalyzer\Client */
$service = IBM\Watson\ToneAnalyzer\Client::create('username', 'password');

/** @var $analysis \IBM\Watson\ToneAnalyzer\Model\ToneAnalysis */
$analysis = $service->tone()->analyze('My fake plants died because I did not pretend to water them.', [
    'content_language' => 'en',
    'accept_language' => 'en',
    'sentences' => true
]);

/** @var $documentAnalysis \IBM\Watson\ToneAnalyzer\Model\DocumentAnalysis */
$documentAnalysis = $analysis->getDocumentAnalysis();

foreach ($documentAnalysis->getTones() as $tone) {
    /** @var $tone \IBM\Watson\ToneAnalyzer\Model\ToneScore */
    echo $tone->getName() . ': ' . $tone->getScore() . PHP_EOL;
}

// Sadness: 0.6165
// Analytical: 0.829888

$sentenceAnalysis = $analysis->getSentenceAnalysis();

foreach ($sentenceAnalysis as $sentence) {
    echo sprintf('#%d - %s: ', $sentence->getId(), $sentence->getText()) . PHP_EOL;
    foreach ($sentence->getTones() as $tone) {
        echo $tone->getName() . ': ' . $tone->getScore() . PHP_EOL;
    }
}

// #0 - Team, I know that times are tough!
// Analytical: 0.801827
// #1 - Product sales have been disappointing for the past three quarters.
// Sadness: 0.771241
// Analytical: 0.687768

Engagement Tone

use IBM\Watson\ToneAnalyzer\Model\Utterance;

/** @var $service IBM\Watson\ToneAnalyzer\Client */
$service = IBM\Watson\ToneAnalyzer\Client::create('username', 'password');

$utterances = [
    Utterance::create([
        Utterance::KEY_TEXT => 'Hello, I\'m having a problem with your product.',
        Utterance::KEY_USER => 'customer'
    ]),
    Utterance::create([
        Utterance::KEY_TEXT => 'Sorry to hear that, let me know what\'s going on, please.',
        Utterance::KEY_USER => 'agent'
    ])
];

/** @var $analysis \IBM\Watson\ToneAnalyzer\Model\UtteranceAnalyses */
$analysis = $service->toneChat()->analyze($utterances);

/** @var $documentAnalysis \IBM\Watson\ToneAnalyzer\Model\UtteranceAnalyses */
$utteranceAnalysis = $analysis->getTones();

if (null !== $utteranceAnalysis->getWarning()) {
    echo $utterances->getWarning();
}

foreach ($utterances->getTones() as $tone) {
    /** @var $tone \IBM\Watson\ToneAnalyzer\Model\UtteranceAnalysis */
    echo sprintf('#%d - %s: ', $tone->getId(), $tone->getText()) . PHP_EOL;
    foreach ($tone->getTones() as $utteranceTone) {
        /** @var $utteranceTone \IBM\Watson\ToneAnalyzer\Model\ToneScore */
        echo $utteranceTone->getName() . ': ' . $utteranceTone->getScore() . PHP_EOL;
    }
}

// #0 - Hello, I'm having a problem with your product.
// Polite: 0.686361
// #1 - Sorry to hear that, let me know what's going on, please.
// Polite: 0.92724
// Sympathetic: 0.672499

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email hello[at]adampaterson.co.uk instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

[READ ONLY] ToneAnalyzer Component for IBM Watson SDK

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages