Skip to main content
Speech to TextBatch TranscriptionSpeech Intelligence

Sentiment analysis

Learn about the sentiment analysis offering for the Speechmatics Batch API

Speechmatics enables you to understand the sentiment of what was spoken in your audio. With just a single API call, you can quickly transcribe and extract the sentiment, either positive, negative or neutral to quickly detect areas of conversation to analyse further.

If you're new to Speechmatics, please see our guide on Transcribing a File. Once you are set up, include the following config to enable Sentiment:

{
"type": "transcription",
"transcription_config": {
"language": "en"
},
"sentiment_analysis_config": {}
}

Example

Python client example to return transcription with sentiment analysis for an audio file.

from speechmatics.models import ConnectionSettings
from speechmatics.batch_client import BatchClient
from httpx import HTTPStatusError

API_KEY = "YOUR_API_KEY"
PATH_TO_FILE = "example.wav"
LANGUAGE = "en" # Transcription language

settings = ConnectionSettings(
url="https://asr.api.speechmatics.com/v2",
auth_token=API_KEY,
)

# Define transcription parameters
conf = {
"type": "transcription",
"transcription_config": {"language": LANGUAGE},
"sentiment_analysis_config": {},
}

# Open the client using a context manager
with BatchClient(settings) as client:
try:
job_id = client.submit_job(
audio=PATH_TO_FILE,
transcription_config=conf,
)
print(f"job {job_id} submitted successfully, waiting for transcript")

# Note that in production, you should set up notifications instead of polling.
# Notifications are described here: https://docs.speechmatics.com/speech-to-text/batch/notifications
transcript = client.wait_for_completion(job_id, transcription_format="json-v2")
sentiment = transcript["sentiment_analysis"]
sentiment_summary = sentiment["summary"]
sentiment_segments = sentiment["segments"]

print(sentiment_summary["overall"])
for segment in sentiment_segments:
print(
f" {segment['text']} ({segment['sentiment']})"
) # print the sentiment for each segment
except HTTPStatusError as e:
if e.response.status_code == 401:
print("Invalid API key - Check your API_KEY at the top of the code!")
elif e.response.status_code == 400:
print(e.response.json()["detail"])
else:
raise e

Understanding the Response

Sentiment results are only available in the JSON output.

Segments of the transcript are labeled with either positive, negative or neutral and a confidence score. This score indicates the model's certainty regarding the assigned sentiment prediction.

sentiment_analysis object

Holds the detailed sentiment analysis information.

segments object[]

An array of objects that represent a segment of text and its associated sentiment.

  • Array [
  • textstring

    Represents the transcript of the analysed segment

    sentimentstring

    The assigned sentiment to the segment, which can be positive, neutral or negative

    start_timefloat

    The timestamp corresponding to the beginning of the transcription segment

    end_timefloat

    The timestamp corresponding to the end of the transcription segment

    speakerstring

    The speaker label for the segment, if speaker diarization is enabled

    channelstring

    The channel label for the segment, if channel diarization is enabled

    confidencefloat

    A confidence score in the range of 0-1

  • ]
  • summary object

    An object that holds overall sentiment information, and per-speaker and per-channel sentiment data.

    overall object

    Summary for all segments in the file

    positive_countinteger
    negative_countinteger
    neutral_countinteger
    speakers object[]

    An array of objects that represent sentiment data for a specific speaker.

  • Array [
  • speakerstring
    positive_countinteger
    negative_countinteger
    neutral_countinteger
  • ]
  • channels object[]

    An array of objects that represent sentiment data for a specific channel.

  • Array [
  • channelstring
    positive_countinteger
    negative_countinteger
    neutral_countinteger
  • ]
  • {
    "job": { ... },
    "metadata": {
    "created_at": "2023-07-17T15:01:48.412714Z",
    "type": "transcription",
    "transcription_config": {
    "language": "en",
    "diarization": "speaker"
    },
    "sentiment_analysis_config": {}
    ...
    },
    "results": [...],
    "sentiment_analysis": {
    "segments": [
    {
    "confidence": 0.88,
    "end_time": 7.94,
    "sentiment": "positive",
    "speaker": "S1",
    "start_time": 3.0,
    "text": "We're delighted that you've decided to try our speech to text software to get going."
    },
    {
    "confidence": 0.76,
    "end_time": 12.18,
    "sentiment": "neutral",
    "speaker": "S1",
    "start_time": 7.95,
    "text": "Just create an API key and submit a transcription request to our API."
    }
    ],
    "summary": {
    "overall": {
    "negative_count": 0,
    "neutral_count": 1,
    "positive_count": 1
    },
    "speakers": [
    {
    "speaker": "S1",
    "negative_count": 0,
    "neutral_count": 1,
    "positive_count": 1
    }
    ]
    }
    }
    }

    Considerations

    • Sentiment is only supported for English
    • Typically, segments equate to full sentences. However, in cases of short utterances composed of 1-2 words, these can be combined with a neighboring sentence to form a segment
    • Punctuation plays a significant role in the accuracy of the Sentiment. It is therefore recommended to avoid disabling any end of sentence punctuation or reducing the punctuation sensitivity to ensure the best possible results
    • Speaker or channel labels will only be available if Diarization is enabled. By default when Diarization is not enabled "speaker":"UU" is returned to indicate that speakers of each segment are unknown
    • Speaker Change is not supported in combination with Sentiment

    Error Responses

    Learn about the sentiment analysis offering for the Speechmatics Batch API

    Unsupported Language

    Currently Sentiment is only supported for English.

    If Sentiment is requested for an unsupported language, the transcription process will complete. However, the Sentiment will not be returned, and an error message will be included in the final JSON output.

    {
    "job": { ... },
    "metadata": {
    "created_at": "2023-05-26T15:01:48.412714Z",
    "type": "transcription",
    "transcription_config": {
    "language": "cy"
    },
    "sentiment_analysis_config": {},
    "sentiment_analysis_errors": [
    {"type": "unsupported_language", "message": "Sentiment analysis not supported for cy."}
    ],
    ...
    },
    "results": [...]
    }

    Sentiment Failed

    If Sentiment fails, the transcription process will complete but the Sentiment will not be returned, and an error message will be included in the final JSON output.

    {
    "job": { ... },
    "metadata": {
    "created_at": "2023-05-26T15:01:48.412714Z",
    "type": "transcription",
    "transcription_config": {
    "language": "en"
    },
    "sentiment_analysis_config": {},
    "sentiment_analysis_errors": [
    {"type": "sentiment_analysis_failed", "message": "Sentiment Analysis failed."}
    ],
    ...
    },
    "results": [...]
    }