-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from AntoineAugusti/revert-9-analysis-z9ndA5
Revert "Applied fixes from StyleCI"
- Loading branch information
Showing
4 changed files
with
201 additions
and
224 deletions.
There are no files selected for viewing
22 changes: 9 additions & 13 deletions
22
src/Antoineaugusti/LaravelSentimentAnalysis/Facades/SentimentAnalysis.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,14 @@ | ||
<?php | ||
|
||
namespace Antoineaugusti\LaravelSentimentAnalysis\Facades; | ||
<?php namespace Antoineaugusti\LaravelSentimentAnalysis\Facades; | ||
|
||
use Illuminate\Support\Facades\Facade; | ||
|
||
class SentimentAnalysis extends Facade | ||
{ | ||
/** | ||
* Get the registered name of the component. | ||
* | ||
* @return string | ||
*/ | ||
protected static function getFacadeAccessor() | ||
{ | ||
return 'sentimentanalysis'; | ||
} | ||
} | ||
/** | ||
* Get the registered name of the component. | ||
* | ||
* @return string | ||
*/ | ||
protected static function getFacadeAccessor() { return 'sentimentanalysis'; } | ||
|
||
} |
79 changes: 39 additions & 40 deletions
79
src/Antoineaugusti/LaravelSentimentAnalysis/LaravelSentimentAnalysisServiceProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,46 @@ | ||
<?php | ||
|
||
namespace Antoineaugusti\LaravelSentimentAnalysis; | ||
<?php namespace Antoineaugusti\LaravelSentimentAnalysis; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
|
||
class LaravelSentimentAnalysisServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Indicates if loading of the provider is deferred. | ||
* | ||
* @var bool | ||
*/ | ||
protected $defer = false; | ||
class LaravelSentimentAnalysisServiceProvider extends ServiceProvider { | ||
|
||
/** | ||
* Indicates if loading of the provider is deferred. | ||
* | ||
* @var bool | ||
*/ | ||
protected $defer = false; | ||
|
||
/** | ||
* Bootstrap the application events. | ||
* | ||
* @return void | ||
*/ | ||
public function boot() | ||
{ | ||
$this->package('antoineaugusti/laravel-sentiment-analysis'); | ||
} | ||
|
||
/** | ||
* Bootstrap the application events. | ||
* | ||
* @return void | ||
*/ | ||
public function boot() | ||
{ | ||
$this->package('antoineaugusti/laravel-sentiment-analysis'); | ||
} | ||
/** | ||
* Register the service provider. | ||
* | ||
* @return void | ||
*/ | ||
public function register() | ||
{ | ||
$this->app['sentimentanalysis'] = $this->app->share(function($app) { | ||
return new SentimentAnalysis; | ||
}); | ||
} | ||
|
||
/** | ||
* Register the service provider. | ||
* | ||
* @return void | ||
*/ | ||
public function register() | ||
{ | ||
$this->app['sentimentanalysis'] = $this->app->share(function ($app) { | ||
return new SentimentAnalysis(); | ||
}); | ||
} | ||
/** | ||
* Get the services provided by the provider. | ||
* | ||
* @return array | ||
*/ | ||
public function provides() | ||
{ | ||
return array(); | ||
} | ||
|
||
/** | ||
* Get the services provided by the provider. | ||
* | ||
* @return array | ||
*/ | ||
public function provides() | ||
{ | ||
return []; | ||
} | ||
} |
213 changes: 98 additions & 115 deletions
213
src/Antoineaugusti/LaravelSentimentAnalysis/SentimentAnalysis.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,118 +1,101 @@ | ||
<?php | ||
|
||
namespace Antoineaugusti\LaravelSentimentAnalysis; | ||
<?php namespace Antoineaugusti\LaravelSentimentAnalysis; | ||
|
||
use PHPInsight\Sentiment; | ||
|
||
class SentimentAnalysis | ||
{ | ||
private $sentiment; | ||
const NEGATIVE = 'negative'; | ||
const NEUTRAL = 'neutral'; | ||
const POSITIVE = 'positive'; | ||
|
||
public function __construct() | ||
{ | ||
$this->sentiment = new Sentiment(); | ||
} | ||
|
||
/** | ||
* Get the sentiment of a phrase. | ||
* | ||
* @param string $string The given sentence | ||
* | ||
* @return string Possible values: negative|neutral|positive | ||
*/ | ||
public function decision($string) | ||
{ | ||
// Do not call functions so that we'll compute only one time | ||
$dominantClass = $this->sentiment->categorise($string); | ||
|
||
switch ($dominantClass) { | ||
case 'neg': | ||
return self::NEGATIVE; | ||
break; | ||
|
||
case 'neu': | ||
return self::NEUTRAL; | ||
break; | ||
|
||
case 'pos': | ||
return self::POSITIVE; | ||
break; | ||
} | ||
} | ||
|
||
/** | ||
* Get scores for each decision. | ||
* | ||
* @param string $string The original string | ||
* | ||
* @return array An array containing keys 'negative', 'neutral' and 'positive' with a float. The closer to 1, the better | ||
* | ||
* @example ['negative' => 0.5, 'neutral' => 0.25, 'positive' => 0.25] | ||
*/ | ||
public function scores($string) | ||
{ | ||
$scores = $this->sentiment->score($string); | ||
$array = []; | ||
|
||
// The original keys are 'neg' / 'neu' / 'pos' | ||
// We will remap to 'negative' / 'neutral' / 'positive' and round with 2 digits | ||
foreach ([self::NEGATIVE, self::NEUTRAL, self::POSITIVE] as $value) { | ||
$array[$value] = round($scores[substr($value, 0, 3)], 2); | ||
} | ||
|
||
return $array; | ||
} | ||
|
||
/** | ||
* Get the confidence of a decision for a result. The closer to 1, the better. | ||
* | ||
* @param string $string The given sentence | ||
* | ||
* @return float The confidence of a decision for a result. The close to 1, the better | ||
*/ | ||
public function score($string) | ||
{ | ||
$scores = $this->scores($string); | ||
|
||
return max($scores); | ||
} | ||
|
||
/** | ||
* Tells if a sentence is positive. | ||
* | ||
* @param string $string The given sentence | ||
* | ||
* @return bool | ||
*/ | ||
public function isPositive($string) | ||
{ | ||
return $this->decision($string) == self::POSITIVE; | ||
} | ||
|
||
/** | ||
* Tells if a sentence is negative. | ||
* | ||
* @param string $string The given sentence | ||
* | ||
* @return bool | ||
*/ | ||
public function isNegative($string) | ||
{ | ||
return $this->decision($string) == self::NEGATIVE; | ||
} | ||
|
||
/** | ||
* Tells if a sentence is neutral. | ||
* | ||
* @param string $string The given sentence | ||
* | ||
* @return bool | ||
*/ | ||
public function isNeutral($string) | ||
{ | ||
return $this->decision($string) == self::NEUTRAL; | ||
} | ||
} | ||
class SentimentAnalysis { | ||
|
||
private $sentiment; | ||
const NEGATIVE = 'negative'; | ||
const NEUTRAL = 'neutral'; | ||
const POSITIVE = 'positive'; | ||
|
||
public function __construct() | ||
{ | ||
$this->sentiment = new Sentiment(); | ||
} | ||
|
||
/** | ||
* Get the sentiment of a phrase | ||
* @param string $string The given sentence | ||
* @return string Possible values: negative|neutral|positive | ||
*/ | ||
public function decision($string) | ||
{ | ||
// Do not call functions so that we'll compute only one time | ||
$dominantClass = $this->sentiment->categorise($string); | ||
|
||
switch ($dominantClass) { | ||
case 'neg': | ||
return self::NEGATIVE; | ||
break; | ||
|
||
case 'neu': | ||
return self::NEUTRAL; | ||
break; | ||
|
||
case 'pos': | ||
return self::POSITIVE; | ||
break; | ||
} | ||
} | ||
|
||
/** | ||
* Get scores for each decision | ||
* @param string $string The original string | ||
* @return array An array containing keys 'negative', 'neutral' and 'positive' with a float. The closer to 1, the better | ||
* @example ['negative' => 0.5, 'neutral' => 0.25, 'positive' => 0.25] | ||
*/ | ||
public function scores($string) | ||
{ | ||
$scores = $this->sentiment->score($string); | ||
$array = array(); | ||
|
||
// The original keys are 'neg' / 'neu' / 'pos' | ||
// We will remap to 'negative' / 'neutral' / 'positive' and round with 2 digits | ||
foreach ([self::NEGATIVE, self::NEUTRAL, self::POSITIVE] as $value) | ||
$array[$value] = round($scores[substr($value, 0, 3)], 2); | ||
|
||
return $array; | ||
} | ||
|
||
/** | ||
* Get the confidence of a decision for a result. The closer to 1, the better | ||
* @param string $string The given sentence | ||
* @return float The confidence of a decision for a result. The close to 1, the better | ||
*/ | ||
public function score($string) | ||
{ | ||
$scores = $this->scores($string); | ||
return max($scores); | ||
} | ||
|
||
/** | ||
* Tells if a sentence is positive | ||
* @param string $string The given sentence | ||
* @return boolean | ||
*/ | ||
public function isPositive($string) | ||
{ | ||
return $this->decision($string) == self::POSITIVE; | ||
} | ||
|
||
/** | ||
* Tells if a sentence is negative | ||
* @param string $string The given sentence | ||
* @return boolean | ||
*/ | ||
public function isNegative($string) | ||
{ | ||
return $this->decision($string) == self::NEGATIVE; | ||
} | ||
|
||
/** | ||
* Tells if a sentence is neutral | ||
* @param string $string The given sentence | ||
* @return boolean | ||
*/ | ||
public function isNeutral($string) | ||
{ | ||
return $this->decision($string) == self::NEUTRAL; | ||
} | ||
} |
Oops, something went wrong.