I currently have a text conversation as a list of dictionaries in the form:
conversation = [
{sender:"name1", message:"String1...", time:"2022-01-27 10:30:00 UTC"},
{sender:"name1", message:"String2...", time:"2022-01-27 10:30:30 UTC"},
{sender:"name2", message:"String3...", time:"2022-01-27 10:35:00 UTC"}
{sender:"name1", message:"String4...", time:"2022-01-27 10:36:00 UTC"}
{sender:"name1", message:"String5...", time:"2022-01-27 10:40:00 UTC"}
]
What I want to do is group the messages so that if they were sent by the same person and within 1 minute of the previous message they are grouped. What is the best way to do this?
For example, in the list shown above I would want String1 and String2 to be grouped into 1 message, because they were sent by the same person within the same minute, the rest of the list would then stay the same, because the sender is either different to the previous message or the messages are more than 1 minute apart.
Although ideally I would like them grouped by both sender and time, it would also be fine if someone could just show me how to just group contiguous messages with the same sender, no matter when they were sent.
I am using Python 3.9 if that makes any difference
CodePudding user response:
The 'easiest' way to do this is just sort your data by send timestamp (if it isn't already sorted) and then loop over the list, appending to a new list as you go along. Something like
def are_within_one_minute(date_string1, date_string2):
# Left as an exercise to figure out how to compare those two strings
# should be easy if you google the python datetime library
def merge_conversations(conversations):
# Sort the conversations by timestamp
# If you don't want to do an in place sort for whatever reason
# use 'sorted' instead
conversations.sort(key=lambda x: x["time"])
aggregated_conversations = []
merged_message = None
for message in conversation:
if merged_message == None:
merged_message = message
elif merged_message['sender'] == message['sender'] and are_within_one_minute(merged_message['time'], message['time']):
merged_message_content = merged_message['message'] ' ' message['message']
merged_message['message'] = merged_message_content
merged_message['time'] = message['time']
else:
aggregated_conversations.append(merged_message)
merged_message = message
# Catch the final message. No off-by-one errors!
if merged_message != None:
aggregated_conversations.append(merged_message)
return aggregated_conversations
Also remember that Python dictionary keys must be quoted unlike Javascript object keys.
