PHP code example of michaeljwright / aws-comprehend
1. Go to this page and download the library: Download michaeljwright/aws-comprehend 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/ */
$config = [
'LanguageCode' => 'en',
'TextList' => ['This is good', 'This is bad'],
];
$jobSentiment = \Comprehend::batchDetectSentiment($config);
dd($jobSentiment['ResultList']);
// FIRST create a function to call the comprehend facade and parse the results (below will return an array with the overall sentiment as well as positive/negative scores)
public function sentimentAnalysis($comments) {
$results = array();
if(count($comments)>0) {
$config = [
'LanguageCode' => 'en',
'TextList' => $comments,
];
$jobSentiment = \Comprehend::batchDetectSentiment($config);
$positive = array();
$negative = array();
if(count($jobSentiment['ResultList'])) {
foreach($jobSentiment['ResultList'] as $result){
$positive[] = $result['SentimentScore']['Positive'];
$negative[] = $result['SentimentScore']['Negative'];
}
}
$results['positive'] = array_sum($positive)/count($positive);
$results['negative'] = array_sum($negative)/count($negative);
$results['sentiment'] = ($results['positive'] > $results['negative'] ? 'POSITIVE' : 'NEGATIVE');
return $results;
} else {
return $results['sentiment'] = 'INVALID';
}
}
// SECOND create an array of comments for the analysis and call the above function
$comments = [
'I think this is very good considering I created a package/wrapper for Amazon Comprehend. Yay me!',
'Oh my good this is such a bloody rubbish package/wrapper. I hope the author stops coding immediately.',
'This is really good, I really love this stand by this',
'This is sooooo bad'
];
dd($this->sentimentAnalysis($comments));
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.