Skip to content

tootootltd/azure-text-analytics

Repository files navigation

PHP Azure Text Analytics

Latest Version on Packagist MIT Licensed run-tests

A very simple wrapper around version 3.0 of Azure Cognitive Services' Text Analytics API: https://docs.microsoft.com/en-gb/azure/cognitive-services/text-analytics/

Installation

You can install the package via composer:

composer require tootootltd/azure-text-analytics

Publish the config file and put your Azure Cognitive Services endpoint and key in your env file.

php artisan vendor:publish --provider="Tootootltd\AzureTextAnalytics\AzureTextAnalyticsServiceProvider"

Requirements

  1. An Azure Cognitive Services endpoint and key.
  2. PHP 7.4

Usage

This package supports all 5 Text Analytics endpoints and each return the full raw response body.

You can pass your text into the constructor in a few different formats:

String

$myText = 'Example';

String and ID

$myText = [
	'id' => 1,
	'text' => 'Example'
];

Multiple strings and ID's

$myText = [
	[
		'id' => 1,
		'text' => 'Example one'
	],
	[
		'id' => 2,
		'text' => 'Example two'
	],
	[
		'id' => 3,
		'text' => 'Example three'
	]
];

Just pass any of these into the constructor.

$text = new AzureTextAnalytics($myText)

This package will do a bit of validation on your text before hitting Azure's API, such as;

  1. Checking the length of each document (string of text) and the number of documents per request to ensure they aren't above the Azure API's limits (5,120 characters and 1,000 documents at time of writing respectively). In both these instances an ExceededApiLimit exception will be thrown. More info on these limits can be found on Azure's documentation.
  2. Ensuring that the required fields are present when passing an array (id and text at time of writing). More info on these can be found on Azure's documentation.

Methods:

Sentiment Analysis - View example response

$text = new AzureTextAnalytics($myText)
$text->sentimentAnalsis();

Key Phrases - View example response

$text = new AzureTextAnalytics($myText)
$text->keyPhrases();

Language Detection - View example response

$text = new AzureTextAnalytics($myText)
$text->detectLanguage();

Named Entity Recognition - View example response

$text = new AzureTextAnalytics($myText)
$text->namedEntityRecognition();

Entity Linking - View example response

$text = new AzureTextAnalytics($myText)
$text->entityLinking();

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

License

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