public function getCommentSentiment() { $sentiment = new \PHPInsight\Sentiment(); $content = $this->content; $class = $sentiment->categorise($content); $this->rating = $class; //echo '<pre>'; //var_dump($sentiment->score($content)); }
function sentiment_analysis_post($text) { // sentiment analysis on sentence base - phpInsight & SentimentAnalysis $sentenceArray = explode(". ", $text); // insight & SA // require_once __DIR__ . '/sentiment/src/test/style.php'; require_once __DIR__ . '/insight/autoload.php'; $sentiment = new \PHPInsight\Sentiment(); //foreach ($strings as $string) { // calculations: $sentences = explode(". ", $text); $counter = 0; foreach ($sentences as $sentence) { $counter++; if (strlen(trim($sentence))) { $scores = $sentiment->score($sentence); $class = $sentiment->categorise($sentence); // output: if ($class == "pos") { $result += 3 + $scores[pos] * 5; } else { $result += $scores[pos] * 5; } //echo $result/$counter; return $result / $counter; } } }
function getSentimentClass($hasil) { $sentiment = new \PHPInsight\Sentiment(); // calculations: $scores = $sentiment->score($hasil); $class = $sentiment->categorise($hasil); if ($class == 'neg') { $class = 'buruk'; } elseif ($class == 'pos') { $class = 'baik'; } elseif ($class == 'neu') { $class = 'netral'; } // output: // echo "String: $hasil\n"; // echo "Dominant: $class, scores: "; // print_r($scores); // echo "\n"; return $class; }
protected function findSentiment($tweets) { $sentiment = new \PHPInsight\Sentiment(); //initialize the php-insight api $results = array(); foreach ($tweets['statuses'] as $tweet) { //foreach of the tweets that we received if (isset($tweet['metadata']['iso_language_code']) && $tweet['metadata']['iso_language_code'] == 'en') { //perform sentiment analysis only for the English Tweets $insight = $sentiment->categorise($tweet['text']); //call php-insight array_push($results, $insight); } } unset($tweets); unset($DatumboxAPI); return $results; }
function sentimentAnalysis() { echo "<br><br>"; echo "<h1>Sentiment analysis</h1>"; require_once __DIR__ . '/libs/sentimentAnalysis/autoload.php'; $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?', 8 => "Sad"); $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 "<br><br>"; } }
function getScore($comment) { require __DIR__ . '/vendor/autoload.php'; require_once __DIR__ . '/libs/sentimentAnalysis/autoload.php'; //Translate first $tr = new TranslateClient(); // Default is from 'auto' to 'en' //$tr->setSource('it'); //$tr->setTarget('en'); $comment = $tr->translate($comment); //Calculate sentiment $sentiment = new \PHPInsight\Sentiment(); // calculations: $scores = $sentiment->score($comment); $class = $sentiment->categorise($comment); $toReturn['dom'] = $class; $toReturn['pos'] = $scores['pos']; $toReturn['neg'] = $scores['neg']; $toReturn['neu'] = $scores['neu']; return $toReturn; }
public function get_stem() { require_once app_path() . '/library/PHPInsight/autoload.php'; $stemmerFactory = new \Sastrawi\Stemmer\StemmerFactory(); $sentiment = new \PHPInsight\Sentiment(); $stemmer = $stemmerFactory->createStemmer(); $getdata = DB::collection('scraping')->orderBy('id', 'asc')->get(); foreach ($getdata as $key) { $output = $stemmer->stem($key['article']); $scores = $sentiment->score($output); $class = $sentiment->categorise($output); $update = new Classifying(); $update->category = $class; $update->article = $key['article']; $update->user_validation = ""; $update->title = ""; $update->scraping_id = $key['_id']; $update->classifier = "new"; $update->save(); echo "<pre>" . $output . "</pre>"; echo "<blockquote>" . $class . "</blockquote>"; echo "<br><hr>"; } }
<?php session_start(); require './Base.php'; require './Exception.php'; require './StanfordTagger.php'; require './NERTagger.php'; require './POSTagger.php'; require_once './twitteroauth/twitteroauth.php'; require_once './config.php'; require_once './autoload.php'; $sentiment = new \PHPInsight\Sentiment(); $access_token = $_SESSION['access_token']; $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_KEY, ACCESS_SECRET); ?> <!DOCTYPE html> <meta charset="utf-8"> <title>Web Speech API Demo</title> <style> * { font-family: Verdana, Arial, sans-serif; } a:link { color:#000; text-decoration: none; } a:visited { color:#000; } a:hover {
/** * Created by PhpStorm. * User: Administrator * Date: 7/30/2015 * Time: 12:33 AM */ namespace tweet_ks; require_once getcwd() . '/twitteroauth/autoload.php'; require_once getcwd() . '/phpInsight-master/autoload.php'; require_once getcwd() . '/NewWorld.php'; use Abraham\TwitterOAuth\TwitterOAuth; use NewWorld; try { $sentiment = new \PHPInsight\Sentiment(); $nw = new NewWorld(); $toa = $nw->GetAPICon(); #echo $toa; #echo '<h2>STEP 1</h2><br>'; if (isset($_GET['link']) && $_GET['link'] != NULL && $_GET['link'] != '') { $hstr = rawurldecode($_GET['link']); } else { $hstr = "#Verizon"; } $query = array("q" => rawurlencode($hstr), "count" => 1000, "result_type" => "recent"); $results = $nw->GetSearchTweet($toa, $query); $twt = array(); $pos_tot = 0; $neg_tot = 0; $neu_tot = 0;
<?php 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"; }
<?php if (PHP_SAPI != 'cli') { echo "<pre>"; } $text = "Loved it! My wife and I just saw it today and were pleasantly surprised! I'm not sure what some of these reviewers are talking about. It's a very creative and really intelligent film. I loved all the twisted science and art direction they used. The story and characters were interesting and made you excited to see what would happen next. I won't give any details but if you appreciate movies that make you delve a little deeper than what's on the surface, you'll like this movie too. It's not your ordinary mindless adventure."; require_once __DIR__ . '/../autoload.php'; $sentiment = new \PHPInsight\Sentiment(); //foreach ($strings as $string) { // calculations: $sentences = explode(". ", $text); $counter = 0; foreach ($sentences as $sentence) { $counter++; if (strlen(trim($sentence))) { $scores = $sentiment->score($sentence); $class = $sentiment->categorise($sentence); // output: echo "String: {$sentence}\n"; echo "Dominant: {$class}, scores: "; print_r($scores); echo "\n"; if ($class == "pos") { $result += 3 + $scores[pos] * 5; } else { $result += $scores[pos] * 5; } echo $result / $counter; } }
function sentiment_analysis($text) { $servername = "engr-cpanel-mysql.engr.illinois.edu"; $username = "******"; $password = "******"; $dbname = "backpack_user"; $conn = new mysqli($servername, $username, $password, $dbname); $htmlTagArray = ["<div>", '</div>', "<p>", "</p>", "<h1>", "</h1>", "<h2>", "</h2>", "<h3>", "</h3>", "<h4>", "</h4>", "<h5>", "</h5>", "<h6>", "</h6>", "<a>", "</a>", "<b>", "</b>", "<br>", "</br>", "!", "?", ";", "@", "#", "\$", "%", "^", "&", "*", "(", ")", "<em>", "</em>", "<article>", "</article>", "<aside>", "</aside>", "<base>", "</base>", "<strong>", "</strong>", "<i>", "</i>", "<dt>", "</dt>", "<dd>", "</dd>", "<blockquote>", "</blockquote>", "<ul>", "</ul>", "<li>", "</li>", "<figure>", "</figure>", "<ol>", "</ol>", "<br>", "<br />", '\\n']; //$text = mysqli_real_escape_string($conn,$text); //echo $text; // clear crawled data foreach ($htmlTagArray as $htmlTag) { $text = str_replace($htmlTag, ".", $text); } $text = preg_replace('/<\\/?a[^>]*>/', ' ', $text); $text = preg_replace('/<\\/?img[^>]*>/', ' ', $text); $text = preg_replace('/<\\/?audio[^>]*>/', ' ', $text); $description = preg_replace("/\r\n|\r|\n/", '', $description); $text = str_replace("..", ". ", $text); $text = str_replace(". .", ". ", $text); $text = str_replace(" .", ". ", $text); $text = str_replace("..", ". ", $text); $text = str_replace(". .", ". ", $text); $text = str_replace(" .", ". ", $text); $text = str_replace(".,", ",", $text); echo $text; //$len = strlen( $text ); //for( $i = 0; $i <= $len; $i++ ) { // $char = substr( $text, $i, 1 ); // if($char=='"'|| $char=='''){ // } //} // sentiment analysis on sentence base - phpInsight & SentimentAnalysis $sentenceArray = explode(".", $text); // insight & SA // require_once __DIR__ . '/sentiment/src/test/style.php'; require_once __DIR__ . '/sentiment/src/SentimentAnalyzer.php'; require_once __DIR__ . '/insight/autoload.php'; $insightVer = new \PHPInsight\Sentiment(); $sat = new SentimentAnalyzerTest(new SentimentAnalyzer()); $positive = 0; $negative = 0; $counter = 0; $sat->trainAnalyzer('/home/backpacker/public_html/sentiment/trainingSet/data.neg', 'negative', 5000); //training with negative data $sat->trainAnalyzer('/home/backpacker/public_html/sentiment/trainingSet/data.pos', 'positive', 5000); //training with positive data $text = mb_convert_encoding($text, "UTF-8"); foreach ($sentenceArray as $sentence) { if (strlen(trim($sentence))) { $sentimentAnalysisOfSentence1 = $sat->analyzeSentence($sentence); $resultofAnalyzingSentence1 = $sentimentAnalysisOfSentence1['sentiment']; $probabilityofSentence1BeingPositive = $sentimentAnalysisOfSentence1['accuracy']['positivity']; $probabilityofSentence1BeingNegative = $sentimentAnalysisOfSentence1['accuracy']['negativity']; $scoresSA = $probabilityofSentence1BeingPositive - $probabilityofSentence1BeingNegative; if ($scoresSA > 0.8) { $positive += 5; } else { if ($scoresSA > 0.6) { $positive += 4; } else { if ($scoresSA > 0.4) { $positive += 3; } else { if ($scoresSA > 0.2) { $positive += 2; } else { if ($scoresSA > 0.0) { $positive += 1; } else { if ($scoresSA > -0.2) { $negative += 1; } else { if ($scoresSA > -0.4) { $negative += 2; } else { if ($scoresSA > -0.6) { $negative += 3; } else { if ($scoresSA > -0.8) { $negative += 4; } else { if ($scoresSA > -1.0) { $negative += 5; } } } } } } } } } } $scoresInsight = $insightVer->score($sentence); $classInsight = $insightVer->categorise($sentence); $positive += ($scoresInsight[pos] + $scoresInsight[neu] / 2) * 5; //positiveness rated by 5 $negative += ($scoresInsight[neg] + $scoresInsight[neu] / 2) * 5; //negativeness rated by 5 // $neutural += $scoresInsight[neu]; //echo "sentence: ".$sentence."<br>"; //echo "SA positiveness: ".$probabilityofSentence1BeingPositive." negativeness: ".$probabilityofSentence1BeingNegative."<br>"; //echo "Insight positiveness: ".$scoresInsight[pos]." negativeness: ".$scoresInsight[neg]." neuturalness: ".$scoresInsight[neu]."<br><br>"; } $counter++; } $positive = $positive / (2 * $counter); $negative = -$negative / (2 * $counter); if ($positive + $negative >= 0) { //echo $positive; return $positive * 2.5 + 5; } else { //echo $negative; return $negative * 2.5 + 5; } //$negative=$negative/$counter; //echo $counter."<br>"; //echo "resultant postiveness: ".$positive."<br>"; //echo "resultant negativeness: ".$negative."<br>"; }