Calculate the Talk / Listen Ratio of Speakers

This guide will show you how to use AssemblyAI’s API to calculate the talk/listen ratio of speakers in a transcript. The following code uses the Python SDK.

Quickstart

1import assemblyai as aai
2
3aai.settings.api_key = "YOUR_API_KEY"
4
5def calculate_talk_listen_ratios(transcript):
6 """
7 :param transcript: AssemblyAI Transcript object
8 :return: Dictionary with talk time, percentage, and talk-listen ratios for each speaker
9 """
10 # Ensure speaker labels were enabled
11 if not transcript.utterances:
12 raise ValueError("Speaker labels were not enabled for this transcript.")
13
14 speaker_talk_time = {}
15 total_time = 0
16
17 for utterance in transcript.utterances:
18 speaker = f"Speaker {utterance.speaker}"
19 duration = utterance.end - utterance.start
20
21 speaker_talk_time[speaker] = speaker_talk_time.get(speaker, 0) + duration
22 total_time += duration
23
24 # Calculate percentages and ratios
25 result = {}
26 for speaker, talk_time in speaker_talk_time.items():
27 percentage = (talk_time / total_time) * 100
28 result[speaker] = {
29 "talk_time_ms": talk_time,
30 "percentage": round(percentage, 2)
31 }
32
33 # Calculate talk-listen ratios for each speaker against all others
34 for speaker in result.keys():
35 other_speakers_time = sum(talk_time for spk, talk_time in speaker_talk_time.items() if spk != speaker)
36 if other_speakers_time > 0:
37 ratio = speaker_talk_time[speaker] / other_speakers_time
38 result[speaker]["talk_listen_ratio"] = round(ratio, 2)
39 else:
40 result[speaker]["talk_listen_ratio"] = None # Handle cases with only one speaker
41
42 return result
43
44transcriber = aai.Transcriber()
45audio_url = ("YOUR_AUDIO_URL")
46config = aai.TranscriptionConfig(speaker_labels=True)
47transcript = transcriber.transcribe(audio_url, config)
48
49talk_listen_stats = calculate_talk_listen_ratios(transcript)
50print(talk_listen_stats)

Get started

Before we begin, make sure you have an AssemblyAI account and an API key. You can sign up for an AssemblyAI account and get your API key from your dashboard.

Step-by-Step Instructions

Install the SDK:

$pip install assemblyai

Import the assemblyai package and set the API key.

1import assemblyai as aai
2
3aai.settings.api_key = "YOUR_API_KEY"

Define a function called calculate_talk_listen_ratios, which will calculate the talk-listen ratios for all speakers from a transcript with speaker labels. Speaker labels must be enabled for the ratios to be calculated.

1def calculate_talk_listen_ratios(transcript):
2 """
3 :param transcript: AssemblyAI Transcript object
4 :return: Dictionary with talk time, percentage, and talk-listen ratios for each speaker
5 """
6 # Ensure speaker labels were enabled
7 if not transcript.utterances:
8 raise ValueError("Speaker labels were not enabled for this transcript.")
9
10 speaker_talk_time = {}
11 total_time = 0
12
13 for utterance in transcript.utterances:
14 speaker = f"Speaker {utterance.speaker}"
15 duration = utterance.end - utterance.start
16
17 speaker_talk_time[speaker] = speaker_talk_time.get(speaker, 0) + duration
18 total_time += duration
19
20 # Calculate percentages and ratios
21 result = {}
22 for speaker, talk_time in speaker_talk_time.items():
23 percentage = (talk_time / total_time) * 100
24 result[speaker] = {
25 "talk_time_ms": talk_time,
26 "percentage": round(percentage, 2)
27 }
28
29 # Calculate talk-listen ratios for each speaker against all others
30 for speaker in result.keys():
31 other_speakers_time = sum(talk_time for spk, talk_time in speaker_talk_time.items() if spk != speaker)
32 if other_speakers_time > 0:
33 ratio = speaker_talk_time[speaker] / other_speakers_time
34 result[speaker]["talk_listen_ratio"] = round(ratio, 2)
35 else:
36 result[speaker]["talk_listen_ratio"] = None # Handle cases with only one speaker
37
38 return result

Define a transcriber, an audio_url set to a link to the audio file (replace the example link that is provided with your own), and a TranscriptionConfig with speaker_labels=True. Then create a transcript which will be sent to the function calculate_talk_listen_ratios and print out the results.

1transcriber = aai.Transcriber()
2audio_url = ("https://api.assemblyai-solutions.com/storage/v1/object/public/dual-channel-phone-data/Fisher_Call_Centre/audio05851.wav")
3config = aai.TranscriptionConfig(speaker_labels=True)
4transcript = transcriber.transcribe(audio_url, config)
5
6talk_listen_stats = calculate_talk_listen_ratios(transcript)
7print(talk_listen_stats)

Example output when using the above sample audio file:

{'Speaker A': {'talk_time_ms': 244196, 'percentage': 42.77, 'talk_listen_ratio': 0.75}, 'Speaker B': {'talk_time_ms': 326766, 'percentage': 57.23, 'talk_listen_ratio': 1.34}}