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] ;
        callWatsonToneAnalyser(c);
    }
    
    public  void callWatsonToneAnalyser(Case c) {
        HttpRequest req = new HttpRequest();
        req.setEndpoint('callout:Bluemix_Tone_Analyser/v3/tone_chat?version=2016-05-18');
        req.setMethod('POST');
        req.setHeader('Content-Type', 'application/json');
        req.setBody(stringToJSON(c));
        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) ; 
        //System.debug('spliteBodsy'+spliteBodsy);     
        JSONGenerator gen = JSON.createGenerator(true);
        gen.writeStartObject();
        gen.writeFieldName('utterances');
        gen.writeStartArray();
        for (integer i = 0; i <spliteBodsy.size(); i++){
            String res = spliteBodsy[i]; 
            List<String> restem =res.Split('Visitor') ; 
            If(restem.size()>1){
                gen.writeStartObject();
                gen.writeStringField('text', res.Split('Visitor')[1]);
                gen.writeStringField('user', 'customer');
                gen.writeEndObject();
                gen.writeStartObject();
                gen.writeStringField('text', res.Split('Visitor')[0]);
                gen.writeStringField('user', 'agent');
                gen.writeEndObject();
                
            }
        }
        gen.writeEndArray();
        gen.writeEndObject();
        
        String pretty = gen.getAsString();
        System.debug('pretty'+pretty);
        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="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" lang="en">
        <head>
            <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 />
            
        </head>
        <body>
            
            <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>
            </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">
                                            {!cls1.score}  
                                        </p>
                                    </div>
                                </div>
                            </apex:repeat>
                        </div>
                        
                    </apex:repeat>
                    
                </div>
            </div>
        </body>
    </html>
</apex:page>

 

 

 

 

 

 

Tags:,