ABSYZ ABSYZ

  • Home

    Home

  • About us

    Who We Are

  • Our Expertise

    What we Do

  • Our Approach

    How We Do It

  • Products

    What We Made

  • Industries

    Who We Do It For

  • Clients

    Whom We Did It For.

  • Article & Blogs

    What Experts Think

  • Careers

    Join The Team

  • Get In Touch

    Let’s Get Started

ABSYZ

Einstein Sentiment Analysis

Home / Article & Blogs / Salesforce / Lightning / Einstein Sentiment Analysis
By Pushmitha Babu inLightning, Salesforce, Salesforce Einstein

Einstein Sentiment is something to predict the reviews or messages whether it is positive, negative or neutral. Using these companies can categorize the customer attitudes and take action appropriately to build their insights. As earlier discussed in marketing cloud (https://teamforcesite.wordpress.com/2018/02/07/marketing-cloud-social-studio-series-macros/) sentiment analysis can be achieved even using Einstein.

Here user enters some message, Einstein finds the sentiment that will be useful to companies finding it as an appreciation when it is positive and take action when it is negative. This scenario will be helpful when handling more number of clients. For example in real-time usage facebook comments, client reply, etc.

To start with Einstein, first set up your environment with the help of the trailhead (https://trailhead.salesforce.com/modules/einstein_intent_basics/units/einstein_intent_basics_env). For one account mail id, only one key is generated that is stored in files and we get access token generated with the time limit being activated. Create two apex class to generate a JWT access token, you can refer the trailhead (https://trailhead.salesforce.com/projects/predictive_vision_apex/steps/predictive_vision_apex_get_code).

[sourcecode language=”java”]
//JWT.apex
public class JWT {

public String alg {get;set;}
public String iss {get;set;}
public String sub {get;set;}
public String aud {get;set;}
public String exp {get;set;}
public String iat {get;set;}
public Map claims {get;set;}
public Integer validFor {get;set;}
public String cert {get;set;}
public String pkcs8 {get;set;}
public String privateKey {get;set;}

public static final String HS256 = ‘HS256’;
public static final String RS256 = ‘RS256’;
public static final String NONE = ‘none’;
public JWT(String alg) {
this.alg = alg;
this.validFor = 300;
}

public String issue() {
String jwt = ”;
JSONGenerator header = JSON.createGenerator(false);
header.writeStartObject();
header.writeStringField(‘alg’, this.alg);
header.writeEndObject();
String encodedHeader = base64URLencode(Blob.valueOf(header.getAsString()));

JSONGenerator body = JSON.createGenerator(false);
body.writeStartObject();
body.writeStringField(‘iss’, this.iss);
body.writeStringField(‘sub’, this.sub);
body.writeStringField(‘aud’, this.aud);
Long rightNow = (dateTime.now().getTime()/1000)+1;
body.writeNumberField(‘iat’, rightNow);
body.writeNumberField(‘exp’, (rightNow + validFor));
if (claims != null) {
for (String claim : claims.keySet()) {
body.writeStringField(claim, claims.get(claim));
}
}
body.writeEndObject();

jwt = encodedHeader + ‘.’ + base64URLencode(Blob.valueOf(body.getAsString()));

if ( this.alg == HS256 ) {
Blob key = EncodingUtil.base64Decode(privateKey);
Blob signature = Crypto.generateMac(‘hmacSHA256’,Blob.valueof(jwt),key);
jwt += ‘.’ + base64URLencode(signature);
} else if ( this.alg == RS256 ) {
Blob signature = null;

if (cert != null ) {
signature = Crypto.signWithCertificate(‘rsa-sha256’, Blob.valueOf(jwt), cert);
} else {
Blob privateKey = EncodingUtil.base64Decode(pkcs8);
signature = Crypto.sign(‘rsa-sha256’, Blob.valueOf(jwt), privateKey);
}
jwt += ‘.’ + base64URLencode(signature);
} else if ( this.alg == NONE ) {
jwt += ‘.’;

return jwt;
}
public String base64URLencode(Blob input){
String output = encodingUtil.base64Encode(input);
output = output.replace(‘+’, ‘-‘);
output = output.replace(‘/’, ‘_’);
while ( output.endsWith(‘=’)){
output = output.subString(0,output.length()-1);
}
return output;
}
}
[/sourcecode]

To generate new access token each and every time JWTBearerFlow class is used

[sourcecode language=”java”]
//JWTBearerFlow.apex
public class JWTBearerFlow {

public static String getAccessToken(String tokenEndpoint, JWT jwt) {

String access_token = null;
String body = ‘grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=’ + jwt.issue();
HttpRequest req = new HttpRequest();
req.setMethod(‘POST’);
req.setEndpoint(tokenEndpoint);
req.setHeader(‘Content-type’, ‘application/x-www-form-urlencoded’);
req.setBody(body);
Http http = new Http();
HTTPResponse res = http.send(req);

if ( res.getStatusCode() == 200 ) {
System.JSONParser parser = System.JSON.createParser(res.getBody());
while (parser.nextToken() != null) {
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == ‘access_token’)) {
parser.nextToken();
access_token = parser.getText();
break;
}
}
}
return access_token;
}
} [/sourcecode]

Now the sentiment is analyzed through the probability given by Einstein. In the below class we refer to JWT apex class to pass the key and generate a new access token.(https://metamind.readme.io/docs/what-you-need-to-call-api). The below apex class returns a string with labels and its probability.

[sourcecode language=”java”]
//EinsteinVision_Sentiment.apex
@auraenabled
public static String findSentiment(String text)
{
ContentVersion con = [SELECT Title,VersionData
FROM ContentVersion
WHERE Title = ‘einstein_platform’
OR Title = ‘predictive_services’
ORDER BY Title LIMIT 1];

String key = con.VersionData.tostring();
key = key.replace( ‘—–BEGIN RSA PRIVATE KEY—–‘, ” );
key = key.replace( ‘—–END RSA PRIVATE KEY—–‘, ” );
key = key.replace( ‘\n’, ” );
JWT jwt = new JWT( ‘RS256’ );
jwt.pkcs8 = key;
jwt.iss = ‘developer.force.com’;
jwt.sub = ‘xxx@xxx.com’; // Update with your own email ID
jwt.aud = ‘https://api.metamind.io/v1/oauth2/token’;
jwt.exp = String.valueOf(3600);
String access_token = JWTBearerFlow.getAccessToken( ‘https://api.metamind.io/v1/oauth2/token’, jwt );
String keyaccess = access_token;

Http http = new Http();
HttpRequest req = new HttpRequest();
req.setMethod( ‘POST’ );
req.setEndpoint( ‘https://api.einstein.ai/v2/language/sentiment’);
req.setHeader( ‘Authorization’, ‘Bearer ‘ + keyaccess );
req.setHeader( ‘Content-type’, ‘application/json’ );
String body = ‘{\”modelId\”:\”CommunitySentiment\”,\”document\”:\”‘ + text + ‘\”}’;
req.setBody( body );
HTTPResponse res = http.send( req );
string fullString=res.getBody();
string removeString=fullString.removeEnd(‘],”object”:”predictresponse”}’);
string stringBody=removeString.removeStart(‘{“probabilities”:[‘);
return stringBody;
} [/sourcecode]

Using the probability, I display them as charts using chart.js which can be referred to the given link (http://www.chartjs.org/docs/latest/getting-started/). The chart.js is saved as a static resource in Salesforce and used in the component.

lightng.png

The response from apex controller is handled in js and we split the data into two lists that are passed as values to the chart data.

[sourcecode language=”java”]
({
extractfile: function(component, event, helper) {
var val = component.find(“select”).get(“v.value”);
alert(‘value’+val);
var action1 = component.get(“c.findSentiment”);
action1.setParams({ text: val });

action1.setCallback(this, function(response) {

var ret=response.getReturnValue();
component.set(“v.probability”,ret);
alert(‘probability ‘+component.get(“v.probability”));

var line=component.get(“v.probability”);
var list=line.split(‘,’);
var temp=0;
var labels=[];
var values=[];
for(var i=0;i <list.length;i++){
if(i%2==0){
list[i]=list[i].match(‘:”(.*)”‘)[1];
temp=temp+1;
labels.push(list[i]);
}
else
{
temp=temp+1;
list[i]=list[i].match(‘:(.*)}’)[1];
values.push(list[i]);
}
}
component.set(“v.labels”,labels);
component.set(“v.values”,values);

var label=component.get(“v.labels”);
var value=component.get(“v.values”);
var data = {
labels: [label[0],label[1],label[2]],
datasets: [
{
fillColor: ‘#b9f6ca’,
strokeColor: ‘#b9f6ca’,
data: [value[0],value[1],value[2]]
}
]
}
var options = { responsive: true };
var element = component.find(‘chart’).getElement();

var ctx = element.getContext(‘2d’);

var myBarChart = new Chart(ctx).Bar(data, options);
});
$A.enqueueAction(action1);
},

})
[/sourcecode]

The result of above data is shown below,

output for sentiment.png

To know more about Einstein keep visiting our blog. For any doubts, you can feel free to reach out us.

chart.jsEinsteinjwtMarketing cloudSentiment Analysis
148
Like this post
11 Posts
Pushmitha Babu

Search Posts

Archives

Categories

Recent posts

Meet OmniStudio – Revolutionize Your Creative Work

Meet OmniStudio – Revolutionize Your Creative Work

BioAsia 2023 in Hyderabad: An Annual International Event

BioAsia 2023 in Hyderabad: An Annual International Event

The Role Of Marketing in Small & Medium Enterprises

The Role Of Marketing in Small & Medium Enterprises

Salesforce For Retail: How Salesforce CRM Can Help Retailers

Salesforce For Retail: How Salesforce CRM Can Help Retailers

What is ChatGPT & How Does It Work?

What is ChatGPT & How Does It Work?

  • Previous PostMarketing Cloud Social Studio Series– Macros
  • Next PostEinstein Vision - Real Estate App

Related Posts

Meet OmniStudio – Revolutionize Your Creative Work
Salesforce

Meet OmniStudio – Revolutionize Your Creative Work

Salesforce For Retail: How Salesforce CRM Can Help Retailers
Salesforce

Salesforce For Retail: How Salesforce CRM Can Help Retailers

Introduction To Copado Devops Tool
Salesforce

Introduction To Copado Devops Tool

What is Salesforce Code Builder?
Salesforce

What is Salesforce Code Builder?

2 Comments

  1. Einstein Intent Analysis Using Einstein Language on Salesforce Chatter – ForceOlympus
    Reply
    27 February 2018
    Reply
  2. Einstein Language – Facebook Integration – ForceOlympus
    Reply
    1 March 2018
    Reply

Leave a Reply (Cancel reply)

Your email address will not be published. Required fields are marked *

*
*

ABSYZ Logo

INDIA | USA | UAE

  • About us
  • Article & Blogs
  • Careers
  • Get In Touch
  • Our Expertise
  • Our Approach
  • Products
  • Industries
  • Clients
  • White Papers

Copyright ©2022 Absyz Inc. All Rights Reserved.

youngsoft
Copy
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “ACCEPT ALL”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent. Privacy Policy
Cookie SettingsREJECT ALLACCEPT ALL
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled

Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.

CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.

Functional

Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.

Performance

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

Analytics

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.

Advertisement

Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.

Others

Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.

SAVE & ACCEPT