michael-hopkins / Laravel-Aylien-Wrapper by michael-hopkins

A laravel friendly wrapper around the Aylien PHP SDK
499
13
2
Package Data
Maintainer Username: michael-hopkins
Maintainer Contact: mhopkins321@gmail.com (Michael Hopkins)
Package Create Date: 2015-02-19
Package Last Update: 2018-05-18
Home Page: http://michaeljhopkins.github.io/Laravel-Aylien-Wrapper/
Language: PHP
License: MIT
Last Refreshed: 2024-04-23 03:08:36
Package Statistics
Total Downloads: 499
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 13
Total Watchers: 2
Total Forks: 6
Total Open Issues: 1

A Laravel 5 friendly wrapper around the Aylien PHP SDK

Aylien is a package consisting of eight different Natural Language Processing, Information Retrieval and Machine Learning APIs that can be adapted to your processes and applications with relative ease. Admittidly this wrapper doesn't add a significant value to their extensive, well documented PHP SDK, as the primary purpose is to provide the facade Aylien:: to your Laravel 5 app.


Latest Stable Version Total Downloads Latest Unstable Version License SensioLabsInsight


#Installation

Require this package in your composer.json and update composer. Run/add either of the below two commands

"hopkins/laravel-aylien-wrapper": "dev-master"

or

composer require hopkins/laravel-aylien-wrapper=dev-master

After updating composer, add the ServiceProvider to the providers array in app/config/app.php

'Hopkins\LaravelAylienWrapper\Providers\AylienServiceProvider',

and the facade into your array of facades

'Aylien'    => 'Hopkins\LaravelAylienWrapper\Facades\Aylien',

Run the artisan command to bring the config into your project

php artisan vendor:publish

I put my api keys in the .env file. To use this style of config you can copy the below into your config/aylien.php

return [
    'app_id' => env('API_AYLIEN_APP_ID'),
    'app_key' => env('API_AYLIEN_APP_KEY')
];

#How to use

Aylien provides a number of great examples on their site, as well as terrific documentation. However I'd feel bad if I didn't at least provide one example. The example at the bottom of the readme uses the Sentiment analysis, however all of the following are available for use

Aylien::Sentiment();
Aylien::Extract();
Aylien::Classify();
Aylien::Concepts();
Aylien::Hashtags();
Aylien::Entities();
Aylien::Language();
Aylien::Related();
Aylien::Summarize();
Aylien::Microformats();
Aylien::UnsupervisedClassify();

Let's say you want to log all messages you receive from a chat/email client/contact form. You'd have a Message.php model in this case that saved to your database. By adding a public static function boot() method we can utilize Laravel's model events to make sure first we get the message into the db, and then take the time to call Aylien's API. The below example is using the Sentiment anaylisis

class Message extends BaseModel
{
    protected $guarded = ['id'];
    public static function boot()
    {
        parent::boot();
        Message::created(function(Message $message)
        {
            $aylienResponse = \Aylien::Sentiment(['text'=>$message->message]);
            $message->update([
                'polarity' => $aylienResponse->polarity,
                'polarity_confidence' => $aylienResponse->polarity_confidence,
                'subjectivity' => $aylienResponse->subjectivity,
                'subjectivity_confidence' => $aylienResponse->subjectivity_confidence
            ]);
        });
    }
}