**** Training Mode ****

A simple implementation of naive Bayesian prediction, turned on twitter.

 

"") { // the training form was submitted. $pos_handle = fopen("rt-polarity.pos", "a+"); $neg_handle = fopen("rt-polarity.neg", "a+"); for ($i=0; $i<50; $i++){ $row_name = "sentiment_judgement".$i; $tweet_body_text = "tweet_text".$i; echo "Testing ".$_GET[$tweet_body_text]." which is ".$tweet_body_text."
"; if ($_POST[$row_name] == "pos"){ fwrite($pos_handle, $_GET[$tweet_body_text]."\n"); echo "Wrote: ".$_GET[$tweet_body_text]." to the positive
"; } if ($_POST[$row_name] == "neg"){ fwrite($neg_handle, $_GET[$tweet_body_text]."\n"); echo "Wrote: ".$_GET[$tweet_body_text]." to the negative
"; } } fclose($pos_handle); fclose($neg_handle); echo "Data updated."; } if (array_key_exists('_submit_check', $_POST)) { //the form was submitted. // First, parse the search term. if ($_POST[terms] <> ""){ $search_terms = urlencode(strval($_POST[terms])); } //then, grab the info from twitter $url = "http://search.twitter.com/search.json?q=".$search_terms."&rpp=50&lang=en"; $ch = curl_init($url); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_VERBOSE, 1); curl_setopt($ch, CURLOPT_NOBODY, 0); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $resp = curl_exec($ch); curl_close($ch); $result = json_decode($resp); ?>
results as $tweet) { $tweet_body = strip_twitter_usernames($tweet->text); $tweet_body = strip_twitter_hashtags($tweet_body); ?>
-

Sentiment Analysis

A simple implementation of naive Bayesian prediction, turned on twitter.

 

""){ $search_terms = urlencode(strval($_POST[terms])); } //then, grab the info from twitter $url = "http://search.twitter.com/search.json?q=".$search_terms."&rpp=50&lang=en"; $ch = curl_init($url); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_VERBOSE, 1); curl_setopt($ch, CURLOPT_NOBODY, 0); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $resp = curl_exec($ch); curl_close($ch); $result = json_decode($resp); //finally, run the twitter data through the Opinion class. $op = new Opinion(); $op->addToIndex('rt-polarity.neg', 'neg'); $op->addToIndex('rt-polarity.pos', 'pos'); foreach($result->results as $tweet) { $string = $tweet->text; $judgement = $op->classify($string); echo "Classifying '$string' - " . $op->classify($string) . "\n"; echo "

"; if ($judgement == "pos"){ $positive_sentiment=$positive_sentiment+1; } else{ $negative_sentiment=$negative_sentiment+1; } } $total_sentiment = $positive_sentiment+$negative_sentiment; if ($total_sentiment > 1){ $pos_percent = ($positive_sentiment/$total_sentiment)*100; $neg_percent = ($negative_sentiment/$total_sentiment)*100; echo "

Totals: ".$positive_sentiment." positive tweets, and ".$negative_sentiment." negative tweets.

Overall, the results are ".$pos_percent." percent positive, and ".$neg_percent." percent negative.

"; } else { echo "Not enough tweets to judge the sentiment. Try a different search term."; } } // end submit check ?>

 

This is by Ian Monroe, built upon work originally done here and here and here.

PHP version: 5.2.14