PHP code example of bredmor / comment-analyzer

1. Go to this page and download the library: Download bredmor/comment-analyzer 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/ */

    

bredmor / comment-analyzer example snippets



redmor\CommentAnalyzer\Comment;
use bredmor\CommentAnalyzer\Analyzer;

$key = 'your_api_key';

try {
    // Instantiate API and define an attribute model
    $api = new Analyzer($key);
    $api->addAttributeModel(Analyzer::MODEL_TOXICITY);
} catch (Exception $e) {
    echo 'Error: ' . $e->getMessage();
}

// Build your comments from text
$comment = new Comment('Hello my good sir, how are you this fine evening?');
$comment2 = new Comment('You suck, jerkwad.');


try {
    // Analyze the comments and fetch a score for the attribute model you want
    $api->analyze($comment);
    $scoreObj = $comment->getSummaryScore(Analyzer::MODEL_TOXICITY);
    if($scoreObj) {
        echo 'Comment 1 Toxicity rating: ' . floor($scoreObj->value * 100) . '%';
    }

    $api->analyze($comment2);
    $scoreObj2 = $comment2->getSummaryScore(Analyzer::MODEL_TOXICITY);
    if($scoreObj2) {
        echo "\n" . 'Comment 2 Toxicity rating: ' . floor($scoreObj2->value * 100) . '%';
    }
} catch (Exception $e) {
    echo 'Error: ' . $e->getMessage();
}