Analyze Sentiment in Text with Confluent Cloud for Apache Flink

You can use Aspect-Based Sentiment Analysis (ABSA) to determine sentiment toward specific aspects or topics within text. Instead of analyzing overall sentiment, you can identify how users feel about particular features, attributes, or elements mentioned in reviews, feedback, or social media posts.

For example, in the customer feedback “Battery life is amazing, but the screen is too dim outdoors,” you can identify positive sentiment toward “battery” and negative sentiment toward “screen,” capturing nuanced insights that overall sentiment analysis would miss.

Confluent Cloud for Apache Flink® integrates sentiment analysis capabilities on your streams by using this function:

  • AI_SENTIMENT (Early Access Program): Analyze sentiment toward specific aspects in text using aspect-based sentiment analysis with transformer models like DeBERTa.

AI_SENTIMENT

You can use the AI_SENTIMENT function to analyze text and determine sentiment toward specific aspects. The function uses a transformer model that is managed by Confluent and hosted in Confluent Cloud to classify sentiment for each aspect you specify as positive, negative, or neutral with a confidence score.

AI_SENTIMENT uses DeBERTa by default, a transformer model designed for aspect-based sentiment analysis.

During the Early Access Program, you can request additional sentiment analysis models to be made available. To request additional models, contact your Confluent representative.

Important

The AI_SENTIMENT function is an Early Access Program feature in Confluent Cloud.

An Early Access feature is a component of Confluent Cloud introduced to gain feedback. This feature should be used only for evaluation and non-production testing purposes or to provide feedback to Confluent, particularly as it becomes more widely available in follow-on preview editions.

Early Access Program features are intended for evaluation use in development and testing environments only, and not for production use. Early Access Program features are provided: (a) without support; (b) “AS IS”; and (c) without indemnification, warranty, or condition of any kind. No service level commitment will apply to Early Access Program features. Early Access Program features are considered to be a Proof of Concept as defined in the Confluent Cloud Terms of Service. Confluent may discontinue providing preview releases of the Early Access Program features at any time in Confluent’s sole discretion.

Note

AI_SENTIMENT uses models that are managed by Confluent and hosted in Confluent Cloud. You cannot use your own managed models or remote models from cloud providers with this function.

Parameters

The AI_SENTIMENT function accepts the following parameters.

aspects

  • Type: ARRAY<STRING>

  • Required: Yes

The aspects to evaluate sentiment for. For example, if you specify the array value ARRAY['battery', 'screen'], the function analyzes sentiment toward battery and screen. The array must not be null or empty.

config

  • Type: STRING

  • Required: No

A JSON object with configuration parameters. The configuration must be constant within a query.

text

  • Type: STRING

  • Required: Yes

The text to analyze for sentiment. The text must not be null or blank.

Configuration parameters

You can pass the following configuration parameters in the JSON object for the config parameter.

batchSize
  • Type: INTEGER

  • Default: 32

  • Validation: Must be > 0

The number of aspect-text pairs to process per batch.

model
  • Type: STRING

  • Default: deberta

  • Required: No

The model to use for sentiment analysis.

Output

The AI_SENTIMENT function returns a ROW data type with the following fields.

sentiment

  • Type: ARRAY<ROW<aspect STRING, label STRING, score DOUBLE>>

An array of sentiment results, one per aspect. Each entry contains the following fields:

  • aspect: The aspect name

  • label: The sentiment label. Possible string values are 'positive', 'negative', or 'neutral'.

  • score: Confidence score as a double value from 0.0 to 1.0

metadata

  • Type: STRING

A JSON string containing model metadata.

Examples

The following examples demonstrate how to use the AI_SENTIMENT function for various sentiment analysis scenarios.

Analyzing product reviews

The following example analyzes sentiment for multiple aspects in product reviews.

CREATE TABLE reviews (
    review_id BIGINT,
    review_text STRING
);

INSERT INTO reviews VALUES
    (1, 'Battery life is amazing, but the screen is too dim outdoors'),
    (2, 'The performance is excellent and screen quality is superb'),
    (3, 'Battery drains too quickly, though the performance is decent');

SELECT
    review_id,
    review_text,
    AI_SENTIMENT(
        review_text,
        ARRAY['battery', 'screen', 'performance']
    ) AS sentiment_result
FROM reviews;

Extracting individual aspect sentiments

The following example shows how you can extract specific sentiment results for individual aspects from the returned row.

SELECT
    review_id,
    review_text,
    sentiment_result.sentiment[0].aspect AS first_aspect,
    sentiment_result.sentiment[0].label AS first_label,
    sentiment_result.sentiment[0].score AS first_score,
    sentiment_result.metadata AS model_metadata
FROM (
    SELECT
        review_id,
        review_text,
        AI_SENTIMENT(
            review_text,
            ARRAY['battery', 'screen'],
            JSON_OBJECT('model' VALUE 'deberta')
        ) AS sentiment_result
    FROM reviews
);

Filtering for negative sentiment

The following example shows how you can filter reviews to find those with negative sentiment toward specific aspects.

SELECT
    review_id,
    review_text,
    s.sentiment
FROM (
    SELECT
        review_id,
        review_text,
        AI_SENTIMENT(
            review_text,
            ARRAY['battery', 'screen']
        ) AS s
    FROM reviews
)
WHERE s.sentiment[0].label = 'negative'
   OR s.sentiment[1].label = 'negative';

Using custom batch size

The following example shows how you can process sentiment analysis with a custom batch size for performance optimization.

SELECT
    review_id,
    review_text,
    AI_SENTIMENT(
        review_text,
        ARRAY['product', 'shipping', 'support'],
        JSON_OBJECT(
            'model' VALUE 'deberta',
            'batchSize' VALUE 64
        )
    ) AS sentiment_result
FROM reviews;