Download preprocessor.py from SurajJha21/whatsapp_analysis24hrformat: direct link, hf CLI and curl.
- Browser
- Download file 1.75 kB
-
https://huggingface.co/spaces/SurajJha21/whatsapp_analysis24hrformat/resolve/main/preprocessor.py
- Command line
-
hf download hf://spaces/SurajJha21/whatsapp_analysis24hrformat/preprocessor.py
-
curl -L -o preprocessor.py https://huggingface.co/spaces/SurajJha21/whatsapp_analysis24hrformat/resolve/main/preprocessor.py
1.75 kB
| import re | |
| import pandas as pd | |
| import dateutil.parser | |
| def preprocess(data): | |
| messages = [] | |
| dates = [] | |
| for line in data.splitlines(): # Assuming data is line-based | |
| try: | |
| date_obj = dateutil.parser.parse(line.split(',')[0]) # Assuming date is in first part | |
| messages.append(line.split(',')[1]) # Assuming message is in second part | |
| dates.append(date_obj) # Keep as datetime object | |
| except (ValueError, IndexError): | |
| pass # Handle parsing errors or missing data silently (optional) | |
| df = pd.DataFrame({'user_message': messages, 'date': dates}) | |
| return df | |
| users = [] | |
| messages = [] | |
| for message in df['user_message']: | |
| entry = re.split('([\w\W]+?):\s', message) | |
| if entry[1:]: # user name | |
| users.append(entry[1]) | |
| messages.append(" ".join(entry[2:])) | |
| else: | |
| users.append('group_notification') | |
| messages.append(entry[0]) | |
| df['user'] = users | |
| df['message'] = messages | |
| df.drop(columns=['user_message'], inplace=True) | |
| df['only_date'] = df['date'].dt.date | |
| df['year'] = df['date'].dt.year | |
| df['month_num'] = df['date'].dt.month | |
| df['month'] = df['date'].dt.month_name() | |
| df['day'] = df['date'].dt.day | |
| df['day_name'] = df['date'].dt.day_name() | |
| df['hour'] = df['date'].dt.hour | |
| df['minute'] = df['date'].dt.minute | |
| period = [] | |
| for hour in df[['day_name', 'hour']]['hour']: | |
| if hour == 23: | |
| period.append(str(hour) + "-" + str('00')) | |
| elif hour == 0: | |
| period.append(str('00') + "-" + str(hour + 1)) | |
| else: | |
| period.append(str(hour) + "-" + str(hour + 1)) | |
| df['period'] = period | |
| return df |