Master LLMs with our FREE course in collaboration with Activeloop & Intel Disruptor Initiative. Join now!

Publication

Prerequisites
Latest   Machine Learning

Prerequisites

Last Updated on July 25, 2023 by Editorial Team

Author(s): Thomas Kraehe

Originally published on Towards AI.

Using Google Cloud’s Machine Learning as a Service U+007C Towards AI

Analyzing the Mood of Chat Messages with Google Cloud’s Natural Language API

With the help of NLP services like the Natural Language API of Google Cloud, it is possible to analyze the mood of a text. Google calls this feature the “Sentiment Analysis”. Let’s explore how this works and how you can use it for your chatbot or CRM.

Before starting the development, let’s have a quick look at the try out tool. You can just enter a random message in the text field and click “Analyze”. In my example, I typed the message “I am so happy that the sun is shining here in Munich and we can swim in the Isar.”.

As you can see, the service detected all kinds of things in the message, for example, entities like the place Munich. But we are interested in the sentiment analysis, so please choose the tab “Sentiment”.

The result of the sentiment analysis shows a score of 0.8 and a magnitude of 0.8 for my provided text. As you can see in the color scheme, a score between 0.25 and 1.0 stands for positive sentiment, a score between -0.25 an 0.25 for a neutral mood and a score between -0.25 and -1.0 determines a negative feeling. The magnitude tells you how strong the detected mood is. The value can reach from 0.0 till infinitely.

Pretty impressive, isn’t it? So, how can we make use of this feature? In my example, I am analyzing incoming chat messages from users on WhatsApp. This is useful if you are for example providing customer service via the WhatsApp Business API and want to know how the mood of your customers is. Here you can see how we are using it at MessengerPeople:

So, if you are thrilled now, I will show you how to implement that feature.

Of course, you need a Google Cloud account to make use of the API. If you already have that, you can jump directly to the quick start section of the GCP NLP documentation. The basics of the NLP API are explained here.

Now you have to create a GCP Console-Project and add a key for your service account. Then you have to store that key as a JSON-file on your system and add the path to that key as an environment variable GOOGLE_APPLICATION_CREDENTIALS on your system, like this:

export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"

Now you are principally ready to make calls to the NLP API. You can try the command line version first if you like. However, in my example, I will show you how to use the PHP client library.

A real-world use case

Ok, let’s start coding! Please note, that I will only give you some quick examples. This is no production-ready code!

First, install the PHP client library in your project:

composer require google/cloud

Then load the library:

use Google\Cloud\Language\LanguageClient;

Now I can make use of the methods of the client in my own function:

public function sendToGoogleCloud($text)
{
// Your Google Cloud Platform project ID
$projectId = 'xxxxxxxxx';
$keyfilepath = "/var/www/xxxxxxxx.json";

// Instantiates a client
$language = new LanguageClient([
'projectId' => $projectId,
'keyFilePath' => $keyfilepath
]);

// Detects the sentiment of the text
$annotation = $language->analyzeSentiment($text);
$sentiment = $annotation->sentiment();
$language = $annotation->language();

$returnarray = [];
array_push($returnarray, $sentiment);
array_push($returnarray, $language);

return $returnarray;

}

In my tests I had problems with the environment variable, that is why I used the other method of providing the key as a parameter. The response of the sentiment analysis looks like this:

{
"documentSentiment": {
"magnitude": 0.8,
"score": 0.8
},
"language": "en",
"sentences": [
{
"sentiment": {
"magnitude": 0.8,
"score": 0.8
},
"text": {
"beginOffset": 0,
"content": "I am so happy that the sun is shining here in Munich and we can swim in the Isar."
}
}
]
}

As you can see, the NLP API gives you a score and a magnitude as well as the detected language of the text. This is why we could also make use of the language.

So what can we do with this information? Well, depending on the detected sentiment, we can set labels in our system, respond accordingly to the mood (and language) or set some properties in the user's profile.

if ($sentiment_score > 0.25) {
$sentiment = 'positive';
$addlabel = [3345];
$removelabel = [3344, 3346];
} elseif (($sentiment_score >= -0.25) && ($sentiment_score <= 0.25)) {
$sentiment = 'neutral';
$addlabel = [3346];
$removelabel = [3344, 3345];
} elseif ($sentiment_score < -0.25) {
$sentiment = 'negative';
$addlabel = [3344];
$removelabel = [3345, 3346];
} else {
$sentiment = 'neutral';
$addlabel = [3346];
$removelabel = [3344, 3345];
}


$customfields = [
'customfields' => [
'sentiment_score' => $sentiment_score,
'sentiment_magnitude' => $sentiment_magnitude,
'sentiment' => $sentiment,
'language' => $sentiment_language
],
'addlabel' => $addlabel,
'removelabel' => $removelabel
];

The result looks like this:

I set the label “Gute Stimmung” which is German for “good mood” and I set the user properties “Sentiment” to “positive” and “Language” to “en”.

Of course, we can also send a response to the chat message depending on the mood. So, let’s just add a call for this.

if ($sentiment_score > 0.25) {
$responseText = "Good to hear that you are happy U+1F600";
$this->sendChatResponse($msg['usernumber'], $responseText);
}

I hope you got a good impression of what great things are possible with the NLP API. And last but not least: of course there are also other providers out there like Microsoft Azure, IBM Watson or AWS that offer similar products. By now I cannot tell which service returns the best results.

Happy exploring!

Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming a sponsor.

Published via Towards AI

Feedback ↓