forked from tirgue/Data-Science
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassify.py
126 lines (103 loc) · 3.47 KB
/
classify.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
import csv, json, sys, datetime
from script_clearDatas import extract_mails
def createDate(data):
year = int(data.get('Date')[0:4])
month = int(data.get('Date')[5:7])
day = int(data.get('Date')[8:10])
hour = int(data.get('Date')[11:13])
minute = int(data.get('Date')[14:16])
return datetime.datetime(year, month, day, hour, minute)
def retrieve_sending_month(date):
return date.strftime("%b")
def retrieve_sending_day(date):
return date.strftime("%a")
def retrieve_sending_time(date):
if date.hour < 0:
return "bet-0-5"
if date.hour < 5:
return "bet-5-8"
if date.hour < 8:
return "bet-8-11"
if date.hour < 11:
return "bet-11-14"
if date.hour < 14:
return "bet-14-18"
if date.hour < 18:
return "bet-18-20"
if date.hour < 20:
return "bet-20-22"
if date.hour < 22:
return "bet-22-0"
def retrieve_long_subject(data):
# Moyenne de lettres par mot : 4.65
# On estime qu'un sujet est long au delà d'une dizaine de mots (10 * 4.65 = 46)
# Donc 46 caractères est la limite entre un sujet long et court
return "yes" if len(data.get('Subject')) > 46 else "no"
def retrieve_content_size(data):
if len(data.get('content')) < 150:
return "small"
elif len(data.get('content')) > 300:
return "high"
else:
return "medium"
def retrieve_formal(data):
return "yes"
def retrieve_time_until_answer(data, datas):
subject = data.get("Subject")
if subject.startswith("Re:"):
sender = extract_mails(data.get('From'))
d_subject = data.get("Subject")[4:]
for d in datas:
if d.get("Subject") == d_subject:
receivers = extract_mails(d.get("To"))
if sender[0] in receivers:
break
respondingDate = createDate(data)
sendingDate = createDate(d)
delta = respondingDate - sendingDate
if delta < datetime.timedelta():
return -1
return str(delta)
else:
return "None"
if __name__ == "__main__":
maxInt = sys.maxsize
while True:
try:
csv.field_size_limit(maxInt)
break
except OverflowError:
maxInt = int(maxInt/10)
print("Loading...")
filename = "data_response.csv"
datas = csv.DictReader(open(filename, "r"))
datas = list(datas)
emails = []
print("Computing...")
for data in datas:
sendingDate = createDate(data)
time_until_answer = retrieve_time_until_answer(data, datas)
if time_until_answer != -1:
sending_month = retrieve_sending_month(sendingDate)
sending_day = retrieve_sending_day(sendingDate)
sending_time = retrieve_sending_time(sendingDate)
long_subject = retrieve_long_subject(data)
content_size = retrieve_content_size(data)
formal = retrieve_formal(data)
email = {
"sending_month" : sending_month,
"sending_day" : sending_day,
"sending_time" : sending_time,
"long_subject" : long_subject,
"content_size" : content_size,
"formal" : formal,
"time_until_answer" : time_until_answer,
}
emails.append(email)
output = {
"emails" : emails
}
print("Writing...")
outputFile = open("classesDatas.json", "w")
json.dump(output, outputFile)
print("Done")