Jump to content

Calculate total score of strings


dumb2champ

Recommended Posts

I use PHPInsight  scripts which provide opinion mining test.

Below are example codes that provide scores and it class either positive, negative, or neutral

<?php
//sentimen order
if (PHP_SAPI != 'cli') {
	echo "<pre>";
}

$strings = array(
	1 => 'Weather today is rubbish',
	2 => 'This cake looks amazing',
	3 => 'His skills are mediocre',
	4 => 'He is very talented',
	5 => 'She is seemingly very agressive',
	6 => 'Marie was enthusiastic about the upcoming trip. Her brother was also passionate about her leaving - he would finally have the house for himself.',
	7 => 'To be or not to be?',
);

require_once __DIR__ . '/../autoload.php';
$sentiment = new \PHPInsight\Sentiment();

foreach ($strings as $string) {

	// calculations:
	$scores = $sentiment->score($string);
	$class = $sentiment->categorise($string);

	// output:
	echo "String: $string\n";
	echo "Dominant: $class, scores: ";
	print_r($scores);
	echo "\n";
	echo $a;
}

Result from above scripts => http://postimg.org/image/bf6fugyo5/

 

The result show scores and classes of each strings...

But how to summarize total scores and classes of entire strings???

For example if negative more(>) than positive, then total class will be negative and vice versa.

And also total scores of each string into one single scores which not more than 1

Kindy need help...thanks

Link to comment
https://forums.phpfreaks.com/topic/295265-calculate-total-score-of-strings/
Share on other sites

Create a $totals array with the same pos/neu/neg indexes and sum the values as you loop.

$totals=array('pos' => 0, 'neu' => 0, 'neg' => 0);
foreach ($strings as $string){
    $scores = $sentiment->score($string);
    $class = $sentiment->categorise($string);
    $totals[$class] += $scores[$class];
}

var_dump($totals);

Pretty much the same way. An array of counts.

$counts=array('pos' => 0, 'neu' => 0, 'neg' => 0);
foreach ($strings as $string){
  $scores = $sentiment->score($string);
  $class = $sentiment->categorise($string);
  $counts[$class]++;  // increment count for the class
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.