import json import os import gzip import logging import re import random # Setup basic logging logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s") def clean_string(s): """Remove special characters, keeping only alphanumeric characters and spaces.""" if isinstance(s, list): # Extract text from each dictionary in the list and join into a single string s = " ".join([d.get("body", "") if isinstance(d, dict) else str(d) for d in s]) return re.sub(r'[^A-Za-z0-9 ]+', '', s) def process_file(input_file, output_file): try: dataset = [] with gzip.open(input_file, 'rt') as infile: for line in infile: # Parse the JSON line try: data = json.loads(line) except json.JSONDecodeError: logging.error(f"Invalid JSON format in {input_file}: {line}") continue # Extract and clean the 'body' and 'answers' fields prompt = clean_string(data.get("body", "")) completion = clean_string(data.get("answers", "")) # For each body found, make a new row and duplicate the prompt for it if isinstance(data.get("body", ""), list): for body in data.get("body", []): cleaned_body = clean_string(body) dataset.append({"prompt": cleaned_body, "completion": completion}) else: dataset.append({"prompt": prompt, "completion": completion}) # Shuffle the dataset random.shuffle(dataset) # Write the shuffled dataset to the output file with open(output_file, 'a') as outfile: for item in dataset: json.dump(item, outfile) outfile.write('\n') logging.info(f"Processed file: {input_file}") except Exception as e: logging.error(f"Error processing file {input_file}: {e}") def process_files(file_list, output_dir): # Ensure the output directory exists if not os.path.exists(output_dir): os.makedirs(output_dir) # Create a single output file path output_file = os.path.join(output_dir, 'synthesized_dataset.jsonl') for input_file in file_list: process_file(input_file, output_file) # Update with your list of .gz file paths 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 output_dir = r'C:\Users\MeMyself\reddit_question_best_answers\processed' process_files(file_list, output_dir)