Skip to content

Commit

Permalink
Merge pull request #1 from AntoineAugusti/laravel-5
Browse files Browse the repository at this point in the history
Laravel 5
  • Loading branch information
AntoineAugusti committed Feb 8, 2015
2 parents 802e2a8 + d29c1af commit 5d0cf43
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 61 deletions.
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,26 @@ A Laravel wrapper for [phpInsight](https://github.com/JWHennessey/phpInsight).

## Installation

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

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.
To get the latest version of Laravel Sentiment Analysis, simply add the following line to the require block of your `composer.json` file:

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.
```
"antoineaugusti/laravel-sentiment-analysis": "~2.0"
```

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

You can register the SentimentAnalysis facade in the `aliases` key of your `app/config/app.php` file if you like.
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.

* `'SentimentAnalysis' => 'Antoineaugusti\LaravelSentimentAnalysis\Facades\SentimentAnalysis'`
`'Antoineaugusti\LaravelSentimentAnalysis\LaravelSentimentAnalysisServiceProvider'`

You can register the SentimentAnalysis facade in the `aliases` key of your `config/app.php` file if you like.

`SentimentAnalysis' => 'Antoineaugusti\LaravelSentimentAnalysis\Facades\SentimentAnalysis'`

#### Looking for a Laravel 4 compatible version?
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"`.

## Usage
Sentences can be classified as **negative**, **neutral** or **positive**. The only supported language for the moment is **English**.
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
],
"require": {
"php": ">=5.4.0",
"illuminate/support": "~4.2",
"illuminate/support": "~5.0",
"jwhennessey/phpinsight": "~2.0"
},
"require-dev": {
"phpunit/phpunit": "~4.0"
},
"autoload": {
"psr-0": {
"psr-4": {
"Antoineaugusti\\LaravelSentimentAnalysis\\": "src/"
}
}
Expand Down

This file was deleted.

File renamed without changes.
18 changes: 18 additions & 0 deletions src/LaravelSentimentAnalysisServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php namespace Antoineaugusti\LaravelSentimentAnalysis;

use Illuminate\Support\ServiceProvider;

class LaravelSentimentAnalysisServiceProvider extends ServiceProvider {

/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app['sentimentanalysis'] = $this->app->share(function($app) {
return new SentimentAnalysis;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,47 @@

class SentimentAnalysis {

/**
* @var \PHPInsight\Sentiment
*/
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) {

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]
Expand All @@ -54,23 +58,26 @@ public function scores($string)
// 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
*/
Expand All @@ -81,6 +88,7 @@ public function isPositive($string)

/**
* Tells if a sentence is negative
*
* @param string $string The given sentence
* @return boolean
*/
Expand All @@ -91,6 +99,7 @@ public function isNegative($string)

/**
* Tells if a sentence is neutral
*
* @param string $string The given sentence
* @return boolean
*/
Expand Down

0 comments on commit 5d0cf43

Please sign in to comment.