/
textToSentiment.php
51 lines (37 loc) · 1.32 KB
/
textToSentiment.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
/**
Object that encapsulates the sentiment of a tweet
*/
class sentiment
{
public $score=""; //The score the tweet has recieved between 1->-1
public $type=""; //The type of tweet, positve, negative, neutral
}
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.00;
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
}
function getNumHashTags($line){
return substr_count($line, '#');
}
?>