Skip to content

michaeljhopkins/Laravel-Aylien-Wrapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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
            ]);
        });
    }
}

About

A wrapper around the Aylien PHP SDK to make it Laravel 5 friendly

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages