|
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
|
21 |
|
public function analyze($text, array $params = null) |
|
26
|
|
|
{ |
|
27
|
21 |
|
$headers = []; |
|
28
|
|
|
|
|
29
|
21 |
|
if (!isset($params['version'])) { |
|
30
|
21 |
|
$params['version'] = \date('Y-m-d'); |
|
31
|
7 |
|
} |
|
32
|
|
|
|
|
33
|
21 |
|
if (!isset($params['sentences'])) { |
|
34
|
21 |
|
$params['sentences'] = true; |
|
35
|
7 |
|
} |
|
36
|
|
|
|
|
37
|
21 |
|
if (isset($params['content_language'])) { |
|
38
|
6 |
|
$headers['Content-Language'] = $params['content_language']; |
|
39
|
2 |
|
} |
|
40
|
|
|
|
|
41
|
21 |
|
if (isset($params['accept_language'])) { |
|
42
|
6 |
|
$headers['Accept-Language'] = $params['accept_language']; |
|
43
|
2 |
|
} |
|
44
|
|
|
|
|
45
|
21 |
|
if (isset($params['learning_opt_out'])) { |
|
46
|
3 |
|
$headers['X-Watson-Learning-Opt-Out'] = true; |
|
47
|
1 |
|
} |
|
48
|
|
|
|
|
49
|
21 |
|
if (isset($params['is_html'])) { |
|
50
|
3 |
|
unset($params['is_html']); |
|
51
|
3 |
|
$response = $this->analyzeHtml($text, $params, $headers); |
|
52
|
19 |
|
} elseif (json_decode($text) && json_last_error() === JSON_ERROR_NONE) { |
|
53
|
3 |
|
$response = $this->analyzeJson($text, $params, $headers); |
|
54
|
1 |
|
} else { |
|
55
|
15 |
|
$response = $this->get('/api/v3/tone', $params, $headers); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
21 |
|
if ($response->getStatusCode() !== 200) { |
|
59
|
9 |
|
$this->handleErrors($response); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
12 |
|
return $this->hydrator->hydrate($response, ToneAnalysis::class); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Analyze HTML |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $text |
|
69
|
|
|
* @param array $params |
|
70
|
|
|
* @param array $headers |
|
71
|
|
|
* |
|
72
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
73
|
|
|
* |
|
74
|
|
|
* @throws \Http\Client\Exception |
|
75
|
|
|
* @throws \Exception |
|
76
|
|
|
*/ |
|
77
|
3 |
|
public function analyzeHtml($text, array $params = [], array $headers = []) |
|
78
|
|
|
{ |
|
79
|
3 |
|
$headers['Content-Type'] = 'text/html'; |
|
80
|
|
|
|
|
81
|
3 |
|
return $this->post('/api/v3/tone' . '?' . http_build_query($params), ['text' => $text], $headers); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Analyze JSON |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $text |
|
88
|
|
|
* @param array $params |
|
89
|
|
|
* @param array $headers |
|
90
|
|
|
* |
|
91
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
92
|
|
|
* |
|
93
|
|
|
* @throws \Http\Client\Exception |
|
94
|
|
|
* @throws \Exception |
|
95
|
|
|
*/ |
|
96
|
3 |
|
public function analyzeJson($text, array $params = [], array $headers = []) |
|
97
|
|
|
{ |
|
98
|
3 |
|
$headers['Content-Type'] = 'application/json'; |
|
99
|
|
|
|
|
100
|
3 |
|
return $this->post('/api/v3/tone' . '?' . http_build_query($params), ['text' => $text], $headers); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|