1. Vonage Learn
  2. Blog
  3. 2020
  4. 07
  5. 02
  6. Multi Channel Tone Analysis in Php With Amazon Comprehend
Multi-Channel Tone Analysis in PHP with Amazon Comprehend

< Tutorial />

Multi-Channel Tone Analysis in PHP with Amazon Comprehend

In this post, you'll update an AWS Lambda function introduced in a previous post where the results of a voice transcription get stored in RDS. The objective for this example is to use Amazon Comprehend to retrieve the tone analysis for an entire conversation, by channels, and then add the results to an RDS MySQL database instance.

See nexmo-community/voice-channels-aws-transcribe-php, and nexmo-community/aws-voice-transcription-rds-callback-php for an understanding of what you will be working with for this example.

Prerequisites

Setup Instructions

With the nexmo-community/aws-voice-transcription-rds-callback-php code deployed, you will need to update the index.php file in the following ways:

  • Add an import statement for AWS Comprehend, to the top of the file with the other imports.
use Aws\Comprehend\ComprehendClient;
  • From line 54 to 62, update the $conn-insert() contents to include the sentiment field.
$conn->insert('transcriptions', [
    'conversation_uuid' => $conversation_uuid,
    'channel' => ($channel['channel_label'] == 'ch_0' ? 'caller' : 'recipient'),
    'start_time' => $startTime,
    'end_time' => $endTime,
    'content' => $item['alternatives'][0]['content'],
    'sentiment' => serialize(getSentiment($conversation['content'])->toArray()),
    'created' => $record_date,
    'modified' => $record_date
]);
  • Add the following function to the end of the file, that enables sentiment to get populated for the database insertion.
function getSentiment(string $content) : Aws\Result {

    $comprehendClient = new ComprehendClient([
        'region' => $_ENV['AWS_REGION'],
        'version' => $_ENV['AWS_VERSION'],
    ]);

    return $comprehendClient->detectSentiment([
        'LanguageCode' => 'en',
        'Text' => $content,
    ]);
}
  • Update the conversations table in the RDS database created in the previous post, to include the sentiment content.
USE `voice_transcriptions`;

ALTER TABLE `conversations` ADD COLUMN `sentiment` TEXT AFTER `content`

Deploy to Lambda

With all the above updated successfully, you can now use Serverless to redeploy the app to AWS Lambda.

serverless deploy

Ready

Your app is now updated to include tone analysis with the conversation content, going forward.

Next Steps

If you have any questions or run into troubles, you can reach out to @VonageDev on Twitter or inquire in the Nexmo Community Slack team. Good luck.

Comments currently disabled.