Jump to content

Calculate total score of strings


dumb2champ
Go to solution Solved by kicken,

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
Share on other sites

  • Solution

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);
  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.