| import csv | |
| # Open sexting_conversations.txt and read all lines | |
| with open('sexting_conversations.txt', 'r', encoding='utf-8') as f: | |
| lines = f.readlines() | |
| # Initialize variables | |
| data = [] | |
| text = "" | |
| char_count = 0 | |
| # Loop through each line in the file | |
| for line in lines: | |
| # Increase character count | |
| char_count += len(line) | |
| # Add line to text | |
| text += line | |
| # 92 is the avg character length per line multiplied by 2 | |
| if char_count >= 92: | |
| # Add text to data array with is_toxic value of 1 | |
| data.append([text.strip(), 1]) | |
| # Reset text and character count | |
| text = "" | |
| char_count = 0 | |
| # If there is any remaining text, add it to data array with is_toxic value of 1 | |
| if text: | |
| data.append([text.strip(), 1]) | |
| # Save data array to CSV with column "text,is_toxic" | |
| with open('output.csv', 'w', newline='', encoding='utf-8') as f: | |
| writer = csv.writer(f) | |
| writer.writerow(['text', 'is_toxic']) | |
| writer.writerows(data) | |