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;
public function getSentiments() { $connection = parent::connect(); $selectSQL = "SELECT * FROM SENTIMENT"; $rows = $connection->query($selectSQL); $sentiments = array(); foreach ($rows as $row) { $sentiment = new Sentiment(); $sentiment->setSentimentId($row[0]); $sentiment->setSentimentSourceName($row[1]); $sentiment->setSentimentSourceLink($row[2]); $sentiment->setSentimentSourceLinkType($row[3]); $sentiments[] = $sentiment; } parent::disconnect($connection); return $sentiments; }
/** * Get the shared instance of Sentiment * * @return Sentiment **/ public static function instance() { // Initialize shared instance. if (!Sentiment::$_instance) { Sentiment::$_instance = new Sentiment(); } return Sentiment::$_instance; }
<?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";
} .pos { color:green; } .neg { color:red; } </style> </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>"; }
<?php require_once "SentimentPDO.php"; require_once "Sentiment.php"; if (isset($_POST['submit'])) { $sentimentid = $_POST["sentimentid"]; $sentimentsourcename = $_POST["sentimentsourcename"]; // echo $username . "<br>\n"; $sentimentsourcelink = $_POST["sentimentsourcelink"]; //echo $password ."<br>\n"; $sentimentsourcelinktype = $_POST["sentimentsourcelinktype"]; if ($sentimentsourcename != '' && $sentimentsourcelink != '' && $sentimentsourcelinktype != '') { $sentimentPDO = new SentimentPDO(); $sentiment = new Sentiment(); $sentiment->setSentimentId($sentimentid); $sentiment->setSentimentSourceName($sentimentsourcename); $sentiment->setSentimentSourceLink($sentimentsourcelink); $sentiment->setSentimentSourceLinkType($sentimentsourcelinktype); $sentimentPDO->updateSentiment($sentiment); echo "<html>"; echo "<head>"; echo "<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' >"; echo "<link rel='stylesheet' type='text/css' href='menu.css'>"; echo "<title>News Portal - Update Sentiment Source</title>"; echo "</head>"; echo "<body>"; include "menu.php"; echo "Updated a Sentiment Source </br>"; echo "<a href='Home.php'> Back to Home</a>"; echo "</body>"; echo "</html";
<?php require_once "SentimentPDO.php"; require_once "Sentiment.php"; if (isset($_POST['submit'])) { $sentimentsourcename = $_POST["sentimentsourcename"]; // echo $username . "<br>\n"; $sentimentsourcelink = $_POST["sentimentsourcelink"]; //echo $password ."<br>\n"; $sentimentsourcelinktype = $_POST["sentimentsourcelinktype"]; if ($sentimentsourcename != '' && $sentimentsourcelink != '' && $sentimentsourcelinktype != '') { $sentimentPDO = new SentimentPDO(); $sentiment = new Sentiment(); $sentiment->setSentimentSourceName($sentimentsourcename); $sentiment->setSentimentSourceLink($sentimentsourcelink); $sentiment->setSentimentSourceLinkType($sentimentsourcelinktype); $sentimentPDO->insertSentiment($sentiment); echo "<html>"; echo "<head>"; echo "<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' >"; echo "<link rel='stylesheet' type='text/css' href='menu.css'>"; echo "<title>News Portal - Insert Sentiment Source</title>"; echo "</head>"; echo "<body>"; include "menu.php"; echo "Created a Sentiment Source </br>"; echo "<a href='index.php'> Back to Home</a>"; echo "</body>"; echo "</html"; } else { echo "Please fill all of the fields" . "<br>\n";