Download preprocess.py from Hemg/Introduction-greeting: direct link, hf CLI and curl.
- Browser
- Download file 1.31 kB
-
https://huggingface.co/datasets/Hemg/Introduction-greeting/resolve/f4fdbd3717ada0eb9fe91c8f08fd37c77d629f68/preprocess.py
- Command line
-
hf download hf://datasets/Hemg/Introduction-greeting@f4fdbd3717ada0eb9fe91c8f08fd37c77d629f68/preprocess.py
-
curl -L -o preprocess.py https://huggingface.co/datasets/Hemg/Introduction-greeting/resolve/f4fdbd3717ada0eb9fe91c8f08fd37c77d629f68/preprocess.py
1.31 kB
| import csv | |
| greeting_file = 'D:/greeting-introduction/greeting.text' | |
| introduction_file = 'D:/greeting-introduction/introduction.txt' | |
| def clean_quotes(text): | |
| # Remove all types of quotes while preserving the text | |
| return text.replace('"""', '').replace('"', '').replace("'''", '').replace("'", '').strip() | |
| # Read and clean greeting text | |
| with open(greeting_file, 'r', encoding='utf-8') as f: | |
| greeting_text = set(clean_quotes(line) for line in f if clean_quotes(line)) | |
| # Read and clean introduction text | |
| with open(introduction_file, 'r', encoding='utf-8') as f: | |
| introduction_text = set(clean_quotes(line) for line in f if clean_quotes(line)) | |
| # Create data list with cleaned text | |
| data = [] | |
| # Add greeting text with label 1 | |
| for line in greeting_text: | |
| if line: # Only add non-empty lines | |
| data.append([line, 1]) | |
| # Add introduction text with label 0 | |
| for line in introduction_text: | |
| if line: # Only add non-empty lines | |
| data.append([line, 0]) | |
| # Write to CSV file | |
| csv_file = 'preprocess.csv' | |
| with open(csv_file, 'w', newline='', encoding='utf-8') as f: | |
| writer = csv.writer(f) | |
| writer.writerow(['Text', 'Label']) | |
| writer.writerows(data) | |
| print(f"Data has been written to {csv_file} with duplicates removed and quotes cleaned") | |