liveBook · Manning

Sentiment analysis

As explained in Spring AI in Action

Sentiment analysis is the computational classification of text according to expressed opinions or emotional polarity.

Sentiment analysis uses an LLM to classify user text along a continuous polarity scale, helping applications respond differently to positive, neutral, or negative experiences. section 11.3

A practical Spring implementation follows this shape:

  • Define a prompt that requests a score from -1.0 to +1.0 and a brief explanation. section 11.3
  • Represent the request with text input and return the analyzed text, score, and explanation. section 11.3
  • Expose a POST endpoint and configure ChatClient with the sentiment system prompt. section 11.3

Interpreting context

“It’s the end of the world” receives -1.0, while adding “And I feel fine” changes the result to 0.5, showing that context can reverse polarity. section 11.3

Listing 11.4 Analyzing sentiment using ChatClient
package com.example.sentimentanalysis;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SentimentController {

  private final ChatClient chatClient;

  public SentimentController(
      ChatClient.Builder chatClientBuilder,
      @Value("classpath:/sentimentSystemPrompt.st")    #1
      Resource sentimentSystemPrompt) {

    this.chatClient = chatClientBuilder
        .defaultSystem(sentimentSystemPrompt)
        .build();
  }

  @PostMapping("/sentiment")
  public SentimentAnalysis getSentiment(@RequestBody TextInput textInput) {
    return chatClient.prompt()
        .user(userSpec -> userSpec
            .text("User text: {text}")
            .param("text", textInput.text()))       #2
        .call()
        .entity(SentimentAnalysis.class);
  }

}

Inspect the controller pattern connecting Spring’s ChatClient, a system prompt resource, and the POST sentiment endpoint. section 11.3

How Spring AI in Action covers it

How deep it goes
Explains prompt design, data types, controller wiring, and endpoint results, but defers sentiment-model internals.
Examples
Evaluates increasingly contextual phrases about the end of the world, plus a positive lyric-like passage.
What it assumes
Assumes familiarity with ChatClient and comparable controllers while defining the sentiment-specific request and response types.
What you can do after
Build a Spring endpoint that returns sentiment polarity and an explanation for submitted text.
How it connects
Connects natural language experiences, LLM prompting, system prompts, HTTP POST endpoints, and Spring AI ChatClient.

Also explained in

Compare how 12 books explain sentiment analysis →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring AI in Action. The section links point to the parts of the book it draws on.