Skip to content

Commit

Permalink
Merge pull request #9 from AntoineAugusti/analysis-z9ndA5
Browse files Browse the repository at this point in the history
Applied fixes from StyleCI
  • Loading branch information
AntoineAugusti authored Nov 11, 2016
2 parents 802e2a8 + 0e3f8a5 commit 7f08080
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 201 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
<?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';
}
}
Original file line number Diff line number Diff line change
@@ -1,46 +1,47 @@
<?php namespace Antoineaugusti\LaravelSentimentAnalysis;
<?php

use Illuminate\Support\ServiceProvider;

class LaravelSentimentAnalysisServiceProvider extends ServiceProvider {
namespace Antoineaugusti\LaravelSentimentAnalysis;

/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
use Illuminate\Support\ServiceProvider;

/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
$this->package('antoineaugusti/laravel-sentiment-analysis');
}
class LaravelSentimentAnalysisServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;

/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app['sentimentanalysis'] = $this->app->share(function($app) {
return new SentimentAnalysis;
});
}
/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
$this->package('antoineaugusti/laravel-sentiment-analysis');
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return array();
}
/**
* 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 [];
}
}
213 changes: 115 additions & 98 deletions src/Antoineaugusti/LaravelSentimentAnalysis/SentimentAnalysis.php
Original file line number Diff line number Diff line change
@@ -1,101 +1,118 @@
<?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 = 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;
}
}
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;
}
}
Loading

0 comments on commit 7f08080

Please sign in to comment.