Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assign_word_speakers fix #590

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions whisperx/diarize.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,18 @@ def assign_word_speakers(diarize_df, transcript_result, fill_nearest=False):
# assign speaker to segment (if any)
diarize_df['intersection'] = np.minimum(diarize_df['end'], seg['end']) - np.maximum(diarize_df['start'], seg['start'])
diarize_df['union'] = np.maximum(diarize_df['end'], seg['end']) - np.minimum(diarize_df['start'], seg['start'])
# remove no hit, otherwise we look for closest (even negative intersection...)
if not fill_nearest:
dia_tmp = diarize_df[diarize_df['intersection'] > 0]
else:
dia_tmp = diarize_df
if len(dia_tmp) > 0:
# sum over speakers
speaker = dia_tmp.groupby("speaker")["intersection"].sum().sort_values(ascending=False).index[0]

intersected = diarize_df[diarize_df["intersection"] > 0]

speaker = None
if len(intersected) > 0:
# Choosing most strong intersection
speaker = intersected.groupby("speaker")["intersection"].sum().sort_values(ascending=False).index[0]
elif fill_nearest:
# Otherwise choosing closest
speaker = diarize_df.sort_values(by=["intersection"], ascending=False)["speaker"].values[0]

if speaker is not None:
seg["speaker"] = speaker

# assign speaker to words
Expand All @@ -54,15 +58,19 @@ def assign_word_speakers(diarize_df, transcript_result, fill_nearest=False):
if 'start' in word:
diarize_df['intersection'] = np.minimum(diarize_df['end'], word['end']) - np.maximum(diarize_df['start'], word['start'])
diarize_df['union'] = np.maximum(diarize_df['end'], word['end']) - np.minimum(diarize_df['start'], word['start'])
# remove no hit
if not fill_nearest:
dia_tmp = diarize_df[diarize_df['intersection'] > 0]
else:
dia_tmp = diarize_df
if len(dia_tmp) > 0:
# sum over speakers
speaker = dia_tmp.groupby("speaker")["intersection"].sum().sort_values(ascending=False).index[0]
word["speaker"] = speaker

intersected = diarize_df[diarize_df["intersection"] > 0]

word_speaker = None
if len(intersected) > 0:
# Choosing most strong intersection
word_speaker = intersected.groupby("speaker")["intersection"].sum().sort_values(ascending=False).index[0]
elif fill_nearest:
# Otherwise choosing closest
word_speaker = diarize_df.sort_values(by=["intersection"], ascending=False)["speaker"].values[0]

if word_speaker is not None:
word["speaker"] = word_speaker

return transcript_result

Expand Down