summaryrefslogtreecommitdiff
path: root/bin/grep.js
diff options
context:
space:
mode:
Diffstat (limited to 'bin/grep.js')
-rw-r--r--bin/grep.js126
1 files changed, 126 insertions, 0 deletions
diff --git a/bin/grep.js b/bin/grep.js
new file mode 100644
index 0000000..af30bf2
--- /dev/null
+++ b/bin/grep.js
@@ -0,0 +1,126 @@
+
+/*
+ This script parses the big gun violence CSV and filters for lines containing a particular string.
+
+ 479363,
+ 2013-01-19,New Mexico,Albuquerque,2806 Long Lane,
+ 5,0,
+ http://www.gunviolencearchive.org/incident/479363,
+ http://hinterlandgazette.com/2013/01/pastor-greg-griego-identified-victims-killed-nehemiah-griego-albuquerque-nm-shooting.html,
+ False,1,
+
+ gun_stolen,
+ 0::Unknown||1::Unknown,
+
+ gun_type,
+ 0::22 LR||1::223 Rem [AR-15],
+
+ incident_characteristics,
+ "Shot - Dead (murder, accidental, suicide)
+ ||Mass Shooting (4+ victims injured or killed excluding the subject/suspect/perpetrator, one location)
+ ||Domestic Violence",
+
+ latitude, location_description, longitude,
+ 34.9791,,-106.716,
+
+ n_guns_involved, notes,
+ 2,,
+
+ participant_age,
+ 0::51||1::40||2::9||3::5||4::2||5::15,
+
+ participant_age_group,
+ 0::Adult 18+||1::Adult 18+||2::Child 0-11||3::Child 0-11||4::Child 0-11||5::Teen 12-17,
+
+ participant_gender,
+ 0::Male||1::Female||2::Male||3::Female||4::Female||5::Male,
+
+ participant_name,
+ 0::Greg Griego||1::Sara Griego||2::Zephania Griego||3::Jael Griego||4::Angelina Griego||5::Nehemiah Griego,
+
+ participant_relationship,
+ 5::Family,
+
+ participant_status,
+ "0::Killed||1::Killed||2::Killed||3::Killed||4::Killed||5::Unharmed, Arrested",
+
+ participant_type,
+ 0::Victim||1::Victim||2::Victim||3::Victim||4::Victim||5::Subject-Suspect,
+
+ http://www.cbsnews.com/news/nehemiah-gringo-case-memorial-service-planned-for-family-allegedly-slain-by-new-mexico-teen/||
+ http://www.thewire.com/national/2013/01/teenager-reportedly-used-ar-15-kill-five-new-mexico/61199/||
+ http://bigstory.ap.org/article/officials-nm-teen-gunman-kills-5-inside-home||
+ http://www.huffingtonpost.com/2013/01/21/nehemiah-griego-teen-shoots-parents-3-children_n_2519359.html||
+ http://murderpedia.org/male.G/g/griego-nehemiah.htm||
+ http://hinterlandgazette.com/2013/01/pastor-greg-griego-identified-victims-killed-nehemiah-griego-albuquerque-nm-shooting.html,
+ 10,14
+*/
+
+const fs = require('fs')
+const parse = require('csv-parse')
+const stringify = require('csv-stringify')
+
+const search = "AR-15"
+const fields = ("incident_id,date,state,city_or_county,"
+ + "address,n_killed,n_injured,incident_url,source_url,"
+ + "incident_url_fields_missing,congressional_district,"
+ + "gun_stolen,gun_type,incident_characteristics,latitude,"
+ + "location_description,longitude,n_guns_involved,notes,"
+ + "participant_age,participant_age_group,participant_gender,"
+ + "participant_name,participant_relationship,"
+ + "participant_status,participant_type,"
+ + "sources,state_house_district,state_senate_district")
+ .split(',')
+ .reduce((a,b,i) => {
+ a[b] = i
+ }, {})
+
+const search = 'AR-15'
+const regexp = new RegExp(search, 'i')
+
+function test(row){
+ return row[fields.gun_type].match(regexp)
+}
+
+const input = fs.createReadStream('./data/gun_violence.csv')
+const parser = parse()
+const stringifier = stringify()
+const output = fs.createWriteStream('./data/' + search + '.csv')
+
+stream.on('readable', function() {
+ var buf;
+ while ((buf = stream.read()) !== null) {
+ parser.write(buf)
+ }
+});
+stream.on('finish', function(){
+ parser.end()
+})
+
+parser.on('readable', function(){
+ let record
+ while (record = parser.read()) {
+ if (test(record[fields])) {
+ stringifier.write(record)
+ }
+ }
+})
+parser.on('error', function(err){
+ console.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(err.message)
+})
+stringifier.on('finish', function(){
+ output.end()
+})