/
teste.php
45 lines (38 loc) · 1.29 KB
/
teste.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
use CViniciusSDias\AnaliseSentimento\Chart\PieChartBuilder;
use CViniciusSDias\AnaliseSentimento\Twitter\TwitterApi;
use Ds\Map;
use GuzzleHttp\Client;
use Sentiment\Analyzer;
require_once 'vendor/autoload.php';
$twitterApi = new TwitterApi(new Client(), 'token');
$query = [
'"Falcon and the winter soldier"',
'-is:verified',
'-is:retweet',
'lang:en',
];
$recentTweets = $twitterApi->searchRecentTweets(implode(' ', $query));
$sentiments = new Map();
$sentiments->put('neg', 0);
$sentiments->put('neu', 0);
$sentiments->put('pos', 0);
$sentimentAnalyzer = new Analyzer();
foreach ($recentTweets as $tweet) {
/** @var array{"neg": float, "neu": float, "pos": float, "compound": float} $sentiment */
$sentiment = $sentimentAnalyzer->getSentiment((string) $tweet);
if ($sentiment['compound'] > 0) {
$key = 'pos';
} elseif ($sentiment['compound'] < 0) {
$key = 'neg';
} else {
$key = 'neu';
}
$sentiments[$key]++;
}
PieChartBuilder::createWithSize(500, 500)
->withTitle('Sentimento sobre o seriado')
->withSubTitle('The Falcon and the Winter Soldier')
->withValues($sentiments->values()->toArray())
->withLegends(['Negativo' => 'red', 'Neutro' => 'blue', 'Positivo' => 'green'])
->drawToImageFile(__DIR__ . '/sentimentos.png');