SCP-RECURSAL / tools /clean.py
KaraKaraWitch's picture
Upload folder using huggingface_hub
5ed99d0 verified
import json
import re
def Clean_output_file(input_file_path, output_file_path):
with open(input_file_path, 'r') as input_file, open(output_file_path, 'w') as output_file:
for line in input_file:
# Load the line as a JSON object
item = json.loads(line)
# Check if 'text' field exists
if 'text' in item:
patterns_to_remove = [
r'Author:.*?\n', # Remove author part
r'\+ More articles by.*?\n', # Remove "+ More articles" lines
r'- Hide list.*?\n', # Remove "- Hide list" lines
r'Rating:.*?\n', # Remove lines starting with "Rating:"
r'\n\uf129\n.*?\n', # Remove lines with special characters and any text until the next newline
]
# Apply each pattern removal
for pattern in patterns_to_remove:
item['text'] = re.sub(pattern, '', item['text'], flags=re.DOTALL)
# Replace specific unicode characters with a space or equivalent character
item['text'] = item['text'].replace('\u00a0', ' ')
item['text'] = item['text'].replace('\u2008', ' ')
item['text'] = item['text'].replace('\u2019', '\'')
# Remove extra newline characters
item['text'] = re.sub(r'\n\s*\n', '\n\n', item['text'])
# Check for unwanted phrases and skip the line if any are found
if ("This page doesn" in item['text'] or
"+ Show component code" in item['text'] or
"position: relative" in item['text'] or
"Don't forget to fill out this" in item['text']or
"Table of Contents" in item['text'] or
"Tales by SCP Series" in item['text']) :
continue # Skip this line
# Remove "rating: +" and any text until the next newline
item['text'] = re.sub(r'rating: \+.*?\n', '', item['text'], flags=re.DOTALL)
# Write the updated item back to the file if it doesn't contain unwanted phrases
json.dump(item, output_file)
output_file.write('\n')
# stories
for num in range(1,9):
input_file_path = f'scp_stories{num}.jsonl' # Replace with your input file path
output_file_path = f'stories{num}.jsonl' # Replace with your desired output file path
Clean_output_file(input_file_path, output_file_path)
# tales
input_file_path = f'scp_tales.jsonl' # Replace with your input file path
output_file_path = f'scp_tales_cleaned.jsonl' # Replace with your desired output file path
Clean_output_file(input_file_path, output_file_path)
# jokes
input_file_path = f'scp_jokes.jsonl' # Replace with your input file path
output_file_path = f'jokes-cleaned.jsonl' # Replace with your desired output file path
Clean_output_file(input_file_path, output_file_path)