-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfind_bad_choices.py
61 lines (49 loc) · 1.84 KB
/
find_bad_choices.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
#!/usr/bin/env python
#
# Copyright (c) 2013 Gold Brand Software, LLC. and (c) 2014 George Mason University
# This software was developed under U.S. Government contract number D11PC20062.
# The U.S. Federal Government is granted unlimited rights as defined in FAR 52.227-14.
# All other rights reserved.
#
'''get_question_choices.py
Retrieve question_choices and do some stuff with it. Notably, find
badly-constructed choices.
ctwardy, 2014.
'''
import sys
import get_questions as getQ
if __name__ == '__main__':
if len(sys.argv) < 2:
print "Usage: %s api_key" % (sys.argv[0])
sys.exit(1)
api_key = sys.argv[1]
payload = {'api_key': api_key, 'format': 'json'}
# Get question data and write link files
questions = getQ.get_questions(payload)
print '%d Questions Retrieved' % len(questions)
odd_questions = {}
for q in questions:
id = q['question_id']
try:
# Before mid-March 2014, we return a string
choices=q['choices'].split(',')
except KeyError:
# After mid-March 2014, we return a list
choices = q['choices_list']
if any([(choice.startswith(' ') or
choice.startswith(u'\xa0') or
choice.endswith('\r') or
choice.endswith('\n') or
'?' in choice) for choice in choices]):
odd_questions[id] = choices
filename = 'bad_choices.csv'
print '%d Questions had bad choices. See %s' % (len(odd_questions), filename)
s = '''
# Questions with bad choices
# Bad choices start with spacelike, end with CR/LF, or include '?'.
# ====================================================================
'''
with open(filename,'w') as f:
f.write(s)
for key, val in odd_questions.iteritems():
f.write('%s, %s\n' % (key, val))