-
Notifications
You must be signed in to change notification settings - Fork 0
/
play.py
311 lines (240 loc) · 9.98 KB
/
play.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#
# Don't assume nothing smart from this file I'm just messing around with
# the data files and getting to know them. Anything of value will go
# elsewhere.
#
# TODO: Break up legistlator documents so that sub document are other collections. Write performance.
# TODO: for legislator add subject with activity count
# TODO: Map legislator to congresses
# TODO: Map legislator to committees
# import nltk
# from nltk import bigrams
# from nltk.corpus import stopwords
# from nltk.tokenize import RegexpTokenizer
import operator
import os
import calendar
import pymongo
import bson
import json
import multiprocessing
import socket
# TODO: when backonline load by hostname or rolename
from conf.vance import DB, DB_HOST, DB_USER, DB_PASS
#
# Settings
#
APP_DIR = os.path.dirname(os.path.abspath(__file__))
DATA_DIR = "{0}/data/congress".format(APP_DIR)
db = pymongo.MongoClient(DB_HOST,j=False).congress
#
# Drop collections while we play'
#
db.states.drop()
db.create_collection("states")
db.subject.drop()
db.create_collection("subject")
def chunks(l, n):
""" Yield successive n-sized chunks from a list
"""
for i in xrange(0, len(l), n):
yield l[i:i+n]
def process_bills(subset):
""" Loop over bills. Count and tally statistics on congressmen
for each peace of legislation. """
for subdir in subset:
this_dir = "{0}/{1}/bills".format(DATA_DIR, subdir)
for root, dirs, files in os.walk(this_dir):
if "data.json" in files and "text-versions" not in root:
file_path = "{0}/data.json".format(root)
print file_path
bill = json.loads( open(file_path, 'r').read() )
sponsor = bill.get('sponsor', None)
# Check if the bill has a sponsor if so give 'em credit
if sponsor and sponsor.has_key("state"):
db.states.update(
{ "name" : sponsor['state'] },
{ "$inc" : { "sponsor_count" : 1} },
True,
False
)
if sponsor:
# some bills are sponsored by legislators some by committees
if sponsor.get('thomas_id', None ):
db.legislator.update(
{"thomas_id": sponsor['thomas_id'] },
{
"$inc" : {
"sponsor_count" : 1,
},
}
)
db.legislator_sponsored.insert(
{
"thomas_id": sponsor['thomas_id'],
"bill_id" : bill.get("bill_id", "NOID"),
"title": bill.get("official_title", "NO TITLE") # make a fucntion that gets on of the titles
}
)
else:
db.committee.update(
{"committee_id": sponsor['committee_id'] },
{
"$inc" : {
"sponsor_count" : 1,
},
}
)
db.committee_sponsored.insert(
{
"committee_id": sponsor['committee_id'],
"bill_id" : bill.get("bill_id", "NOID"),
"title": bill.get("official_title", "NO TITLE") # make a fucntion that gets on of the titles
}
)
#print bill.get('subjects', None)
for subject in bill.get('subjects', []):
db.subject.update(
{"name": subject},
{
"$inc": {
"count": 1
},
}
)
db.subject_bills.insert(
{
"sub"
"bill_id" : bill.get("bill_id", "NOID"),
"type" : bill.get('bill_type',None),
"title": bill.get("official_title", "TITLE") # TODO: make a function that gets one of the titles
}
)
if sponsor:
# We are interested in the subjects on which legislators are active
# FIXME: figure out how to do this inside the legislator document. It's damn stupid to have a seperate collection
try:
db.legislator_subjects.update(
{"thomas_id": sponsor['thomas_id'] },
{
"$inc" : {
"{0}".format(subject) : 1
}
},
True,
False
)
except:
print "No sponsor"
else:
print "Bill {0}".format(bill.get("official_title", "TITLE"))
#print bill.get('subjects_top_term', None)
if bill.get('subjects_top_term', None):
db.subject.update(
{"name": subject},
{
"$inc": {
"top_count": 1
}
}
)
# Loop through all the cosponsors of a bill and give them each credit for the bills
# they cosponsor
for cosponsor in bill.get('cosponsors', ()):
db.states.update(
{ "name" : cosponsor['state'] },
{ "$inc" : { "cosponsor_count" : 1 } },
True,
False
)
db.legislator.update(
{"thomas_id": cosponsor['thomas_id'] },
{
"$inc" : { "cosponsor_count" : 1},
}
)
db.legislator_cosponsored.insert(
{
"thomas_id": sponsor['thomas_id'],
"bill_id" : bill.get("bill_id", "NO ID"),
"title": bill.get("official_title", "NO TITLE") # make a fucntion that gets on of the titles
}
)
if __name__ == '__main__':
jobs = []
#dirs = os.walk(DATA_DIR).next()[1]
#dirs = [103,104,105,106,107,108,109,110,111,112,113]
state_sponser_count = {}
dirs = []
num = len(dirs)
procs = num / 4
for subset in list(chunks(dirs, procs)):
p = multiprocessing.Process(target=process_bills, args=(subset,))
jobs.append(p)
p.start()
# bigram_counts = {}
# stop_words = stopwords.words('english')
# stop_words = stop_words + [
# "senate", "congress", "session", "113th", "u.s.","government",
# "bill", "in", "the", "Res","office", "rule", "law","ats" ]
# stop_bigrams = ['people united', 'exceed 000', 'resolution considered',
# 'rules senate', 'standing rules', 'act u', 'whereas united', 'u c',
# 'senate 1st', 'res agreed', 'united states', 'senate united',
# 'congressional bills', 'printing res', 'u printing', 'bills u',
# 'submitted following', 'following resolution', 'therefore resolved',
# '1st res', 'states whereas', 'resolved senate', '000 000', 'referred committee',
# 'resolution referred', 'senate ats']
# # very niaeve fix me
# for n in range(0,2015):
# stop_words.append("{0}".format(n))
# # very niaeve fix me
# for n in range(1,12):
# stop_words.append(calendar.month_name[n])
# bigram_strings = []
# {
# "district": null,
# "name": "Reid, Harry",
# "state": "NV",
# "thomas_id": "00952",
# "title": "Sen",
# "sponser_count" : 0,
# "sponsered_resolutions" : [
# {
# "id": "name"
# "title": "name"
# },
# ],
# "sponsered_bills" : [
# {
# "id": "name"
# "title": "name"
# },
# ],
# "sponsered_subjects" : {
# "Civil actions and liability" : 35,
# "Evidence and witnesses": 2,
# }
# },
# if "document.txt" in files:
# file_path = "{0}/document.txt".format(root)
# print file_path
# f = open(file_path, 'r')
# text = f.read()
# db.bigrams.update({})
# tokenizer = RegexpTokenizer(r'[a-zA-Z0-9]+')
# tokenized = tokenizer.tokenize(text)
# for b in bigrams([word.strip().lower() for word in tokenized if word.strip().lower() not in stop_words]):
# if '%s %s' % b not in stop_bigrams:
# bigram_strings.append(b)
# print "######## Counting Bigrams ###################"
# for bi in bigram_strings:
# bi_key = '%s %s' % bi
# if bi_key not in bigram_counts.keys():
# bigram_counts[bi_key] = 1
# else:
# bigram_counts[bi_key] = bigram_counts[bi_key] + 1
# print "######## Sorting Bigrams ###################"
# sorted_bigrams = sorted(bigram_counts.iteritems(), key=operator.itemgetter(1),reverse=True)
# for i in range(0, 100):
# print sorted_bigrams[i]
# #print " {0} : {1} ".format(sorted_bigrams[i])