How to Segment Words from a Text String in PHP

Cloudmersive
1 min readApr 3, 2023

--

Without an efficient segmentation workflow in place, it’s extremely difficult to train Natural Language Processing (NLP) services. Thankfully, using the code examples below, you can easily take advantage of a free-to-use Word Segmentation API to streamline the segmentation process. The underlying service will return each word from the original input sentence in a list, also identifying the index, start position and end position of those words. Refer to the below JSON response model:

{
"Words": [
{
"Word": "string",
"WordIndex": 0,
"StartPosition": 0,
"EndPosition": 0
}
]
}

To take advantage of this API, let’s first install the PHP client. We can do so with Composer by executing the following command:

composer require cloudmersive/cloudmersive_nlp_api_client

After that, let’s structure our API calls with the following examples:

<?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\GetWordsRequest(); // \Swagger\Client\Model\GetWordsRequest | String to process

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

To authenticate requests for free, grab a free-tier API key from our website; you can do so by registering a free account. Your free-tier API key will supply a limit of 800 API calls per month and no additional commitments (once you hit your limit, the total will reset the following month).

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet