Modify this function to deal with dialogue data being concatenated by turns: "" def…
- User
Modify this function to deal with dialogue data being concatenated by turns:
"" def merge_turns_by_discussion(df): # remove unnecessary columns df = df[["Discussion", "Turn", "Author", "Timestamp", "Text_str"]] df = df.rename(columns={"Text_str": "Text"}) df["Turn"] = df["Turn"].astype(int) grouped = df.groupby(['Discussion', 'Turn']) merged_rows = []
for (discussion, turn), group in grouped: # Concatenate all Text values merged_text = ' '.join(group['Text'].astype(str)) # Merge authors (unique) authors = ', '.join(group['Author'].unique()) # Use the first row as reference for other fields first_row = group.iloc[0] merged_row = { 'Discussion': discussion, 'Turn': turn, 'Author': authors, 'Timestamp': first_row['Timestamp'], 'Text': merged_text } merged_rows.append(merged_row) # save the merged DataFrame to a CSV file merged_df = pd.DataFrame(merged_rows) # add column 'speaker_text' that contains the name of the speaker and the concatenated text merged_df['speaker_text'] = merged_df.apply(lambda row: f"{row['Author']}: {row['Text']}", axis=1) merged_df = merged_df.sort_values(by=['Discussion', 'Turn']) # add turn number so that the final rows are in the form: # Turn: [integer] # Speaker: [initials or name] # Text: [raw conversation text # merged_df.to_csv(f"merged_turns_by_discussion_{DATE}.csv") # Create merged DataFrame return merged_df""
- Assistant