Sentiment analysis tool for PHP based on the AFINN-111 wordlist.
composer require certifiedwebninja/caroline:1.0.1
use CertifiedWebNinja\Caroline\Analysis;
$caroline = new Analysis;
$result = $caroline->analyze('Hey you worthless scumbag');
echo 'Score: '.$result->getScore().PHP_EOL;
echo 'Comparative: '.$result->getComparative().PHP_EOL;
By default if no dataset is passed to the constructor, it uses the AFINN dataset. You can create your own datasets or even modify a default dataset.
Here is how you can use another dataset.
use CertifiedWebNinja\Caroline\Analysis;
use CertifiedWebNinja\Caroline\DataSets\AFINN;
$afinn = new AFINN;
$caroline = new Analysis($afinn);
$result = $caroline->analyze('Hey you worthless scumbag');
echo 'Score: '.$result->getScore().PHP_EOL;
echo 'Comparative: '.$result->getComparative().PHP_EOL;
This will return the same results as the Simple Example above, what's neat about this though is by instantiating the AFINN dataset before the analysis class, you can replace and even extend the dataset as AFINN
extends AbstractDataSet
which gives a few helper methods on the dataset.
$afinn->replace(['love' => 5]);
$caroline = new Analysis($afinn);
$result = $caroline->analyze('I love my cat.');
echo $result->getScore(); // 5
$afinn->extend(['cat' => 3]);
$caroline = new Analysis($afinn);
$result = $caroline->analyze('I love my cat.');
echo $result->getScore(); // 6 because "love" and "cat" both have a score of 3 each.
You can also create your own datasets to use by extending AbstractDataSet
<?php namespace Acme;
use CertifiedWebNinja\Caroline\DataSets\AbstractDataSet;
class DataSet extends AbstractDataSet
{
protected $dataSet = [
'anvil' => -4,
'catch' => 3
];
}