-
Notifications
You must be signed in to change notification settings - Fork 266
/
createDataset.py
299 lines (278 loc) · 13.6 KB
/
createDataset.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import pandas as pd
import numpy as np
import os
import re
from datetime import datetime
fbData = input('Do you have Facebook data to parse through (y/n)?')
googleData = input('Do you have Google Hangouts data to parse through (y/n)?')
linkedInData = input('Do you have LinkedIn data to parse through (y/n)?')
whatsAppData = input('Do you have whatsAppData to parse through (y/n)?')
discordData = input('Do you have discordData to parse through (y/n)?')
def getWhatsAppDataCSV(personName):
df = pd.read_csv('whatsapp_chats.csv')
responseDictionary = dict()
receivedMessages = df[df['From'] != personName]
sentMessages = df[df['From'] == personName]
combined = pd.concat([sentMessages, receivedMessages])
otherPersonsMessage, myMessage = "",""
firstMessage = True
for index, row in combined.iterrows():
if (row['From'] != personName):
if myMessage and otherPersonsMessage:
otherPersonsMessage = cleanMessage(otherPersonsMessage)
myMessage = cleanMessage(myMessage)
responseDictionary[otherPersonsMessage.rstrip()] = myMessage.rstrip()
otherPersonsMessage, myMessage = "",""
otherPersonsMessage = otherPersonsMessage + str(row['Content']) + " "
else:
if (firstMessage):
firstMessage = False
# Don't include if I am the person initiating the convo
continue
myMessage = myMessage + str(row['Content']) + " "
return responseDictionary
def getWhatsAppDataTXT(personName):
# Putting all the file names in a list
allFiles = []
# Edit these file and directory names if you have them saved somewhere else
for filename in os.listdir('WhatsAppChatLogs'):
if filename.endswith(".txt"):
allFiles.append('WhatsAppChatLogs/' + filename)
responseDictionary = dict()
"""
The key is the other person's message, and the value is my response
Going through each file, and recording everyone's messages to me, and my
responses
"""
for currentFile in allFiles:
myMessage, otherPersonsMessage, currentSpeaker = "","",""
with open(currentFile, 'r',encoding="utf-8") as openedFile:
allLines = openedFile.readlines()
for index,line in enumerate(allLines):
# The sender's name is separated by a ']' or '-' and a ': ' (The whitespace is important)
leftDelimPattern = re.compile(r'[\]\-]')
# A pattern to match either `]` or `-`
leftDelim = leftDelimPattern.search(line)
leftDelim = leftDelim.start() if leftDelim else -1
rightColon = line.find(': ')
# Find messages that I sent
if (line[leftDelim + 1:rightColon].strip() == personName):
if not myMessage:
# Want to find the first message that I send (if I send
# multiple in a row)
startMessageIndex = index - 1
myMessage += line[rightColon + 1:].strip()
elif myMessage:
# Now go and see what message the other person sent by looking at
# previous messages
for counter in range(startMessageIndex, 0, -1):
currentLine = allLines[counter]
# Extracting the values of left and right delimiters
leftDelim = leftDelimPattern.search(currentLine)
leftDelim = leftDelim.start() if leftDelim else -1
rightColon = line.find(': ')
if (leftDelim < 0 or rightColon < 0):
# In case the message above isn't in the right format
myMessage, otherPersonsMessage, currentSpeaker = "","",""
break
if not currentSpeaker:
# The first speaker not named me
currentSpeaker = currentLine[leftDelim + 1:rightColon].strip()
elif (currentSpeaker != currentLine[leftDelim + 1:rightColon].strip()):
# A different person started speaking, so now I know that
# the first person's message is done
otherPersonsMessage = cleanMessage(otherPersonsMessage)
myMessage = cleanMessage(myMessage)
responseDictionary[otherPersonsMessage] = myMessage
break
otherPersonsMessage = currentLine[rightColon + 1:].strip() + otherPersonsMessage
myMessage, otherPersonsMessage, currentSpeaker = "","",""
return responseDictionary
def getWhatsAppData():
personName = input('Enter your full WhatsApp name: ')
if os.path.isfile('whatsapp_chats.csv'):
return getWhatsAppDataCSV(personName)
else:
return getWhatsAppDataTXT(personName)
def getGoogleHangoutsData():
personName = input('Enter your full Hangouts name: ')
# Putting all the file names in a list
allFiles = []
# Edit these file and directory names if you have them saved somewhere else
for filename in os.listdir('GoogleTextForm'):
if filename.endswith(".txt"):
allFiles.append('GoogleTextForm/' + filename)
responseDictionary = dict()
"""
The key is the other person's message, and the value is my response
Going through each file, and recording everyone's messages to me, and my
responses
"""
for currentFile in allFiles:
myMessage, otherPersonsMessage, currentSpeaker = "","",""
with open(currentFile, 'r') as openedFile:
allLines = openedFile.readlines()
for index,lines in enumerate(allLines):
# The sender's name is separated by < and >
leftBracket = lines.find('<')
rightBracket = lines.find('>')
# Find messages that I sent
if (lines[leftBracket + 1:rightBracket] == personName):
if not myMessage:
# Want to find the first message that I send (if I send
# multiple in a row)
startMessageIndex = index - 1
myMessage += lines[rightBracket + 1:]
elif myMessage:
# Now go and see what message the other person sent by looking at
# previous messages
for counter in range(startMessageIndex, 0, -1):
currentLine = allLines[counter]
# In case the message above isn't in the right format
if (currentLine.find('<') < 0 or currentLine.find('>') < 0):
myMessage, otherPersonsMessage, currentSpeaker = "","",""
break
if not currentSpeaker:
# The first speaker not named me
currentSpeaker = currentLine[currentLine.find('<') + 1:currentLine.find('>')]
elif (currentSpeaker != currentLine[currentLine.find('<') + 1:currentLine.find('>')]):
# A different person started speaking, so now I know that
# the first person's message is done
otherPersonsMessage = cleanMessage(otherPersonsMessage)
myMessage = cleanMessage(myMessage)
responseDictionary[otherPersonsMessage] = myMessage
break
otherPersonsMessage = currentLine[currentLine.find('>') + 1:] + otherPersonsMessage
myMessage, otherPersonsMessage, currentSpeaker = "","",""
return responseDictionary
def getFacebookData():
personName = input('Enter your full Facebook name: ')
responseDictionary = dict()
with open('fbMessages.txt', 'r') as fbFile:
allLines = fbFile.readlines()
myMessage, otherPersonsMessage, currentSpeaker = "","",""
for index,lines in enumerate(allLines):
rightBracket = lines.find(']') + 2
justMessage = lines[rightBracket:]
colon = justMessage.find(':')
# Find messages that I sent
if (justMessage[:colon] == personName):
if not myMessage:
# Want to find the first message that I send (if I send multiple
# in a row)
startMessageIndex = index - 1
myMessage += justMessage[colon + 2:]
elif myMessage:
# Now go and see what message the other person sent by looking at
# previous messages
for counter in range(startMessageIndex, 0, -1):
currentLine = allLines[counter]
rightBracket = currentLine.find(']') + 2
justMessage = currentLine[rightBracket:]
colon = justMessage.find(':')
if not currentSpeaker:
# The first speaker not named me
currentSpeaker = justMessage[:colon]
elif (currentSpeaker != justMessage[:colon] and otherPersonsMessage):
# A different person started speaking, so now I know that the
# first person's message is done
otherPersonsMessage = cleanMessage(otherPersonsMessage)
myMessage = cleanMessage(myMessage)
responseDictionary[otherPersonsMessage] = myMessage
break
otherPersonsMessage = justMessage[colon + 2:] + otherPersonsMessage
myMessage, otherPersonsMessage, currentSpeaker = "","",""
return responseDictionary
def getLinkedInData():
personName = input('Enter your full LinkedIn name: ')
df = pd.read_csv('Inbox.csv')
dateTimeConverter = lambda x: datetime.strptime(x,'%B %d, %Y, %I:%M %p')
responseDictionary = dict()
peopleContacted = df['From'].unique().tolist()
for person in peopleContacted:
receivedMessages = df[df['From'] == person]
sentMessages = df[df['To'] == person]
if (len(sentMessages) == 0 or len(receivedMessages) == 0):
# There was no actual conversation
continue
combined = pd.concat([sentMessages, receivedMessages])
combined['Date'] = combined['Date'].apply(dateTimeConverter)
combined = combined.sort(['Date'])
otherPersonsMessage, myMessage = "",""
firstMessage = True
for index, row in combined.iterrows():
if (row['From'] != personName):
if myMessage and otherPersonsMessage:
otherPersonsMessage = cleanMessage(otherPersonsMessage)
myMessage = cleanMessage(myMessage)
responseDictionary[otherPersonsMessage.rstrip()] = myMessage.rstrip()
otherPersonsMessage, myMessage = "",""
otherPersonsMessage = otherPersonsMessage + str(row['Content']) + " "
else:
if (firstMessage):
firstMessage = False
# Don't include if I am the person initiating the convo
continue
myMessage = myMessage + str(row['Content']) + " "
return responseDictionary
def getDiscordData():
personName = input('Enter your full Discord name: ')
# Putting all the file names in a list
allFiles = []
# Edit these file and directory names if you have them saved somewhere else
for filename in os.listdir('DiscordChatLogs'):
if filename.endswith(".txt"):
allFiles.append('DiscordChatLogs/' + filename)
responseDictionary = dict()
"""
The key is the other person's message, and the value is my response
Going through each file, and recording everyone's messages to me, and my
responses
"""
for currentFile in allFiles:
with open(currentFile, 'r') as openedFile:
allLines = openedFile.readlines()
data = ''.join(allLines)
otherPersonsMessage, myMessage = "",""
response_sets = re.findall(r'\[.+\] (?!' + re.escape(personName) + r').+\n(.+)\n{2}(?:\[.+\] ' + re.escape(personName) + r'\n(.+)\n{2})', data)
for response_set in response_sets:
otherPersonsMessage = response_set[0]
myMessage = response_set[1]
responseDictionary[otherPersonsMessage] = cleanMessage(myMessage)
otherPersonsMessage, myMessage = "",""
return responseDictionary
def cleanMessage(message):
# Remove new lines within message
cleanedMessage = message.replace('\n',' ').lower()
# Deal with some weird tokens
cleanedMessage = cleanedMessage.replace("\xc2\xa0", "")
# Remove punctuation
cleanedMessage = re.sub('([.,!?])','', cleanedMessage)
# Remove multiple spaces in message
cleanedMessage = re.sub(' +',' ', cleanedMessage)
return cleanedMessage
combinedDictionary = {}
if (googleData == 'y'):
print ('Getting Google Hangout Data')
combinedDictionary.update(getGoogleHangoutsData())
if (fbData == 'y'):
print ('Getting Facebook Data')
combinedDictionary.update(getFacebookData())
if (linkedInData == 'y'):
print ('Getting LinkedIn Data')
combinedDictionary.update(getLinkedInData())
if (whatsAppData == 'y'):
print ('Getting whatsApp Data')
combinedDictionary.update(getWhatsAppData())
if (discordData == 'y'):
print ('Getting Discord Data')
combinedDictionary.update(getDiscordData())
print (Total len of dictionary', len(combinedDictionary))
print('Saving conversation data dictionary')
np.save('conversationDictionary.npy', combinedDictionary)
conversationFile = open('conversationData.txt', 'w',encoding="utf-8")
for key, value in combinedDictionary.items():
if (not key.strip() or not value.strip()):
# If there are empty strings
continue
conversationFile.write(key.strip() + value.strip())