-
Notifications
You must be signed in to change notification settings - Fork 0
/
narrate.py
185 lines (160 loc) · 15.2 KB
/
narrate.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
#!/usr/bin/python
import csv, os, pprint
# STEP 4 of lacrashbot script package
#Create the Narration CSV from the Decode CSV. Output contains date, time, and tweet contents.
#Note that this file could probably use clean-up. Such as, more variables could be defined up front.
#There are a lot of IFs in here, and some of them are only for one or two crashes - outlier cases that I discovered
#while visually looking at the output.
#TODO: create a test that checks for blank rows; rewrite the bicyclist section; add more comments
test_file = './LACinjury2014_Decode.csv'
csv_file = csv.DictReader(open(test_file, 'rb'), delimiter=',', quotechar='"')
with open('LACinjury2014_Narrate.csv', 'w') as csvoutput:
writer = csv.writer(csvoutput)
for line in csv_file:
Decode_NARRATE = None
timestamp = ' (' + line['COLLISION_DATE'] + ' ' + line['COLLISION_TIME'] + ' ' + line['ACCIDENT_YEAR'] + ')'
if line['NUMBER_KILLED'] == '1':
number_killed = line['NUMBER_KILLED'] + ' person killed'
elif line['NUMBER_KILLED'] > '1':
number_killed = line['NUMBER_KILLED'] + ' people killed'
if line['NUMBER_INJURED'] == '1':
number_injured = line['NUMBER_INJURED'] + ' person injured'
elif line['NUMBER_INJURED'] > '1':
number_injured = line['NUMBER_INJURED'] + ' people injured'
if line['Decode_HIT_AND_RUN'] == 'yes':
hit_and_run = ' Hit & run.'
else:
hit_and_run = ''
intro = 'A person ' + line['Decode_STWD_VEHTYPE_AT_FAULT'] + ' on ' + line['PRIMARY_RD']
intro_type = intro + ' ' + line['Decode_TYPE_OF_COLLISION']
#if a bicyclist hit a pedestrian
if line['Decode_STWD_VEHTYPE_AT_FAULT'] == 'riding a bicycle' and line['Decode_TYPE_OF_COLLISION'] == 'hit':
if line['COUNT_PED_INJURED'] == '1' and line['Decode_COLLISION_SEVERITY'] != 'severely injured':
Decode_NARRATE = intro_type + ' & injured a pedestrian.' + hit_and_run + timestamp
elif line['COUNT_PED_INJURED'] == '1' and line['Decode_COLLISION_SEVERITY'] == 'severely injured':
Decode_NARRATE = intro_type + ' & ' + line['Decode_COLLISION_SEVERITY'] + ' a pedestrian.' + hit_and_run + timestamp
elif line['COUNT_PED_INJURED'] > '1':
Decode_NARRATE = intro_type + ' & injured ' + line['COUNT_PED_INJURED'] + ' pedestrians.' + hit_and_run + timestamp
elif line['COUNT_PED_KILLED'] == '1':
Decode_NARRATE = intro_type + ' & killed a pedestrian.' + hit_and_run + timestamp
elif line['COUNT_PED_KILLED'] > '1':
Decode_NARRATE = intro_type + ' & killed ' + line['COUNT_PED_KILLED'] + ' pedestrians.' + hit_and_run + timestamp
if line['Decode_PED_BIKE'] == 'no' and line['Decode_MVIW'] != 'a non-collision':
if line['NUMBER_KILLED'] > '0' and line['NUMBER_INJURED'] == '0':
Decode_NARRATE = intro_type + ' ' + line['Decode_MVIW'] + '; ' + number_killed + '.' + hit_and_run + timestamp
elif line['NUMBER_KILLED'] > '0' and line['NUMBER_INJURED'] > '0':
Decode_NARRATE = intro_type + ' ' + line['Decode_MVIW'] + '; ' + number_killed + ' & ' + number_injured + '.' + hit_and_run + timestamp
elif line['NUMBER_KILLED'] == '0' and line['NUMBER_INJURED'] > '0':
Decode_NARRATE = intro_type + ' ' + line['Decode_MVIW'] + '; ' + number_injured + '.' + hit_and_run + timestamp
elif line['Decode_PED_BIKE'] == 'ped' and line['Decode_MVIW'] != 'a non-collision':
if line['COUNT_PED_KILLED'] == '1':
if line['NUMBER_INJURED'] == '0':
Decode_NARRATE = intro_type + ' & killed a pedestrian.' + hit_and_run + timestamp
elif line['NUMBER_INJURED'] == '1':
if line['Decode_COLLISION_SEVERITY'] != 'severely injured':
Decode_NARRATE = intro_type + ' & killed a pedestrian & injured another person.' + hit_and_run + timestamp
else:
Decode_NARRATE = intro_type + ' & killed a pedestrian & ' + line['Decode_COLLISION_SEVERITY'] + ' another person.' + hit_and_run + timestamp
elif line['NUMBER_INJURED'] > '1':
Decode_NARRATE = intro_type + ' & killed a pedestrian & injured ' + line['NUMBER_INJURED'] + ' other people.' + hit_and_run + timestamp
elif line['COUNT_PED_KILLED'] > '1':
if line['NUMBER_INJURED'] == '0':
Decode_NARRATE = intro_type + ' & killed ' + line['COUNT_PED_KILLED'] + ' pedestrians.' + hit_and_run + timestamp
elif line['NUMBER_INJURED'] == '1':
if line['Decode_COLLISION_SEVERITY'] != 'severely injured':
Decode_NARRATE = intro_type + ' & killed ' + line['COUNT_PED_KILLED'] + ' pedestrians & injured another person.' + hit_and_run + timestamp
else:
Decode_NARRATE = intro_type + ' & killed ' + line['COUNT_PED_KILLED'] + ' pedestrians & ' + line['Decode_COLLISION_SEVERITY'] + ' another person.' + hit_and_run + timestamp
elif line['NUMBER_INJURED'] > '1':
Decode_NARRATE = intro_type + ' & killed ' + line['COUNT_PED_KILLED'] + ' pedestrians & injured ' + line['NUMBER_INJURED'] + ' other people.' + hit_and_run + timestamp
elif line['COUNT_PED_KILLED'] == '0':
if line['NUMBER_INJURED'] == '1': #Guaranteed the person injured is a ped
if line['Decode_COLLISION_SEVERITY'] != 'severely injured':
Decode_NARRATE = intro_type + ' & injured a pedestrian.' + hit_and_run + timestamp
else:
Decode_NARRATE = intro_type + ' & ' + line['Decode_COLLISION_SEVERITY'] + ' a pedestrian.' + hit_and_run + timestamp
elif line['NUMBER_INJURED'] > '1': #Not guaranteed the person injured is a ped
if line['COUNT_PED_INJURED'] == line['NUMBER_INJURED']:
Decode_NARRATE = intro_type + ' & injured ' + line['COUNT_PED_INJURED'] + ' pedestrians.' + hit_and_run + timestamp
elif line['COUNT_PED_INJURED'] < line['NUMBER_INJURED']:
Decode_NARRATE = intro_type + ' & injured ' + line['NUMBER_INJURED'] + ' people.' + hit_and_run + timestamp
if line['Decode_PED_BIKE'] == 'bike' and line['Decode_MVIW'] != 'a non-collision':
if line['Decode_STWD_VEHTYPE_AT_FAULT'] != 'riding a bicycle':
if line['COUNT_BICYCLIST_KILLED'] == '1':
if line['NUMBER_INJURED'] == '0':
Decode_NARRATE = intro_type + ' & killed a person on a bike.' + hit_and_run + timestamp
elif line['NUMBER_INJURED'] == '1':
if line['Decode_COLLISION_SEVERITY'] != 'severely injured':
Decode_NARRATE = intro_type + ' & killed a person on a bike & injured another.' + hit_and_run + timestamp
else:
Decode_NARRATE = intro_type + ' & killed a person on a bike & ' + line['Decode_COLLISION_SEVERITY'] + ' another.' + hit_and_run + timestamp
elif line['NUMBER_INJURED'] > '1':
Decode_NARRATE = intro_type + ' & killed a person on a bike & injured ' + line['NUMBER_INJURED'] + ' others.' + hit_and_run + timestamp
elif line['COUNT_BICYCLIST_KILLED'] > '1':
if line['NUMBER_INJURED'] == '0':
Decode_NARRATE = intro_type + ' & killed ' + line['COUNT_BICYCLIST_KILLED'] + ' people on bikes.' + hit_and_run + timestamp
elif line['NUMBER_INJURED'] > '1':
Decode_NARRATE = intro_type + ' & killed ' + line['COUNT_BICYCLIST_KILLED'] + ' people on bikes & injured ' + line['NUMBER_INJURED'] + ' others.' + hit_and_run + timestamp
elif line['COUNT_BICYCLIST_KILLED'] == '0':
if line['COUNT_BICYCLIST_INJURED'] == '1':
if line['Decode_COLLISION_SEVERITY'] != 'severely injured':
Decode_NARRATE = intro_type + ' & injured a person on a bike.' + hit_and_run + timestamp
else:
Decode_NARRATE = intro_type + ' & ' + line['Decode_COLLISION_SEVERITY'] + ' a person on a bike.' + hit_and_run + timestamp
elif line['COUNT_BICYCLIST_INJURED'] == '0' and line['NUMBER_INJURED'] == '1': #necessary because of screw ups in the data
Decode_NARRATE = intro_type + ' ' + line['Decode_MVIW'] + '; 1 person injured.' + hit_and_run + timestamp
elif line['NUMBER_INJURED'] > '1':
if line['COUNT_BICYCLIST_INJURED'] == line['NUMBER_INJURED']:
Decode_NARRATE = intro_type + ' & injured ' + line['COUNT_BICYCLIST_INJURED'] + 'people on bikes.' + hit_and_run + timestamp
elif line['COUNT_BICYCLIST_INJURED'] < line['NUMBER_INJURED'] and line['COUNT_BICYCLIST_INJURED'] == '1' and line['NUMBER_INJURED'] == '2':
Decode_NARRATE = 'A person driving a car on ' + line['PRIMARY_RD'] + line['Decode_TYPE_OF_COLLISION'] + ' & injured ' + line['COUNT_BICYCLIST_INJURED'] + ' person on a bike & injured another person.' + hit_and_run + timestamp
elif line['COUNT_BICYCLIST_INJURED'] < line['NUMBER_INJURED'] and line['COUNT_BICYCLIST_INJURED'] == '1' and line['NUMBER_INJURED'] > '2':
Decode_NARRATE = 'A person driving a car on ' + line['PRIMARY_RD'] + line['Decode_TYPE_OF_COLLISION'] + ' & injured ' + line['COUNT_BICYCLIST_INJURED'] + ' people on bikes & injured others.' + hit_and_run + timestamp
#Unfortunately necessary to change the 'at fault' to car when the injured party is a bicyclist, due to widespread false attribution of blame on bicyclists. This is MORE accurate than leaving it as is.
#Hopefully, "collided with" is neutral enough to inform readers that we're not assigning fault to the driver. Fault is unclear. In the future, we may need to coordinate this data with the party data,
#to determine if the bicyclist is injuring themselves, or another bicyclist, or what. If I didn't make the alterations below, 4% of the crashes would have been like "a bicyclist broadsided a bicyclist."
elif line['Decode_STWD_VEHTYPE_AT_FAULT'] == 'riding a bicycle':
if line['COUNT_BICYCLIST_KILLED'] == '1':
if line['NUMBER_INJURED'] == '0':
Decode_NARRATE = 'A person driving a car on ' + line['PRIMARY_RD'] + ' collided with & killed a person on a bike.' + hit_and_run + timestamp
elif line['NUMBER_INJURED'] == '1':
if line['Decode_COLLISION_SEVERITY'] != 'severely injured':
Decode_NARRATE = 'A person driving a car on ' + line['PRIMARY_RD'] + ' collided with & killed a person on a bike & injured another.' + hit_and_run + timestamp
else:
Decode_NARRATE = 'A person driving a car on ' + line['PRIMARY_RD'] + ' collided with & killed a person on a bike & ' + line['Decode_COLLISION_SEVERITY'] + ' another.' + hit_and_run + timestamp
elif line['NUMBER_INJURED'] > '1':
Decode_NARRATE = 'A person driving a car on ' + line['PRIMARY_RD'] + ' collided with & killed a person on a bike & injured ' + line['NUMBER_INJURED'] + ' others.' + hit_and_run + timestamp
elif line['COUNT_BICYCLIST_KILLED'] > '1':
if line['NUMBER_INJURED'] == '0':
Decode_NARRATE = 'A person driving a car on ' + line['PRIMARY_RD'] + ' collided with & killed ' + line['COUNT_BICYCLIST_KILLED'] + ' people on bikes.' + hit_and_run + timestamp
elif line['NUMBER_INJURED'] > '1':
Decode_NARRATE = 'A person driving a car on ' + line['PRIMARY_RD'] + ' collided with & killed ' + line['COUNT_BICYCLIST_KILLED'] + ' people on bikes & injured ' + line['NUMBER_INJURED'] + ' others.' + hit_and_run + timestamp
elif line['COUNT_BICYCLIST_KILLED'] == '0':
if line['NUMBER_INJURED'] == '1':
if line['Decode_COLLISION_SEVERITY'] != 'severely injured':
Decode_NARRATE = 'A person driving a car on ' + line['PRIMARY_RD'] + ' collided with & injured a person on a bike.' + hit_and_run + timestamp
else:
Decode_NARRATE = 'A person driving a car on ' + line['PRIMARY_RD'] + ' collided with & ' + line['Decode_TYPE_OF_COLLISION'] + ' a person on a bike.' + hit_and_run + timestamp
elif line['COUNT_BICYCLIST_INJURED'] == '0' and line['NUMBER_INJURED'] == '1':
Decode_NARRATE = intro_type + '; 1 person injured.'
elif line['NUMBER_INJURED'] > '1':
if line['COUNT_BICYCLIST_INJURED'] == line['NUMBER_INJURED']:
Decode_NARRATE = 'A person driving a car on ' + line['PRIMARY_RD'] + ' collided with & injured ' + line['COUNT_BICYCLIST_INJURED'] + ' people on bikes.' + hit_and_run + timestamp
elif line['COUNT_BICYCLIST_INJURED'] < line['NUMBER_INJURED'] and line['COUNT_BICYCLIST_INJURED'] == '1' and line['NUMBER_INJURED'] == '2':
Decode_NARRATE = 'A person driving a car on ' + line['PRIMARY_RD'] + ' collided with & injured ' + line['COUNT_BICYCLIST_INJURED'] + ' person on a bike & injured another person.' + hit_and_run + timestamp
elif line['COUNT_BICYCLIST_INJURED'] < line['NUMBER_INJURED'] and line['COUNT_BICYCLIST_INJURED'] == '1' and line['NUMBER_INJURED'] > '2':
Decode_NARRATE = 'A person driving a car on ' + line['PRIMARY_RD'] + ' collided with & injured ' + line['COUNT_BICYCLIST_INJURED'] + ' people on bikes & injured others.' + hit_and_run + timestamp
elif line['COUNT_BICYCLIST_INJURED'] < line['NUMBER_INJURED'] and line['COUNT_BICYCLIST_INJURED'] == '0' and line['NUMBER_INJURED'] > '1':
Decode_NARRATE = intro + ' collided w/ ' + 'another bicyclist. ' + line['NUMBER_INJURED'] + ' other people injured. Strange.' + hit_and_run + timestamp
if line['Decode_MVIW'] == 'a non-collision':
if line['NUMBER_KILLED'] > '0' and line['NUMBER_INJURED'] == '0':
Decode_NARRATE = intro + ' was involved in a non-collision; ' + number_killed + '.' + timestamp
elif line['NUMBER_KILLED'] > '0' and line['NUMBER_INJURED'] > '0':
Decode_NARRATE = intro + ' was involved in a non-collision; ' + number_killed + ' & ' + number_injured + '.' + timestamp
elif line['NUMBER_KILLED'] == '0' and line['NUMBER_INJURED'] > '0':
Decode_NARRATE = intro + ' was involved in a non-collision; ' + number_injured + '.' + timestamp
writer.writerow([
line['COLLISION_TIME'],
line['COLLISION_DATE'],
Decode_NARRATE
])