Skip to main content
One major part of understanding intent is understanding sentiment. Fusion has done the heavy lifting by integrating state-of-the-art, pre-trained sentiment analysis models directly into the platform. Lucidworks makes these models readily available for search developers to embed sentiment scoring into the query and indexing pipelines as stages.

What are sentiment analysis and sentiment prediction?

Sentiment analysis is the interpretation of queries or content to determine some of their subjective aspects, such as emotions, intentions, opinions, and so on, using machine learning. Sentiment analysis produces a sentiment model that Fusion can use to perform sentiment prediction. Since stronger sentiments can be positive or negative while a weaker sentiment is more neutral, sentiment analysis uses the positive/negative polarity to measure a sentiment’s strength:
Strongly                Strongly
Negative    Neutral     Positive
  -2    -1     0     1     2
For example, a query on a customer support site can express negative emotions, as in “How can I fix this frustrating upgrade problem?” Similarly, a shopper may imply a positive or negative intention to purchase an item, or a positive or negative opinion of that item.

Intelligent applications with sentiment analysis and prediction

When you assign sentiment values to incoming queries or to documents at indexing time, you can leverage that data to deliver more insight to your business applications. Understanding the intent and sentiment of customers or employees can help determine which information you deliver or how you treat subsequent interactions in the workflow. This kind of intelligence is valuable for e-commerce, call centers, social media, or surveys.

Pre-trained sentiment models

Lucidworks provides two pre-trained sentiment models that you can deploy to get started:
sentiment-general:v1.0sentiment-reviews:v1.0
A general-purpose model, trained on short sentences.

Suitable for short texts and for intent prediction.

See Deploy the sentiment-general Model for instructions.
A model trained on a variety of customer reviews.

Optimized for longer texts. It also supports highlighting the tokens that provide stronger sentiment.

See Deploy the sentiment-reviews Model for instructions.
This topic explains how to deploy the sentiment-general pre-trained sentiment prediction model. This is a general-purpose sentiment prediction model, trained on short sentences. It is suitable for short texts and for intent prediction.

Install the model in Fusion

  1. Navigate to Collections > Jobs.
  2. Select New > Create Seldon Core Model Deployment.
  3. Configure the job as follows:
    • Job ID. The ID for this job, such as deploy-sentiment-general.
    • Model Name. The model name of the Seldon Core deployment that will be referenced in the Machine Learning pipeline stage configurations, such as sentiment-general.
    • Docker Repository. The value is lucidworks.
    • Image Name. The value is sentiment-general:v1.0.
    • Kubernetes Secret Name for Model Repo. This value is left empty.
    • Output Column Names for Model. The value is [label, score].
  4. Click Save.
  5. Click Run > Start.
If you are running a private Docker repository that virtualizes external images, enter your private registry into the Docker Repository field and lucidworks/sentiment-general:v1.0 into the Image Name field.

Configure the Machine Learning pipeline stages

In your index or query pipelines add Machine Learning stage and specify sentiment-general in the Model ID field (or a custom model name that was used during deployment).

Configure the Machine Learning index stage

  1. In your index pipeline, click Add a Stage > Machine Learning.
  2. In the Model ID field, enter the model name you configured above, such as sentiment-general.
  3. In the Model input transformation script field, enter the following:
    var modelInput = new java.util.HashMap()
    modelInput.put("text", doc.getFirstFieldValue("text"))
    modelInput
    
  4. In the Model output transformation script field, enter the following:
    doc.addField("sentiment_label_s", modelOutput.get("label")[0])
    doc.addField("sentiment_score_d", modelOutput.get("score")[0])
    
  5. Save the pipeline.

Configure the Machine Learning query stage

  1. In your query pipeline, click Add a Stage > Machine Learning.
  2. In the Model ID field, enter the model name you configured above, such as sentiment-general.
  3. In the Model input transformation script field, enter the following:
    var modelInput = new java.util.HashMap()
    modelInput.put("text", request.getFirstParam("q"))
    modelInput
    
  4. In the Model output transformation script field, enter the following:
    {/* // To put into request */}
    request.putSingleParam("sentiment_label", modelOutput.get("label")[0])
    request.putSingleParam("sentiment_score", modelOutput.get("score")[0])
    
    {/* // To put into query context */}
    context.put("sentiment_label", modelOutput.get("label")[0])
    context.put("sentiment_score", modelOutput.get("score")[0])
    
    {/* // To put into response documents. NOTE: This can be done only after Solr Query stage */}
    var docs = response.get().getInnerResponse().getDocuments();
    var ndocs = new java.util.ArrayList();
    
    for (var i=0; i<docs.length;i++){
      var doc = docs[i];
      doc.putField("query_sentiment_label", modelOutput.get("label")[0])
      doc.putField("query_sentiment_score", modelOutput.get("score")[0])
      ndocs.add(doc);
    }
    
    response.get().getInnerResponse().updateDocuments(ndocs);
    
  5. Save the pipeline.

Model output

Both of the pre-trained models output the following:
  • a label, negative or positive
  • a score from -2 to 2
The sentiment-reviews:v1.0 model also optionally outputs tokens and their corresponding attention weights, that is, the weight that each token carries in the sentiment prediction. The total of all attention weights is always 1, that is, each value represents a percentage of the total weight. In the example below, “awesome” has the highest weight because it expresses the strongest sentiment compared to other tokens in the string:
{/* // Input */}
text = "That is awesome!"

{/* // Output */}
sentiment_label = ‘positive’
sentiment_score = 1.998
sentiment_attention_tokens = ['That', "'", 's', 'awesome', '!']
sentiment_attention_weights = [0.154, 0.078, 0.069, 0.444, 0.255]
This topic explains how to deploy the sentiment-reviews pre-trained sentiment prediction model. This model is trained on a variety of customer reviews and optimized for longer texts. It also supports attention weights output that can be used for highlighting the tokens that provide stronger sentiment; see Model output below for an example.

Install the model in Fusion

  1. Navigate to Collections > Jobs.
  2. Select New > Create Seldon Core Model Deployment.
  3. Configure the job as follows:
    • Job ID. The ID for this job, such as deploy-sentiment-reviews
    • Model Name. The model name of the Seldon Core deployment that will be referenced in the Machine Learning pipeline stage configurations, such as sentiment-reviews.
    • Docker Repository. The value is lucidworks.
    • Image Name. The value is sentiment-reviews:v1.0.
    • Kubernetes Secret Name for Model Repo. The value is left empty.
    • Output Column Names for Model. The value is [label, score, tokens, attention_weights].
  4. Click Save.
  5. Click Run > Start.

Configure the Machine Learning pipeline stages

You can put your sentiment prediction model to work using the Machine Learning index stage or Machine Learning query stage. You will specify the same Model Name that you used when you installed the model above.Generally, you only need to apply the model in the index pipeline, in order to perform sentiment prediction on your content. Optionally, you can configure the query pipeline in a similar way, to perform sentiment prediction on incoming queries and outgoing responses and apply special treatment depending on the prediction.

Configure the Machine Learning index stage

  1. In your index pipeline, click Add a Stage > Machine Learning.
  2. In the Model ID field, enter the model name you configured above, such as sentiment-reviews.
  3. In the Model input transformation script field, enter one of the following, depending on whether you want to output attention weights:
    Without attention weights:
    var modelInput = new java.util.HashMap()
    modelInput.put("text", doc.getFirstFieldValue("text"))
    modelInput
    
    With attention weights:
    var modelInput = new java.util.HashMap()
    modelInput.put("text", doc.getFirstFieldValue("text"))
    modelInput.put("attention_output", "true")
    modelInput
    
  4. In the Model output transformation script field, enter the following:
    Without attention weights:
    doc.addField("sentiment_label_s", modelOutput.get("label")[0])
    doc.addField("sentiment_score_d", modelOutput.get("score")[0])
    
    With attention weights:
    doc.addField("sentiment_label_s", modelOutput.get("label")[0])
    doc.addField("sentiment_score_d", modelOutput.get("score")[0])
    doc.addField("sentiment_attention_tokens_ss", modelOutput.get("tokens"))
    doc.addField("sentiment_attention_weights_ds", modelOutput.get("attention_weights"))
    
  5. Save the pipeline.

Optional: Configure the Machine Learning query stage

  1. In your query pipeline, click Add a Stage > Machine Learning.
  2. In the Model ID field, enter the model name you configured above, such as sentiment-reviews.
  3. In the Model input transformation script field, enter the following:
    Without attention weights:
    var modelInput = new java.util.HashMap()
    modelInput.put("text", request.getFirstParam("q"))
    modelInput
    
    With attention weights:
    var modelInput = new java.util.HashMap()
    modelInput.put("text", request.getFirstParam("q"))
    modelInput.put("attention_output", "true")
    modelInput
    
  4. In the Model output transformation script field, enter the following, noting the sections that need to be uncommented if you are using attention weights:
    {/* // To put into request */}
    request.putSingleParam("sentiment_label", modelOutput.get("label")[0])
    request.putSingleParam("sentiment_score", modelOutput.get("score")[0])
    
    {/* // With attention output also uncomment this */}
    {/* // request.putSingleParam("sentiment_attention_tokens", modelOutput.get("tokens")) */}
    {/* // request.putSingleParam("sentiment_attention_weights", modelOutput.get("attention_weights")) */}
    
    {/* // To put into query context */}
    context.put("sentiment_label", modelOutput.get("label")[0])
    context.put("sentiment_score", modelOutput.get("score")[0])
    
    {/* // With attention output also uncomment this */}
    {/* // context.put("sentiment_attention_tokens", modelOutput.get("tokens")) */}
    {/* // context.put("sentiment_attention_weights", modelOutput.get("attention_weights")) */}
    
    {/* // To put into response documents (can be done only after Solr Query stage) */}
    var docs = response.get().getInnerResponse().getDocuments();
    var ndocs = new java.util.ArrayList();
    
    var attention_tokens = modelOutput.get("tokens")
    var attention_weights = modelOutput.get("attention_weights")
    var attention_tokens_arr = new java.util.ArrayList(attention_tokens.size());
    var attention_weights_arr = new java.util.ArrayList(attention_weights.size());
    for ( i = 0; i < attention_tokens.size(); i++) {
     attention_tokens_arr.add(attention_tokens[i])
     attention_weights_arr.add(attention_weights[i])
    }
    
    for (var i=0; i<docs.length;i++){
     var doc = docs[i];
     doc.putField("query_sentiment_label", modelOutput.get("label")[0])
     doc.putField("query_sentiment_score", modelOutput.get("score")[0])
    {/*   // With attention output also uncomment this */}
    {/*   // doc.putField("query_sentiment_attention_tokens", attention_tokens_arr) */}
    {/*   // doc.putField("query_sentiment_attention_weights", attention_weights_arr) */}
     ndocs.add(doc);
    }
    
    response.get().getInnerResponse().updateDocuments(ndocs);
    
  5. Save the pipeline.

Model output

Both of the pre-trained models output the following:
  • a label, negative or positive
  • a score from -2 to 2
The sentiment-reviews:v1.0 model also optionally outputs tokens and their corresponding attention weights, that is, the weight that each token carries in the sentiment prediction. The total of all attention weights is always 1, that is, each value represents a percentage of the total weight. In the example below, “awesome” has the highest weight because it expresses the strongest sentiment compared to other tokens in the string:
{/* // Input */}
text = "That is awesome!"

{/* // Output */}
sentiment_label = ‘positive’
sentiment_score = 1.998
sentiment_attention_tokens = ['That', "'", 's', 'awesome', '!']
sentiment_attention_weights = [0.154, 0.078, 0.069, 0.444, 0.255]
These models support the English language only.

Model output

Both of the pre-trained models output the following:
  • a label, negative or positive
  • a score from -2 to 2
The sentiment-reviews:v1.0 model also optionally outputs tokens and their corresponding attention weights, that is, the weight that each token carries in the sentiment prediction. The total of all attention weights is always 1, that is, each value represents a percentage of the total weight. In the example below, “awesome” has the highest weight because it expresses the strongest sentiment compared to other tokens in the string:
// Input
text = "That is awesome!"

// Output
sentiment_label = ‘positive
sentiment_score = 1.998
sentiment_attention_tokens = ['That', "'", 's', 'awesome', '!']
sentiment_attention_weights = [0.154, 0.078, 0.069, 0.444, 0.255]
I