public function TwitterSentiment($tweet_text) { require_once 'DatumboxAPI.php'; $api_key = 'd4a4b20a7ed191960a123464873ebfb4'; $DatumboxAPI = new DatumboxAPI($api_key); // PHP makes problems with "@" in HTTP POST --> workaround. $tweet_escaped = addcslashes($tweet_text, '@'); $DocumentClassification['TwitterSentimentAnalysis'] = $DatumboxAPI->TwitterSentimentAnalysis($tweet_escaped); unset($DatumboxAPI); //Return the result if (isset($DocumentClassification['TwitterSentimentAnalysis'])) { return $DocumentClassification['TwitterSentimentAnalysis']; } else { return "none"; } }
protected function findSentiment($tweets) { $DatumboxAPI = new DatumboxAPI($this->datumbox_api_key); //initialize the DatumboxAPI client $results = array(); foreach ($tweets['statuses'] as $tweet) { //foreach of the tweets that we received if (isset($tweet['metadata']['iso_language_code']) && $tweet['metadata']['iso_language_code'] == 'en') { //perform sentiment analysis only for the English Tweets $sentiment = $DatumboxAPI->TwitterSentimentAnalysis($tweet['text']); //call Datumbox service to get the sentiment if ($sentiment != false) { //if the sentiment is not false, the API call was successful. $results[] = array('id' => $tweet['id_str'], 'user' => $tweet['user']['name'], 'time' => $tweet['created_at'], 'text' => $tweet['text'], 'url' => 'https://twitter.com/' . $tweet['user']['name'] . '/status/' . $tweet['id_str'], 'sentiment' => $sentiment); } } } unset($tweets); unset($DatumboxAPI); return $results; }
/** * Finds the Sentiment for a list of Facebook posts. * * @param array $posts List of posts coming from Facebook's API * * @param array $posts */ protected function findSentiment($posts) { $DatumboxAPI = new DatumboxAPI($this->datumbox_api_key); //initialize the DatumboxAPI client $results = array(); if (!isset($posts['data'])) { return $results; } foreach ($posts['data'] as $post) { //foreach of the posts that we received $message = isset($post['message']) ? $post['message'] : ''; if (isset($post['caption'])) { $message .= "\n\n" . $post['caption']; } if (isset($post['description'])) { $message .= "\n\n" . $post['description']; } if (isset($post['link'])) { $message .= "\n\n" . $post['link']; } $message = trim($message); if ($message != '') { $sentiment = $DatumboxAPI->SentimentAnalysis(strip_tags($message)); //call Datumbox service to get the sentiment if ($sentiment != false) { //if the sentiment is not false, the API call was successful. $tmp = explode('_', $post['id']); if (!isset($tmp[1])) { $tmp[1] = ''; } $results[] = array('id' => $post['id'], 'user' => $post['from']['name'], 'text' => $message, 'url' => 'https://www.facebook.com/' . $tmp[0] . '/posts/' . $tmp[1], 'sentiment' => $sentiment); } } } unset($posts); unset($DatumboxAPI); return $results; }