FredZhang7
commited on
Commit
·
e0e5b16
1
Parent(s):
fc85d91
example of how to preprocess conversations
Browse files- preprocess_conversations.py +34 -0
preprocess_conversations.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import csv
|
2 |
+
|
3 |
+
# Open sexting_conversations.txt and read all lines
|
4 |
+
with open('sexting_conversations.txt', 'r', encoding='utf-8') as f:
|
5 |
+
lines = f.readlines()
|
6 |
+
|
7 |
+
# Initialize variables
|
8 |
+
data = []
|
9 |
+
text = ""
|
10 |
+
char_count = 0
|
11 |
+
|
12 |
+
# Loop through each line in the file
|
13 |
+
for line in lines:
|
14 |
+
# Increase character count
|
15 |
+
char_count += len(line)
|
16 |
+
# Add line to text
|
17 |
+
text += line
|
18 |
+
# 92 is the avg character length per line multiplied by 2
|
19 |
+
if char_count >= 92:
|
20 |
+
# Add text to data array with is_toxic value of 1
|
21 |
+
data.append([text.strip(), 1])
|
22 |
+
# Reset text and character count
|
23 |
+
text = ""
|
24 |
+
char_count = 0
|
25 |
+
|
26 |
+
# If there is any remaining text, add it to data array with is_toxic value of 1
|
27 |
+
if text:
|
28 |
+
data.append([text.strip(), 1])
|
29 |
+
|
30 |
+
# Save data array to CSV with column "text,is_toxic"
|
31 |
+
with open('output.csv', 'w', newline='', encoding='utf-8') as f:
|
32 |
+
writer = csv.writer(f)
|
33 |
+
writer.writerow(['text', 'is_toxic'])
|
34 |
+
writer.writerows(data)
|