**** Training Mode ****
A simple implementation of naive Bayesian prediction, turned on twitter.
if ($_POST["training_submit"] <> "") { // 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."
A simple implementation of naive Bayesian prediction, turned on twitter.
//We have our class, let's handle form submission here. if (array_key_exists('_submit_check', $_POST)) { //the form was submitted. //set up our counters: $positive_sentiment = 0; $negative_sentiment = 0; $search_terms = ""; // 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); //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 "
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 ?> } // end mode check?>
This is by Ian Monroe, built upon work originally done here and here and here.
PHP version: 5.2.14 //echo phpinfo(); ?>