-
Notifications
You must be signed in to change notification settings - Fork 4
/
mine_submissions.py
179 lines (140 loc) · 3.82 KB
/
mine_submissions.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
import sqlite3, itertools, datetime
import numpy as np
import webbrowser,os,textwrap
from parsing import tokenize, frequency_table
'''
Small interactive program to help a human label the prospective submissions.
'''
conn = sqlite3.connect("db/report.db",
detect_types=sqlite3.PARSE_DECLTYPES|
sqlite3.PARSE_COLNAMES)
# Reads a single keystroke (why so hard?!)
def _find_getch():
try:
import termios
except ImportError:
# Non-POSIX. Return msvcrt's (Windows') getch.
import msvcrt
return msvcrt.getch
# POSIX system. Create and return a getch that manipulates the tty.
import sys, tty
def _getch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(fd)
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
return _getch
getch = _find_getch()
# Keep track of what we've seen before
cmd_template = '''
--DROP TABLE human_tracking;
CREATE TABLE IF NOT EXISTS human_tracking(
report_idx INTEGER PRIMARY KEY,
status STRING,
submitted_status BOOL DEFAULT 0,
date_classified TIMESTAMP
);
CREATE TABLE IF NOT EXISTS suggestions
(report_idx INTEGER PRIMARY KEY);
'''
conn.executescript(cmd_template)
cmd_mark_tracking = '''
INSERT INTO human_tracking (report_idx, status,date_classified)
VALUES (?,?,?)
'''
cmd_search_new = '''
SELECT A.report_idx,
wikipedia_title, wikipedia_text,wikipedia_url
FROM report as A
JOIN crossref AS B
ON A.report_idx==B.report_idx
WHERE positive_search_result=0
AND A.report_idx NOT IN (SELECT report_idx FROM human_tracking)
ORDER BY RANDOM()
LIMIT 1
'''
cmd_search_suggestions = '''
SELECT report_idx,
wikipedia_title, wikipedia_text, wikipedia_url
FROM suggestions as A
NATURAL JOIN report AS B
NATURAL JOIN crossref AS C
LIMIT 1
'''
cmd_remove_suggestion = '''
DELETE FROM suggestions
WHERE report_idx=?
'''
def get_remaining_suggestions():
cursor = conn.execute("SELECT COUNT(*) FROM suggestions")
return cursor.next()[0]
fmt = u'''\t\t\t\t=== {} ===
{}
{}
1. Interesting!
2. Mildly interesting...
3. Not interesting :(
4. Book/Film/Movie
s SKIP!
x EXIT PROGRAM
d DUMP Interesting
'''
response = {
"1":"interesting",
"2":"mildly_interesting",
"3":"not_interesting",
"4":"media",
"x":None,
"s":None,
"d":None,
}
while True:
os.system("clear")
SUGGESTION_FLAG = False
if not get_remaining_suggestions():
cursor = conn.execute(cmd_search_new)
else:
SUGGESTION_FLAG = True
cursor = conn.execute(cmd_search_suggestions)
ridx,title,text,url = cursor.next()
cursor.close()
# Let the user know this is a suggested flag
if SUGGESTION_FLAG:
title = u"(S) {}".format(title)
# Make this nice to read on the terminal
text = textwrap.fill(text, 80)
key = "."
while key not in response.keys():
webbrowser.open(url,new=0)
print fmt.format(title,url,text)
key = getch()
status = response[key]
if key.lower() == "x":
exit()
if key.lower() == "d":
break
if key.lower() != "s":
time = datetime.datetime.now()
conn.execute(cmd_mark_tracking, (ridx,status,time))
conn.commit()
# Even if skipped, remove the suggestion
if SUGGESTION_FLAG:
conn.execute(cmd_remove_suggestion,(ridx,))
conn.commit()
cmd_search_top = '''
SELECT
A.wikipedia_title, B.date_classified, A.wikipedia_text
FROM report as A
JOIN human_tracking AS B
ON A.report_idx==B.report_idx
WHERE B.status=="interesting"
ORDER BY date_classified
'''
if key.lower() == "d":
cursor = conn.execute(cmd_search_top)
for title,date,text in cursor:
print title,'\t', date,'\t',text