private static function getPhpInsightResult($text) { $returnObj = array(); $sentiment = new Sentiment(); $scores = $sentiment->score($text); $returnObj['likelihood'] = (object) array_change_key_case($scores, CASE_UPPER); $returnObj['label'] = strtoupper($sentiment->categorise($text)); return (object) $returnObj; }
<?php include 'sentiment.class.php'; $sentiment = new Sentiment(); $example1 = $sentiment->categorise('Today was rubbish'); //Neg $example2 = $sentiment->categorise('Today was amazing'); // Pos $example3 = $sentiment->categorise('Today was ok'); // Neu ?> <html> <head> </head> <body> <h1>phpInsight Example</h1> <h2>Example 1</h2> <p>Today was rubbish</p> <p>Classification - <?php echo $example1; ?> </p> <h2>Example 2</h2> <p>Today was amazing</p> <p>Classification - <?php echo $example2;
<?php include 'sentiment.class.php'; $sentiment = new Sentiment(); $examples = 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?', 8 => 'What The Hell !', 9 => 'Bitch'); foreach ($examples as $key => $value) { echo '<div class="example">'; echo "<h2>Example {$key}</h2>"; echo "<blockquote>{$value}</blockquote>"; echo "Scores: <br />"; $scores = $sentiment->score($value); echo "<ul>"; foreach ($scores as $class => $score) { $string = "{$class} -- <i>{$score}</i>"; if ($class == $sentiment->categorise($value)) { $string = "<b class=\"{$class}\">{$string}</b>"; } echo "<ol>{$string}</ol>"; } echo "</ul>"; echo '</div>'; } echo "Process Completed";
</head> <body> <h1>phpInsight Example</h1> <?php $sentiment = new Sentiment(); $examples = 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?'); foreach ($examples as $key => $example) { echo '<div class="example">'; echo "<h2>Example {$key}</h2>"; echo "<blockquote>{$example}</blockquote>"; echo "Scores: <br />"; $scores = $sentiment->score($example); echo "<ul>"; foreach ($scores as $class => $score) { $string = "{$class} -- <i>{$score}</i>"; if ($class == $sentiment->categorise($example)) { $string = "<b class=\"{$class}\">{$string}</b>"; } echo "<ol>{$string}</ol>"; } echo "</ul>"; echo '</div>'; } ?> </body> </html>