Upload preprocess.py
Browse files- preprocess.py +38 -0
preprocess.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import csv
|
| 2 |
+
|
| 3 |
+
greeting_file = 'D:/greeting-introduction/greeting.text'
|
| 4 |
+
introduction_file = 'D:/greeting-introduction/introduction.txt'
|
| 5 |
+
|
| 6 |
+
def clean_quotes(text):
|
| 7 |
+
# Remove all types of quotes while preserving the text
|
| 8 |
+
return text.replace('"""', '').replace('"', '').replace("'''", '').replace("'", '').strip()
|
| 9 |
+
|
| 10 |
+
# Read and clean greeting text
|
| 11 |
+
with open(greeting_file, 'r', encoding='utf-8') as f:
|
| 12 |
+
greeting_text = set(clean_quotes(line) for line in f if clean_quotes(line))
|
| 13 |
+
|
| 14 |
+
# Read and clean introduction text
|
| 15 |
+
with open(introduction_file, 'r', encoding='utf-8') as f:
|
| 16 |
+
introduction_text = set(clean_quotes(line) for line in f if clean_quotes(line))
|
| 17 |
+
|
| 18 |
+
# Create data list with cleaned text
|
| 19 |
+
data = []
|
| 20 |
+
|
| 21 |
+
# Add greeting text with label 1
|
| 22 |
+
for line in greeting_text:
|
| 23 |
+
if line: # Only add non-empty lines
|
| 24 |
+
data.append([line, 1])
|
| 25 |
+
|
| 26 |
+
# Add introduction text with label 0
|
| 27 |
+
for line in introduction_text:
|
| 28 |
+
if line: # Only add non-empty lines
|
| 29 |
+
data.append([line, 0])
|
| 30 |
+
|
| 31 |
+
# Write to CSV file
|
| 32 |
+
csv_file = 'preprocess.csv'
|
| 33 |
+
with open(csv_file, 'w', newline='', encoding='utf-8') as f:
|
| 34 |
+
writer = csv.writer(f)
|
| 35 |
+
writer.writerow(['Text', 'Label'])
|
| 36 |
+
writer.writerows(data)
|
| 37 |
+
|
| 38 |
+
print(f"Data has been written to {csv_file} with duplicates removed and quotes cleaned")
|