|
import re |
|
|
|
def clean_line(line): |
|
"""Clean a line by stripping formatting differences.""" |
|
line = line.strip().lower() |
|
line = re.sub(r'[^\w\s]', '', line) |
|
line = ' '.join(line.split()) |
|
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}'.") |
|
|
|
|
|
input_file = 'all_prompts.txt' |
|
output_file = 'all_prompts_deduped.txt' |
|
remove_duplicates(input_file, output_file) |
|
|