coxlr/laravel-natural-language

Laravel package for the Google Natural language API

1.0.0 2024-03-27 20:25 UTC

This package is auto-updated.

Last update: 2024-05-01 19:48:28 UTC


README

Latest Version Tests Total Downloads

This package makes using the Google Natural API in your laravel app a breeze with minimum to no configuration, clean syntax and a consistent package API. All methods accept a string and return an array: Docs below.

It builds upon JoggApp/laravel-natural-language, a package initially developed by Harish Toshniwal which is no longer activly maintained.

natural

Installation

This package requires PHP 8.1 and Laravel 10 or higher.

You can install this package via composer using this command:

composer require coxlr/laravel-natural-language
  • The package will automatically register itself.

  • You can find documentation on how to set up the project and get the necessary configurations from the Google Cloud Platform console in a step by step detailed manner here.

  • You can publish the config file using the following command:

php artisan vendor:publish --provider="Coxlr\NaturalLanguage\NaturalLanguageServiceProvider"

This will create the package's config file called naturallanguage.php in the config directory. These are the contents of the published config file:

return [
    /*
    |--------------------------------------------------------------------------
    | The id of project created in the Google Cloud Platform console.
    |--------------------------------------------------------------------------
    */
    'project_id' => env('NATURAL_LANGUAGE_PROJECT_ID', 'sample-12345'),

    /*
    |--------------------------------------------------------------------------
    | Path to the json file containing the authentication credentials.
    |--------------------------------------------------------------------------
    */
    'key_file_path' => base_path('composer.json'),
];

Usage

After setting up the config file values you are all set to use the following methods:

  • Detect the Sentiment: Accepts a string and returns an array.
NaturalLanguage::sentiment(string $text): array
  • Detect the Entities: Accepts a string and returns an array.
NaturalLanguage::entities(string $text): array
  • Detect the Sentiment per-entity basis: Accepts a string and returns an array.
NaturalLanguage::entitySentiment(string $text): array
  • Detect the syntax: Accepts a string and returns an array.
NaturalLanguage::syntax(string $text): array
  • Detect the categories: Accepts a string and returns an array.
NaturalLanguage::categories(string $text): array
  • Annotate text: Accepts a string and an optional features array & returns an array.
NaturalLanguage::annotateText(string $text, array $features = ['sentiment', 'syntax']): array

Testing

You can run the tests with:

composer test

Changelog

Please see the CHANGELOG for more information about what has changed recently.

Security

If you discover any security related issues, please email them to hello@leecox.dev instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see the License File for more information.