function getTweetSentiment($tweet)
{
    require_once 'Alchemy/alchemyapi_php/alchemyapi.php';
    $alchemyapi = new AlchemyAPI();
    //Object that represents the alchemy API
    $firstChar = $tweet[0];
    //Gets first character of the tweet
    $tweetSentiment = new sentiment();
    if ($firstChar == 'R' || $firstChar == '@') {
        //If it's retweet or mention, get to the meat of the tweet
        $colonIndex = strpos($tweet, ':');
        $tweet = substr($tweet, $colonIndex + 1);
    }
    try {
        $response = $alchemyapi->sentiment("text", $tweet, null);
        //Send a sentiment alchemy request
        if (strcmp($response['status'], "ERROR") != 0) {
            $tweetSentiment->type = $response['docSentiment']['type'];
            //Set the type
            if (strcmp($tweetSentiment->type, "neutral") == 0) {
                $tweetSentiment->score = 0.0;
            } else {
                $tweetSentiment->score = $response['docSentiment']['score'];
            }
            //Set the score
        }
    } catch (Exception $e) {
        echo 'Caught Exception: ', $e->getMessage(), "\n";
        //Catch the exception if the request breaks somehow
    }
    return $tweetSentiment;
    // return object
}
Exemple #2
0
        echo PHP_EOL;
    }
} else {
    echo 'Error in the entity extraction call: ', $response['statusInfo'];
}
echo PHP_EOL;
echo PHP_EOL;
echo PHP_EOL;
echo '############################################', PHP_EOL;
echo '#   Sentiment Analysis Example             #', PHP_EOL;
echo '############################################', PHP_EOL;
echo PHP_EOL;
echo PHP_EOL;
echo 'Processing HTML: ', $demo_html, PHP_EOL;
echo PHP_EOL;
$response = $alchemyapi->sentiment('html', $demo_html, null);
if ($response['status'] == 'OK') {
    echo '## Response Object ##', PHP_EOL;
    echo print_r($response);
    echo PHP_EOL;
    echo '## Document Sentiment ##', PHP_EOL;
    echo 'type: ', $response['docSentiment']['type'], PHP_EOL;
    echo 'score: ', $response['docSentiment']['score'], PHP_EOL;
} else {
    echo 'Error in the sentiment analysis call: ', $response['statusInfo'];
}
echo PHP_EOL;
echo PHP_EOL;
echo PHP_EOL;
echo '############################################', PHP_EOL;
echo '#   Keyword Extraction Example             #', PHP_EOL;
Exemple #3
0
echo 'Keyword tests complete!', PHP_EOL, PHP_EOL;
//Concepts
echo 'Checking concepts . . . ', PHP_EOL;
$response = $alchemyapi->concepts('text', $test_text, null);
assert($response['status'] == 'OK');
$response = $alchemyapi->concepts('html', $test_html, null);
assert($response['status'] == 'OK');
$response = $alchemyapi->concepts('url', $test_url, null);
assert($response['status'] == 'OK');
$response = $alchemyapi->concepts('random', $test_url, null);
assert($response['status'] == 'ERROR');
//invalid flavor
echo 'Concept tests complete!', PHP_EOL, PHP_EOL;
//Sentiment
echo 'Checking sentiment . . . ', PHP_EOL;
$response = $alchemyapi->sentiment('text', $test_text, null);
assert($response['status'] == 'OK');
$response = $alchemyapi->sentiment('html', $test_html, null);
assert($response['status'] == 'OK');
$response = $alchemyapi->sentiment('url', $test_url, null);
assert($response['status'] == 'OK');
$response = $alchemyapi->sentiment('random', $test_url, null);
assert($response['status'] == 'ERROR');
//invalid flavor
echo 'Sentiment tests complete!', PHP_EOL, PHP_EOL;
//Sentiment Targeted
echo 'Checking targeted sentiment . . . ', PHP_EOL;
$response = $alchemyapi->sentiment_targeted('text', $test_text, 'heart', null);
assert($response['status'] == 'OK');
$response = $alchemyapi->sentiment_targeted('html', $test_html, 'language', null);
assert($response['status'] == 'OK');
if ($_SESSION["rawsearch"] == 0) {
    $myurl = $_SESSION["facedquery"] . "&fq=lang%3Afr";
} else {
    $myurl = "http://52.32.49.169:8983/solr/proj3/select?q=" . $translation_fr . "&fq=lang%3Afr" . "&start=0&rows=10&wt=json&indent=true";
}
$response3 = $alchemyapi3->sentiment("url", $myurl, null);
$s_fr_socore = $response3["docSentiment"]["score"];
//echo $s_fr_socore;
//ar
$alchemyapi3 = new AlchemyAPI();
if ($_SESSION["rawsearch"] == 0) {
    $myurl = $_SESSION["facedquery"] . "&fq=lang%3Aar";
} else {
    $myurl = "http://52.32.49.169:8983/solr/proj3/select?q=" . $translation_ar . "&fq=lang%3Aar" . "&start=0&rows=10&wt=json&indent=true";
}
$response3 = $alchemyapi3->sentiment("url", $myurl, null);
$s_ar_socore = $response3["docSentiment"]["score"];
//echo $s_ar_socore;
?>

                                                        <center>
                                                          <P>cultural differences of sentiment</P>
                                                        <script src="Chart.js"></script>
                                                      <style>
                                                        body{
                                                          padding: 0;
                                                          margin: 0;
                                                        }
                                                        #canvas-holder{
                                                          width:85%;
                                                        }
Exemple #5
0
<?php

include '../../nlp/alchemyapi_php/alchemyapi.php';
if (isset($_POST['input'])) {
    $input = $_POST['input'];
} else {
    $input = "You're f****d up";
}
$alchemyapi = new AlchemyAPI();
$response = $alchemyapi->sentiment('text', $input, array('sentiment' => 1));
if ($response['status'] == 'OK') {
    echo 'Sentiment: ', $response['docSentiment']['type'], '<br>';
    if (array_key_exists('score', $response['docSentiment'])) {
        echo 'score: ', $response['docSentiment']['score'], '<br>';
    }
} else {
    echo 'Error in the sentiment analysis call: ', $response['statusInfo'];
}