-
Notifications
You must be signed in to change notification settings - Fork 0
/
map-mentions-by-state.py
executable file
·61 lines (50 loc) · 1.83 KB
/
map-mentions-by-state.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 1 15:48:08 2020
@author: briancroxall
A script to find incidence of key terms on a per-state basis
"""
from collections import defaultdict as dd
state_set = set()
terms = ['wk', 'pr', 'gd', 'wk+pr+gd']
# find all states in data set
with open('map_states.tsv') as data:
for counter, line in enumerate(data):
if counter == 0:
continue # skip header line
state = line.split('\t')[3]
state_set.add(state)
# build empty dictionary
state_dict = dd(dict) # [state][term][count]
for state in state_set:
for term in terms:
state_dict[state][term] = 0
# fill dictionary
with open('map_states.tsv') as data:
for counter, line in enumerate(data):
if counter == 0:
continue # skip header line
state = line.split('\t')[3]
wk_mentions = int(line.split('\t')[4])
pr_mentions = int(line.split('\t')[5])
gd_mentions = int(line.split('\t')[6])
wk_pr_gd = wk_mentions + pr_mentions + gd_mentions
state_dict[state]['wk'] += wk_mentions
state_dict[state]['pr'] += pr_mentions
state_dict[state]['gd'] += gd_mentions
state_dict[state]['wk+pr+gd'] += wk_pr_gd
# create save file
with open('map-mentions-by-state.tsv', 'w') as savefile:
print('state', 'issues mentioning wk', 'issues mentioning pr',
'issues mentioning gd', 'issues mentioning wk+pr+gd', sep='\t',
file=savefile)
# print results to save file
for state, results in state_dict.items():
wk_total = results['wk']
pr_total = results['pr']
gd_total = results['gd']
wk_pr_gd_total = results['wk+pr+gd']
with open('map-mentions-by-state.tsv', 'a') as savefile:
print(state, wk_total, pr_total, gd_total, wk_pr_gd_total,
sep='\t', file=savefile)