Skip to content

Commit 5d0cf43

Browse files
Merge pull request #1 from AntoineAugusti/laravel-5
Laravel 5
2 parents 802e2a8 + d29c1af commit 5d0cf43

File tree

6 files changed

+51
-61
lines changed

6 files changed

+51
-61
lines changed

README.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,26 @@ A Laravel wrapper for [phpInsight](https://github.com/JWHennessey/phpInsight).
1111

1212
## Installation
1313

14-
[PHP](https://php.net) 5.4+ or [HHVM](http://hhvm.com) 3.2+, and [Composer](https://getcomposer.org) are required.
14+
[PHP](https://php.net) 5.4+ or [HHVM](http://hhvm.com) 3.3+, and [Composer](https://getcomposer.org) are required.
1515

16-
To get the latest version of Laravel Sentiment Analysis, simply require `"antoineaugusti/laravel-sentiment-analysis": "1.1.*"` in your `composer.json` file. You'll then need to run `composer install` or `composer update` to download it and have the autoloader updated.
16+
To get the latest version of Laravel Sentiment Analysis, simply add the following line to the require block of your `composer.json` file:
1717

18-
Once Laravel Sentiment Analysis is installed, you need to register the service provider. Open up `app/config/app.php` and add the following to the `providers` key.
18+
```
19+
"antoineaugusti/laravel-sentiment-analysis": "~2.0"
20+
```
1921

20-
* `'Antoineaugusti\LaravelSentimentAnalysis\LaravelSentimentAnalysisServiceProvider'`
22+
You'll then need to run `composer install` or `composer update` to download it and have the autoloader updated.
2123

22-
You can register the SentimentAnalysis facade in the `aliases` key of your `app/config/app.php` file if you like.
24+
Once Laravel Sentiment Analysis is installed, you need to register the service provider. Open up `config/app.php` and add the following to the `providers` key.
2325

24-
* `'SentimentAnalysis' => 'Antoineaugusti\LaravelSentimentAnalysis\Facades\SentimentAnalysis'`
26+
`'Antoineaugusti\LaravelSentimentAnalysis\LaravelSentimentAnalysisServiceProvider'`
27+
28+
You can register the SentimentAnalysis facade in the `aliases` key of your `config/app.php` file if you like.
29+
30+
`SentimentAnalysis' => 'Antoineaugusti\LaravelSentimentAnalysis\Facades\SentimentAnalysis'`
31+
32+
#### Looking for a Laravel 4 compatible version?
33+
Checkout the [1.1.1 version](https://github.com/AntoineAugusti/laravel-sentiment-analysis/releases/tag/v1.1.1), installable by requiring `"antoineaugusti/laravel-sentiment-analysis": "1.1.1"`.
2534

2635
## Usage
2736
Sentences can be classified as **negative**, **neutral** or **positive**. The only supported language for the moment is **English**.

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
],
1212
"require": {
1313
"php": ">=5.4.0",
14-
"illuminate/support": "~4.2",
14+
"illuminate/support": "~5.0",
1515
"jwhennessey/phpinsight": "~2.0"
1616
},
1717
"require-dev": {
1818
"phpunit/phpunit": "~4.0"
1919
},
2020
"autoload": {
21-
"psr-0": {
21+
"psr-4": {
2222
"Antoineaugusti\\LaravelSentimentAnalysis\\": "src/"
2323
}
2424
}

src/Antoineaugusti/LaravelSentimentAnalysis/LaravelSentimentAnalysisServiceProvider.php

Lines changed: 0 additions & 46 deletions
This file was deleted.

src/Antoineaugusti/LaravelSentimentAnalysis/Facades/SentimentAnalysis.php renamed to src/Facades/SentimentAnalysis.php

File renamed without changes.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php namespace Antoineaugusti\LaravelSentimentAnalysis;
2+
3+
use Illuminate\Support\ServiceProvider;
4+
5+
class LaravelSentimentAnalysisServiceProvider extends ServiceProvider {
6+
7+
/**
8+
* Register the service provider.
9+
*
10+
* @return void
11+
*/
12+
public function register()
13+
{
14+
$this->app['sentimentanalysis'] = $this->app->share(function($app) {
15+
return new SentimentAnalysis;
16+
});
17+
}
18+
}

src/Antoineaugusti/LaravelSentimentAnalysis/SentimentAnalysis.php renamed to src/SentimentAnalysis.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,47 @@
44

55
class SentimentAnalysis {
66

7+
/**
8+
* @var \PHPInsight\Sentiment
9+
*/
710
private $sentiment;
11+
812
const NEGATIVE = 'negative';
913
const NEUTRAL = 'neutral';
1014
const POSITIVE = 'positive';
1115

1216
public function __construct()
13-
{
17+
{
1418
$this->sentiment = new Sentiment();
1519
}
1620

1721
/**
1822
* Get the sentiment of a phrase
23+
*
1924
* @param string $string The given sentence
2025
* @return string Possible values: negative|neutral|positive
2126
*/
2227
public function decision($string)
2328
{
2429
// Do not call functions so that we'll compute only one time
2530
$dominantClass = $this->sentiment->categorise($string);
26-
27-
switch ($dominantClass) {
31+
32+
switch ($dominantClass)
33+
{
2834
case 'neg':
2935
return self::NEGATIVE;
30-
break;
3136

3237
case 'neu':
3338
return self::NEUTRAL;
34-
break;
3539

3640
case 'pos':
3741
return self::POSITIVE;
38-
break;
3942
}
4043
}
4144

4245
/**
4346
* Get scores for each decision
47+
*
4448
* @param string $string The original string
4549
* @return array An array containing keys 'negative', 'neutral' and 'positive' with a float. The closer to 1, the better
4650
* @example ['negative' => 0.5, 'neutral' => 0.25, 'positive' => 0.25]
@@ -54,23 +58,26 @@ public function scores($string)
5458
// We will remap to 'negative' / 'neutral' / 'positive' and round with 2 digits
5559
foreach ([self::NEGATIVE, self::NEUTRAL, self::POSITIVE] as $value)
5660
$array[$value] = round($scores[substr($value, 0, 3)], 2);
57-
61+
5862
return $array;
5963
}
6064

6165
/**
6266
* Get the confidence of a decision for a result. The closer to 1, the better
67+
*
6368
* @param string $string The given sentence
6469
* @return float The confidence of a decision for a result. The close to 1, the better
6570
*/
6671
public function score($string)
6772
{
6873
$scores = $this->scores($string);
74+
6975
return max($scores);
7076
}
7177

7278
/**
7379
* Tells if a sentence is positive
80+
*
7481
* @param string $string The given sentence
7582
* @return boolean
7683
*/
@@ -81,6 +88,7 @@ public function isPositive($string)
8188

8289
/**
8390
* Tells if a sentence is negative
91+
*
8492
* @param string $string The given sentence
8593
* @return boolean
8694
*/
@@ -91,6 +99,7 @@ public function isNegative($string)
9199

92100
/**
93101
* Tells if a sentence is neutral
102+
*
94103
* @param string $string The given sentence
95104
* @return boolean
96105
*/

0 commit comments

Comments
 (0)