-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess.py
62 lines (56 loc) · 2.3 KB
/
preprocess.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# -*- coding: utf-8 -*-
'''Preprocess module
This module provides a function to clean and preprocess WhatsApp chat data.
The `preprocess` function takes a string of WhatsApp chat data as input,
extracts the dates, users, and messages, and returns a pandas DataFrame'''
import re
import pandas as pd
from dateutil import parser
def convert_to_datetime(datetime_str):
# Remove the trailing '-' character
datetime_str = datetime_str.rstrip(' -')
# Use dateutil parser to parse the datetime string
datetime_obj = parser.parse(datetime_str)
return pd.to_datetime(datetime_obj)
def preprocess(data:str) -> pd.DataFrame:
'''Cleaning and preprocessing data'''
pattern = r'\d{1,2}\/\d{1,2}\/\d{2,4},\s\d{1,2}:\d{2}[\s\S][p,a]m\s-[\s\S]'
messages = re.split(pattern, data)[1:]
if len(messages)==0:
return pd.DataFrame({'Error':["your chat file is courrpted or something went wrong!"]})
raw_dates = re.findall(pattern, data)
# replacing 'narrow no-break space' with a 'spcae'
dates = [i.replace('\u202f'," ").strip() for i in raw_dates]
# seperating users and messages
users = []
filter_messages = []
for msg in messages:
entry = re.split(r'(?<=\S):\s', msg, 1) # Split only at the first colon followed by a space
if len(entry) > 1:
users.append(entry[0])
filter_messages.append(entry[1])
else:
users.append('group_notification')
filter_messages.append(entry[0])
# converting data into dataFrame
df = pd.DataFrame({'date':dates, 'user':users,'message':filter_messages})
# converting string into datetime object
#df['date'] = pd.to_datetime(df['date'], format='%d/%m/%y, %I:%M %p -')
df['date']= df['date'].apply(convert_to_datetime)
# seprating year column
df['year'] = df['date'].dt.year
# seperating month
df['month'] = df['date'].dt.month_name()
# separating day
df['day'] = df['date'].dt.day
# seperating hour
df['hour'] = df['date'].dt.hour
# separating minute
df['minute'] = df['date'].dt.minute
# adding month name column in words
df['month_num'] = df['date'].dt.month
# adding only_date without time
df['only_date'] = df['date'].dt.date
# adding day_name column
df['day_name'] = df['date'].dt.day_name()
return df