PHP code example of risan / sentiment-analysis

1. Go to this page and download the library: Download risan/sentiment-analysis library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

risan / sentiment-analysis example snippets


use Risan\Sentiment\Sentiment;

$result = Sentiment::analyze(
    'This package is awesome!',
);

$result->label;    // Label::Positive
$result->compound; // 0.6588

$result->positive; // 0.594
$result->neutral;  // 0.406
$result->negative; // 0.0

$result->isPositive(); // true
$result->isNegative(); // false
$result->isNeutral();  // false

$result->toArray();
// ['label' => 'positive', 'compound' => 0.6588,
//  'positive' => 0.594, 'negative' => 0.0,
//  'neutral' => 0.406]

json_encode($result);
// {"label":"positive","compound":0.6588,
//  "positive":0.594,"negative":0,
//  "neutral":0.406}

use Risan\Sentiment\Language;
use Risan\Sentiment\Sentiment;

$result = Sentiment::analyze(
    'Filmnya bagus banget!',
    Language::Indonesian,
);

$result->label;    // Label::Positive
$result->compound; // 0.623

Sentiment::analyze('Filmnya bagus banget!', 'id');

use Risan\Sentiment\Analyzer;
use Risan\Sentiment\Language;

$analyzer = (new Analyzer(Language::Indonesian))
    ->withWords(['cuan' => 2.5])
    ->withoutWords(['kasar'])
    ->withThreshold(0.1);

$result = $analyzer->analyze('Investasinya cuan!');

$result->label;    // Label::Positive
$result->compound; // 0.5848