Datasets:
initial commit
Browse files- processdata.py +70 -0
processdata.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
+
import gzip
|
4 |
+
import logging
|
5 |
+
import re
|
6 |
+
import random
|
7 |
+
|
8 |
+
# Setup basic logging
|
9 |
+
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
10 |
+
|
11 |
+
def clean_string(s):
|
12 |
+
"""Remove special characters, keeping only alphanumeric characters and spaces."""
|
13 |
+
if isinstance(s, list):
|
14 |
+
# Extract text from each dictionary in the list and join into a single string
|
15 |
+
s = " ".join([d.get("body", "") if isinstance(d, dict) else str(d) for d in s])
|
16 |
+
return re.sub(r'[^A-Za-z0-9 ]+', '', s)
|
17 |
+
|
18 |
+
def process_file(input_file, output_file):
|
19 |
+
try:
|
20 |
+
dataset = []
|
21 |
+
with gzip.open(input_file, 'rt') as infile:
|
22 |
+
for line in infile:
|
23 |
+
# Parse the JSON line
|
24 |
+
try:
|
25 |
+
data = json.loads(line)
|
26 |
+
except json.JSONDecodeError:
|
27 |
+
logging.error(f"Invalid JSON format in {input_file}: {line}")
|
28 |
+
continue
|
29 |
+
|
30 |
+
# Extract and clean the 'body' and 'answers' fields
|
31 |
+
prompt = clean_string(data.get("body", ""))
|
32 |
+
completion = clean_string(data.get("answers", ""))
|
33 |
+
|
34 |
+
# For each body found, make a new row and duplicate the prompt for it
|
35 |
+
if isinstance(data.get("body", ""), list):
|
36 |
+
for body in data.get("body", []):
|
37 |
+
cleaned_body = clean_string(body)
|
38 |
+
dataset.append({"prompt": cleaned_body, "completion": completion})
|
39 |
+
else:
|
40 |
+
dataset.append({"prompt": prompt, "completion": completion})
|
41 |
+
|
42 |
+
# Shuffle the dataset
|
43 |
+
random.shuffle(dataset)
|
44 |
+
|
45 |
+
# Write the shuffled dataset to the output file
|
46 |
+
with open(output_file, 'a') as outfile:
|
47 |
+
for item in dataset:
|
48 |
+
json.dump(item, outfile)
|
49 |
+
outfile.write('\n')
|
50 |
+
|
51 |
+
logging.info(f"Processed file: {input_file}")
|
52 |
+
|
53 |
+
except Exception as e:
|
54 |
+
logging.error(f"Error processing file {input_file}: {e}")
|
55 |
+
|
56 |
+
def process_files(file_list, output_dir):
|
57 |
+
# Ensure the output directory exists
|
58 |
+
if not os.path.exists(output_dir):
|
59 |
+
os.makedirs(output_dir)
|
60 |
+
|
61 |
+
# Create a single output file path
|
62 |
+
output_file = os.path.join(output_dir, 'synthesized_dataset.jsonl')
|
63 |
+
|
64 |
+
for input_file in file_list:
|
65 |
+
process_file(input_file, output_file)
|
66 |
+
|
67 |
+
# Update with your list of .gz file paths
|
68 |
+
file_list = [r'C:\Users\MeMyself\reddit_question_best_answers\RS_2010-01.jsonl.gz', r'C:\Users\MeMyself\reddit_question_best_answers\RS_2010-02.jsonl.gz', r"C:\Users\MeMyself\reddit_question_best_answers\RS_2010-03.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2010-04.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2010-05.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2010-06.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2010-07.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2010-08.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2010-09.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2010-10.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2010-11.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2010-12.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2011-01.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2011-02.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2011-03.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2011-04.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2011-05.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2011-06.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2011-07.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2011-08.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2011-09.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2011-10.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2011-11.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2011-12.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2012-01.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2012-02.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2012-03.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2012-04.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2012-05.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2012-06.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2012-07.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2012-08.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2012-09.jsonl.gz",r"C:\Users\MeMyself\reddit_question_best_answers\RS_2012-10.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2012-11.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2012-12.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2013-01.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2013-02.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2013-03.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2013-04.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2013-05.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2013-06.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2013-07.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2013-08.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2013-09.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2013-10.jsonl.gz", r"c:\Users\MeMyself\reddit_question_best_answers\RS_2013-11.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2013-12.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2014-01.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2014-02.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2014-03.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2014-04.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2014-05.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2014-06.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2014-07.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2014-08.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2014-09.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2014-10.jsonl.gz",r"C:\Users\MeMyself\reddit_question_best_answers\RS_2014-11.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2014-12.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2015-01.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2015-02.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2015-03.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2015-04.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2015-05.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2015-06.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2015-07.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2015-08.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2015-09.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2015-10.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2015-11.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2015-12.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2016-01.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2016-02.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2016-03.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2016-04.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2016-05.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2016-06.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2016-07.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2016-08.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2016-09.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2016-10.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2016-11.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2016-12.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2017-01.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2017-02.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2017-03.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2017-04.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2017-05.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2017-06.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2017-07.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2017-08.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2017-09.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2017-10.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2017-11.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2017-12.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2018-01.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2018-02.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2018-03.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2018-04.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2018-05.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2018-06.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2018-07.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2018-08.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2018-09.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2018-10.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2018-11.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2018-12.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2019-01.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2019-02.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2019-03.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2019-04.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2019-05.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2019-06.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2019-07.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2019-08.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2019-09.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2019-10.jsonl.gz", r"c:\Users\MeMyself\reddit_question_best_answers\RS_2019-11.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2019-12.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2020-01.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2020-02.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2020-03.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2020-04.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2020-05.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2020-06.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2020-07.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2020-08.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2020-09.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2020-10.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2020-11.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2020-12.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2021-01.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2021-02.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2021-03.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2021-04.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2021-05.jsonl.gz", r"C:\Users\MeMyself\reddit_question_best_answers\RS_2021-06.jsonl.gz" ] # Update with your list of .gz file paths
|
69 |
+
output_dir = r'C:\Users\MeMyself\reddit_question_best_answers\processed'
|
70 |
+
process_files(file_list, output_dir)
|