<jupyter_start><jupyter_text>Extracting from Raw Data<jupyter_code>data = pd.read_csv('emails.csv')
data.head()
# Sample Data
data.iloc[133,1].split('\n')
metadata = data.iloc[0,1].split('\n')

# Sender
for line in metadata:
    if(line.find('X-From') == -1 and line.find('From:') != -1):
        sender = line.split(':')[1].strip()
        sender = sender.split('@')[0].strip()
        print('Sender: ', sender)
        break
        
# Receiver
for line in metadata:
    if(line.find('X-To') == -1 and line.find('To:') != -1):
        receiver = line.split(':')[1].strip()
        receiver = receiver.split(',')
        for i,rec in enumerate(receiver): 
            receiver[i] = rec.split('<')[0].strip()
            receiver[i] = receiver[i].split('@')[0].strip()
        if len(receiver) == 1:
            receiver.append('')
            receiver.append('')
        elif len(receiver) == 2:
            receiver.append('')
        print('Recipient 1: ', receiver[0])
        print('Recipient 2: ', receiver[1])
        print('Recipient 3: ', receiver[2])
        break
        
# Subject
for line in metadata:
    if(line.find('Subject') != -1):
        subject = line.split(':')[-1].strip()
        print('Subject: ', subject)
        break
        
# Date
for line in metadata:
    if(line.find('Date') != -1):
        date = ':'.join(line.split(':')[1:]).strip()
        date = date.split('(')[0].strip()
        date = datetime.datetime.strptime(date, "%a, %d %b %Y %H:%M:%S %z")
        print('Date: ', date)
        break
        
# Message
message = [];
for line in metadata:
    if(line.find('Message-ID') == -1 and line.find('Date') == -1 and line.find('From') == -1 and line.find('To') == -1 and line.find('Subject') == -1 and line.find('Mime-Version') == -1 and line.find('Content-Type') == -1 and line.find('Content-Transfer-Encoding') == -1 and line.find('X-From') == -1 and line.find('X-To') == -1 and line.find('X-cc') == -1 and line.find('X-bcc') == -1 and line.find('X-Folder') == -1 and line.find('X-Origin') == -1 and line.find('X-FileName') == -1):
        message.append(line)
print('Message: ', ''.join(message))
extracted_data = []
for i in range(data.shape[0]):
    metadata = data.iloc[i,1].split('\n')

    new_data = [] 
    # Sender
    for line in metadata:
        if(line.find('X-From') == -1 and line.find('From:') != -1):
            sender = line.split(':')[1].strip()
            sender = sender.split('@')[0].strip()
            new_data.append(sender)
            break

    # Receiver
    for line in metadata:
        if(line.find('X-To') == -1 and line.find('To:') != -1):
            receiver = line.split(':')[1].strip()
            receiver = receiver.split(',')
            for i,rec in enumerate(receiver): 
                receiver[i] = rec.split('<')[0].strip()
                receiver[i] = receiver[i].split('@')[0].strip()
            if len(receiver) == 1:
                receiver.append('')
                receiver.append('')
            elif len(receiver) == 2:
                receiver.append('')
            new_data.append(receiver[0])
            new_data.append(receiver[1])
            new_data.append(receiver[2])
            break

    # Subject
    for line in metadata:
        if(line.find('Subject') != -1):
            subject = line.split(':')[-1].strip()
            new_data.append(subject)
            break

    # Date
    for line in metadata:
        if(line.find('Date') != -1):
            date = ':'.join(line.split(':')[1:]).strip()
            date = date.split('(')[0].strip()
            date = datetime.datetime.strptime(date, "%a, %d %b %Y %H:%M:%S %z")
            new_data.append(date)
            break

    # Message
    message = [];
    for line in metadata:
        if(line.find('Message-ID') == -1 and line.find('Date') == -1 and line.find('From') == -1 and line.find('To') == -1 and line.find('Subject') == -1 and line.find('Mime-Version') == -1 and line.find('Content-Type') == -1 and line.find('Content-Transfer-Encoding') == -1 and line.find('X-From') == -1 and line.find('X-To') == -1 and line.find('X-cc') == -1 and line.find('X-bcc') == -1 and line.find('X-Folder') == -1 and line.find('X-Origin') == -1 and line.find('X-FileName') == -1):
            message.append(line)
    message = ''.join(message)
    new_data.append(message)
    
    extracted_data.append(new_data)
# New Dataset
columns = ['Sender', 'Recipient 1', 'Recipient 2', 'Recipient 3', 'Subject', 'Date', 'Message body']
extracted_DF = pd.DataFrame(data = extracted_data, columns = columns)
extracted_DF.head()
extracted_DF.to_csv('extracted_data.csv', index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20522707797772066, 'fraction_numerical': 0.017566409597257925, 'mean_word_length': 2.413011695906433, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '96382', 'license': 'MIT', 'n_tokens_mistral': 1581, 'n_tokens_neox': 1561, 'n_words': 324, 'path': 'Preprocessing.ipynb', 'repo_name': 'Aditya-11/Eron-Email-Analysis', 'size': '4668'}