gpt-4-0314· OpenAI
WildChat-4.8M (AI2)
I have a chat logs file in json format. it is a list with lot of dictionaries like…
- User
I have a chat logs file in json format. it is a list with lot of dictionaries like {"id":id, "post":text, "replying_to":id_of_post_replying_to}. Please write a python script that creates branches of dialoges based on what post user replied to. Return a list of those dialogues
- Assistant
Here's a Python script that takes a JSON chat logs file and creates branches of dialogues based on the post a user replied to. It returns a list of those dialogues:
import json def read_chat_logs(file_name): with open(file_name, 'r') as f: chat_logs = json.load(f) return chat_logs def create_dialogue_branches(chat_logs): dialogue_references = {} for chat in chat_logs: id = chat['id'] post = chat['post'] replying_to = chat['replying_to'] if replying_to not in dialogue_references: dialogue_references[replying_to] = [[post, id]] else: dialogue_references[replying_to].append([post, id]) dialogues = [] def build_dialogue(id): if id in dialogue_references: branches = [] for post, post_id in dialogue_references[id]: branch = {'id': post_id, 'post': post, 'children': build_dialogue(post_id)} branches.append(branch) return branches else: return [] root_id = None for chat in chat_logs: if chat['replying_to'] == "": root_id = chat['id'] break if root_id: dialogues = build_dialogue(root_id) return dialogues def main(): file_name = 'chat_logs.json' # Replace with your JSON file name chat_logs = read_chat_logs(file_name) dialogues = create_dialogue_branches(chat_logs) print(json.dumps(dialogues, indent=2)) if __name__ == '__main__': main()