How to Segment Sentences from a String in PHP

Cloudmersive
1 min readApr 3, 2023

--

Segmentation is a key step in a variety of downstream Natural Langauge Processing (NLP) tasks. Using the below code, you can easily implement a free API to automate sentence segmentation from large text strings. The underlying service will return each sentence from the original input (separately) along with a sentence count for reference.

To get started, let’s install the PHP client using Composer. Let’s execute the following command from the command line:

composer require cloudmersive/cloudmersive_nlp_api_client

With that out of the way, let’s now use the following code example to structure our API call in PHP:

<?php
require_once(__DIR__ . '/vendor/autoload.php');

// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');



$apiInstance = new Swagger\Client\Api\SegmentationApi(


new GuzzleHttp\Client(),
$config
);
$input = new \Swagger\Client\Model\SentenceSegmentationRequest(); // \Swagger\Client\Model\SentenceSegmentationRequest | Input string

try {
$result = $apiInstance->segmentationGetSentences($input);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling SegmentationApi->segmentationGetSentences: ', $e->getMessage(), PHP_EOL;
}
?>

To authenticate your requests, head to our website and grab a free-tier API key (by registering a free account). This will provide a limit of 800 API calls per month and no additional commitments.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet