AFINN-based sentiment analysis

1.0.1 2014-09-18 05:33 UTC

This package is not auto-updated.

Last update: 2024-04-22 11:29:58 UTC


README

Build Status Coverage Status Software License Packagist Version Total Downloads

Sentiment analysis tool for PHP based on the AFINN-111 wordlist.

Install

composer require certifiedwebninja/caroline:1.0.1

Simple Example

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;

DataSet Example

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.

Replace dataset words

$afinn->replace(['love' => 5]);

$caroline = new Analysis($afinn);

$result = $caroline->analyze('I love my cat.');

echo $result->getScore(); // 5

Extend dataset

$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
    ];
}