Skip to content

Commit f4f31fc

Browse files
New method: scores and rewrite score
1 parent c06c01b commit f4f31fc

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/Antoineaugusti/LaravelSentimentAnalysis/SentimentAnalysis.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,34 @@ public function decision($string)
3939
}
4040
}
4141

42+
/**
43+
* Get scores for each decision
44+
* @param string $string The original string
45+
* @return array An array containing keys 'negative', 'neutral' and 'positive' with a float. The closer to 1, the better
46+
* @example ['negative' => 0.5, 'neutral' => 0.25, 'positive' => 0.25]
47+
*/
48+
public function scores($string)
49+
{
50+
$scores = $this->sentiment->score($string);
51+
$array = array();
52+
53+
// The original keys are 'neg' / 'neu' / 'pos'
54+
// We will remap to 'negative' / 'neutral' / 'positive' and round with 2 digits
55+
foreach ([self::NEGATIVE, self::NEUTRAL, self::POSITIVE] as $value)
56+
$array[$value] = round($scores[substr($value, 0, 3)], 2);
57+
58+
return $array;
59+
}
60+
4261
/**
4362
* Get the confidence of a decision for a result. The closer to 1, the better
4463
* @param string $string The given sentence
4564
* @return float The confidence of a decision for a result. The close to 1, the better
4665
*/
4766
public function score($string)
4867
{
49-
$scores = $this->sentiment->score($string);
50-
return $scores[$this->sentiment->categorise($string)];
68+
$scores = $this->scores($string);
69+
return max($scores);
5170
}
5271

5372
/**

0 commit comments

Comments
 (0)