xzuyn's picture
Upload 2 files
0e71e85
import re
def clean_line(line):
"""Clean a line by stripping formatting differences."""
line = line.strip().lower() # Remove leading/trailing spaces and convert to lowercase
line = re.sub(r'[^\w\s]', '', line) # Remove punctuation marks
line = ' '.join(line.split()) # Remove extra spaces
return line
def remove_duplicates(input_file, output_file):
"""Remove duplicate lines from a text file while ignoring formatting differences."""
unique_lines = set()
cleaned_lines = []
duplicate_count = 0
with open(input_file, 'r', encoding='utf-8') as file:
for line in file:
cleaned_line = clean_line(line)
if cleaned_line not in unique_lines:
unique_lines.add(cleaned_line)
cleaned_lines.append(line)
else:
duplicate_count += 1
with open(output_file, 'w', encoding='utf-8') as file:
file.writelines(cleaned_lines)
print(f"{duplicate_count} duplicates removed. Cleaned lines saved to '{output_file}'.")
# Usage example:
input_file = 'all_prompts.txt'
output_file = 'all_prompts_deduped.txt'
remove_duplicates(input_file, output_file)