12.01.4 Using AI to analyze the sentiment of a text
note
Link to the scenario template
As an example of using AI nodes to classify text, let's create a script whose execution will:
- analyze a message sent to a Telegram chat room;
- process the result of text classification and determine its "mood";
- send a notification email to the user with the content of the message, if the text is classified as negative;
- send a standard response to Telegram chat if the text is classified as positive.
tip
Such a scenario can be used, for example, to respond to and evaluate user feedback.
For text processing we will use the node distilbert-sst-2-int8 (preview) from the group AI: Text Classification.
Several nodes and links with filters must be added for the script to work successfully:
- (1) New Message Updates node to run a script when a new message is sent to Telegram chat. In order for the node to work correctly, it is necessary to perform authorization and make sure that the bot is allowed to work with messages;
- (2) Node distilbert-sst-2-int8 (preview) to process message text. The text is the output parameter of the New Message Updates node;
- (3) A JavaScript node to process the distilbert-sst-2-int8 (preview) node data and return a response with the code below. The code categorizes a message as negative if the
NEGATIVE
is greater than 0.3. This number can be replaced if necessary:
export default async function run({execution_id, input, data, store}) {
// Get the JSON string with classification results from the previous node and parse it into an array
const classificationResults = JSON.parse(data["{{2.\`result\`.\`result\`}}"]);
// Variable to store the sum of negative scores
let negativeScoreSum = 0;
// Iterate through the classification results and sum up the negative scores
classificationResults.forEach(classification => {
if (classification.label === 'NEGATIVE') {
negativeScoreSum += classification.score;
}
});
// Determine the overall result based on the sum of negative scores
const overallResult = negativeScoreSum > 0.3 ? "negative" : "positive";
// Return the result
return {
result: overallResult
};
}
- (4) Send a Text Message or Reply node to send a standard message "Thank you for your review, it is very important to us!" if the text is classified as positive. The output of the New Message Updates node can be used as the chat identifier;
- (5) Link negative before the Send a Text Message or Reply node with the filter condition
{{$3.result != "negative"}}
set so that the script proceeds to execute the Send a Text Message or Reply node if the text is classified as positive;
- (6) Send Email node for sending an email to a specified mail address with the text of a Telegram message and mentioning the negative coloring of the message. Authorization is required for the node to work correctly;
- (7) Link positive before the Send Email node with the filtering condition
{{$3.result = "negative"}}
set so that the script proceeds to execute the Send Email node if the text is classified as negative;
The outcome of the script execution is one of two actions:
- sending a standard message to Telegram;
- a letter with a customized notice.