|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace IBM\Watson\ToneAnalyzer\Api; |
|
4
|
|
|
|
|
5
|
|
|
use IBM\Watson\Common\Api\AbstractApi; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Analyze general tone. |
|
9
|
|
|
* |
|
10
|
|
|
* Use the general purpose endpoint to analyze the tone of your input content. The service analyzes the content for |
|
11
|
|
|
* emotional and language tones. The method always analyzes the tone of the full document; by default, it also |
|
12
|
|
|
* analyzes the tone of each individual sentence of the content. |
|
13
|
|
|
* |
|
14
|
|
|
* You can submit no more than 128 KB of total input content and no more than 1000 individual sentences in JSON, plain |
|
15
|
|
|
* text, or HTML format. The service analyzes the first 1000 sentences for document-level analysis and only the first |
|
16
|
|
|
* 100 sentences for sentence-level analysis. |
|
17
|
|
|
* |
|
18
|
|
|
* Per the JSON specification, the default character encoding for JSON content is effectively always UTF-8; per the |
|
19
|
|
|
* HTTP specification, the default encoding for plain text and HTML is ISO-8859-1 (effectively, the ASCII character |
|
20
|
|
|
* set). When specifying a content type of plain text or HTML, include the `charset` parameter to indicate the |
|
21
|
|
|
* character encoding of the input text; for example: `Content-Type: text/plain;charset=utf-8`. For `text/html`, the |
|
22
|
|
|
* service removes HTML tags and analyzes only the textual content. |
|
23
|
|
|
*/ |
|
24
|
|
|
class Tone extends AbstractApi |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
const ENDPOINT = '/tone'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
const CONTENT_TYPE_TEXT_PLAIN = 'text/plain'; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
const CONTENT_TYPE_HTML = 'text/html'; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var boolean |
|
43
|
|
|
*/ |
|
44
|
|
|
private $isHtml; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var string |
|
48
|
|
|
*/ |
|
49
|
|
|
private $contentType; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string $text |
|
53
|
|
|
* @param array $params |
|
54
|
|
|
* |
|
55
|
|
|
* @return mixed |
|
56
|
|
|
* |
|
57
|
|
|
* @throws \Http\Client\Exception |
|
58
|
|
|
* @throws \IBM\Watson\Common\Exception\Domain\InsufficientPrivilegesException |
|
59
|
|
|
* @throws \IBM\Watson\Common\Exception\Domain\NotFoundException |
|
60
|
|
|
* @throws \IBM\Watson\Common\Exception\Domain\UnknownErrorException |
|
61
|
|
|
* @throws \IBM\Watson\Common\Exception\Api\BadRequestException |
|
62
|
|
|
*/ |
|
63
|
|
|
public function analyze($text, array $params = []) |
|
64
|
|
|
{ |
|
65
|
|
|
$params['text'] = $text; |
|
66
|
|
|
|
|
67
|
|
|
$headers = [ |
|
68
|
|
|
'Content-Type' => $this->getContentType() |
|
69
|
|
|
]; |
|
70
|
|
|
|
|
71
|
|
|
if (isset($params['content_language'])) { |
|
72
|
|
|
$headers['Content-Language'] = $params['content_language']; |
|
73
|
|
|
unset($params['content_language']); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
if (isset($params['accept_language'])) { |
|
77
|
|
|
$headers['Accept-Language'] = $params['accept_language']; |
|
78
|
|
|
unset($params['accept_language']); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$response = $this->post(self::ENDPOINT, $params, $headers); |
|
82
|
|
|
|
|
83
|
|
|
if ($response->getStatusCode() !== 200) { |
|
84
|
|
|
$this->handleErrors($response); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $this->hydrator->hydrate($response); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Is supplied text html? |
|
92
|
|
|
* |
|
93
|
|
|
* @param bool $flag |
|
94
|
|
|
* |
|
95
|
|
|
* @return $this |
|
96
|
|
|
*/ |
|
97
|
|
|
public function isHtml($flag = false) |
|
98
|
|
|
{ |
|
99
|
|
|
$this->isHtml = $flag; |
|
100
|
|
|
|
|
101
|
|
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @return string |
|
106
|
|
|
*/ |
|
107
|
|
|
private function getContentType() |
|
108
|
|
|
{ |
|
109
|
|
|
$this->isHtml ? $this->contentType = static::CONTENT_TYPE_HTML |
|
110
|
|
|
: $this->contentType = static::CONTENT_TYPE_TEXT_PLAIN; |
|
111
|
|
|
|
|
112
|
|
|
return $this->contentType; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|