-
Notifications
You must be signed in to change notification settings - Fork 68
/
clean_whatsapp_chats.py
66 lines (56 loc) · 1.32 KB
/
clean_whatsapp_chats.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
63
64
65
66
import pickle
import random
import sys
import os
chat_file = sys.argv[1]
f = open(chat_file,'r', encoding="utf8")
content = f.readlines()
all_text = []
your_sents = []
other_sents = []
YOUR_NAME = 'YOUR NAME HERE'
OTHER_NAME = 'OTHER NAME HERE'
prev_pr_to_sp = {}
prev = None
for line in content[1:]:
if 'Missed Voice Call' in line:
continue
if 'image omitted' in line:
continue
if ' %s: '%YOUR_NAME in line:
text = line.split(' %s: '%YOUR_NAME)[-1]
your_sents.append(text)
all_text.append(text)
if prev == 'None':
continue
if prev == 'pr':
prev_pr_to_sp[other_sents[-1]] = text
prev = 'sp'
elif ' %s: '%OTHER_NAME in line:
text = line.split(' %s: '%OTHER_NAME)[-1]
other_sents.append(text)
all_text.append(text)
prev = 'pr'
else:
print(line)
all_text[-1] += line
if prev == 'sp':
your_sents[-1] += line
elif prev == 'pr':
other_sents[-1] += line
if not os.path.isdir('res/whatsapp'):
if not os.path.isdir('res'):
os.mkdir('res')
os.mkdir('res/whatsapp')
f = open('res/whatsapp/dilogues.p', 'wb')
pickle.dump(prev_pr_to_sp, f)
f.close()
f = open('res/whatsapp/all_text.p', 'wb')
pickle.dump(all_text, f)
f.close()
f = open('res/whatsapp/your_sents.p', 'wb')
pickle.dump(your_sents, f)
f.close()
f = open('res/whatsapp/other_sents.p', 'wb')
pickle.dump(other_sents, f)
f.close()