|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace IBM\Watson\ToneAnalyzer\Api; |
|
4
|
|
|
|
|
5
|
|
|
use IBM\Watson\Common\Api\AbstractApi; |
|
6
|
|
|
use IBM\Watson\ToneAnalyzer\Model\ToneAnalysis; |
|
7
|
|
|
|
|
8
|
|
|
class Tone extends AbstractApi |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Analyze tone |
|
12
|
|
|
* |
|
13
|
|
|
* @param $text |
|
14
|
|
|
* @param array|null $params |
|
15
|
|
|
* |
|
16
|
|
|
* @return mixed|\Psr\Http\Message\ResponseInterface |
|
17
|
|
|
* |
|
18
|
|
|
* @throws \IBM\Watson\Common\Exception\UnknownErrorException |
|
19
|
|
|
* @throws \IBM\Watson\Common\Exception\NotFoundException |
|
20
|
|
|
* @throws \IBM\Watson\Common\Exception\InsufficientPrivilegesException |
|
21
|
|
|
* @throws \Http\Client\Exception |
|
22
|
|
|
* @throws \IBM\Watson\ToneAnalyzer\Exception |
|
23
|
|
|
* @throws \Exception |
|
24
|
|
|
*/ |
|
25
|
|
|
public function analyze($text, array $params = null) |
|
26
|
|
|
{ |
|
27
|
|
|
$headers = []; |
|
28
|
|
|
|
|
29
|
|
|
$params['text'] = $text; |
|
30
|
|
|
|
|
31
|
|
|
if (!isset($params['version'])) { |
|
32
|
|
|
$params['version'] = \date('Y-m-d'); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
if (!isset($params['sentences'])) { |
|
36
|
|
|
$params['sentences'] = true; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
if (isset($params['content_language'])) { |
|
40
|
|
|
$headers['Content-Language'] = $params['content_language']; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
if (isset($params['accept_language'])) { |
|
44
|
|
|
$headers['Accept-Language'] = $params['accept_language']; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
if (isset($params['learning_opt_out'])) { |
|
48
|
|
|
$headers['X-Watson-Learning-Opt-Out'] = true; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$response = $this->get('/api/v3/tone', $params, $headers); |
|
52
|
|
|
|
|
53
|
|
|
if ($response->getStatusCode() !== 200) { |
|
54
|
|
|
$this->handleErrors($response); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $this->hydrator->hydrate($response, ToneAnalysis::class); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|