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
|
/*
This script parses the mass shootings CSV and makes it easier to deal with.
case,
Rite Aid warehouse shooting,
location,date,summary,
"Perryman, MD",
9/20/18,
"Snochia Moseley, 26, reportedly a disgruntled employee, shot her victims outside the building and on the warehouse floor; she later died from a self-inflicted gunshot at a nearby hospital. (No law enforcement officers responding to her attack fired shots.)",
fatalities,injured,total_victims,
3,3,6,
location_type,
age_of_shooter,
Workplace,
26,
prior_signs_mental_health_issues,mental_health_details,
-,-,
weapons_obtained_legally,where_obtained,weapon_type,weapon_details,
Yes,-,semiautomatic handgun,Glock 9 mm,
race,gender,
Black,F,
sources,mental_health_sources,sources_additional_age,
http://www.baltimoresun.com/news/maryland/crime/bs-md-harford-shooting-20180920-story.html; https://www.washingtonpost.com/local/public-safety/maryland-sheriffs-office-says-multiple-victims-in-shooting/2018/09/20/9d2c6464-bcda-11e8-be70-52bd11fe18af_story.html; https://www.cnn.com/2018/09/20/us/maryland-shooting/index.html; https://heavy.com/news/2018/09/snochia-moseley/,-,-,
latitude,longitude,type,year
39.455658,-76.208485,Mass,2018
*/
const fs = require('fs')
const parse = require('csv-parse')
const stringify = require('csv-stringify')
const fields = (
"case," +
"location,date,summary,fatalities,injured,total_victims," +
"location_type," +
"age_of_shooter," +
"prior_signs_mental_health_issues,mental_health_details," +
"weapons_obtained_legally,where_obtained,weapon_type,weapon_details," +
"race,gender," +
"sources,mental_health_sources,sources_additional_age," +
"latitude,longitude,type,year").split(',').reduce((a,b,i) => {
a[b] = i
return a
}, {})
const headings = ["date", "timestamp", "fatalities", "injured", "total_victims", "age", "case", "weapon_type", "weapon_details"]
function test(row){
return (
!!row
// row[fields.incident_characteristics].indexOf('Samaritan') !== -1
// // && row[fields.participant_age_group].indexOf('Teen') !== -1
// && row[fields.participant_age_group].indexOf('Child') !== -1
// && row[fields.gun_type].indexOf('AR-15') !== -1
)
}
function apply(row){
let [m, d, y] = row[fields.date].split('/')
if (y < 20) y = parseInt(y) + 2000
return [
[y, pad(m), pad(d)].join('/'),
+new Date(y, m, d),
row[fields.fatalities],
row[fields.injured],
row[fields.total_victims],
row[fields.age_of_shooter],
row[fields.case],
row[fields.weapon_type].replace(/\s+$/g, ''),
row[fields.weapon_details].replace(/\s+$/g, ''),
]
}
function pad(n) { return (n < 10) ? ("0" + n) : n }
const input = fs.createReadStream('./data/mass_shootings.csv')
const parser = parse()
const stringifier = stringify()
const output = fs.createWriteStream('./data/mass_shootings_lite.csv')
input.on('readable', function() {
let buf
while ((buf = input.read()) !== null) {
parser.write(buf)
}
})
input.on('error', function(err){
console.error('input error', err.message)
})
input.on('finish', function(){
parser.end()
})
let i = 0
parser.on('readable', function(){
let row
while (row = parser.read()) {
if (i === 0) {
stringifier.write(headings)
i += 1
continue
}
if ((++i % 10000) === 0) {
console.log(i + '...')
}
if (test(row)) {
stringifier.write(apply(row))
}
}
})
parser.on('error', function(err){
console.error('parser error', err.message)
})
parser.on('end', function(){
stringifier.end()
})
stringifier.on('readable', function(){
let row
while(row = stringifier.read()){
output.write(row)
}
})
stringifier.on('error', function(err){
console.error('stringifier error', err.message)
})
stringifier.on('finish', function(){
output.end()
})
|