<?php
$i = 1;
$query = mysqli_query($conn, 'SELECT fback FROM whatyouthink');
$featured = array();
$count = 1;
while($a = mysqli_fetch_array($query)) {
$featured[$count] = $a[0];
$count++;
}
//I'm passing all the selected records to another script to reformat
require_once __DIR__ . '/../autoload.php';
$sentiment = new \PHPInsight\Sentiment();
foreach ($featured as $string) {
// calculations:
$scores = $sentiment->score($string);
$class = $sentiment->categorise($string);
// output:
//echo "String: $string\n";
echo "<br/>";
echo "Dominant: $class, scores: ";
print_r($scores);
echo "\n";
echo "<br/>";
}
这是我用上面的代码分析后得到的输出。
显性:neg,得分:数组([neg] =&gt; 0.5 [neu] =&gt; 0.25 [pos] =&gt; 0.25)
显性:pos,得分:数组([pos] =&gt; 0.5 [neu] =&gt; 0.25 [neg] =&gt; 0.25)
显性:pos,得分:数组([pos] =&gt; 0.5 [neu] =&gt; 0.25 [neg] =&gt; 0.25)
显性:neg,得分:数组([neg] =&gt; 0.667 [neu] =&gt; 0.167 [pos] =&gt; 0.167)
显性:neu,得分:数组([neu] =&gt; 0.333 [neg] =&gt; 0.333 [pos] =&gt; 0.333)
显性:pos,得分:数组([pos] =&gt; 0.4 [neg] =&gt; 0.4 [neu] =&gt; 0.2 )
/ code添加sum数组元素组(neg,pos,neu)
$sumArray = array();
foreach ($scores as $k=>$subArray) {
foreach ($scores as $id=>$value) {
$sumArray[$id]+=$value;
}
}
print_r($sumArray)
?>
**输出:
Array ( [pos] => 1.2 [neg] => 1.2 [neu] => 0.6 )**
我希望我的输出像这样加起来三个元素,即neg,pos,neu:
$rating_data = array(
array('Sentiment', 'sd'),
array('neg',sum_neg),
array('pos',sum_pos),
array('neu',sum_neu),
);
答案 0 :(得分:1)
您可以执行以下操作:
$rating_data = array(
array('Sentiment', 'sd'),
);
foreach ($sumArray as $k => $v) {
$rating_data[] = array($k, $v);
}