Esempio n. 1
1
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;
        }
    }
}
Esempio n. 2
1
 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;
 }
Esempio n. 3
0
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>";
    }
}
Esempio n. 4
0
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;
}
Esempio n. 5
0
 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>";
     }
 }
Esempio n. 6
0
 //STRIP THE HASHTAG CHARACTER FROM THE STRING
 if (stripos($mySentence, $strippedhashtag) !== false) {
     $valid = true;
     echo "<br>";
     speak("More detailed information about {$strippedhashtag} is shown below");
     echo "<br><br>";
     $contents = $connection->get("https://api.twitter.com/1.1/search/tweets.json?q=" . $strippedhashtag . "&result_type=recent&locate=en&count=100");
     $decode = $contents;
     $_SESSION['detailedtweets'] = $decode;
     foreach ($decode->statuses as $result) {
         $onehourago = date("H:i:s", time() - 3600);
         $createdtime = strtotime($result->created_at);
         $formattedcreatedtime = date("H:i:s", $createdtime);
         if ($formattedcreatedtime > $onehourago) {
             $tweetText = $result->text;
             $scores = $sentiment->score($tweetText);
             //Sentimental analysis of tweets
             $class = $sentiment->categorise($tweetText);
             echo $formattedcreatedtime . "<br>";
             $totalTweetText .= $tweetText . " ";
             //SUPERVISED LEARNING PATTERN IMPLEMENTATION
             if (stripos($tweetText, "can't") !== false && stripos($tweetText, "cute") !== false) {
                 $class = "positive";
             } else {
                 if (stripos($tweetText, "no") === false && stripos($tweetText, "cute") !== false) {
                     $class = "positive";
                 } else {
                     if (stripos($tweetText, "sad") === false && stripos($tweetText, "can't") !== false && stripos($tweetText, "wait") !== false) {
                         $class = "positive";
                     }
                 }
Esempio n. 7
0
 $neg_tot = 0;
 $neu_tot = 0;
 $pos_cnt = 0;
 $neg_cnt = 0;
 $neu_cnt = 0;
 $cnt = 0;
 $chrt_jsn = '[';
 foreach ($results->statuses as $result) {
     $tmp = $result->text;
     $tmp = str_replace(" ", " ", $tmp);
     $tmp = str_replace("http://t.co/[a-z,A-Z,0-9]*{8}", "", $tmp);
     $tmp = str_replace("RT @[a-z,A-Z]*: ", "", $tmp);
     $tmp = str_replace("#[a-z,A-Z]*", "", $tmp);
     $tmp = str_replace("@[a-z,A-Z]*", "", $tmp);
     array_push($twt, $tmp);
     $scores = $sentiment->score($tmp);
     $class = $sentiment->categorise($tmp);
     $chrt_jsn = $chrt_jsn . '{
         "Tweet":"' . $cnt . '","POS":' . $scores["pos"] . ',"NEU":' . $scores["neu"] . ',"NEG":' . $scores["neg"] . '},';
     $cnt += 1;
     if ($class == 'pos') {
         $pos_cnt += 1;
         $pos_tot += $scores["pos"];
     } else {
         if ($class == 'neg') {
             $neg_cnt += 1;
             $neg_tot += $scores["neg"];
         } else {
             $neu_cnt += 1;
             $neu_tot += $scores["neu"];
         }
<?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";
}
Esempio n. 9
0
<?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;
    }
}
Esempio n. 10
0
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>";
}