├── .gitignore ├── logo.png ├── src ├── Providers │ ├── Exceptions │ │ ├── ErrorFetchingTwitter.php │ │ ├── InvalidTwitterConfiguration.php │ │ └── DefaultException.php │ ├── SocialNetworks │ │ ├── ProviderInterface.php │ │ ├── Validators │ │ │ └── TwitterValidator.php │ │ └── TwitterProvider.php │ ├── Sentiment │ │ ├── SentimentProviderInterface.php │ │ └── SentimentProvider.php │ ├── SocialNetwork.php │ ├── DataModels │ │ └── SentimentResponse.php │ └── SentimentAnalytics.php └── SentimentThermometer.php ├── examples └── index.php ├── composer.json ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor/ 2 | -------------------------------------------------------------------------------- /logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/isfonzar/sentiment-thermometer/HEAD/logo.png -------------------------------------------------------------------------------- /src/Providers/Exceptions/ErrorFetchingTwitter.php: -------------------------------------------------------------------------------- 1 | message = $message; 20 | } 21 | } -------------------------------------------------------------------------------- /examples/index.php: -------------------------------------------------------------------------------- 1 | [ 9 | 'consumer_key' => 'CONSUMER_KEY_HERE', 10 | 'consumer_secret' => 'CONSUMER_SECRET_HERE', 11 | 'type' => 'recent', 12 | 'count' => '100' 13 | ] 14 | ]; 15 | 16 | $sentimentThermometer = new SentimentThermometer($config); 17 | 18 | $thermomether = $sentimentThermometer->get('Donald Trump'); 19 | 20 | print_r($thermomether); -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "iiiicaro/sentiment-thermometer", 3 | "description": "Measure the sentiment around a word, name, sentence or hashtag using social networks.", 4 | "type": "library", 5 | "require": { 6 | "jwhennessey/phpinsight": "v2.0.9", 7 | "abraham/twitteroauth": "v0.7.2" 8 | }, 9 | "license": "MIT", 10 | "authors": [ 11 | { 12 | "name": "icaro", 13 | "email": "icaro.isf@gmail.com" 14 | } 15 | ], 16 | "autoload": { 17 | "psr-4": { 18 | "iiiicaro\\SentimentThermometer\\": "src/" 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/Providers/Sentiment/SentimentProvider.php: -------------------------------------------------------------------------------- 1 | sentiment = new Sentiment(); 20 | } 21 | 22 | /** 23 | * @param $sentence 24 | * 25 | * @return int 26 | */ 27 | public function score($sentence) 28 | { 29 | return $this->sentiment->score($sentence); 30 | } 31 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Icaro Souza Fonzar 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/Providers/SocialNetwork.php: -------------------------------------------------------------------------------- 1 | settings = $settings; 28 | } 29 | 30 | /** 31 | * @param $keyword 32 | * 33 | * @return array 34 | */ 35 | public function get($keyword) 36 | { 37 | $response = []; 38 | 39 | if (!empty($this->twitterProvider)) 40 | { 41 | $results = $this->twitterProvider->getByKeyword($keyword); 42 | 43 | $response = array_merge($response, $results); 44 | } 45 | 46 | return $response; 47 | } 48 | 49 | /** 50 | * @param \iiiicaro\SentimentThermometer\Providers\SocialNetworks\ProviderInterface $twitterProvider 51 | */ 52 | public function setTwitter(ProviderInterface $twitterProvider) 53 | { 54 | $this->twitterProvider = $twitterProvider; 55 | } 56 | } -------------------------------------------------------------------------------- /src/SentimentThermometer.php: -------------------------------------------------------------------------------- 1 | socialNetwork = new SocialNetwork($settings); 30 | $this->socialNetwork->setTwitter(new TwitterProvider($settings)); 31 | 32 | $this->sentimentAnalytics = new SentimentAnalytics(new SentimentProvider()); 33 | } 34 | 35 | /** 36 | * @param $keyword 37 | * 38 | * @return \iiiicaro\SentimentThermometer\Providers\DataModels\SentimentResponse 39 | */ 40 | public function get($keyword) 41 | { 42 | $results = $this->socialNetwork->get($keyword); 43 | 44 | $results = $this->sentimentAnalytics->analyze($results); 45 | 46 | return $results; 47 | } 48 | } -------------------------------------------------------------------------------- /src/Providers/DataModels/SentimentResponse.php: -------------------------------------------------------------------------------- 1 | positive += $positive; 30 | 31 | return $this; 32 | } 33 | 34 | /** 35 | * @param float $negative 36 | * 37 | * @return SentimentResponse 38 | */ 39 | public function sumNegative($negative) 40 | { 41 | $this->negative += $negative; 42 | 43 | return $this; 44 | } 45 | 46 | /** 47 | * @param float $neutral 48 | * 49 | * @return SentimentResponse 50 | */ 51 | public function sumNeutral($neutral) 52 | { 53 | $this->neutral += $neutral; 54 | 55 | return $this; 56 | } 57 | 58 | /** 59 | * @param int $divisor 60 | * 61 | * @return SentimentResponse 62 | */ 63 | public function divideAllFields($divisor) 64 | { 65 | $this->positive = $this->positive / $divisor; 66 | $this->negative = $this->negative / $divisor; 67 | $this->neutral = $this->neutral / $divisor; 68 | 69 | return $this; 70 | } 71 | } -------------------------------------------------------------------------------- /src/Providers/SentimentAnalytics.php: -------------------------------------------------------------------------------- 1 | sentimentProvider = $sentimentProvider; 23 | } 24 | 25 | /** 26 | * @param $results 27 | * 28 | * @return \iiiicaro\SentimentThermometer\Providers\DataModels\SentimentResponse 29 | */ 30 | public function analyze($results) 31 | { 32 | $sentimentResponse = new SentimentResponse(); 33 | 34 | if (empty($results)) 35 | { 36 | return $sentimentResponse; 37 | } 38 | 39 | foreach ($results as $sentence) 40 | { 41 | $sentenceAnalysis = $this->sentimentProvider->score($sentence); 42 | 43 | $sentimentResponse 44 | ->sumPositive($sentenceAnalysis['pos']) 45 | ->sumNegative($sentenceAnalysis['neg']) 46 | ->sumNeutral($sentenceAnalysis['neu']); 47 | } 48 | 49 | $sentimentResponse->divideAllFields(count($results)); 50 | 51 | return $sentimentResponse; 52 | } 53 | } -------------------------------------------------------------------------------- /src/Providers/SocialNetworks/Validators/TwitterValidator.php: -------------------------------------------------------------------------------- 1 | validateConsumerKey($settings)) 24 | { 25 | throw new InvalidTwitterConfiguration('Twitter consumer key missing.'); 26 | } 27 | 28 | if (!$this->validateConsumerSecret($settings)) 29 | { 30 | throw new InvalidTwitterConfiguration('Twitter consumer secret missing.'); 31 | } 32 | } 33 | 34 | /** 35 | * @param $settings 36 | * 37 | * @return bool 38 | */ 39 | private function validateConsumerKey($settings) 40 | { 41 | if (empty($settings['consumer_key'])) 42 | { 43 | return false; 44 | } 45 | 46 | return true; 47 | } 48 | 49 | /** 50 | * @param $settings 51 | * 52 | * @return bool 53 | */ 54 | private function validateConsumerSecret($settings) 55 | { 56 | if (empty($settings['consumer_key'])) 57 | { 58 | return false; 59 | } 60 | 61 | return true; 62 | } 63 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
