Delta-Vector
commited on
Commit
•
8b9ed47
1
Parent(s):
fe2a73e
Delete .py
Browse files
.py
DELETED
@@ -1,41 +0,0 @@
|
|
1 |
-
import re
|
2 |
-
def process_conversation(file_path):
|
3 |
-
# Read the TXT file
|
4 |
-
with open(file_path, "r", encoding="utf-8") as file:
|
5 |
-
lines = file.readlines()
|
6 |
-
# Store the conversation
|
7 |
-
conversation = []
|
8 |
-
is_user = True # To alternate between user and gpt
|
9 |
-
# Regex pattern to match speaker before the colon
|
10 |
-
pattern = re.compile(r"^(.*?):")
|
11 |
-
for line in lines:
|
12 |
-
line = line.strip() # Remove leading/trailing whitespace
|
13 |
-
if not line:
|
14 |
-
continue # Skip empty lines
|
15 |
-
# Match the speaker before the colon
|
16 |
-
match = pattern.match(line)
|
17 |
-
if match:
|
18 |
-
speaker = match.group(1)
|
19 |
-
message_dict = {
|
20 |
-
"from": "user" if is_user else "gpt",
|
21 |
-
"value": line
|
22 |
-
}
|
23 |
-
conversation.append(message_dict)
|
24 |
-
|
25 |
-
# Alternate turns between 'user' and 'gpt'
|
26 |
-
is_user = not is_user
|
27 |
-
# Ensure the conversation ends on GPT's turn
|
28 |
-
if conversation[-1]["from"] == "user":
|
29 |
-
# If it ends on 'user', remove the last entry to ensure alternation
|
30 |
-
conversation.pop()
|
31 |
-
return conversation
|
32 |
-
def write_json(conversation, output_path):
|
33 |
-
import json
|
34 |
-
with open(output_path, "w", encoding="utf-8") as outfile:
|
35 |
-
json.dump(conversation, outfile, ensure_ascii=False, indent=2)
|
36 |
-
# Call the processing function
|
37 |
-
input_file = r"AIChat [log, chat] #C.txt" # Path to your input text file
|
38 |
-
output_file = r"output.json" # Path where you want the JSON to be saved
|
39 |
-
conversation_data = process_conversation(input_file)
|
40 |
-
write_json(conversation_data, output_file)
|
41 |
-
print("Conversion complete! JSON written to", output_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|