コード例 #1
0
ファイル: sentiment.php プロジェクト: amanelis/zoonika
function sentimentAnalysis_1($sentence, $user, $search)
{
    if (empty($sentence)) {
        echo 'FUNCTION ERROR: sentimentAnalysis_1($sentence, $user, $search), param array looks to be empty -> $sentence<br />';
        exit(-1);
    }
    if (empty($user)) {
        echo 'FUNCTION ERROR: sentimentAnalysis_1($sentence, $user, $search), param string user looks to be empty -> $user<br />';
        exit(-1);
    }
    if (empty($search)) {
        echo 'FUNCTION ERROR: sentimentAnalysis_1($sentence, $search), param string looks to be empty -> $search<br />';
        exit(-1);
    }
    //Start execution timer
    $time_start = microtime_float();
    //Get pos/neg adj arrays from dictionary files
    $pos_arr_adj = positiveAdj();
    $neg_arr_adj = negativeAdj();
    //Compute array size
    $pos_sz = count($pos_arr_adj) - 1;
    $neg_sz = count($neg_arr_adj) - 1;
    //make a copy of tokenized array passed
    $ta_copy_1 = tokenizeSentence($sentence);
    $ta_sz = count($ta_copy_1) - 1;
    //Set indexing variables
    $index = 0;
    $pos_cnt = 0;
    $neg_cnt = 0;
    $neu_cnt = 0;
    $string_count = 0;
    $complex_pos = 0;
    $complex_neg = 0;
    //Counter arrays for words
    $pos_found = array();
    $neg_found = array();
    foreach ($ta_copy_1 as $ta_word) {
        foreach ($pos_arr_adj as $pos_w => $pos_word) {
            if ($ta_word == $pos_word['word']) {
                $pos_found[$pos_cnt] = $pos_word['word'];
                $complex_pos = 1;
                $pos_cnt++;
            }
            $string_count++;
        }
        foreach ($neg_arr_adj as $neg_w => $neg_word) {
            if ($ta_word == $neg_word['word']) {
                $neg_found[$neg_cnt] = $neg_word['word'];
                $complex_neg = 1;
                $neg_cnt++;
            }
            $string_count++;
        }
        if ($complex_pos == 0 && $complex_neg == 0) {
            $neu_cnt = 1;
        } else {
            $neu_cnt = 0;
        }
        $index++;
    }
    /*
    function posWordsFound() {
    	if(!empty($pos_found)){
    		echo "Word: <strong>".$pos_found[0]."</strong>";
    	}
    }
    
    function negWordsFound() {
    	if(!empty($neg_found)){
    		echo "Word: <strong>".$neg_found[0]."</strong>";
    	}
    }
    */
    if ($pos_cnt != 0) {
        //$sentiment_positive = round((($string_count / $ta_sz) / ($pos_cnt)), 2);
        $sentiment_positive = round($ta_sz / $pos_cnt, 2);
    } else {
        $sentiment_positive = 0;
    }
    if ($neg_cnt != 0) {
        $sentiment_negative = round($ta_sz / $neg_cnt, 2);
    } else {
        $sentiment_negative = 0;
    }
    //Return the twitter API calls left
    $limit = hourly_hits_left();
    $time_end = microtime_float();
    $time = $time_end - $time_start;
    $total_time = round($time, 5);
    echo "|******************START SENTIMENT ANALYSIS REPORT******************|<br />";
    echo "Scrapping twitter for: <strong><font color=\"blue\">{$search}</font></strong><br />";
    echo "Original sentence: <strong>{$sentence}</strong><br />";
    echo "Tokenized sentence: ";
    for ($r = 0; $r < $ta_sz + 1; $r++) {
        echo "<strong>" . $ta_copy_1[$r] . "</strong> ";
    }
    echo "<br />";
    echo "Tweet user: <a href=\"http://twitter.com/{$user}\" target=\"_blank\">{$user}</a><br />";
    echo "Pos: <strong><font color='green'>{$pos_cnt}</font></strong><br />";
    echo "Neg: <strong><font color='red'>{$neg_cnt}</font></strong><br />";
    echo "Neu: <strong>{$neu_cnt}</strong><br />";
    if ($pos_cnt > $neg_cnt) {
        echo "Sentiment: <strong><font color='green'>Positive</font></strong><br />";
        echo "Sentiment: <strong>{$sentiment_positive}</strong><br />";
    } else {
        if ($neg_cnt > $pos_cnt) {
            echo "Sentiment: <strong><font color='red'>Negative</font></strong><br />";
            echo "Sentiment: <strong>{$sentiment_negative}</strong><br />";
        } else {
            echo "Sentiment: <strong>0.0</strong><br />";
        }
    }
    echo "Total Strings Analyzed: <strong>{$string_count}</strong><br />";
    echo "Total execution time <strong>{$total_time}</strong> seconds<br />";
    echo $limit;
    echo "|******************END SENTIMENT ANALYSIS REPORT********************|<br /><br /><br />";
}
コード例 #2
0
ファイル: scrape.php プロジェクト: amanelis/zoonika
function sendTweets($search_term)
{
    //username/pass db credentials
    include '/home/51949/users/.home/domains/z2.zootzy.com/html/ztbot/config/config.inc.php';
    //Date/Time of execution
    $today = date("F j, Y, g:i a");
    //base url for posting with curl
    $url = 'http://twitter.com/statuses/update.xml';
    //Call this function in scrape.php, will return array
    $posts = getTwitterSearchFeedByJSON($search_term);
    $user_count = 0;
    echo "<h1>Twitter bot: <strong>{$bot_username}</strong></h1>";
    echo "<h2>Tweet date/time: <strong>{$today}</strong></h2>";
    for ($i = 0; $i < 10; $i++) {
        //grab user, post from array, then increment user counter
        $r_user = $posts[$user_count][0];
        $r_text = $posts[$user_count][1];
        $user_count++;
        //concatenate message to send
        $quo = getQuote();
        $msg = "@{$r_user} {$quo}";
        if ($bot_username != $r_user) {
            $curl_handle = curl_init();
            curl_setopt($curl_handle, CURLOPT_URL, $url);
            curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
            curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl_handle, CURLOPT_POST, 1);
            curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status={$msg}");
            curl_setopt($curl_handle, CURLOPT_USERPWD, "{$bot_username}:{$bot_password}");
            $buffer = curl_exec($curl_handle);
            curl_close($curl_handle);
            if (empty($buffer)) {
                echo "Failure: tweet unsuccessfully posted<br />";
            } else {
                echo "Your tweet has been submitted: <strong>{$msg}</strong> in response to: <strong>{$r_text}</strong><br />";
            }
        } else {
            echo "Oooops, almost just tweeted myself, nope I caught it!<br />";
        }
    }
    echo "<br />Searching and tweeting for: <strong>{$search_term}</strong><br />";
    echo hourly_hits_left();
}