Customer Tone Analyze from Live Agent Chat

In this blog, I am going to explain how to understand the customer tone from the live agent chat and this post is an extended version of my post about customer tone. IBM Watson has different tone analyzer services like from the text, email, and customer engagement.
You can use live agent transcript for tone analysis. simple live agent chat is looking as shown below

the result of the analysis will look as shown below.

The code is here for the same is shown here below.

public class IBMWatsonToneChatAnalyzer {
    Private String caseId {get;set;}
    public Case c{get;set;}
    public String jsonData{get;set;}
    public ToneAnalysis tones {get;set;}
    public IBMWatsonToneChatAnalyzer(ApexPages.StandardController con){
        caseId = con.getId() ; 
        c =[Select Id , Subject,Description from Case where id=:caseId] ;
    public  void callWatsonToneAnalyser(Case c) {
        HttpRequest req = new HttpRequest();
        req.setHeader('Content-Type', 'application/json');
        Http http = new Http();
        HTTPResponse res = http.send(req);
        jsonData = res.getBody();
        tones =  (ToneAnalysis) JSON.deserialize(res.getBody(), ToneAnalysis.class);
    private static String stringToJSON(Case c) {
        LiveChatTranscript trans = [Select Id , Body from LiveChatTranscript where CaseId =:c.Id ];
        String body =trans.Body;
        return     buildJson(body);
    public static String buildJson(String body){
        List<String> strSplite = body.Split('<p align="center">') ;
        String againSpl = strSplite[3];
        String spl2 = againSpl.Split('</p>')[0];
        String agentName = spl2.Split('Agent')[1] ; 
        String toMakeJson = strSplite[3].replace('Agent','') ;
        String temp = strSplite[3].replace('Agent','');
        List<String> spliteBodsy = temp.Split(agentName) ; 
        JSONGenerator gen = JSON.createGenerator(true);
        for (integer i = 0; i <spliteBodsy.size(); i++){
            String res = spliteBodsy[i]; 
            List<String> restem =res.Split('Visitor') ; 
                gen.writeStringField('text', res.Split('Visitor')[1]);
                gen.writeStringField('user', 'customer');
                gen.writeStringField('text', res.Split('Visitor')[0]);
                gen.writeStringField('user', 'agent');
        String pretty = gen.getAsString();
        return pretty ;
    public class Tones {
        public Double score{get;set;}
        public String tone_id{get;set;}
        public String tone_name{get;set;}
    public class ToneAnalysis {
        public List<Utterances_tone> utterances_tone{get;set;}
    public class Utterances_tone {
        public Integer utterance_id{get;set;}
        public String utterance_text{get;set;}
        public List<Tones> tones{get;set;}
    public class Tones_Z {
    public static ToneAnalysis parse(String json) {
        return (ToneAnalysis) System.JSON.deserialize(json, ToneAnalysis.class);

The below is the visualforce page for the same.

<apex:page standardController="Case" extensions="IBMWatsonToneChatAnalyzer" showHeader="false"  standardStylesheets="false" sidebar="false" applyHtmlTag="false" applyBodyTag="false" docType="html-5.0">
    <html xmlns="" xmlns:xlink="" lang="en">
            <meta charset="utf-8" />
            <meta http-equiv="x-ua-compatible" content="ie=edge" />
            <title>Image Recognization with Watson </title>
            <meta name="viewport" content="width=device-width, initial-scale=1" />
            <apex:slds />
            <div class="slds-scope">
                <div class="demo-only" style="padding: 0.5rem; background: rgb(51, 135, 133);">
                    <div class="slds-text-color_inverse-weak slds-text-heading_large slds-text-align_center"> Case Tone Analyzer </div>
            <div class="slds-scope">
                <div class="slds-grid">
                    <apex:repeat value="{!tones.utterances_tone}" var="cls">
                        <div class="slds-col">
                            <apex:repeat value="{!cls.tones}" var="cls1">
                                <div class="slds-page-header color-background-error-dark">
                                    <div class="slds-media__body">
                                        <p class="slds-text-body_small slds-line-height_reset">{!cls1.tone_name}</p>
                                        <p class="slds-text-body_small slds-line-height_reset">