-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseiMessages.py
65 lines (58 loc) · 2.04 KB
/
parseiMessages.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
# Parses iMessages.csv
# message data dump to count
# the number of emoji occurances
import csv
import logging
import os
import json
import numpy as np
# Default values
logging.getLogger().setLevel(logging.INFO)
combinedDictionary = {}
# Check if iMessages.csv file exists
if (os.path.isfile('./iMessages.csv') != True):
logging.error('Cannot find valid file named: iMessages.csv')
quit()
# Read in iMessages.csv by the predefined
# column labels
messages = []
dictReader = csv.DictReader(open('iMessages.csv', 'rb'),
fieldnames = ['ROWID', 'text', 'service', 'account', 'date'], delimiter = ',', quotechar = '"')
# Parsing through iMessages.csv to get
# messages and responses
for row in dictReader:
for key in row:
if (key == 'text'):
if (row[key] != "NULL" and row[key] != ""):
messages.append(row[key])
# Create output file composed of
# messages and responses
# Message: text text text text
# Reponse: text text text text
messageResponseDictionary = ""
messageResponseString = ""
currentMessageString = ""
responseDictionary = dict()
senderToggle = True
for message in messages:
if (senderToggle == True):
messageResponseString += "Message: " + message + "\n"
messageResponseDictionary += "(" + message + ","
currentMessageString = message
senderToggle = False
else:
messageResponseString += "Response: " + message + "\n"
messageResponseDictionary += message + ")\n"
responseDictionary[message] = currentMessageString
senderToggle = True
# Generating conversationData.txt
conversationDataFile = open("conversationData.txt","w")
conversationDataFile.write(messageResponseString)
conversationDataFile.close()
# Create output file of message pairs
# (FRIENDS_MESSAGE, YOUR_RESPONSE)
combinedDictionary.update(responseDictionary)
np.save('conversationDictionary.npy', combinedDictionary)
# conversationDictionaryFile = open("conversationDictionary.npy","w")
# conversationDictionaryFile.write(messageResponseDictionary)
# conversationDictionaryFile.close()