Example #1
0
<?php

//////////MUST INCLUDE TO WORK/////////
/*************************************/
//include config file
require_once "config/config.inc.php";
//include the dom class
require_once "classes/dom.class.php";
//include twitter api class
require_once "classes/twit.class.php";
//include the scrapper class
require_once 'functions/scrape.php';
//include the sentiment analysis class
require_once 'functions/sentiment.php';
/*************************************/
/********** MAIN PROGRAM *************/
$sentence = ' I enjoy@ going to~ ]\' the mall with:';
$tok = tokenizeSentence($sentence);
echo "Original Sentence: {$sentence}<br />";
printArray($tok);
<?php

include "../model/api.php";
$q = "select * from passage where id=" . $_REQUEST['id'];
$res = $con->query($q)->fetch_array();
//echo $res[3];
//echo "<br>=============================<br>";
$res = tokenizeSentence($res[3]);
$temp = array();
foreach ($res[0] as $k => $val) {
    $temp[$k]['word'] = $res[0][$k];
    $temp[$k]['stem'] = "";
    $temp[$k]['entity'] = "";
}
$_SESSION['current_chunk'] = $temp;
echo str_replace("  ", " ", implode(" ", $res[0]));
/*
echo "<pre>";
print_r($_SESSION['current_chunk']);
echo "</pre>";
*/
function tokenizeSentence($txt)
{
    $txt = str_replace('،', ',', $txt);
    $txt = str_replace('؛', ';', $txt);
    $returnValue = preg_split('/[\\s,:;.\\[\\]]+/', $txt, -1, PREG_SPLIT_NO_EMPTY);
    preg_match_all('/[\\s,:;.\\[\\]]+/', $txt, $matches, PREG_PATTERN_ORDER);
    $counter = 0;
    $ret = array();
    $distinct = array();
    $capitals = array();
Example #3
0
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 />";
}