Skip to content

Commit d408b07

Browse files
author
Alex Belenky
committed
Add suggestScore method
1 parent f630b01 commit d408b07

File tree

6 files changed

+134
-14
lines changed

6 files changed

+134
-14
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor/
2+
/.idea
3+
/tests/.env

README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# PHP Perspective Comment Analyzer API
2+
3+
![Minimal PHP version](https://img.shields.io/packagist/php-v/stajor/perspectiveapi.svg)
4+
5+
PHP library for Perspective Comment Analyzer API.
6+
7+
## Installation
8+
9+
Via Composer:
10+
11+
```bash
12+
composer require stajor/perspectiveapi
13+
```
14+
15+
## Usage
16+
17+
#### Scoring comments: AnalyzeComment
18+
19+
```php
20+
<?php
21+
22+
$commentsClient = new PerspectiveApi\CommentsClient('PERSPECTIVE-API-TOKEN');
23+
$commentsClient->comment(['text' => 'What kind of idiot name is foo? Sorry, I like your name.']);
24+
$commentsClient->languages(['en']);
25+
$commentsClient->context(['entries' => ['text' => 'off-topic', 'type' => 'PLAIN_TEXT']]);
26+
$commentsClient->requestedAttributes(['TOXICITY' => ['scoreType' => 'PROBABILITY', 'scoreThreshold' => 0]]);
27+
28+
// Analyze and get response
29+
$response = $commentsClient->analyze();
30+
31+
// Print scores
32+
print_r($response->attributeScores());
33+
```
34+
35+
#### Sending feedback: SuggestCommentScore
36+
```php
37+
<?php
38+
39+
$commentsClient = new PerspectiveApi\CommentsClient('PERSPECTIVE-API-TOKEN');
40+
$commentsClient->comment(['text' => 'What kind of idiot name is foo? Sorry, I like your name.']);
41+
$commentsClient->languages(['en']);
42+
$commentsClient->context(['entries' => ['text' => 'off-topic', 'type' => 'PLAIN_TEXT']]);
43+
$commentsClient->clientToken('some-token');
44+
$commentsClient->communityId('unit-test');
45+
$commentsClient->attributeScores(['TOXICITY' => [
46+
'summaryScore' => ['value' => 0.83785176, 'type' => 'PROBABILITY'],
47+
'spanScores' => [['begin' => 0, 'end' => 32, 'score' => ['value' => 0.9208521, 'type' => 'PROBABILITY']]]]
48+
]);
49+
50+
// Suggest Score and get response
51+
$response = $commentsClient->suggestScore();
52+
53+
// Print scores
54+
print_r($response->attributeScores());
55+
```
56+
57+
## Contributing
58+
59+
Bug reports and pull requests are welcome on GitHub at https://github.com/Stajor/perspectiveapi.
60+
This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
61+
62+
## License
63+
64+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).

src/CommentsClient.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,24 @@ public function __construct(string $token) {
2929
* @throws CommentsException
3030
*/
3131
public function analyze(): CommentsResponse {
32-
$data = [];
3332
$fields = [
3433
'comment', 'languages', 'requestedAttributes', 'context', 'spanAnnotations', 'doNotStore', 'clientToken',
3534
'sessionId'
3635
];
3736

38-
foreach ($fields AS $field) {
39-
if (isset($this->{$field})) {
40-
$data[$field] = $this->{$field};
41-
}
42-
}
43-
44-
return $this->request('analyze', $data);
37+
return $this->request('analyze', $fields);
4538
}
4639

4740
/**
48-
*
41+
* Sending feedback: SuggestCommentScore
4942
*
5043
* @return CommentsResponse
5144
* @throws CommentsException
5245
*/
5346
public function suggestScore(): CommentsResponse {
54-
//TODO
47+
$fields = ['comment', 'context', 'attributeScores', 'languages', 'communityId', 'clientToken'];
48+
49+
return $this->request('suggestscore', $fields);
5550
}
5651

5752
/**
@@ -161,15 +156,22 @@ public function communityId(string $communityId) {
161156
* Send request to API
162157
*
163158
* @param string $method
164-
* @param array $data
159+
* @param array $fields
165160
* @return CommentsResponse
166161
* @throws CommentsException
167162
*/
168-
protected function request(string $method, array $data): CommentsResponse {
163+
protected function request(string $method, array $fields): CommentsResponse {
164+
$data = [];
169165
$client = new Client(['defaults' => [
170166
'headers' => ['content-type' => 'application/json', 'Accept' => 'application/json'],
171167
]]);
172168

169+
foreach ($fields AS $field) {
170+
if (isset($this->{$field})) {
171+
$data[$field] = $this->{$field};
172+
}
173+
}
174+
173175
try {
174176
$response = $client->post(self::API_URL."/comments:{$method}?key={$this->token}", ['json' => $data]);
175177
} catch (ClientException $e) {

src/CommentsResponse.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ public function languages(): ?array {
1818
public function clientToken(): ?string {
1919
return $this->response['clientToken'] ?? null;
2020
}
21+
22+
public function detectedLanguages(): ?array {
23+
return $this->response['detectedLanguages'] ?? null;
24+
}
2125
}

tests/CommentsClientAnalyzeTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class CommentsClientAnalyzeTest extends TestCase {
1111

1212
public static function setUpBeforeClass(): void {
1313
$commentsClient = new CommentsClient(getenv('PERSPECTIVE_API_TOKEN'));
14-
$commentsClient->comment(['text' => 'You are an idiot', 'type' => 'PLAIN_TEXT']);
14+
$commentsClient->comment(['text' => 'What kind of idiot name is foo? Sorry, I like your name.']);
1515
$commentsClient->languages(['en']);
1616
$commentsClient->context(['entries' => ['text' => 'off-topic', 'type' => 'PLAIN_TEXT']]);
1717
$commentsClient->requestedAttributes(['TOXICITY' => ['scoreType' => 'PROBABILITY', 'scoreThreshold' => 0]]);
@@ -32,7 +32,7 @@ public function testAttributeScores(): void {
3232

3333
$this->assertIsArray($attributeScores);
3434
$this->arrayHasKey('TOXICITY');
35-
$this->assertGreaterThan(0.9, $attributeScores['TOXICITY']['summaryScore']['value']);
35+
$this->assertGreaterThan(0.8, $attributeScores['TOXICITY']['summaryScore']['value']);
3636
}
3737

3838
public function testLanguages() {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php namespace PerspectiveApi\Test;
2+
3+
use PerspectiveApi\CommentsClient;
4+
use PerspectiveApi\CommentsException;
5+
use PerspectiveApi\CommentsResponse;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class CommentsClientSuggestScoreTest extends TestCase {
9+
/** @var CommentsResponse */
10+
protected static $response;
11+
12+
public static function setUpBeforeClass(): void {
13+
$commentsClient = new CommentsClient(getenv('PERSPECTIVE_API_TOKEN'));
14+
$commentsClient->comment(['text' => 'What kind of idiot name is foo? Sorry, I like your name.']);
15+
$commentsClient->languages(['en']);
16+
$commentsClient->context(['entries' => ['text' => 'off-topic', 'type' => 'PLAIN_TEXT']]);
17+
$commentsClient->clientToken('some-token');
18+
$commentsClient->communityId('unit-test');
19+
$commentsClient->attributeScores(['TOXICITY' => [
20+
'summaryScore' => ['value' => 0.83785176, 'type' => 'PROBABILITY'],
21+
'spanScores' => [['begin' => 0, 'end' => 32, 'score' => ['value' => 0.9208521, 'type' => 'PROBABILITY']]]]
22+
]);
23+
24+
self::$response = $commentsClient->suggestScore();
25+
}
26+
27+
public function testLanguages() {
28+
$detectedLanguages = self::$response->detectedLanguages();
29+
30+
$this->assertIsArray($detectedLanguages);
31+
$this->assertContains('en', $detectedLanguages);
32+
}
33+
34+
public function testClientToken() {
35+
$clientToken = self::$response->clientToken();
36+
37+
$this->assertEquals('some-token', $clientToken);
38+
}
39+
40+
public function testCommentsException() {
41+
$this->expectException(CommentsException::class);
42+
$this->expectExceptionMessage('The request is missing a valid API key.');
43+
44+
$commentsClient = new CommentsClient('');
45+
$commentsClient->suggestScore();
46+
}
47+
}

0 commit comments

Comments
 (0)